import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;

public class Validate {
	final int MAXN = 100000;
	final int MAXX = 1000000000;

	public void run() {
		StrictScanner inf = new StrictScanner(System.in);
		int n = inf.nextInt();
		ensureLimits(n, 1, MAXN, "incorrect n");
		inf.nextLine();
		HashSet<String> reserv = new HashSet<String>();
		reserv.add("new");
		reserv.add("list");
		reserv.add("set");
		reserv.add("add");
		reserv.add("get");
		int sum = 0;
		HashMap<String, Integer> map = new HashMap<String, Integer>();
		HashMap<String, String> edge = new HashMap<String, String>();
		for (int i = 0; i < n; i++) {
			String st = inf.getNextLine();
			inf.nextLine();
			if (st.startsWith("List")) {
				String[] ss = st.split(" ");
				ensure(ss.length >= 4, line(i + 2));
				if (ss[3].equals("new")) {
					ensure(ss.length == 5 && ss[0].equals("List")
							&& ss[2].equals("="), line(i + 2));
					ensure(ss[4].startsWith("List("), line(i + 2));
					ensureString(ss[1], "qwertyuiopasdfghjklzxcvbnm",
							line(i + 2));
					ensure(ss[1].length() > 0 && ss[1].length() <= 10,
							line(i + 2));
					ensure(!reserv.contains(ss[1]), line(i + 2));

					ensure(st.charAt(st.length() - 1) == ')', line(i + 2));
					int kol = 0;
					for (int j = 0; j < st.length(); j++) {
						if (st.charAt(j) == '(')
							kol++;
					}
					ensure(kol == 1, line(i + 2));
					int l = st.lastIndexOf('(');
					String num = st.substring(l + 1, st.length() - 1);
					String[] numbers = num.split(",");
					for (int j = 0; j < numbers.length; j++) {
						Integer tmp = Integer.parseInt(numbers[j]);
						ensureLimits(tmp, 1, MAXX, line(i + 2));
					}
					sum += numbers.length;
					String name = ss[1];
					ensure(!map.containsKey(name), line(i + 2));
					map.put(name, numbers.length);
				} else {
					ensure(ss.length == 4 && ss[0].equals("List")
							&& ss[2].equals("="), line(i + 2));
					ensureString(ss[1], "qwertyuiopasdfghjklzxcvbnm",
							line(i + 2));
					ensure(ss[1].length() > 0 && ss[1].length() <= 10,
							line(i + 2));
					ensure(!reserv.contains(ss[1]), line(i + 2));
					ensure(st.charAt(st.length() - 1) == ')', line(i + 2));
					int kol = 0;
					for (int j = 0; j < st.length(); j++) {
						if (st.charAt(j) == '(')
							kol++;
					}
					ensure(kol == 1, line(i + 2));
					int l = st.lastIndexOf('(');
					String num = st.substring(l + 1, st.length() - 1);
					String[] numbers = num.split(",");
					ensure(numbers.length == 2, line(i + 2));
					l = Integer.parseInt(numbers[0]);
					int r = Integer.parseInt(numbers[1]);

					ensureLimits(l, 1, MAXX, line(i + 1));
					ensureLimits(r, l, MAXX, line(i + 1));

					String name = ss[1];
					ensure(!map.containsKey(name), line(i + 2));
					String sp = ss[3];
					String[] parse = sp.split("\\.");
					ensure(parse.length == 2 && parse[1].startsWith("subList("),
							line(i + 2));

					ensure(map.containsKey(parse[0]), line(i + 2));
					ensure(map.get(parse[0]) >= r, line(i + 2));
					edge.put(name, parse[0]);
					map.put(name, r - l + 1);
				}
				continue;
			}

			String[] s = st.split("\\.");
			ensure(s.length == 2, line(i + 2));
			String name = s[0];
			ensureString(name, "qwertyuiopasdfghjklzxcvbnm", line(i + 2));
			ensure(name.length() > 0 && name.length() <= 10, line(i + 2));
			ensure(!reserv.contains(name), line(i + 2));
			ensure(map.containsKey(name), line(i + 2));

			ensure(st.charAt(st.length() - 1) == ')', line(i + 2));
			int kol = 0;
			for (int j = 0; j < st.length(); j++) {
				if (st.charAt(j) == '(')
					kol++;
			}
			ensure(kol == 1, line(i + 2));
			int l = st.lastIndexOf('(');
			String num = st.substring(l + 1, st.length() - 1);
			String[] numbers = num.split(",");

			if (s[1].startsWith("get(")) {
				ensure(numbers.length == 1, line(i + 2));
				int tmp = Integer.parseInt(numbers[0]);
				ensureLimits(tmp, 1, MAXX, line(i + 1));
				ensure(map.get(name) >= tmp, line(i + 2));
				continue;
			}

			if (s[1].startsWith("set(")) {
				ensure(numbers.length == 2, line(i + 2));
				int tmp = Integer.parseInt(numbers[0]);
				int tmp2 = Integer.parseInt(numbers[1]);
				ensureLimits(tmp, 1, MAXX, line(i + 1));
				ensureLimits(tmp2, 1, MAXX, line(i + 1));
				ensure(map.get(name) >= tmp, line(i + 2));
				continue;
			}

			if (s[1].startsWith("add(")) {
				ensure(numbers.length == 1, line(i + 2));
				int tmp = Integer.parseInt(numbers[0]);
				ensureLimits(tmp, 1, MAXX, line(i + 1));
				String cur = name;
				boolean f;
				do {
					f = false;
					tmp = map.get(cur);
					tmp++;
					map.remove(cur);
					map.put(cur, tmp);
					if (edge.containsKey(cur)) {
						f = true;
						cur = edge.get(cur);
					}
				} while (f);
				continue;
			}
			ensure(false, line(i + 2));
		}
		ensureLimits(sum, 1, 100000, "bad sum");
	}

