1 #include <stdio.h>
2 int r[30010] = {0}, p[30010], n, x, y, d[30010];
3 char s[3];
4
5 int find(int x) {
6 if (x != p[x]) {
7 int t = p[x];
8 p[x] = find(p[x]);
9 r[x] += r[t];
10 }
11 return p[x];
12 }
13
14 int main() {
15 for (int i = 1; i < 30010; i++)p[i] = i, d[i] = 1;
16 scanf("%d", &n);
17 while (n--) {
18 scanf("%s%d", s, &x);
19 if (s[0] == 'M') {
20 scanf("%d", &y);
21 x = find(x), y = find(y);
22 p[x] = y;
23 r[x] = d[y];
24 d[y] += d[x];
25 } else {
26 find(x);
27 printf("%d\n", r[x]);
28 }
29 }
30 return 0;
31 }