#include<bits/stdc++.h>
using namespace std;
#define mx 10005
#define ll long long
int n,m;
int a,b,c,d,x,y;
vector< int >g[mx];
int depth[mx],subsize[mx],heavy[mx];
int par[mx],chain[mx],head[mx],num[mx];
int tree[3*mx];
int cost[mx];
void init(int pos,int lo,int hi)
{
if(lo==hi)
{
tree[pos]=cost[lo];
return;
}
int mid=(lo+hi)/2;
int left=2*pos;
int right=2*pos+1;
init(left,lo,mid);
init(right,mid+1,hi);
tree[pos]=max(tree[left],tree[right]);
}
void update(int pos,int lo,int hi,int l,int r,int val)
{
if(lo>r||hi<l)return;
if(lo==l&&hi==r)
{
tree[pos]=val;
return;
}
int mid=(lo+hi)/2;
int left=2*pos;
int right=2*pos+1;
update(left,lo,mid,l,r,val);
update(right,mid+1,hi,l,r,val);
tree[pos]=max(tree[left],tree[right]);
}
int query(int pos,int lo,int hi,int l,int r)
{
if(lo>r||hi<l)return 0;
if(lo>=l&&hi<=r)
{
return tree[pos];
}
int mid=(lo+hi)/2;
int left=2*pos;
int right=2*pos+1;
return max(query(left,lo,mid,l,r),query(right,mid+1,hi,l,r));
}
void dfs(int src)
{
subsize[src]=1;
for(int i=0;i<g[src].size();i++)
{
int u=g[src][i];
if(u==par[src])continue;
par[u]=src;
depth[u]=depth[src]+1;
dfs(u);
subsize[src]+=subsize[u];
if(heavy[src]==-1||subsize[u]>subsize[heavy[src]])
{
heavy[src]=u;
}
}
}
void heavydfs(int src)
{
memset(par,0,sizeof(par));
memset(heavy,-1,sizeof(heavy));
par[1]=0;
depth[1]=0;
dfs(src);
int c=0,ptr=1;
for(int i=1;i<=n;i++)
{
if(par[i]==-1|| heavy[par[i]]!=i)
{
for(int k=i;k!=-1;k=heavy[k])
{
chain[k]=c;
head[k]=i;
num[k]=ptr++;
}
c++;
}
}
}
int lca(int i,int j)
{
while(chain[i]!=chain[j])
{
if(depth[head[i]]>depth[head[j]])
{
i=par[head[i]];
}
else j=par[head[j]];
}
return depth[i]<depth[j]?i:j;
}
int query(int a,int lca)
{
int res=0;
while(chain[a]!=chain[lca])
{
res=max(res,query(1,1,n,num[head[a]],num[a]));
a=par[head[a]];
}
if(a!=lca)res=max(res,query(1,1,n,num[lca]+1,num[a]));
return res;
}
int main()
{
int tst;
scanf("%d",&tst);
while(tst--)
{
scanf("%d",&n);
vector<pair<int,pair<int,int> > >edge;
for(int i=1;i<n;i++)
{
scanf("%d %d %d",&a,&b,&c);
g[a].push_back(b);
g[b].push_back(a);
edge.push_back(make_pair(c,make_pair(a,b)));
}
heavydfs(1);
for(int i=0;i<n-1;i++)
{
x=edge[i].second.first;
y=edge[i].second.second;
int v=depth[x]>depth[y]?x:y;
cost[num[v]]=edge[i].first;
}
init(1,1,n);
string s;
while(cin>>s)
{
if(s=="DONE")break;
scanf("%d %d",&x,&y);
if(s=="CHANGE")
{
x--;
edge[x].first=y;
a=edge[x].second.first;
b=edge[x].second.second;
int v=depth[a]>depth[b]?a:b;
update(1,1,n,num[v],num[v],y);
}
else if(s=="QUERY")
{
int k=lca(x,y);
printf("%d\n",max(query(x,k),query(y,k)));
}
}
for(int i=1;i<=n;i++)g[i].clear();
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBteCAxMDAwNQojZGVmaW5lIGxsIGxvbmcgbG9uZwppbnQgbixtOwppbnQgYSxiLGMsZCx4LHk7CnZlY3RvcjwgaW50ID5nW214XTsKCmludCBkZXB0aFtteF0sc3Vic2l6ZVtteF0saGVhdnlbbXhdOwppbnQgcGFyW214XSxjaGFpbltteF0saGVhZFtteF0sbnVtW214XTsKaW50IHRyZWVbMypteF07CmludCBjb3N0W214XTsKCnZvaWQgaW5pdChpbnQgcG9zLGludCBsbyxpbnQgaGkpCnsKICAgIGlmKGxvPT1oaSkKICAgIHsKICAgICAgICB0cmVlW3Bvc109Y29zdFtsb107CiAgICAgICAgcmV0dXJuOwogICAgfQogICAgaW50IG1pZD0obG8raGkpLzI7CiAgICBpbnQ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HU9Z1tzcmNdW2ldOwogICAgICAgIGlmKHU9PXBhcltzcmNdKWNvbnRpbnVlOwogICAgICAgIHBhclt1XT1zcmM7CiAgICAgICAgZGVwdGhbdV09ZGVwdGhbc3JjXSsxOwogICAgICAgIGRmcyh1KTsKICAgICAgICBzdWJzaXplW3NyY10rPXN1YnNpemVbdV07CiAgICAgICAgaWYoaGVhdnlbc3JjXT09LTF8fHN1YnNpemVbdV0+c3Vic2l6ZVtoZWF2eVtzcmNdXSkKICAgICAgICB7CiAgICAgICAgICAgIGhlYXZ5W3NyY109dTsKICAgICAgICB9CiAgICB9Cn0KCnZvaWQgaGVhdnlkZnMoaW50IHNyYykKewogICAgbWVtc2V0KHBhciwwLHNpemVvZihwYXIpKTsKICAgIG1lbXNldChoZWF2eSwtMSxzaXplb2YoaGVhdnkpKTsKCiAgICBwYXJbMV09MDsKICAgIGRlcHRoWzFdPTA7CiAgICBkZnMoc3JjKTsKICAgIGludCBjPTAscHRyPTE7CiAgICBmb3IoaW50IGk9MTtpPD1uO2krKykKICAgIHsKICAgICAgICBpZihwYXJbaV09PS0xfHwgaGVhdnlbcGFyW2ldXSE9aSkKICAgICAgICB7CiAgICAgICAgICAgIGZvcihpbnQgaz1pO2shPS0xO2s9aGVhdnlba10pCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGNoYWluW2tdPWM7CiAgICAgICAgICAgICAgICBoZWFkW2tdPWk7CiAgICAgICAgICAgICAgICBudW1ba109cHRyKys7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgYysrOwogICAgICAgIH0KICAgIH0KfQoKaW50IGxjYShpbnQgaSxpbnQgaikKewogICAgd2hpbGUoY2hhaW5baV0hPWNoYWluW2pdKQogICAgewogICAgICAgIGlmKGRlcHRoW2hlYWRbaV1dPmRlcHRoW2hlYWRbal1dKQogICAgICAgIHsKICAgICAgICAgICAgaT1wYXJbaGVhZFtpXV07CiAgICAgICAgfQogICAgICAgIGVsc2Ugaj1wYXJbaGVhZFtqXV07CiAgICB9CiAgICByZXR1cm4gZGVwdGhbaV08ZGVwdGhbal0/aTpqOwp9CgoKaW50IHF1ZXJ5KGludCBhLGludCBsY2EpCnsKICAgIGludCByZXM9MDsKICAgIHdoaWxlKGNoYWluW2FdIT1jaGFpbltsY2FdKQogICAgewogICAgICAgIHJlcz1tYXgocmVzLHF1ZXJ5KDEsMSxuLG51bVtoZWFkW2FdXSxudW1bYV0pKTsKICAgICAgICBhPXBhcltoZWFkW2FdXTsKICAgIH0KICAgIGlmKGEhPWxjYSlyZXM9bWF4KHJlcyxxdWVyeSgxLDEsbixudW1bbGNhXSsxLG51bVthXSkpOwogICAgcmV0dXJuIHJlczsKCn0KCmludCBtYWluKCkKewogICAgaW50IHRzdDsKICAgIHNjYW5mKCIlZCIsJnRzdCk7CiAgICB3aGlsZSh0c3QtLSkKICAgIHsKICAgICAgICBzY2FuZigiJWQiLCZuKTsKICAgICAgICB2ZWN0b3I8cGFpcjxpbnQscGFpcjxpbnQsaW50PiA+ID5lZGdlOwogICAgICAgIGZvcihpbnQgaT0xO2k8bjtpKyspCiAgICAgICAgewogICAgICAgICAgICBzY2FuZigiJWQgJWQgJWQiLCZhLCZiLCZjKTsKICAgICAgICAgICAgZ1thXS5wdXNoX2JhY2soYik7CiAgICAgICAgICAgIGdbYl0ucHVzaF9iYWNrKGEpOwogICAgICAgICAgICBlZGdlLnB1c2hfYmFjayhtYWtlX3BhaXIoYyxtYWtlX3BhaXIoYSxiKSkpOwogICAgICAgIH0KICAgICAgICBoZWF2eWRmcygxKTsKICAgICAgICBmb3IoaW50IGk9MDtpPG4tMTtpKyspCiAgICAgICAgewogICAgICAgICAgICB4PWVkZ2VbaV0uc2Vjb25kLmZpcnN0OwogICAgICAgICAgICB5PWVkZ2VbaV0uc2Vjb25kLnNlY29uZDsKICAgICAgICAgICAgaW50IHY9ZGVwdGhbeF0+ZGVwdGhbeV0/eDp5OwogICAgICAgICAgICBjb3N0W251bVt2XV09ZWRnZVtpXS5maXJzdDsKICAgICAgICB9CiAgICAgICAgaW5pdCgxLDEsbik7CgoKICAgICAgICBzdHJpbmcgczsKICAgICAgICB3aGlsZShjaW4+PnMpCiAgICAgICAgewogICAgICAgICAgICBpZihzPT0iRE9ORSIpYnJlYWs7CiAgICAgICAgICAgIHNjYW5mKCIlZCAlZCIsJngsJnkpOwogICAgICAgICAgICBpZihzPT0iQ0hBTkdFIikKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgeC0tOwogICAgICAgICAgICAgICAgZWRnZVt4XS5maXJzdD15OwogICAgICAgICAgICAgICAgYT1lZGdlW3hdLnNlY29uZC5maXJzdDsKICAgICAgICAgICAgICAgIGI9ZWRnZVt4XS5zZWNvbmQuc2Vjb25kOwogICAgICAgICAgICAgICAgaW50IHY9ZGVwdGhbYV0+ZGVwdGhbYl0/YTpiOwogICAgICAgICAgICAgICAgdXBkYXRlKDEsMSxuLG51bVt2XSxudW1bdl0seSk7CgoKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlIGlmKHM9PSJRVUVSWSIpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGludCBrPWxjYSh4LHkpOwogICAgICAgICAgICAgICAgcHJpbnRmKCIlZFxuIixtYXgocXVlcnkoeCxrKSxxdWVyeSh5LGspKSk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgZm9yKGludCBpPTE7aTw9bjtpKyspZ1tpXS5jbGVhcigpOwogICAgfQp9Cg==