1 #include <cstdio>
2 #include <iostream>
3 #include <cstring>
4 #include <algorithm>
5 using namespace std;
6 int trie[8000010][10],fa[8000010],stop[8000010],tot=1,m;
7 char s[8000010],s1[8000010];
8 int getfather(int x) { return fa[x]==x?x:fa[x]=getfather(fa[x]); }
9 void insert(char *s)
10 {
11 int len=strlen(s+1),p=1;
12 for (int i=1;i<=len;i++)
13 {
14 int ch=s[i]-'0';
15 if (!trie[p][ch]) trie[p][ch]=++tot;
16 p=getfather(trie[p][ch]);
17 }
18 stop[p]++;
19 }
20 int &get(char *s)
21 {
22 int len=strlen(s+1),p=1;
23 for (int i=1;i<len;i++)
24 {
25 int ch=s[i]-'0';
26 if (!trie[p][ch]) trie[p][ch]=++tot;
27 p=getfather(trie[p][ch]);
28 }
29 return trie[p][s[len]-'0'];
30 }
31 bool find(char *s)
32 {
33 int len=strlen(s+1),p=1;
34 for (int i=1;i<=len;i++)
35 {
36 int ch=s[i]-'0';
37 if (!trie[p][ch]) return 0;
38 p=getfather(trie[p][ch]);
39 }
40 if (stop[p]==0) return 0; else return 1;
41 }
42 void together(int &a,int &b,int d)
43 {
44 int u=getfather(a),v=getfather(b);
45 if (u==v&&u==0)
46 {
47 if (d) a=b=++tot;
48 return;
49 }
50 if (u==v) return;
51 if (!a) a=v;
52 else
53 if (!b) b=u;
54 else
55 {
56 fa[v]=u;
57 stop[u]=stop[u]+stop[v];
58 for (int i=0;i<10;i++)
59 {
60 together(trie[u][i],trie[v][i],0);
61 u=getfather(u);
62 }
63 }
64 }
65 int main()
66 {
67 scanf("%d",&m);
68 for (int i=1;i<=8000000;i++) fa[i]=i;
69 for (int i=1;i<=m;i++)
70 {
71 int d;
72 scanf("%d%s",&d,s+1);
73 if (d==1) insert(s);
74 if (d==2) printf("%d\n",find(s));
75 if (d==3)
76 {
77 scanf("%s",s1+1);
78 int &a=get(s),&b=get(s1);
79 together(a,b,1);
80 }
81 }
82 return 0;
83 }