#include using namespace std; #define LL long long typedef pair pii; template< typename T > inline void read(T &x) { static char _c; static bool _f; x = 0; _f = 0; _c = getchar(); while(_c < '0' || '9' < _c) {if(_c == '-') _f = true; _c = getchar();} while('0' <= _c && _c <= '9') {x = (x << 1) + (x << 3) + (_c & 15); _c = getchar();} if(_f) x = -x; } template < typename T > inline void Min(T &x, T y) {if(y < x) x = y;} template < typename T > inline void Max(T &x, T y) {if(x < y) x = y;} #define lowbit(x) ((x) & -(x)) #define lson l,mid,id<<1 #define rson mid+1,r,id<<1|1 #define ls id<<1 #define rs id<<1|1 #define MID(l,r) ((l)+(((r)-(l))>>1)) #define fi first #define se second #define mk make_pair #define pb push_back const int MOD = (int) 1e9 + 7; //const int MOD = (int) 998244353; const int maxn = (int) 1e5 + 20; const int maxm = (int) 1e7 + 20; const double pi = (double) acos(-1.0); LL fp(LL a, LL n, LL m = MOD) {LL res = 1; for(; n; n >>= 1, a = a * a % m) if(n & 1) res = res * a % m; return res;} int n, q; int fa[maxn]; vector G[maxn]; int pre[20][maxn]; inline int find_fa(int x) { if(x == fa[x]) return x; return fa[x] = find_fa(fa[x]); } struct Node { int key, r; int sz; Node *ch[2]; Node() {ch[0] = ch[1] = NULL; r = rand();} inline int cmp(int x) {return x == key ? -1 : x > key;} }; void upd(Node *o) { if(o == NULL) return ; o->sz = 1; if(o->ch[0]) o->sz += o->ch[0]->sz; if(o->ch[1]) o->sz += o->ch[1]->sz; } void ro(Node *&o, int d) { Node *p = o->ch[!d]; o->ch[!d] = p->ch[d]; p->ch[d] = o; upd(o); upd(p); o = p; } void ins(Node *&o, int x) { if(o == NULL) { o = new Node; o->key = x; o->sz = 1; return ; } int d = o->cmp(x); if(d == -1) return ; ins(o->ch[d], x); if(o->ch[d]->r > o->r) ro(o, !d); upd(o); } int rk(Node *o, int x) { if(o == NULL) return 0; int d = o->cmp(x); if(d == -1) { if(o->ch[0]) return o->ch[0]->sz + 1; return 1; } if(d == 0) return rk(o->ch[d], x); else { int res = 1; if(o->ch[0]) res += o->ch[0]->sz; return res + rk(o->ch[d], x); } } int dep[maxn]; int flag[maxn]; Node *rt[maxn]; void dfs(int u, int f) { dep[u] = dep[f] + 1; pre[0][u] = f; for(int i = 1; i < 20; i++) pre[i][u] = pre[i - 1][pre[i - 1][u]]; rt[u] = NULL; ins(rt[u], u); for(int v : G[u]) { if(v == f) continue; dfs(v, u); } } void mer_tree(Node *o, Node *&top) { if(o == NULL) return ; ins(top, o->key); mer_tree(o->ch[0], top); mer_tree(o->ch[1], top); } void mer(int u, int top) { fa[u] = top; if(rt[u]->sz > rt[top]->sz) swap(rt[u], rt[top]); mer_tree(rt[u], rt[top]); } void dfs_update(int u, int top) { if(u != top) { mer(u, top); } if(flag[u]) return ; flag[u] = 1; for(int v : G[u]) { if(v == pre[0][u]) continue; dfs_update(v, top); } } int LCA(int x, int y) { if(dep[x] < dep[y]) swap(x, y); int c = dep[x] - dep[y]; for(int i = 0; i < 20; i++) if((c >> i) & 1) x = pre[i][x]; if(x == y) return x; for(int i = 19; ~i; i--) if(pre[i][x] != pre[i][y]) { x = pre[i][x]; y = pre[i][y]; } return pre[0][x]; } void work() { read(n); for(int i = 1; i <= n; i++) G[i].clear(); for(int i = 1; i < n; i++) { int x, y; read(x), read(y); G[x].push_back(y); G[y].push_back(x); } for(int i = 1; i <= n; i++) fa[i] = i; for(int i = 1; i <= n; i++) flag[i] = 0; dep[1] = 0; dfs(1, 1); read(q); while(q--) { int opt; read(opt); if(opt == 1) { int x; read(x); dfs_update(x, x); } else { int x, y; read(x), read(y); int fx = find_fa(x), fy = find_fa(y); // cout <sz - rk(rt[fx], x), ry = rt[fy]->sz - rk(rt[fy], y); ans += rx + ry; printf("%d\n", ans); } } } } int main() { #ifdef yukihana0416 freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); #endif // yukihana0416 int tc = 1; read(tc); for(int ca = 1; ca <= tc; ca++) { // printf("Case #%d: ", ca); work(); } return 0; }