#include<cmath>
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
#define MX 10004
#define lft n<<1
#define ryt (n<<1)+1
#define mid (int)((b+e)>>1)
#define lson lft,b,mid
#define rson ryt,mid+1,e
#define N tree[n]
#define L tree[lft]
#define R tree[ryt]
////////////////////////////////////////
////////////////////////////////////////
vector<int>g[MX],cost[MX],index[MX];
int lvl[MX],p[32][MX]; //for Sparse Table
int visited[MX],uv[MX]; // for dfs
int hld[MX],hldpos[MX],chainid[MX],head[MX],sub[MX],chain,pos; // for HLD
int tree[4*MX]; //segment tree
//OK
void dfs(int u)
{
visited[u] = 1;
int i,v,sz=g[u].size();
for(i=0;i<sz;i++){
v = g[u][i];
if(!visited[v]){
sub[v]++;
uv[index[u][i]] = v;
p[0][v] = u;
lvl[v] = lvl[u] + 1;
dfs(v);
sub[u] += sub[v];
}
}
}
//OK
void SparseTable(int n)
{
int i,j,lg=log2(n);
for(i=1;i<=lg;i++){
for(j=1;j<=n;j++){
int pp = p[i-1][j];
if(pp==-1) p[i][j] = -1;
else p[i][j] = p[i-1][pp];
}
}
}
//OK
int lca(int n, int u, int v)
{
if(lvl[u]<lvl[v]) swap(u,v);
int i,j,lg=log2(n);
for(i=lg;i>=0;i--){
int uu=p[i][u];
if(uu!=-1 && lvl[u]-(1<<i)>=lvl[v]) u=uu;
}
if(u==v) return u;
for(i=lg;i>=0;i--){
int uu = p[i][u];
int vv = p[i][v];
if(uu!=-1 && vv!=-1 && uu!=vv) u=uu,v=vv;
}
return p[0][u];
}
//OK
void HLD(int u, int c, int pre)
{
if(!head[chain]) head[chain] = u;
chainid[u] = chain;
hldpos[u] = pos;
hld[pos] = c;
pos++;
int heavy = 0, hsub=0;
int i,v,hcost,sz=g[u].size();
for(i=0;i<sz;i++){
v = g[u][i];
if(v!=pre)
if(sub[v]>hsub){
heavy = v;
hsub = sub[v];
hcost = cost[u][i];
}
}
if(heavy) HLD(heavy,hcost,u);
for(i=0;i<sz;i++){
v = g[u][i];
if(v!=pre)
if(v!=heavy){
chain++;
HLD(v,cost[u][i],u);
}
}
}
//OK
void Build(int n, int b, int e)
{
if(b==e){
N = hld[b];
return;
}
Build(lson);
Build(rson);
N = max(L,R);
}
//OK
void Update(int n, int b, int e, int x, int v)
{
if(b>x || e<x) return;
if(b==e && b==x){
N = v;
return;
}
Update(lson,x,v);
Update(rson,x,v);
N = max(L,R);
}
//OK
int Find(int n, int b, int e, int x, int y)
{
if(b>y || e<x) return 0;
if(b>=x && e<=y) return N;
int f1 = Find(lson,x,y);
int f2 = Find(rson,x,y);
return max(f1,f2);
}
//OK
int query(int n, int u, int v)
{
if(u==v) return 0;
int uchain,vchain,i,r=0;
vchain = chainid[v];
while(1){
if(u==v) return r;
uchain = chainid[u];
int up = hldpos[u];
int vp = hldpos[v];
int hp = hldpos[head[uchain]];
if(uchain==vchain){
if(up>vp) swap(up,vp);
r = max(r,Find(1,1,n,up,vp));
return r;
}
r = max(r,Find(1,1,n,hp,up));
u = head[uchain];
u = p[0][u];
}
}
//OK
int main()
{
int i,j,n,test,u,v,w;
scanf("%d",&test);
while(test--){
scanf("%d",&n);
for(i=0;i<=n;i++){
g[i].clear();
cost[i].clear();
index[i].clear();
visited[i]=0;
sub[i]=0;
head[i]=0;
}
for(i=1;i<n;i++){
scanf("%d%d%d",&u,&v,&w);
g[u].push_back(v);
g[v].push_back(u);
cost[u].push_back(w);
cost[v].push_back(w);
index[u].push_back(i);
index[v].push_back(i);
}
sub[1]=1;
lvl[1] = 0;
p[0][1] = -1;
dfs(1);
SparseTable(n);
chain=1;
pos=1;
HLD(1,0,0);
Build(1,1,n);
//for(i=1;i<pos;i++) cout<<hld[i]<<" "; cout<<endl;
//for(i=1;i<=3*n;i++) cout<<tree[i]<<" "; cout<<endl;
char s[100];
while(1){
scanf("%s",s);
if(s[0]=='D') break;
scanf("%d%d",&u,&v);
if(s[0]=='C'){
u = uv[u];
Update(1,1,n,hldpos[u],v);
}
if(s[0]=='Q'){
int _lca = lca(n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WJbdl0rKzsKICAgICAgICAgICAgdXZbaW5kZXhbdV1baV1dID0gdjsKICAgICAgICAgICAgcFswXVt2XSA9IHU7CiAgICAgICAgICAgIGx2bFt2XSA9IGx2bFt1XSArIDE7CiAgICAgICAgICAgIGRmcyh2KTsKICAgICAgICAgICAgc3ViW3VdICs9IHN1Ylt2XTsKICAgICAgICB9CiAgICB9Cn0KCgovL09LCnZvaWQgU3BhcnNlVGFibGUoaW50IG4pCnsKICAgIGludCBpLGosbGc9bG9nMihuKTsKICAgIGZvcihpPTE7aTw9bGc7aSsrKXsKICAgICAgICBmb3Ioaj0xO2o8PW47aisrKXsKICAgICAgICAgICAgaW50IHBwID0gcFtpLTFdW2pdOwogICAgICAgICAgICBpZihwcD09LTEpIHBbaV1bal0gPSAtMTsKICAgICAgICAgICAgZWxzZSBwW2ldW2pdID0gcFtpLTFdW3BwXTsKICAgICAgICB9CiAgICB9Cn0KCi8vT0sKaW50IGxjYShpbnQgbiwgaW50IHUsIGludCB2KQp7CiAgICBpZihsdmxbdV08bHZsW3ZdKSBzd2FwKHUsdik7CiAgICBpbnQgaSxqLGxnPWxvZzIobik7CiAgICBmb3IoaT1sZztpPj0wO2ktLSl7CiAgICAgICAgaW50IHV1PXBbaV1bdV07CiAgICAgICAgaWYodXUhPS0xICYmIGx2bFt1XS0oMTw8aSk+PWx2bFt2XSkgdT11dTsKICAgIH0KICAgIGlmKHU9PXYpIHJldHVybiB1OwogICAgZm9yKGk9bGc7aT49MDtpLS0pewogICAgICAgIGludCB1dSA9IHBbaV1bdV07CiAgICAgICAgaW50IHZ2ID0gcFtpXVt2XTsKICAgICAgICBpZih1dSE9LTEgJiYgdnYhPS0xICYmIHV1IT12dikgdT11dSx2PXZ2OwogICAgfQogICAgcmV0dXJuIHBbMF1bdV07Cn0KCi8vT0sKdm9pZCBITEQoaW50IHUsIGludCBjLCBpbnQgcHJlKQp7CiAgICBpZighaGVhZFtjaGFpbl0pIGhlYWRbY2hhaW5dID0gdTsKICAgIGNoYWluaWRbdV0gPSBjaGFpbjsKICAgIGhsZHBvc1t1XSA9IHBvczsKICAgIGhsZFtwb3NdID0gYzsKICAgIHBvcysrOwogICAgaW50IGhlYXZ5ID0gMCwgaHN1Yj0wOwogICAgaW50IGksdixoY29zdCxzej1nW3VdLnNpemUoKTsKICAgIGZvcihpPTA7aTxzejtpKyspewogICAgICAgIHYgPSBnW3VdW2ldOwogICAgICAgIGlmKHYhPXByZSkKICAgICAgICBpZihzdWJbdl0+aHN1Yil7CiAgICAgICAgICAgIGhlYXZ5ID0gdjsKICAgICAgICAgICAgaHN1YiA9IHN1Ylt2XTsKICAgICAgICAgICAgaGNvc3QgPSBjb3N0W3VdW2ldOwogICAgICAgIH0KICAgIH0KICAgIGlmKGhlYXZ5KSBITEQoaGVhdnksaGNvc3QsdSk7CiAgICBmb3IoaT0wO2k8c3o7aSsrKXsKICAgICAgICB2ID0gZ1t1XVtpXTsKICAgICAgICBpZih2IT1wcmUpCiAgICAgICAgaWYodiE9aGVhdnkpewogICAgICAgICAgICBjaGFpbisrOwogICAgICAgICAgICBITEQodixjb3N0W3VdW2ldLHUpOwogICAgICAgIH0KICAgIH0KfQoKLy9PSwp2b2lkIEJ1aWxkKGludCBuLCBpbnQgYiwgaW50IGUpCnsKICAgIGlmKGI9PWUpewogICAgICAgIE4gPSBobGRbYl07CiAgICAgICAgcmV0dXJuOwogICAgfQogICAgQnVpbGQobHNvbik7CiAgICBCdWlsZChyc29uKTsKICAgIE4gPSBtYXgoTCxSKTsKfQoKLy9PSwp2b2lkIFVwZGF0ZShpbnQgbiwgaW50IGIsIGludCBlLCBpbnQgeCwgaW50IHYpCnsKICAgIGlmKGI+eCB8fCBlPHgpIHJldHVybjsKICAgIGlmKGI9PWUgJiYgYj09eCl7CiAgICAgICAgTiA9IHY7CiAgICAgICAgcmV0dXJuOwogICAgfQogICAgVXBkYXRlKGxzb24seCx2KTsKICAgIFVwZGF0ZShyc29uLHgsdik7CiAgICBOID0gbWF4KEwsUik7Cn0KCi8vT0sKaW50IEZpbmQoaW50IG4sIGludCBiLCBpbnQgZSwgaW50IHgsIGludCB5KQp7CiAgICBpZihiPnkgfHwgZTx4KSByZXR1cm4gMDsKICAgIGlmKGI+PXggJiYgZTw9eSkgcmV0dXJuIE47CiAgICBpbnQgZjEgPSBGaW5kKGxzb24seCx5KTsKICAgIGludCBmMiA9IEZpbmQocnNvbix4LHkpOwogICAgcmV0dXJuIG1heChmMSxmMik7Cn0KCi8vT0sKaW50IHF1ZXJ5KGludCBuLCBpbnQgdSwgaW50IHYpCnsKICAgIGlmKHU9PXYpIHJldHVybiAwOwogICAgaW50IHVjaGFpbix2Y2hhaW4saSxyPTA7CiAgICB2Y2hhaW4gPSBjaGFpbmlkW3ZdOwogICAgd2hpbGUoMSl7CiAgICAgICAgaWYodT09dikgcmV0dXJuIHI7CiAgICAgICAgdWNoYWluID0gY2hhaW5pZFt1XTsKICAgICAgICBpbnQgdXAgPSBobGRwb3NbdV07CiAgICAgICAgaW50IHZwID0gaGxkcG9zW3ZdOwogICAgICAgIGludCBocCA9IGhsZHBvc1toZWFkW3VjaGFpbl1dOwogICAgICAgIGlmKHVjaGFpbj09dmNoYWluKXsKICAgICAgICAgICAgaWYodXA+dnApIHN3YXAodXAsdnApOwogICAgICAgICAgICByID0gbWF4KHIsRmluZCgxLDEsbix1cCx2cCkpOwogICAgICAgICAgICByZXR1cm4gcjsKICAgICAgICB9CiAgICAgICAgciA9IG1heChyLEZpbmQoMSwxLG4saHAsdXApKTsKICAgICAgICB1ID0gaGVhZFt1Y2hhaW5dOwogICAgICAgIHUgPSBwWzBdW3VdOwogICAgfQp9CgovL09LCmludCBtYWluKCkKewogICAgaW50IGksaixuLHRlc3QsdSx2LHc7CiAgICBzY2FuZigiJWQiLCZ0ZXN0KTsKICAgIHdoaWxlKHRlc3QtLSl7CiAgICAgICAgc2NhbmYoIiVkIiwmbik7CiAgICAgICAgZm9yKGk9MDtpPD1uO2krKyl7CiAgICAgICAgICAgIGdbaV0uY2xlYXIoKTsKICAgICAgICAgICAgY29zdFtpXS5jbGVhcigpOwogICAgICAgICAgICBpbmRleFtpXS5jbGVhcigpOwogICAgICAgICAgICB2aXNpdGVkW2ldPTA7CiAgICAgICAgICAgIHN1YltpXT0wOwogICAgICAgICAgICBoZWFkW2ldPTA7CiAgICAgICAgfQogICAgICAgIGZvcihpPTE7aTxuO2krKyl7CiAgICAgICAgICAgIHNjYW5mKCIlZCVkJWQiLCZ1LCZ2LCZ3KTsKICAgICAgICAgICAgZ1t1XS5wdXNoX2JhY2sodik7CiAgICAgICAgICAgIGdbdl0ucHVzaF9iYWNrKHUpOwogICAgICAgICAgICBjb3N0W3VdLnB1c2hfYmFjayh3KTsKICAgICAgICAgICAgY29zdFt2XS5wdXNoX2JhY2sodyk7CiAgICAgICAgICAgIGluZGV4W3VdLnB1c2hfYmFjayhpKTsKICAgICAgICAgICAgaW5kZXhbdl0ucHVzaF9iYWNrKGkpOwogICAgICAgIH0KICAgICAgICBzdWJbMV09MTsKICAgICAgICBsdmxbMV0gPSAwOwogICAgICAgIHBbMF1bMV0gPSAtMTsKICAgICAgICBkZnMoMSk7CiAgICAgICAgU3BhcnNlVGFibGUobik7CgogICAgICAgIGNoYWluPTE7CiAgICAgICAgcG9zPTE7CiAgICAgICAgSExEKDEsMCwwKTsKICAgICAgICBCdWlsZCgxLDEsbik7CiAgICAgICAgLy9mb3IoaT0xO2k8cG9zO2krKykgY291dDw8aGxkW2ldPDwiICI7IGNvdXQ8PGVuZGw7CiAgICAgICAgLy9mb3IoaT0xO2k8PTMqbjtpKyspIGNvdXQ8PHRyZWVbaV08PCIgIjsgY291dDw8ZW5kbDsKICAgICAgICBjaGFyIHNbMTAwXTsKICAgICAgICB3aGlsZSgxKXsKICAgICAgICAgICAgc2NhbmYoIiVzIixzKTsKICAgICAgICAgICAgaWYoc1swXT09J0QnKSBicmVhazsKICAgICAgICAgICAgc2NhbmYoIiVkJWQiLCZ1LCZ2KTsKICAgICAgICAgICAgaWYoc1swXT09J0MnKXsKICAgICAgICAgICAgICAgIHUgPSB1dlt1XTsKICAgICAgICAgICAgICAgIFVwZGF0ZSgxLDEsbixobGRwb3NbdV0sdik7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYoc1swXT09J1EnKXsKICAgICAgICAgICAgICAgIGludCBfbGNhID0gbGNhKG4sdSx2KTsKICAgICAgICAgICAgICAgIGludCB4ID0gcXVlcnkobix1LF9sY2EpOwogICAgICAgICAgICAgICAgaW50IHkgPSBxdWVyeShuLHYsX2xjYSk7CiAgICAgICAgICAgICAgICBwcmludGYoIiVkXG4iLG1heCh4LHkpKTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgIHJldHVybiAwOwp9Cg==