#include<bits/stdc++.h>
using namespace std;
const int M=100010;
map<int,int>mp;
map<int,int>::iterator t;
struct node{
int d,l,to;
}edge[2*M];
int len,index,a[M],b[M],dfn[M],dis[M],dep[M],p[M][30];
void add(int x,int y,int k){
edge[++len]=(node){y,k,a[x]};
a[x]=len;
}
void dfs(int x,int pre){
dfn[x]=++index;
int i,y,k;
for(i=a[x];i!=-1;i=edge[i].to){
y=edge[i].d;
k=edge[i].l;
if(y==pre)continue;
dis[y]=dis[x]+k;
dep[y]=dep[x]+1;
p[y][0]=x;
dfs(y,x);
}
}
int lca(int x,int y){
if(dep[x]<dep[y]){
int t=x;
x=y;
y=t;
}
int j,m=0;
for(;(1<<m)<=dep[x];m++);
m--;
for(j=m;j>=0;j--)
if(dep[x]-(1<<j)>=dep[y])
x=p[x][j];
if(x==y)return x;
for(j=m;j>=0;j--)
if(p[x][j]&&p[x][j]!=p[y][j]){
x=p[x][j];
y=p[y][j];
}
return p[x][0];
}
int main(){
int n,q,i,j,x,y,k,w,l1,l2,l3,ans,kase,left,right;
scanf("%d",&kase);
for(w=1;w<=kase;w++){
scanf("%d%d",&n,&q);
ans=len=index=0;
memset(p,0,sizeof(p));
memset(a,-1,sizeof(a));
for(i=1;i<n;i++){
scanf("%d%d%d",&x,&y,&k);
add(x,y,k);
add(y,x,k);
}
dis[1]=dep[1]=0;
dfs(1,-1);
for(j=1;(1<<j)<=n;j++)
for(i=1;i<=n;i++)
if(p[i][j-1]&&p[p[i][j-1]][j-1])
p[i][j]=p[p[i][j-1]][j-1];
mp.clear();
memset(b,0,sizeof(b));
mp[1e9]=0;
printf("Case #%d:\n",w);
for(i=1;i<=q;i++){
scanf("%d%d",&y,&x);
if(y==1&&b[x]==0||y==2&&b[x]==1){
if(y==2)
mp.erase(dfn[x]);
t=mp.lower_bound(dfn[x]);
if(t!=mp.begin()&&t->first!=1e9){
right=(t--)->second;
left=t->second;
}
else{
t=mp.begin();
left=t->second;
t=mp.end();
t--;t--;
right=t->second;
}
if(y==1)
mp[dfn[x]]=x;
if(left==0)left=x;
if(right==0)right=x;
l1=lca(x,left);
l2=lca(x,right);
l3=lca(left,right);
if(y==1)ans+=dis[x]-dis[l1]-dis[l2]+dis[l3];
else ans-=dis[x]-dis[l1]-dis[l2]+dis[l3];
if(y==1)b[x]=1;
if(y==2)b[x]=0;
}
printf("%d\n",ans);
}
}
return 0;
}