#include<bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
using namespace std;
const int N=10100;
vector <int> edges[N], indices[N], cost[N];
int baseArray[N], ptr;
int chainNo, chainInd[N], chainHead[N], posInBase[N], indexx[N];
int depth[N], parent[N][30], subtree[N], seg[4*N] ,bottomvertexofedge[N];
int n;
// DFS Begin ---------------------------------------------------------------------------
void dfs(int s,int p,int level){
subtree[s]=1;
depth[s]=level;
parent[s][0]=p;
for(int bit=1;bit<15;bit++){
if(parent[s][bit-1]!=-1)
parent[s][bit]=parent[parent[s][bit-1]][bit-1];
}
for(int i=0;i<edges[s].size();i++){
int x=edges[s][i];
if(x==p) continue;
bottomvertexofedge[indices[s][i]]=x;
dfs(x,s,level+1);
subtree[s]+=subtree[x];
}
}
// DFS End ---------------------------------------------------------------------------
// LCA Start --------------------------------------------------------------------------
int getlca(int u, int v) {
if(depth[u] < depth[v]) swap(u,v);
int diff = depth[u] - depth[v];
for(int i=0; i<15; i++) if( (diff>>i)&1 ) u = parent[u][i];
if(u == v) return u;
for(int i=14; i>=0; i--) if(parent[u][i] != parent[v][i]) {
u = parent[u][i];
v = parent[v][i];
}
return parent[u][0];
}
// LCA End ------------------------------------------------------------------------------
// SEGTree Begin-------------------------------------------------------------------------
void update(int index,int data,int segindex,int l,int r)
{
if(l==r){
baseArray[l]=data;
seg[segindex]=data;
return ;
}
int mid=(l+r)/2;
if(index<=mid){
update(index,data,2*segindex+1,l,mid);
}else{
update(index,data,2*segindex+2,mid+1,r);
}
seg[segindex]=(seg[2*segindex+1]>seg[2*segindex+2])?seg[2*segindex+1]:seg[2*segindex+2];
}
int query(int index,int l,int r,int ql,int qr)
{
if(l>=ql && r<=qr) return seg[index];
if(l>qr || r<ql) return 0;
int mid=(l+r)/2;
int q1=query(2*index+1,l,mid,ql,qr);
int q2=query(2*index+2,mid+1,r,ql,qr);
return (q1>q2)?q1:q2;
}
void buildtree(int l,int r,int index)
{
if(l==r){
seg[index]=baseArray[l];
return;
}
int mid=(l+r)/2;
buildtree(l,mid,2*index+1);
buildtree(mid+1,r,2*index+2);
seg[index]=(seg[2*index+1]>seg[2*index+2])?seg[2*index+1]:seg[2*index+2];
}
// SEGTree End--------------------------------------------------------------------------
// HLD Begin ---------------------------------------------------------------------------
void hld(int s,int p,int _cost)
{
if(chainHead[chainNo]==-1){
chainHead[chainNo]=s;
}
posInBase[s]=ptr; //up wala edge
baseArray[ptr++]=_cost;
chainInd[s]=chainNo;
// cout<<s<<" "<<p<<" "<<_cost<<" "<<index<<" "<<posInBase[s]<<" "<<ptr-1<<" "<<baseArray[ptr-1]<<" "<<chainInd[s]<<"\n";
int ma=0,samechain=-1;
for(int i=0;i<edges[s].size();i++){
if(edges[s][i]==p) continue;
if(subtree[edges[s][i]]>ma){
ma=subtree[edges[s][i]];
samechain=i;
}
}
if(samechain!=-1){
hld(edges[s][samechain],s,cost[s][samechain]);
}
for(int i=0;i<edges[s].size();i++){
if(edges[s][i]==p || i==samechain) continue;
chainNo++;
hld(edges[s][i],s,cost[s][i]);
}
}
int query_hld(int u,int v) //reach v from u
{
if(u==v) return 0;
int chainu,chainv=chainInd[v],ans=0;
while(true){
chainu=chainInd[u];
if(chainu==chainv){
if(u==v) break;
int query_result=query(0,0,n-1,posInBase[v]+1,posInBase[u]);
ans=(ans>=query_result)?ans:query_result;
return ans;
}else{
int head=chainHead[chainInd[u]];
int query_result=query(0,0,n-1,posInBase[head],posInBase[u]);
ans=(ans>=query_result)?ans:query_result;
u=parent[head][0];
}
}
return ans;
}
// HLD End ---------------------------------------------------------------------------
signed main(){
fastio
int t;
cin>>t;
while(t--){
scanf("%d",&n);
ptr=0;chainNo=0;
for(int i=0;i<=n+10;i++){
for(int bit=0;bit<=15;bit++){
parent[i][bit]=-1;
}
chainHead[i]=-1;
edges[i].clear();
indices[i].clear();
cost[i].clear();
}
for(int i=0;i<n-1;i++){
int u,v,w;
scanf("%d %d %d", &u, &v, &w);
u--,v--;
edges[u].push_back(v);
edges[v].push_back(u);
indices[u].push_back(i);
indices[v].push_back(i);
cost[u].push_back(w);
cost[v].push_back(w);
}
dfs(0,-1,0);
hld(0,-1,0);
buildtree(0,n-1,0);
while(true){
char _type[10];
scanf("%s",_type);
if(_type[0]=='D'){
break;
}else if(_type[0]=='Q'){
int u,v;
scanf("%d %d", &u, &v);
u--;v--;
if(u==v){
printf("0\n");
continue;
}
int lca=getlca(u,v);
// cout<<"Coming\n";
int q1=query_hld(u,lca),q2=query_hld(v,lca);
int ans=(q1>q2)?q1:q2;
printf("%d\n",ans);
}else{
int index,data;
scanf("%d %d", &index, &data);
index--;
update(posInBase[bottomvertexofedge[index]],data,0,0,n-1);
}
}
}
return 0;
}
/*
1
10
1 2 3
2 3 4
2 4 2
1 5 9
5 7 4
5 8 6
1 6 8
6 9 1
6 10 7
QUERY 3 10
QUERY 7 8
QUERY 7 9
QUERY 5 7
DONE
*/
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2RlZmluZSBmYXN0aW8gaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7Y2luLnRpZShOVUxMKTsKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKY29uc3QgaW50IE49MTAxMDA7CnZlY3RvciA8aW50PiBlZGdlc1tOXSwgaW5kaWNlc1tOXSwgY29zdFtOXTsKaW50IGJhc2VBcnJheVtOXSwgcHRyOwppbnQgY2hhaW5ObywgY2hhaW5JbmRbTl0sIGNoYWluSGVhZFtOXSwgcG9zSW5CYXNlW05dLCBpbmRleHhbTl07CmludCBkZXB0aFtOXSwgcGFyZW50W05dWzMwXSwgc3VidHJlZVtOXSwgc2VnWzQqTl0gLGJvdHRvbXZlcnRleG9mZWRnZVtOXTsKaW50IG47CgovLyBERlMgQmVnaW4gLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tCnZvaWQgZGZzKGludCBz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IiAiPDxfY29zdDw8IiAiPDxpbmRleDw8IiAiPDxwb3NJbkJhc2Vbc108PCIgIjw8cHRyLTE8PCIgIjw8YmFzZUFycmF5W3B0ci0xXTw8IiAiPDxjaGFpbkluZFtzXTw8IlxuIjsgICAKICAgIAogICAgaW50IG1hPTAsc2FtZWNoYWluPS0xOwogICAgZm9yKGludCBpPTA7aTxlZGdlc1tzXS5zaXplKCk7aSsrKXsKICAgICAgICBpZihlZGdlc1tzXVtpXT09cCkgICAgY29udGludWU7CiAgICAgICAgaWYoc3VidHJlZVtlZGdlc1tzXVtpXV0+bWEpewogICAgICAgICAgICBtYT1zdWJ0cmVlW2VkZ2VzW3NdW2ldXTsKICAgICAgICAgICAgc2FtZWNoYWluPWk7CiAgICAgICAgfQogICAgfQogICAgaWYoc2FtZWNoYWluIT0tMSl7CiAgICAgICAgaGxkKGVkZ2VzW3NdW3NhbWVjaGFpbl0scyxjb3N0W3NdW3NhbWVjaGFpbl0pOwogICAgfQogICAgZm9yKGludCBpPTA7aTxlZGdlc1tzXS5zaXplKCk7aSsrKXsKICAgICAgICBpZihlZGdlc1tzXVtpXT09cCB8fCBpPT1zYW1lY2hhaW4pICAgIGNvbnRpbnVlOwogICAgICAgIGNoYWluTm8rKzsKICAgICAgICBobGQoZWRnZXNbc11baV0scyxjb3N0W3NdW2ldKTsKICAgIH0KfQppbnQgcXVlcnlfaGxkKGludCB1LGludCB2KSAgICAgICAgICAvL3JlYWNoIHYgZnJvbSB1CnsKICAgIGlmKHU9PXYpICAgIHJldHVybiAwOwogICAgaW50IGNoYWludSxjaGFpbnY9Y2hhaW5JbmRbdl0sYW5zPTA7CiAgICB3aGlsZSh0cnVlKXsKICAgICAgICBjaGFpbnU9Y2hhaW5JbmRbdV07CiAgICAgICAgaWYoY2hhaW51PT1jaGFpbnYpewogICAgICAgICAgICBpZih1PT12KSBicmVhazsKICAgICAgICAgICAgaW50IHF1ZXJ5X3Jlc3VsdD1xdWVyeSgwLDAsbi0xLHBvc0lu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AgICAgICAgZm9yKGludCBpPTA7aTxuLTE7aSsrKXsKICAgICAgICAgICAgaW50IHUsdix3OwogICAgICAgICAgICBzY2FuZigiJWQgJWQgJWQiLCAmdSwgJnYsICZ3KTsKICAgICAgICAgICAgdS0tLHYtLTsKICAgICAgICAgICAgZWRnZXNbdV0ucHVzaF9iYWNrKHYpOwogICAgICAgICAgICBlZGdlc1t2XS5wdXNoX2JhY2sodSk7CiAgICAgICAgICAgIGluZGljZXNbdV0ucHVzaF9iYWNrKGkpOwogICAgICAgICAgICBpbmRpY2VzW3ZdLnB1c2hfYmFjayhpKTsKICAgICAgICAgICAgY29zdFt1XS5wdXNoX2JhY2sodyk7CiAgICAgICAgICAgIGNvc3Rbdl0ucHVzaF9iYWNrKHcpOwogICAgICAgIH0KICAgICAgICBkZnMoMCwtMSwwKTsKICAgICAgICBobGQoMCwtMSwwKTsKICAgICAgICBidWlsZHRyZWUoMCxuLTEsMCk7CiAgICAgICAgd2hpbGUodHJ1ZSl7CiAgICAgICAgICAgIGNoYXIgX3R5cGVbMTBdOwogICAgICAgICAgICBzY2FuZigiJXMiLF90eXBlKTsKICAgICAgICAgICAgaWYoX3R5cGVbMF09PSdEJyl7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgfWVsc2UgaWYoX3R5cGVbMF09PSdRJyl7CiAgICAgICAgICAgICAgICBpbnQgdSx2OwogICAgICAgICAgICAgICAgc2NhbmYoIiVkICVkIiwgJnUsICZ2KTsKICAgICAgICAgICAgICAgIHUtLTt2LS07CiAgICAgICAgICAgICAgICBpZih1PT12KXsKICAgICAgICAgICAgICAgICAgICBwcmludGYoIjBcbiIpOwogICAgICAgICAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaW50IGxjYT1nZXRsY2EodSx2KTsKICAgICAgICAgICAgICAgIC8vIGNvdXQ8PCJDb21pbmdcbiI7CiAgICAgICAgICAgICAgICBpbnQgcTE9cXVlcnlfaGxkKHUsbGNhKSxxMj1xdWVyeV9obGQodixsY2EpOwogICAgICAgICAgICAgICAgaW50IGFucz0ocTE+cTIpP3ExOnEyOwogICAgICAgICAgICAgICAgcHJpbnRmKCIlZFxuIixhbnMpOwogICAgICAgICAgICB9ZWxzZXsKICAgICAgICAgICAgICAgIGludCBpbmRleCxkYXRhOwogICAgICAgICAgICAgICAgc2NhbmYoIiVkICVkIiwgJmluZGV4LCAmZGF0YSk7CiAgICAgICAgICAgICAgICBpbmRleC0tOwogICAgICAgICAgICAgICAgdXBkYXRlKHBvc0luQmFzZVtib3R0b212ZXJ0ZXhvZmVkZ2VbaW5kZXhdXSxkYXRhLDAsMCxuLTEpOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAgcmV0dXJuIDA7Cn0KCi8qCjEKMTAKMSAyIDMKMiAzIDQKMiA0IDIKMSA1IDkKNSA3IDQKNSA4IDYKMSA2IDgKNiA5IDEKNiAxMCA3ClFVRVJZIDMgMTAKUVVFUlkgNyA4ClFVRVJZIDcgOQpRVUVSWSA1IDcKRE9ORQoqLwo=