import java.io.*;
import java.util.*;
 
public class join_keka {
    private static void solve() throws IOException {
        int n = nextInt();
        Map<String, Integer> lengthForHandle = new HashMap<>();
        Map<String, Integer> curIdForHandle = new HashMap<String, Integer>();
        List<Integer> prevForId = new ArrayList<>();
 
        List<String> mentions = new ArrayList<>();
 
        int bestConversation = 0;
        int bestLast = -1;
 
        for (int i = 0; i < n; i++) {
            String line = br.readLine();
            int ok = line.indexOf(':');
            String handle = line.substring(0, ok).trim();
            String message = line.substring(ok + 1);
 
            StringTokenizer tok = new StringTokenizer(message);
            mentions.clear();
            while (tok.hasMoreTokens()) {
                String s = tok.nextToken();
                if (s.startsWith("@")) {
                    mentions.add(s);
                }
            }
 
            int bestLength = 0;
            String bestHandle = null;
            for (String mention : mentions) {
                if (mention.equals(handle)) {
                    continue;
                }
                Integer haveLength = lengthForHandle.get(mention);
                if (haveLength == null) {
                    haveLength = 0;
                }
                if (haveLength > bestLength) {
                    bestLength = haveLength;
                    bestHandle = mention;
                }
            }
 
            Integer haveLength = lengthForHandle.get(handle);
            if (haveLength == null) {
                haveLength = 0;
            }
            prevForId.add(bestHandle == null ? -1 : curIdForHandle
                    .get(bestHandle));
 
            if (bestLength + 1 > haveLength) {
                curIdForHandle.put(handle, i);
                lengthForHandle.put(handle, bestLength + 1);
            }
             
            if (bestLength + 1 > bestConversation) {
                bestConversation = bestLength + 1;
                bestLast = i;
            }
        }
 
        out.println(bestConversation);
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < bestConversation; i++) {
            list.add(bestLast);
            bestLast = prevForId.get(bestLast);
        }
        Collections.reverse(list);
        for (int i = 0; i < list.size(); i++) {
            if (i > 0) {
                out.print(' ');
            }
            out.print(list.get(i) + 1);
        }
        out.println();
 
    }
 
    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new FileReader("join.in"));
        out = new PrintWriter("join.out");
        solve();
        out.close();
    }
 
    static BufferedReader br;
    static PrintWriter out;
    static StringTokenizer st;
 
    static String nextToken() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            String line = br.readLine();
            if (line == null) {
                return null;
            }
            st = new StringTokenizer(line);
        }
        return st.nextToken();
    }
 
    static int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }
 
    static long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }
}