import java.io.*;
import java.util.*;

/**
 * Solution for NEERC'2013 Problem J: Join the Conversation.
 * This solution checks correctness of the input.
 *
 * @author Roman Elizarov
 */
public class join_re {

    public static void main(String[] args) throws Exception {
        new join_re().go();
    }

    void go() throws Exception {
        long time = System.currentTimeMillis();
        read();
        solve();
        write();
        System.out.printf("Done in %d ms%n", System.currentTimeMillis() - time);
    }

    int n;
    String[] author;
    String[] message;

    void read() throws Exception {
        BufferedReader in = new BufferedReader(new FileReader("join.in"));
        n = Integer.parseInt(in.readLine());
        assert n >= 1 && n <= 50000;
        author = new String[n];
        message = new String[n];
        for (int i = 0; i < n; i++) {
            String s = in.readLine();
            for (int j = 0; j < s.length(); j++)
                assert s.charAt(j) >= 32 && s.charAt(j) <= 126;
            assert s.startsWith("@");
            int j = s.indexOf(':');
            assert j > 0;
            author[i] = s.substring(0, j);
            assert s.charAt(j + 1) == ' ';
            message[i] = s.substring(j + 2);
            assert author[i].length() > 1 && author[i].length() <= 20 :
                    String.format("Invalid author %d length: %d", i + 1, author[i].length());
            assert author[i].indexOf(' ') < 0;
            assert message[i].length() > 0 && message[i].length() <= 139 :
                    String.format("Invalid message %d length: %d", i + 1, message[i].length());
        }
        assert in.readLine() == null;
        in.close();
    }

    int[] br;
    Map<String, Integer> maxLenByAuthor;
    Map<String, Integer> maxIndexByAuthor;
    int maxLen;
    int maxIndex;

    List<Integer> answer;

    void solve() {
        br = new int[n];
        maxLenByAuthor = new HashMap<>();
        maxIndexByAuthor = new HashMap<>();
        for (int i = 0; i < n; i++) {
            String a = author[i];
            String m = message[i];
            br[i] = -1;
            if (!maxLenByAuthor.containsKey(a)) {
                maxLenByAuthor.put(a, 1);
                maxIndexByAuthor.put(a, i);
            }
            int curLen = maxLenByAuthor.get(a);
            for (int j = 0; j < m.length(); j++)
                if (m.charAt(j) == '@' && (j == 0 || m.charAt(j - 1) == ' ')) {
                    String mm = m.substring(j);
                    int k = mm.indexOf(' ');
                    if (k > 0)
                        mm = mm.substring(0, k);
                    if (mm.equals(a))
                        continue;
                    Integer mLen = maxLenByAuthor.get(mm);
                    if (mLen != null && mLen + 1 > curLen) {
                        curLen = mLen + 1;
                        br[i] = maxIndexByAuthor.get(mm);
                        maxLenByAuthor.put(a, curLen);
                        maxIndexByAuthor.put(a, i);
                        if (curLen > maxLen) {
                            maxLen = curLen;
                            maxIndex = i;
                        }
                    }
                }
        }
        int i = maxIndex;
        answer = new ArrayList<>();
        while (i >= 0) {
            answer.add(i + 1);
            i = br[i];
        }
        Collections.reverse(answer);
    }

    void write() throws Exception {
        PrintWriter out = new PrintWriter("join.out");
        out.println(answer.size());
        for (int i = 0; i < answer.size(); i++) {
            if (i > 0)
                out.print(' ');
            out.print(answer.get(i));
        }
        out.println();
        out.close();
    }
}
