/*
Date - 15/9/18
Kishore P
Problem :- SPOJ - QTREE
*/
#include <bits/stdc++.h>
using namespace std;
const int maxn=10004;
int ptr,chain_no;
vector<int> v[maxn],edg_cost[maxn],index1[maxn];
int par[maxn][14],depth[maxn],sub[maxn];
int chain_head[maxn],other_end[maxn],chain_ind[maxn];
int pos_in_arr[maxn],arr[maxn],seg[maxn];
// ----------------------------- DFS and LCA -----------------------------------//
void dfs(int s,int frm,int d)
{
depth[s]=d;
par[s][0]=frm;
sub[s]=1;
int i,ind;
for(i=1;par[s][i-1]!=-1;i++)
par[s][i]=par[par[s][i-1]][i-1];
for(i=0;i<(int)v[s].size();i++)
{
ind=v[s][i];
if(ind!=frm)
{
other_end[index1[s][i]]=ind;
dfs(ind,s,d+1);
sub[s]+=sub[ind];
}
}
}
int lca(int x,int y)
{
if(depth[y] > depth[x])
swap(x,y);
int i;
for(i=13;i>=0;i--)
{if(par[x][i]!=-1 && (depth[par[x][i]] >= depth[y]))
x=par[x][i];
}
if(x==y)
return x;
for(i=13;i>=0;i--)
{
if(par[x][i]!=par[y][i])
{
x=par[x][i];
y=par[y][i];
}
}
return par[x][0];
}
// ---------------------SEGMENT TREE OPERATIONS ---------------- //
void build_seg(int low,int high,int pos)
{
if(low==high)
{
seg[pos]=arr[low];
return;
}
int mid=(low+high)>>1;
build_seg(low,mid,2*pos+1);
build_seg(mid+1,high,2*pos+2);
seg[pos]=(seg[2*pos+1]>seg[2*pos+2])?seg[2*pos+1]:seg[2*pos+2];
}
void update_seg(int low,int high,int pos,int ind,int val)
{
if(low==high)
{
seg[pos]=val;
return ;
}
int mid=(low+high)>>1;
if(ind<=mid)
update_seg(low,mid,2*pos+1,ind,val);
else
update_seg(mid+1,high,2*pos+2,ind,val);
seg[pos]=(seg[2*pos+1]>seg[2*pos+2])?seg[2*pos+1]:seg[2*pos+2];
}
int query_seg(int low,int high,int qlow,int qhigh,int pos)
{
if(high<qlow || qhigh<low)
return 0;
else if(qlow<=low && high<=qhigh)
return seg[pos];
int mid=(low+high)>>1;
int l=query_seg(low,mid,qlow,qhigh,2*pos+1);
int r=query_seg(mid+1,high,qlow,qhigh,2*pos+2);
return (l > r? l : r);
}
// --------------------------- Heavy_light Decomposition Building -------------------------------//
void HLD(int curr,int prev,int cost)
{
if(chain_head[chain_no]==-1)
{
chain_head[chain_no]=curr;
}
chain_ind[curr]=chain_no;
++ptr;
pos_in_arr[curr]=ptr;
arr[ptr]=cost;
int i,ind,mxid=-1,ncost;
for(i=0;i<(int)v[curr].size();i++)
{
ind=v[curr][i];
if(ind!=prev)
{
if(mxid==-1 || (sub[ind] > sub[mxid]))
{
mxid=ind;
ncost=edg_cost[curr][i];
}
}
}
if(mxid!=-1)
{
HLD(mxid,curr,ncost);
}
for(i=0;i<(int)v[curr].size();i++)
{
ind=v[curr][i];
if(ind!=prev)
{
if(ind!=mxid)
{
chain_no++;
HLD(ind,curr,edg_cost[curr][i]);
}
}
}
}
//------------------------- Using HLD to answer queries and updates ---------------------//
void update(int num,int val)
{
int x=other_end[num];
update_seg(0,ptr,0,pos_in_arr[x],val);
}
int query_up(int x,int y) // y is ancestor of x
{
if(x==y)
return 0;
int xchain,ychain=chain_ind[y],ans=0,temp;
while(1)
{
xchain=chain_ind[x];
if(xchain==ychain)
{
if(x==y)
break;
temp=query_seg(0,ptr,pos_in_arr[y]+1,pos_in_arr[x],0);
if(temp > ans)
ans=temp;
break;
}
int head=chain_head[xchain];
temp=query_seg(0,ptr,pos_in_arr[head],pos_in_arr[x],0);
if(temp > ans)
ans=temp;
x=par[head][0];
}
return ans;
}
int query(int x,int y)
{
int L=lca(x,y);
int ans1=query_up(x,L);
int ans2=query_up(y,L);
return ((ans1>ans2)?ans1:ans2);
}
//------------------------------------------------------------------------------------
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
int i,j,a,b,c;
for(i=0;i<=n;i++)
{
v[i].clear();
edg_cost[i].clear();
index1[i].clear();
chain_head[i]=-1;
for(j=0;j<14;j++)
par[i][j]=-1;
}
for(i=1;i<n;i++)
{
scanf("%d %d %d",&a,&b,&c);
v[a].push_back(b);
v[b].push_back(a);
edg_cost[a].push_back(c);
edg_cost[b].push_back(c);
index1[a].push_back(i);
index1[b].push_back(i);
}
dfs(1,-1,0);
ptr=-1;chain_no=0;
HLD(1,-1,0); // (root,frm,cost)
build_seg(0,ptr,0);
while(1)
{
char s[100];
scanf("%s",s);
if(s[0]=='D')
break;
if(s[0]=='Q')
{
scanf("%d %d",&a,&b);
printf("%d\n",query(a,b));
}
else
{
scanf("%d %d",&a,&c);
update(a,c);
}
}
}
return 0;
}
LyogCiBEYXRlIC0gMTUvOS8xOAogS2lzaG9yZSBQCiBQcm9ibGVtIDotIFNQT0ogLSBRVFJFRQoqLwojaW5jbHVkZSA8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKY29uc3QgaW50IG1heG49MTAwMDQ7CmludCBwdHIsY2hhaW5fbm87Cgp2ZWN0b3I8aW50PiB2W21heG5dLGVkZ19jb3N0W21heG5dLGluZGV4MVttYXhuXTsKaW50IHBhclttYXhuXVsxNF0sZGVwdGhbbWF4bl0sc3ViW21heG5dOwoKaW50IGNoYWluX2hlYWRbbWF4bl0sb3RoZXJfZW5kW21heG5dLGNoYWluX2luZFttYXhuXTsKaW50IHBvc19pbl9hcnJbbWF4bl0sYXJyW21heG5dLHNlZ1ttYXhuXTsKCi8vIC0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tIERGUyBhbmQgTENBIC0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLy8Kdm9pZCBkZnMoaW50IHMsaW50IGZybSxpbnQgZCkKewogZGVwdGhbc109ZDsgICAgIAogcGFyW3NdWzBdPWZybTsKIHN1YltzXT0xOwogCiBpbnQgaSxpbmQ7CiAKIGZvcihpPTE7cGFyW3NdW2ktMV0hPS0xO2krKykKIHBhcltzXVtpXT1wYXJbcGFyW3NdW2ktMV1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W2NoYWluX25vXT09LTEpCiAgewogICBjaGFpbl9oZWFkW2NoYWluX25vXT1jdXJyOwogIH0KICAKICBjaGFpbl9pbmRbY3Vycl09Y2hhaW5fbm87CiAgKytwdHI7CiAgcG9zX2luX2FycltjdXJyXT1wdHI7CiAgYXJyW3B0cl09Y29zdDsgICAgCiAgICAgIAogIGludCBpLGluZCxteGlkPS0xLG5jb3N0OwogIAogIGZvcihpPTA7aTwoaW50KXZbY3Vycl0uc2l6ZSgpO2krKykgCiAgewogICAgaW5kPXZbY3Vycl1baV07ICAKICAgIGlmKGluZCE9cHJldikKICAgIHsKICAgICAgaWYobXhpZD09LTEgfHwgKHN1YltpbmRdID4gc3ViW214aWRdKSkKICAgICAgewogICAgICAgbXhpZD1pbmQ7CiAgICAgICBuY29zdD1lZGdfY29zdFtjdXJyXVtpXTsKICAgICAgfQogICAgfQogIH0KICAgIAogIGlmKG14aWQhPS0xKQogIHsKICAgSExEKG14aWQsY3VycixuY29zdCk7CiAgfQogIAogIGZvcihpPTA7aTwoaW50KXZbY3Vycl0uc2l6ZSgpO2krKykKICB7CiAgIGluZD12W2N1cnJdW2ldOyAgICAKICAgaWYoaW5kIT1wcmV2KQogICB7CiAgICAgaWYoaW5kIT1teGlkKQogICAgIHsKICAgICAgY2hhaW5fbm8rKzsKICAgICAgSExEKGluZCxjdXJyLGVkZ19jb3N0W2N1cnJdW2ldKTsKICAgICB9CiAgIH0KICB9Cn0KCi8vLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLSBVc2luZyBITEQgdG8gYW5zd2VyIHF1ZXJpZXMgYW5kIHVwZGF0ZXMgLS0tLS0tLS0tLS0tLS0tLS0tLS0tLy8KCnZvaWQgdXBkYXRlKGludCBudW0saW50IHZhbCkKewogaW50IHg9b3RoZXJfZW5kW251bV07CiB1cGRhdGVfc2VnKDAscHRyLDAscG9zX2luX2Fyclt4XSx2YWwpOwp9CgppbnQgcXVlcnlfdXAoaW50IHgsaW50IHkpIC8vIHkgaXMg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AogICAgaW50IGksaixhLGIsYzsKICAgIAogICAgZm9yKGk9MDtpPD1uO2krKykKICAgIHsKICAgICAgdltpXS5jbGVhcigpOwogICAgICBlZGdfY29zdFtpXS5jbGVhcigpOwogICAgICBpbmRleDFbaV0uY2xlYXIoKTsKICAgICAgCiAgICAgIGNoYWluX2hlYWRbaV09LTE7CiAgICAgICAKICAgICAgZm9yKGo9MDtqPDE0O2orKykKICAgICAgcGFyW2ldW2pdPS0xOwogICAgfQogICAgCiAgICBmb3IoaT0xO2k8bjtpKyspCiAgICB7CiAgICAgc2NhbmYoIiVkICVkICVkIiwmYSwmYiwmYyk7CiAgICAgCiAgICAgdlthXS5wdXNoX2JhY2soYik7CiAgICAgdltiXS5wdXNoX2JhY2soYSk7CiAgICAgCiAgICAgZWRnX2Nvc3RbYV0ucHVzaF9iYWNrKGMpOwogICAgIGVkZ19jb3N0W2JdLnB1c2hfYmFjayhjKTsKICAgICAKICAgICBpbmRleDFbYV0ucHVzaF9iYWNrKGkpOwogICAgIGluZGV4MVtiXS5wdXNoX2JhY2soaSk7CiAgICB9CiAgICAKICAgIGRmcygxLC0xLDApOwogICAgCiAgICBwdHI9LTE7Y2hhaW5fbm89MDsKICAgIEhMRCgxLC0xLDApOyAvLyAocm9vdCxmcm0sY29zdCkgIAogICAgYnVpbGRfc2VnKDAscHRyLDApOwogICAgCiAgICB3aGlsZSgxKQogICAgewogICAgIGNoYXIgc1sxMDBdOwogICAgIHNjYW5mKCIlcyIscyk7CiAgICAgCiAgICAgaWYoc1swXT09J0QnKQogICAgIGJyZWFrOwogICAgIAogICAgICBpZihzWzBdPT0nUScpCiAgICAgewogICAgICBzY2FuZigiJWQgJWQiLCZhLCZiKTsKICAgICAgcHJpbnRmKCIlZFxuIixxdWVyeShhLGIpKTsKICAgICB9CiAgICAgCiAgICAgZWxzZQogICAgIHsKICAgICAgc2NhbmYoIiVkICVkIiwmYSwmYyk7CiAgICAgIHVwZGF0ZShhLGMpOwogICAgIH0KICAgIAogICAgfQogICAgCiAgIH0gICAgCiAgICAKICAgIHJldHVybiAwOwp9