void cut(int x, int y) { makeroot(x); access(y); splay(y); pre[ch[y][0]] = 0; ch[y][0] = 0; }
int main(){ while(cin >> n){ LCTInit(); for (int i = 1;i <= n;i++) siz[i] = 1; for (int i = 1;i < n;i++){ scanf("%d%d",&x,&y); link(x,y); } for (int i = 1;i <= n;i++) scanf("%d",&val[i]),mx[i] = sum[i] = val[i]; int q; scanf("%d",&q); while(q--){ int ty,z,x,y; scanf("%d",&ty); scanf("%d%d",&x,&y); if (ty == 1){ if (root(x) == root(y)){ printf("-1\n"); continue; } link(x,y); } if (ty == 2){ if (root(x) != root(y) || x == y){ printf("-1\n"); continue; } makeroot(x); cutf(y); } if (ty == 3){ scanf("%d",&z); if (root(z) != root(y)){ printf("-1\n"); continue; } makeroot(y); access(z); splay(z); add(z,x); } if (ty == 4){ if (root(x) != root(y)){ printf("-1\n"); continue; } makeroot(x); access(y); splay(y); printf("%d\n",mx[y]); } } printf("\n"); } }
void cut(int x,int y){ makeroot(x); cutf(y); }
void link(int x,int y){ makeroot(x); f[x] = y; access(x); }