#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#include <unordered_map>
#define ms(a,b) memset((a),(b),sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
const int mod = 1e9 + 7;
const int P = 131;
const int N = 30010;
const int inf = 0x3f3f3f3f;
const ll lnf = 9e18;
int n;
int p[N], d[N], cnt[N];
int find( int x ) {
if( p[x] != x ) {
int t = find( p[x] );
d[x] += d[p[x]];
p[x] = t;
}
return p[x];
}
int main() {
for( int i = 1; i <= N; i++ ) {
p[i] = i;
d[i] = 0;
cnt[i] = 1;
}
cin >> n;
while( n-- ) {
string op;
int x, y;
cin >> op >> x;
if( op == "M" ) {
cin >> y;
int fx = find( x ), fy = find( y );
if( fx != fy ) {
p[fx] = fy;
d[fx] = cnt[fy];
cnt[fy] += cnt[fx];
}
}
else {
find( x );
cout << d[x] << endl;
}
}
return 0;
}