#include<bits/stdc++.h>
#define rep(i_,a,b) for (int i_=(a);i_<=(b);i_++)
#define clean(a) memset(a,0,sizeof(a));
using namespace std;
const int maxn=100010;
int n,test,len,nn,root,m,tt;
long long ww;
int own[maxn],last[maxn],siz[maxn],dep[maxn],oo[maxn],fa[maxn];
struct edge{
int y,next;
}e[maxn*2];
struct tree{
int l,r,mark;
long long sum;
}tr[maxn*4];
void ins(int x,int y){
e[++len].y=y;e[len].next=last[x];last[x]=len;
e[++len].y=x;e[len].next=last[y];last[y]=len;
}
void dfs(int x){
int i=last[x];
siz[x]=1;
while (i!=0){
if (siz[e[i].y]==0){
dep[e[i].y]=dep[x]+1;
fa[e[i].y]=x;
dfs(e[i].y);
siz[x]+=siz[e[i].y];
}
i=e[i].next;
}
}
void build_tr(int x,int y){
int i=last[x],j=0;
own[x]=y;
oo[x]=++nn;
if (siz[x]==1) return;
while (i!=0){
if (siz[x]>siz[e[i].y]){
if (siz[e[i].y]>siz[j] || j==0) j=e[i].y;
}
i=e[i].next;
}
build_tr(j,y);
i=last[x];
while (i!=0){
if (e[i].y!=j && siz[x]>siz[e[i].y]) build_tr(e[i].y,e[i].y);
i=e[i].next;
}
}
void release(int now){
if (tr[now].l==tr[now].r) return;
if (tr[now].mark!=0){
int t=tr[now].mark;
tr[now*2].mark+=t;
tr[now*2].sum+=t*(tr[now*2].r-tr[now*2].l+1);
tr[now*2+1].mark+=t;
tr[now*2+1].sum+=t*(tr[now*2+1].r-tr[now*2+1].l+1);
}
tr[now].mark=0;
}
void updata(int now){
release(now);
tr[now].sum=tr[now*2].sum+tr[now*2+1].sum;
}
void build(int now ,int l ,int r){
tr[now].l=l;
tr[now].r=r;
int mid=(l+r)/2;
tr[now].sum=0;
if (l==r) return;
build(now*2,l,mid);
build(now*2+1,mid+1,r);
}
void cover(int now ,int l,int r,int mm){
if (l<=tr[now].l && tr[now].r<=r){
tr[now].mark+=mm;
tr[now].sum+=(long long )mm*(tr[now].r-tr[now].l+1);
return;
}
int mid=(tr[now].l+tr[now].r)/2;
if (r<tr[now].l) return;
if (tr[now].r<l) return;
cover(now*2,l,r,mm);
cover(now*2+1,l,r,mm);
updata(now);
}
int getsum(int now ,int l,int r){
if (l<=tr[now].l && tr[now].r<=r) return tr[now].sum;
release(now);
int mid=(tr[now].l+tr[now].r)/2;
if (r<=mid) return getsum(now*2,l,r); else
if (l>mid) return getsum(now*2+1,l,r);else {
return getsum(now*2,l,mid)+getsum(now*2+1,mid+1,r);
}
}
void add(int x,int mm){
while (x!=0){
cover(1,oo[own[x]],oo[x],mm);
x=fa[own[x]];
}
}
long long getans(int x){
long long wt=0;
while (x!=0){
wt+=getsum(1,oo[own[x]],oo[x]);
x=fa[own[x]];
}
return wt;
}
int main(){
freopen("out.txt","w",stdout);
freopen("in.txt","r",stdin);
scanf("%d",&test);
rep(ii,1,test){
scanf("%d",&n);
ww=0;
len=0;
clean(last);
clean(fa);
clean(siz);
clean(oo);
clean(tr);
rep(i,1,n-1){
int x,y;
scanf("%d%d",&x,&y);
ins(x,y);
}
own[(n+1)/2]=(n+1)/2;
root=(n+1)/2;
dep[(n+1)/2]=1;
dfs((n+1)/2);
nn=0;
build_tr((n+1)/2,(n+1)/2);
build(1,1,n);
//--------
tt=0;ww=0;
scanf("%d",&m);
rep(i,1,m){
int p;
scanf("%d",&p);
if (p){
int x;
scanf("%d",&x);
printf("%I64d\n",ww-2*getans(x)+(long long)tt*(long long)dep[x]);
}else {
int x,w;
scanf("%d%d",&x,&w);
ww+=dep[x]+w;
add(x,1);
tt++;
}
}
}
return 0;
}
解法是十分显然的:每个操作的贡献是dep[x]+dep[y]+w-2*dep[lca(x,y)] 考虑最后一项怎么维护即可。。。对于每个操作,从该点到根加一即可。。。。。询问就是求点到根的和。。。。。。