#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define ll int
#define ld long double
#define rd(a) cin>>a
#define pt(a) cout<<a
#define pb push_back
#define mp make_pair
#define cl endl
#define ifor(i,a,b) for(i=a;i<=b;i++)
#define dfor(i,a,b) for(i=a;i>=b;i--)
#define pii pair<int,int>
#define sz 10100
#define mad 1000000007
int n;
vector<pair<int,int>> adj[sz+5],vden[sz+5];
int anc[15][sz];
int parent[sz+5],subtree[sz+5];
int mainarr[sz+5];
int vchain[sz+5],povinc[sz+5];
int headofchain[sz+5];
int level[sz+5];
int tree[6*sz];
int m=0,chaino=0,den=0;
void build(int node,int s,int e) {
if(s==e) {
tree[node]=mainarr[s];
}
else {
int mid=(s+e)/2;
build(2*node,s,mid);
build(2*node +1,mid+1,e);
tree[node]=max(tree[2*node],tree[2*node +1]);
}
}
void update(int node,int s,int e,int idx,int val) {
if(s==e) {
tree[node]=val;
}
else {
int mid=(s+e)/2;
if(s<=idx && idx<=mid) {
update(2*node,s,mid,idx,val);
}
else {
update(2*node +1,mid+1,e,idx,val);
}
tree[node]=max(tree[2*node],tree[2*node +1]);
}
}
int query(int node,int s,int e,int l,int r) {
if(r<s || e<l) return -1;
if(s>=l && e<=r) return tree[node];
int mid=(s+e)/2;
int p1=query(2*node,s,mid,l,r);
int p2=query(2*node +1,mid+1,e,l,r);
return max(p1,p2);
}
void dfs1(int cur,int prev) {
int i,j,k;
anc[0][cur]=prev;
subtree[cur]=1;
parent[cur]=prev;
for(i=0;i<adj[cur].size();i++) {
if(adj[cur][i].f!=prev) {
level[adj[cur][i].f]=level[cur] +1;
dfs1(adj[cur][i].f,cur);
subtree[cur]+=subtree[adj[cur][i].f];
}
}
}
void hld1(int cur,int prev,int cost) {
int i,j;
if(den==0) {
headofchain[chaino]=cur;
den=1;
}
vchain[cur]=chaino;
povinc[cur]=m;
mainarr[m++]=cost;
int mss=-1,vtx,ec;
for(i=0;i<adj[cur].size();i++) {
if(adj[cur][i].f!=prev) {
if(subtree[adj[cur][i].f]>mss || mss==-1) {
mss=subtree[adj[cur][i].f];
vtx=adj[cur][i].f;
ec=adj[cur][i].s;
}
}
}
if(mss!=-1) hld1(vtx,cur,ec);
for(i=0;i<adj[cur].size();i++) {
if(adj[cur][i].f!=prev && adj[cur][i].f!=vtx) {
den=0;
chaino++;
hld1(adj[cur][i].f,cur,adj[cur][i].s);
}
}
}
void precompute() {
int i,j;
for(i=1;i<=14;i++) {
for(j=1;j<=n;j++) {
if(anc[i-1][j]!=-1) {
anc[i][j]=anc[i-1][anc[i-1][j]];
}
}
}
}
int lca(int x,int y) {
int i,j,k,lg;
if(level[y]>level[x]) swap(x,y);
for(lg=0;1<<lg<=level[x];lg++) {
lg++;
}
lg--;
for(i=lg;i>=0;i--) {
if((level[x]-level[y])>=(1<<i)) {
x=anc[i][x];
}
}
if(x==y) return x;
for(i=lg;i>=0;i--) {
if(anc[i][x]!=-1 && anc[i][x]!=anc[i][y]) {
x=anc[i][x];
y=anc[i][y];
}
}
return parent[x];
}
int querybreak(int u,int v) {
if(level[v]>level[u]) swap(u,v);
int uchain=vchain[u];
int vchaino=vchain[v];
int ans=0,val,mne,mxe;
while(1) {
if(u==v) break;;
uchain=vchain[u];
if(uchain==vchaino) {
if(level[v]>level[u]) swap(u,v);
mne=min(povinc[v]+1,povinc[u]);
mxe=max(povinc[v]+1,povinc[u]);
val=query(1,1,m,mne,mxe);
ans=max(ans,val);
break;
}
mne=min(povinc[u],povinc[headofchain[uchain]]);
mxe=max(povinc[u],povinc[headofchain[uchain]]);
val=query(1,1,m,mne,mxe);
ans=max(ans,val);
u=headofchain[uchain];
u=parent[u];
}
return ans;
}
int _querybreak(int u,int v) {
int la=lca(u,v);
if(level[v]>level[u]) swap(u,v);
int v1=querybreak(u,la);
int v2=querybreak(v,la);
return max(v1,v2);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int i,j,k;
int t;
cin>>t;
while(t--) {
cin>>n;
chaino=0;m=0;den=0;
for(i=0;i<=n;i++) {
adj[i].clear();
vden[i].clear();
headofchain[i]=-1;
}
int u,v,ec;
for(i=1;i<=(n-1);i++) {
cin>>u>>v>>ec;
adj[u].pb(mp(v,ec));
adj[v].pb(mp(u,ec));
vden[i].pb(mp(u,v));
for(j=1;j<=14;j++) {
anc[j][i]=-1;
}
}
level[1]=0;
dfs1(1,0);
hld1(1,0,0);
precompute();
m=m-1;
build(1,1,m);
int x,y;
while(1) {
char s[10];
cin>>s;
if(s[0]=='D') break;
if(s[0]=='Q') {
cin>>x>>y;
cout<<_querybreak(x,y)<<"\n";
}
else {
cin>>x>>y;
int fn=vden[x][0].first,sn=vden[x][0].second;
if(level[sn]>level[fn]) swap(fn,sn);
update(1,1,m,povinc[fn],y);
}
}
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCiNkZWZpbmUgICAgZiAgICAgICAgICAgICAgIGZpcnN0CiNkZWZpbmUgICAgcyAgICAgICAgICAgICAgIHNlY29uZAojZGVmaW5lICAgIGxsICAgICAgICAgICAgICBpbnQKI2RlZmluZSAgICBsZCAgICAgICAgICAgICAgbG9uZyBkb3VibGUKI2RlZmluZSAgICByZChhKSAgICAgICAgICAgY2luPj5hCiNkZWZpbmUgICAgcHQoYSkgICAgICAgICAgIGNvdXQ8PGEKI2RlZmluZSAgICBwYiAgICAgICAgICAgICAgcHVzaF9iYWNrCiNkZWZpbmUgICAgbXAgICAgICAgICAgICAgIG1ha2VfcGFpcgojZGVmaW5lICAgIGNsICAgICAgICAgICAgICBlbmRsCiNkZWZpbmUgICAgaWZvcihpLGEsYikgICAgIGZvcihpPWE7aTw9YjtpKyspCiNkZWZpbmUgICAgZGZvcihpLGEsYikgICAgIGZvcihpPWE7aT49YjtpLS0pCiNkZWZpbmUgICAgcGlpICAgICAgICAgICAgIHBhaXI8aW50LGludD4KI2RlZmluZSAgICBzeiAgICAgICAgICAgICAgMTAxMDAKI2RlZmluZSAgICBtYWQgICAgICAgICAgICAgMTAwMDAwMDAwNwoKaW50IG47CnZlY3RvcjxwYWlyPGludCxpbnQ+PiBhZGpbc3orNV0sdmRlbltzeis1XTsKaW50IGFuY1sxNV1bc3pdOwppbnQgcGFyZW50W3N6KzVdLHN1YnRyZWVbc3orNV07CmludCBtYWluYXJyW3N6KzVdOwppbnQgdmNoYWluW3N6KzVdLHBvdmluY1tzeis1XTsKaW50IGhlYWRvZmNoYWluW3N6KzVdOwppbnQgbGV2ZWxbc3orNV07CmludCB0cmVlWzYqc3pdOwoKaW50IG09MCxjaGFpbm89MCxkZW49MDsKCnZvaWQgYnVpbGQoaW50IG5vZGUsaW50IHMsaW50IGUpIHsKICAgIAogICAgaWYocz09ZSkgewogICAgICAgIHRyZWVbbm9kZV09bWFpbmFycltzXTsKICAgIH0KICAgIGVsc2UgewogICAgICAgIAogICAgICAgIGludCBtaWQ9KHMrZSkvMjsKICAgICAgICAKICAgICAgICBidWlsZCgyKm5vZGUscyxtaWQpOwogICAgICAgIGJ1aWxkKDIqbm9kZSArMSxtaWQrMSxlKTsKICAgICAgICB0cmVlW25vZGVdPW1heCh0cmVlWzIqbm9kZV0sdHJlZVsyKm5vZGUgKzFdKTsKICAgIH0KfQoKdm9pZCB1cGRhdGUoaW50IG5vZGUsaW50IHMsaW50IGUsaW50IGlkeCxpbnQgdmFsKSB7CiAgICAKICAgIGlmKHM9PWUpIHsKICAgICAgICB0cmVlW25vZGVdPXZhbDsKICAgIH0KICAgIGVsc2UgewogICAgICAgIAogICAgICAgIGludCBtaWQ9KHMrZSkvMjsKICAgICAgICAKICAgICAgICBpZihzPD1pZHggJiYgaWR4PD1taWQpIHsKICAgICAgICAgICAgdXBkYXRlKDIqbm9kZSxzLG1pZCxpZHgsdmFsKTsKICAgICAgICB9CiAgICAgICAgZWxzZSB7CiAgICAgICAgICAgIHVwZGF0ZSgyKm5vZGUgKzEsbWlkKzEsZSxpZHgsdmFsKTsKICAgICAgICB9CiAgICAgICAgdHJlZVtub2RlXT1tYXgodHJlZVsyKm5vZGVdLHRyZWVbMipub2RlICsxXSk7CiAgICB9Cn0KCmludCBxdWVyeShpbnQgbm9kZSxpbnQgcyxpbnQgZSxpbnQgbCxpbnQgcikgewogICAgCiAgICBpZihyPHMgfHwgZTxsKSByZXR1cm4gLTE7CiAgICAKICAgIGlmKHM+PWwgICYmIGU8PXIpIHJldHVybiB0cmVlW25vZGVdOwogICAgCiAgICBpbnQgbWlkPShzK2UpLzI7CiAgICAKICAgIGludCBwMT1xdWVyeSgyKm5vZGUscyxtaWQsbCxyKTsKICAgIGludCBwMj1xdWVyeSgyKm5vZGUgKzEsbWlkKzEsZSxsLHIpOwogICAgCiAgICByZXR1cm4gbWF4KHAxLHAyKTsKfQp2b2lkIGRmczEoaW50IGN1cixpbnQgcHJldikgewogICAgCiAgICBpbnQgaSxqLGs7CiAgICBhbmNbMF1bY3VyXT1wcmV2OwogICAgc3VidHJlZVtjdXJdPTE7CiAgICBwYXJlbnRbY3VyXT1wcmV2OwogICAgCiAgICBmb3IoaT0wO2k8YWRqW2N1cl0uc2l6ZSgpO2krKykgewogICAgICAgIAogICAgICAgIGlmKGFkaltjdXJdW2ldLmYhPXByZXYpIHsKICAgICAgICAgICAgCiAgICAgICAgICAgIGxldmVsW2FkaltjdXJdW2ldLmZdPWxldmVsW2N1cl0gKzE7CiAgICAgICAgICAgIGRmczEoYWRqW2N1cl1baV0uZixjdXIpOwogICAgICAgICAgICAKICAgICAgICAgICAgc3VidHJlZVtjdXJdKz1zdWJ0cmVlW2FkaltjdXJdW2ldLmZdOwogICAgICAgIH0KICAgIH0KfQoKdm9pZCBobGQxKGludCBjdXIsaW50IHByZXYsaW50IGNvc3QpIHsKICAgIAogICAgaW50IGksajsKICAgIAogICAgaWYoZGVuPT0wKSB7CiAgICAgICAgaGVhZG9mY2hhaW5bY2hhaW5vXT1jdXI7CiAgICAgICAgZGVuPTE7CiAgICB9CiAgICB2Y2hhaW5bY3VyXT1jaGFpbm87CiAgICBwb3ZpbmNbY3VyXT1tOwogICAgbWFpbmFyclttKytdPWNvc3Q7CiAgICAKICAgIGludCBtc3M9LTEsdnR4LGVjOwogICAgZm9yKGk9MDtpPGFkaltjdXJdLnNpemUoKTtpKyspIHsKICAgICAgICAKICAgICAgICBpZihhZGpbY3VyXVtpXS5mIT1wcmV2KSB7CiAgICAgICAgICAgIAogICAgICAgICAgICBpZihzdWJ0cmVlW2FkaltjdXJdW2ldLmZdPm1zcyB8fCBtc3M9PS0xKSB7CiAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgIG1zcz1zdWJ0cmVlW2FkaltjdXJdW2ldLmZdOwogICAgICAgICAgICAgICAgdnR4PWFkaltjdXJdW2ldLmY7CiAgICAgICAgICAgICAgICBlYz1hZGpbY3VyXVtpXS5zOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAgCiAgICBpZihtc3MhPS0xKSBobGQxKHZ0eCxjdXIsZWMpOwogICAgZm9yKGk9MDtpPGFkaltjdXJdLnNpemUoKTtpKyspIHsKICAgICAgICAKICAgICAgICBpZihhZGpbY3VyXVtpXS5mIT1wcmV2ICYmIGFkaltjdXJdW2ldLmYhPXZ0eCkgewogICAgICAgICAgICBkZW49MDsKICAgICAgICAgICAgY2hhaW5vKys7CiAgICAgICAgICAgIGhsZDEoYWRqW2N1cl1baV0uZixjdXIsYWRqW2N1cl1baV0ucyk7CiAgICAgICAgfQogICAgfQp9Cgp2b2lkIHByZWNvbXB1dGUoKSB7CiAgICAKICAgIGludCBpLGo7CgogICAgZm9yKGk9MTtpPD0xNDtpKyspIHsKICAgICAgICBmb3Ioaj0xO2o8PW47aisrKSB7CiAgICAgICAgICAgIGlmKGFuY1tpLTFdW2pdIT0tMSkgewogICAgICAgICAgICAgICAgYW5jW2ldW2pdPWFuY1tpLTFdW2FuY1tpLTFdW2pdXTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KfQoKaW50IGxjYShpbnQgeCxpbnQgeSkgewogICAgCiAgICBpbnQgaSxqLGssbGc7CiAgICBpZihsZXZlbFt5XT5sZXZlbFt4XSkgc3dhcCh4LHkpOwogICAgCiAgICBmb3IobGc9MDsxPDxsZzw9bGV2ZWxbeF07bGcrKykgewogICAgICAgIGxnKys7CiAgICB9CiAgICBsZy0tOwogICAgZm9yKGk9bGc7aT49MDtpLS0pIHsKICAgICAgICAKICAgICAgICBpZigobGV2ZWxbeF0tbGV2ZWxbeV0pPj0oMTw8aSkpIHsKICAgICAgICAgICAgeD1hbmNbaV1beF07CiAgICAgICAgfQogICAgfQogICAgaWYoeD09eSkgcmV0dXJuIHg7CiAgICAKICAgIGZvcihpPWxnO2k+PTA7aS0tKSB7CiAgICAgICAgCiAgICAgICAgaWYoYW5jW2ldW3hdIT0tMSAmJiBhbmNbaV1beF0hPWFuY1tpXVt5XSkgewogICAgICAgICAgICB4PWFuY1tpXVt4XTsKICAgICAgICAgICAgeT1hbmNbaV1beV07CiAgICAgICAgfQogICAgfQogICAgcmV0dXJuIHBhcmVudFt4XTsKfQoKaW50IHF1ZXJ5YnJlYWsoaW50IHUsaW50IHYpIHsKICAgIAogICAgaWYobGV2ZWxbdl0+bGV2ZWxbdV0pIHN3YXAodSx2KTsKICAgIGludCB1Y2hhaW49dmNoYWluW3VdOwogICAgaW50IHZjaGFpbm89dmNoYWluW3ZdOwogICAgaW50IGFucz0wLHZhbCxtbmUsbXhlOwogICAgCiAgICB3aGlsZSgxKSB7CiAgICAgICAgCiAgICAgICAgaWYodT09dikgYnJlYWs7OwogICAgICAgIAogICAgICAgIHVjaGFpbj12Y2hhaW5bdV07CiAgICAgICAgaWYodWNoYWluPT12Y2hhaW5vKSB7IAogICAgICAgICAgICAgCiAgICAgICAgICAgICBpZihsZXZlbFt2XT5sZXZlbFt1XSkgc3dhcCh1LHYpOwogICAgICAgICAgICAgCiAgICAgICAgICAgICBtbmU9bWluKHBvdmluY1t2XSsxLHBvdmluY1t1XSk7CiAgICAgICAgICAgICBteGU9bWF4KHBvdmluY1t2XSsxLHBvdmluY1t1XSk7CiAgICAgICAgICAgICAKICAgICAgICAgICAgIHZhbD1xdWVyeSgxLDEsbSxtbmUsbXhlKTsKICAgICAgICAgICAgIGFucz1tYXgoYW5zLHZhbCk7CiAgICAgICAgICAgICBicmVhazsKICAgICAgICB9CiAgICAgICAgCiAgICAgICAgbW5lPW1pbihwb3ZpbmNbdV0scG92aW5jW2hlYWRvZmNoYWluW3VjaGFpbl1dKTsKICAgICAgICBteGU9bWF4KHBvdmluY1t1XSxwb3ZpbmNbaGVhZG9mY2hhaW5bdWNoYWluXV0pOwogICAgICAgIAogICAgICAgIHZhbD1xdWVyeSgxLDEsbSxtbmUsbXhlKTsKICAgICAgICBhbnM9bWF4KGFucyx2YWwpOwogICAgICAgIHU9aGVhZG9mY2hhaW5bdWNoYWluXTsKICAgICAgICB1PXBhcmVudFt1XTsKICAgIH0KICAgIHJldHVybiBhbnM7Cn0KCmludCBfcXVlcnlicmVhayhpbnQgdSxpbnQgdikgewogICAgCiAgICBpbnQgbGE9bGNhKHUsdik7CiAgICBpZihsZXZlbFt2XT5sZXZlbFt1XSkgc3dhcCh1LHYpOwogICAgaW50IHYxPXF1ZXJ5YnJlYWsodSxsYSk7CiAgICBpbnQgdjI9cXVlcnlicmVhayh2LGxhKTsKICAgIAogICAgcmV0dXJuIG1heCh2MSx2Mik7Cn0KCmludCBtYWluKCkgewogICAgCiAgICBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKDApOwogICAgY2luLnRpZSgwKTsKICAgIGNvdXQudGllKDApOwogICAgCiAgICBpbnQgaSxqLGs7CiAgICAKICAgIGludCB0OwogICAgY2luPj50OwogICAgd2hpbGUodC0tKSB7CiAgICAgICAgCiAgICAgICAgY2luPj5uOwogICAgICAgIGNoYWlubz0wO209MDtkZW49MDsKICAgICAgICBmb3IoaT0wO2k8PW47aSsrKSB7CiAgICAgICAgICAgIAogICAgICAgICAgICBhZGpbaV0uY2xlYXIoKTsKICAgICAgICAgICAgdmRlbltpXS5jbGVhcigpOwogICAgICAgICAgICBoZWFkb2ZjaGFpbltpXT0tMTsKICAgICAgICB9CiAgICAgICAgaW50IHUsdixlYzsKICAgICAgICAKICAgICAgICBmb3IoaT0xO2k8PShuLTEpO2krKykgewogICAgICAgICAgICAKICAgICAgICAgICAgY2luPj51Pj52Pj5lYzsKICAgICAgICAgICAgYWRqW3VdLnBiKG1wKHYsZWMpKTsKICAgICAgICAgICAgYWRqW3ZdLnBiKG1wKHUsZWMpKTsKICAgICAgICAgICAgdmRlbltpXS5wYihtcCh1LHYpKTsKICAgICAgICAgICAgCiAgICAgICAgICAgIGZvcihqPTE7ajw9MTQ7aisrKSB7CiAgICAgICAgICAgICAgICBhbmNbal1baV09LTE7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgbGV2ZWxbMV09MDsKICAgICAgICBkZnMxKDEsMCk7CiAgICAgICAgaGxkMSgxLDAsMCk7CiAgICAgICAgcHJlY29tcHV0ZSgpOwogICAgICAgIG09bS0xOwogICAgICAgIGJ1aWxkKDEsMSxtKTsKICAgICAgICAKICAgICAgICBpbnQgeCx5OwogICAgICAgIAogICAgICAgIHdoaWxlKDEpIHsKICAgICAgICAgICAgCiAgICAgICAgICAgIGNoYXIgc1sxMF07CiAgICAgICAgICAgIGNpbj4+czsKICAgICAgICAgICAgaWYoc1swXT09J0QnKSBicmVhazsKICAgICAgICAgICAgCiAgICAgICAgICAgIGlmKHNbMF09PSdRJykgewogICAgICAgICAgICAgICBjaW4+Png+Pnk7CiAgICAgICAgICAgICAgIGNvdXQ8PF9xdWVyeWJyZWFrKHgseSk8PCJcbiI7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZSB7CiAgICAgICAgICAgICAgICBjaW4+Png+Pnk7CiAgICAgICAgICAgICAgICBpbnQgZm49dmRlblt4XVswXS5maXJzdCxzbj12ZGVuW3hdWzBdLnNlY29uZDsKICAgICAgICAgICAgICAgIGlmKGxldmVsW3NuXT5sZXZlbFtmbl0pIHN3YXAoZm4sc24pOwogICAgICAgICAgICAgICAgdXBkYXRlKDEsMSxtLHBvdmluY1tmbl0seSk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gMDsKfQ==