	public static void main(String[] args) {
		new Validate().run();
	}

	public class StrictScanner {
		private final BufferedReader in;
		private String line = "";
		private int pos;
		private int lineNo;

		public StrictScanner(InputStream source) {
			in = new BufferedReader(new InputStreamReader(source));
			nextLine();
		}

		public void close() {
			ensure(line == null, "Extra data at the end of file");
			try {
				in.close();
			} catch (IOException e) {
				throw new AssertionError("Failed to close with " + e);
			}
		}

		public void nextLine() {
			ensure(line != null, "EOF");
			ensure(pos == line.length(), "Extra characters on line " + lineNo);
			try {
				line = in.readLine();
			} catch (IOException e) {
				throw new AssertionError("Failed to read line with " + e);
			}
			pos = 0;
			lineNo++;
		}

		public String getNextLine() {
			StringBuilder sb = new StringBuilder("");
			while (!isEoln()) {
				sb.append(next());
				if (!isEoln()) sb.append(" ");
			}
			return new String(sb);
		}

		public boolean isEoln() {
			return (pos == line.length());
		}

		public String next() {
			ensure(line != null, "EOF");
			ensure(line.length() > 0, "Empty line " + lineNo);
			if (pos == 0)
				ensure(line.charAt(0) > ' ', "Line " + lineNo
						+ " starts with whitespace");
			else {
				ensure(pos < line.length(), "Line " + lineNo + " is over");
				ensure(line.charAt(pos) == ' ', "Wrong whitespace on line "
						+ lineNo);
				pos++;
				ensure(pos < line.length(), "Line " + lineNo + " is over");
				ensure(line.charAt(0) > ' ', "Line " + lineNo
						+ " has double whitespace");
			}
			StringBuilder sb = new StringBuilder();
			while (pos < line.length() && line.charAt(pos) > ' ')
				sb.append(line.charAt(pos++));
			return sb.toString();
		}

		public int nextInt() {
			String s = next();
			ensure(s.length() == 1 || s.charAt(0) != '0',
					"Extra leading zero in number " + s + " on line " + lineNo);
			ensure(s.charAt(0) != '+', "Extra leading '+' in number " + s
					+ " on line " + lineNo);
			try {
				return Integer.parseInt(s);
			} catch (NumberFormatException e) {
				throw new AssertionError("Malformed number " + s + " on line "
						+ lineNo);
			}
		}

		public long nextLong() {
			String s = next();
			ensure(s.length() == 1 || s.charAt(0) != '0',
					"Extra leading zero in number " + s + " on line " + lineNo);
			ensure(s.charAt(0) != '+', "Extra leading '+' in number " + s
					+ " on line " + lineNo);
			try {
				return Long.parseLong(s);
			} catch (NumberFormatException e) {
				throw new AssertionError("Malformed number " + s + " on line "
						+ lineNo);
			}
		}

		public double nextDouble() {
			String s = next();
			ensure(s.length() == 1 || s.startsWith("0.") || s.charAt(0) != '0',
					"Extra leading zero in number " + s + " on line " + lineNo);
			ensure(s.charAt(0) != '+', "Extra leading '+' in number " + s
					+ " on line " + lineNo);
			try {
				return Double.parseDouble(s);
			} catch (NumberFormatException e) {
				throw new AssertionError("Malformed number " + s + " on line "
						+ lineNo);
			}
		}
	}

	public static class Geometry {
		/**
		 * Distance between two points (x1; y1) and (x2; y2)
		 * 
		 * @param x1
		 * @param y1
		 * @param x2
		 * @param y2
		 * @return
		 */
		public static double dist(int x1, int y1, int x2, int y2) {
			long x3 = x1;
			long y3 = y1;
			long x4 = x2;
			long y4 = y2;
			return Math.sqrt((x3 - x4) * (x3 - x4) + (y3 - y4) * (y3 - y4));
		}
	}

	public static class Graph {
		/**
		 * Adjacency matrix 0 or -1 if edge is empty other positive int if not
		 * 
		 * @param e
		 * @return
		 */
		public static boolean isTree(int[][] e) {
			for (int i = 0; i < e.length; i++) {
				ensure(e[i].length == e.length,
						"Bad adjacency matrix given to class Graph");
			}
			ensure(e.length != 0, "Empty adjacency matrix given to class Graph");
			boolean[] visited = new boolean[e.length];
			if (!dfsCheckForTree(0, -1, visited, e))
				;
			boolean ans = true;
			for (int i = 0; i < e.length; i++) {
				ans &= visited[i];
			}
			return ans;
		}

		private static boolean dfsCheckForTree(int k, int p, boolean[] f,
				int[][] e) {
			f[k] = true;
			for (int i = 0; i < f.length; i++) {
				if (e[k][i] > 0 && !f[i]) {
					dfsCheckForTree(i, k, f, e);
				} else {
					if (e[k][i] > 0 && f[i] && i != p) {
						return false;
					}
				}
			}
			return true;
		}

	}

	static void ensure(boolean b, String message) {
		if (!b) {
			throw new AssertionError(message);
		}
	}

	void ensureString(String s, String alph, String text) {
		for (int i = 0; i < s.length(); i++) {
			ensure(alph.indexOf(s.charAt(i)) != -1, text);
		}
	}

	void ensureLimits(long n, long from, long to, String name) {
		ensure(from <= n && n <= to, name + " must be from " + from + " to "
				+ to);
	}

	void ensureLimits(int n, int from, int to, String name) {
		ensure(from <= n && n <= to, name + " must be from " + from + " to "
				+ to);
	}

	String line(int x) {
		return "Problem in line #" + Integer.toString(x);
	}

}
