from collections import defaultdict N = int(input()) # Trie木の構築 Trie = {"cnt": [N], "to": [{}]} for i in range(N): S = input() curr = 0 for c in S: if c not in Trie["to"][curr]: Trie["to"][curr][c] = len(Trie["cnt"]) Trie["cnt"].append(0) Trie["to"].append({}) curr = Trie["to"][curr][c] Trie["cnt"][curr] += 1 Q = int(input()) # クエリの処理 curr = 0 history = [0] ans = [] for _ in range(Q): ipt = input().split() if ipt[0] == "1": # 文字列の追加 X = ipt[1] if curr != -1 and X in Trie["to"][curr]: curr = Trie["to"][curr][X] else: curr = -1 history.append(curr) elif ipt[0] == "2": # 文字列の削除 history.pop() curr = history[-1] else: # 現在のノードに対応する文字列の数を出力 if curr == -1: ans.append(0) else: ans.append(Trie["cnt"][curr]) # 結果の出力 for a in ans: print(a)