#include<bits/stdc++.h>
using namespace std;
#define maxn 10004
#define file
#define inf 1000000000
int t;
int n;
struct edge {
int u,v,w;
}edges[maxn];
vector<pair<int,int> > g[maxn];
int val[maxn];
int par[20][maxn],sz[maxn],h[maxn];
void dfs (int u,int p) {
sz[u]++;
for (auto x : g[u]) {
int v = x.first;
int w = x.second;
if (v == p) continue;
par[0][v] = u;
val[v] = w;
h[v] = h[u] + 1;
for (int i=1;i<20;i++) par[i][v] = par[i-1][par[i-1][v]];
dfs(v,u);
sz[u] += sz[v];
}
}
int LCA (int u,int v) {
if (h[u] < h[v]) swap(u,v);
int d = h[u] - h[v];
for (int i=19;i>=0;i--) if ((d>>i) & 1) u = par[i][u];
if (u == v) return u;
for (int i=19;i>=0;i--) if (par[i][u] != par[i][v]) {
u = par[i][u];
v = par[i][v];
}
return par[0][u];
}
int head[maxn],pos[maxn],cur_p;
void hld (int u,int root) {
if (!head[u]) head[u] = root;
pos[u] = ++cur_p;
int heavy = 0;
for (auto x : g[u]) {
int v = x.first;
if (v == par[0][u]) continue;
if (!heavy || sz[v] > sz[heavy]) heavy = v;
}
if (heavy) hld(heavy,root);
for (auto x : g[u]) {
int v = x.first;
if (v == par[0][u] || v == heavy) continue;
hld(v,v);
}
}
struct node {
int ma,mi,lazy;
}st[maxn * 4];
void push (int id) {
if (st[id].lazy == -1){
st[id * 2].ma *= st[id].lazy;
st[id * 2].mi *= st[id].lazy;
st[id * 2].lazy *= st[id].lazy;
st[id * 2 + 1].ma *= st[id].lazy;
st[id * 2 + 1].mi *= st[id].lazy;
st[id * 2 + 1].lazy *= st[id].lazy;
swap(st[id * 2].ma,st[id * 2].mi);
swap(st[id * 2 + 1].ma,st[id * 2 + 1].mi);
st[id].lazy = 1;
}
}
node mer (const node& x,const node& y) {
return {max(x.ma,y.ma),min(x.mi,y.mi),1};
}
void change (int id,int l,int r,int p,int v) {
if (l == r) {
st[id].ma = v;
st[id].mi = v;
return ;
}
push(id);
int mid = (l + r) >> 1;
if (p <= mid) change(id * 2,l,mid,p,v);
else change(id * 2 + 1,mid + 1,r,p,v);
st[id] = mer(st[id * 2], st[id * 2 + 1]);
}
void neg(int id,int l,int r,int u,int v) {
if (u > v) swap(u,v);
if (u > r || l > v) return ;
if (u <= l && r <= v) {
st[id].ma *= -1;
st[id].mi *= -1;
st[id].lazy *= -1;
swap(st[id].ma,st[id].mi);
return ;
}
push(id);
int mid = (l + r) >> 1;
neg(id * 2,l,mid,u,v);
neg(id * 2 + 1,mid + 1,r,u,v);
st[id] = mer(st[id * 2],st[id * 2 + 1]);
}
node getmax (int id,int l,int r,int u,int v) {
if (u > v) swap(u,v);
if (u > r || l > v) return {-inf,inf,1};
if (u <= l && r <= v) return st[id];
push(id);
int mid = (l + r) >> 1;
return mer (getmax(id * 2,l,mid,u,v), getmax(id * 2 + 1,mid + 1,r,u,v));
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
if (fopen(file".INP","r")) {
freopen(file".INP","r",stdin);
freopen(file".OUT","w",stdout);
}
cin >> t;
while (t--) {
cin >> n;
for (int u=1;u<=n;u++) {
sz[u] = 0;
h[u] = 0;
pos[u] = 0;
head[u] = 0;
edges[u] = {0,0,0};
g[u].clear();
}
cur_p = 0;
for (int i=1;i< n*4;i++) {
st[i] = {-inf,inf,1};
}
for (int i=1;i<n;i++) {
int u,v,w;
cin >> u >> v >> w;
edges[i] = {u,v,w};
g[u].push_back({v,w});
g[v].push_back({u,w});
}
dfs(1,-1);
hld(1,1);
for (int u = 1;u<=n;u++) change(1,1,n,pos[u],val[u]);
while (1) {
string s;
cin >> s;
if (s == "DONE") break;
int a,b;
cin >> a >> b;
if (s[0] == 'Q') {
if (a == b) {
cout << "0\n";
continue;
}
int lca = LCA(a,b);
int ans = -inf;
while (head[a] != head[lca]) {
ans = max (ans, getmax(1,1,n,pos[a],pos[head[a]]).ma);
a = par[0][head[a]];
}
while (head[b] != head[lca]) {
ans = max (ans, getmax(1,1,n,pos[b],pos[head[b]]).ma);
b = par[0][head[b]];
}
if (a != lca) ans = max (ans, getmax(1,1,n,pos[a],pos[lca] + 1).ma);
if (b != lca) ans = max (ans, getmax(1,1,n,pos[b],pos[lca] + 1).ma);
cout << ans << '\n';
}
if (s[0] == 'N') {
int lca = LCA(a,b);
while (head[a] != head[lca]) {
neg(1,1,n,pos[a],pos[head[a]]);
a = par[0][head[a]];
}
while (head[b] != head[lca]) {
neg(1,1,n,pos[b],pos[head[b]]);
b = par[0][head[b]];
}
if (a != lca) neg(1,1,n,pos[a],pos[lca] + 1);
if (b != lca) neg(1,1,n,pos[b],pos[lca] + 1);
}
if (s[0] == 'C') {
int u = edges[a].u, v = edges[a].v;
if (u == par[0][v]) change(1,1,n,pos[v],b);
else change(1,1,n,pos[u],b);
}
}
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBtYXhuIDEwMDA0CiNkZWZpbmUgZmlsZQojZGVmaW5lIGluZiAxMDAwMDAwMDAwCgppbnQgdDsKCmludCBuOwpzdHJ1Y3QgZWRnZSB7CiAgICBpbnQgdSx2LHc7Cn1lZGdlc1ttYXhuXTsKdmVjdG9yPHBhaXI8aW50LGludD4gPiBnW21heG5dOwppbnQgdmFsW21heG5dOwppbnQgcGFyWzIwXVttYXhuXSxzelttYXhuXSxoW21heG5dOwoKdm9pZCBkZnMgKGludCB1LGludCBwKSB7CiAgICBzelt1XSsrOwogICAgZm9yIChhdXRvIHggOiBnW3VdKSB7CiAgICAgICAgaW50IHYgPSB4LmZpcnN0OwogICAgICAgIGludCB3ID0geC5zZWNvbmQ7CiAgICAgICAgaWYgKHYgPT0gcCkgY29udGludWU7CiAgICAgICAgcGFyWzBdW3ZdID0gdTsKICAgICAgICB2YWxbdl0gPSB3OwogICAgICAgIGhbdl0gPSBoW3VdICsgMTsKICAgICAgICBmb3IgKGludCBpPTE7aTwyMDtpKyspIHBhcltpXVt2XSA9IHBhcltpLTFdW3BhcltpLTFdW3ZdXTsKICAgICAgICBkZnModix1KTsKICAgICAgICBzelt1XSArPSBzelt2XTsKICAgIH0KfQoKaW50IExDQSAoaW50IHUsaW50IHYpIHsKICAgIGlmIChoW3VdIDwgaFt2XSkgc3dhcCh1LHYpOwogICAgaW50IGQgPSBoW3VdIC0gaFt2XTsKICAgIGZvciAoaW50IGk9MTk7aT49MDtpLS0pIGlmICgoZD4+aSkgJiAxKSB1ID0gcGFyW2ldW3VdOwogICAgaWYgKHUgPT0gdikgcmV0dXJuIHU7CiAgICBmb3IgKGludCBpPTE5O2k+PTA7aS0tKSBpZiAocGFyW2ldW3VdICE9IHBhcltpXVt2XSkgewogICAgICAgIHUgPSBwYXJbaV1bdV07CiAgICAgICAgdiA9IHBhcltpXVt2XTsKICAgIH0KICAgIHJldHVybiBwYXJbMF1bdV07Cn0KCmludCBoZWFkW21heG5dLHBvc1ttYXhuXSxjdXJfcDsKdm9pZCBobGQgKGludCB1LGludCByb290KSB7CiAgICBpZiAoIWhlYWRbdV0pIGhlYWRbdV0gPSByb290OwogICAgcG9zW3VdID0gKytjdXJfcDsKCiAgICBpbnQgaGVhdnkgPSAwOwogICAgZm9yIChhdXRvIHggOiBnW3VdKSB7CiAgICAgICAgaW50IHYgPSB4LmZpcnN0OwogICAgICAgIGlmICh2ID09IHBhclswXVt1XSkgY29udGludWU7CiAgICAgICAgaWYgKCFoZWF2eSB8fCBzelt2XSA+IHN6W2hlYXZ5XSkgaGVhdnkgPSB2OwogICAgfQogICAgaWYgKGhlYXZ5KSBobGQoaGVhdnkscm9vdCk7CiAgICBmb3IgKGF1dG8geCA6IGdbdV0pIHsKICAgICAgICBpbnQgdiA9IHguZmlyc3Q7CiAgICAgICAgaWYgKHYgPT0gcGFyWzBdW3VdIHx8IHYgPT0gaGVhdnkpIGNvbnRpbnVlOwogICAgICAgIGhsZCh2LHYpOwogICAgfQp9CgpzdHJ1Y3Qgbm9kZSB7CiAgICBpbnQgbWEsbWksbGF6eTsKfXN0W21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WQgbmVnKGludCBpZCxpbnQgbCxpbnQgcixpbnQgdSxpbnQgdikgewogICAgaWYgKHUgPiB2KSBzd2FwKHUsdik7CiAgICBpZiAodSA+IHIgfHwgbCA+IHYpIHJldHVybiA7CiAgICBpZiAodSA8PSBsICYmIHIgPD0gdikgewogICAgICAgIHN0W2lkXS5tYSAqPSAtMTsKICAgICAgICBzdFtpZF0ubWkgKj0gLTE7CiAgICAgICAgc3RbaWRdLmxhenkgKj0gLTE7CiAgICAgICAgc3dhcChzdFtpZF0ubWEsc3RbaWRdLm1pKTsKICAgICAgICByZXR1cm4gOwogICAgfQogICAgcHVzaChpZCk7CiAgICBpbnQgbWlkID0gKGwgKyByKSA+PiAxOwogICAgbmVnKGlkICogMixsLG1pZCx1LHYpOwogICAgbmVnKGlkICogMiArIDEsbWlkICsgMSxyLHUsdik7CiAgICBzdFtpZF0gPSBtZXIoc3RbaWQgKiAyXSxzdFtpZCAqIDIgKyAxXSk7Cn0Kbm9kZSBnZXRtYXggKGludCBpZCxpbnQgbCxpbnQgcixpbnQgdSxpbnQgdikgewogICAgaWYgKHUgPiB2KSBzd2FwKHUsdik7CiAgICBpZiAodSA+IHIgfHwgbCA+IHYpIHJldHVybiB7LWluZixpbmYsMX07CiAgICBpZiAodSA8PSBsICYmIHIgPD0gdikgcmV0dXJuIHN0W2lkXTsKICAgIHB1c2goaWQpOwogICAgaW50IG1pZCA9IChsICsgcikgPj4gMTsKICAgIHJldHVybiBtZXIgKGdldG1heChpZCAqIDIsbCxtaWQsdSx2KSwgZ2V0bWF4KGlkICogMiArIDEsbWlkICsgMSxyLHUsdikpOwp9CgppbnQgbWFpbigpewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7CiAgICBjaW4udGllKDApO2NvdXQudGllKDApOwogICAgaWYgKGZvcGVuKGZpbGUiLklOUCIsInIiKSkgewogICAgICAgIGZyZW9wZW4oZmlsZSIuSU5QIiwiciIsc3RkaW4pOwogICAgICAgIGZyZW9wZW4oZmlsZSIuT1VUIiwidyIsc3Rkb3V0KTsKICAgIH0KICAgIGNpbiA+PiB0OwogICAgd2hpbGUgKHQtLSkgewogICAgICAgIGNpbiA+PiBuOwoKICAgICAgICBmb3IgKGludCB1PTE7dTw9bjt1KyspIHsKICAgICAgICAgICAgc3pbdV0gPSAwOwogICAgICAgICAgICBoW3VdID0gMDsKICAgICAgICAgICAgcG9zW3VdID0gMDsKICAgICAgICAgICAgaGVhZFt1XSA9IDA7CiAgICAgICAgICAgIGVkZ2VzW3VdID0gezAsMCwwfTsKICAgICAgICAgICAgZ1t1XS5jbGVhcigpOwogICAgICAgIH0KICAgICAgICBjdXJfcCA9IDA7CiAgICAgICAgZm9yIChpbnQgaT0xO2k8IG4qNDtpKyspIHsKICAgICAgICAgICAgc3RbaV0gPSB7LWluZixpbmYsMX07CiAgICAgICAgfQoKICAgICAgICBmb3IgKGludCBpPTE7aTxuO2krKykgewogICAgICAgICAgICBpbnQgdSx2LHc7CiAgICAgICAgICAgIGNpbiA+PiB1ID4+IHYgPj4gdzsKICAgICAgICAgICAgZWRnZXNbaV0gPSB7dSx2LHd9OwogICAgICAgICAgICBnW3VdLnB1c2hfYmFjayh7dix3fSk7CiAgICAgICAgICAgIGdbdl0ucHVzaF9iYWNrKHt1LHd9KTsKICAgICAgICB9CgogICAgICAgIGRmcygxLC0xKTsKICAgICAgICBobGQoMSwxKTsKICAgICAgICBmb3IgKGludCB1ID0gMTt1PD1uO3UrKykgY2hhbmdlKDEsMSxuLHBvc1t1XSx2YWxbdV0pOwogICAgICAgIHdoaWxlICgxKSB7CiAgICAgICAgICAgIHN0cmluZyBzOwogICAgICAgICAgICBjaW4gPj4gczsKICAgICAgICAgICAgaWYgKHMgPT0gIkRPTkUiKSBicmVhazsKICAgICAgICAgICAgaW50IGEsYjsKICAgICAgICAgICAgY2luID4+IGEgPj4gYjsKICAgICAgICAgICAgaWYgKHNbMF0gPT0gJ1EnKSB7CiAgICAgICAgICAgICAgICBpZiAoYSA9PSBiKSB7CiAgICAgICAgICAgICAgICAgICAgY291dCA8PCAiMFxuIjsKICAgICAgICAgICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGludCBsY2EgPSBMQ0EoYSxiKTsKICAgICAgICAgICAgICAgIGludCBhbnMgPSAtaW5mOwogICAgICAgICAgICAgICAgd2hpbGUgKGhlYWRbYV0gIT0gaGVhZFtsY2FdKSB7CiAgICAgICAgICAgICAgICAgICAgYW5zID0gbWF4IChhbnMsIGdldG1heCgxLDEsbixwb3NbYV0scG9zW2hlYWRbYV1dKS5tYSk7CiAgICAgICAgICAgICAgICAgICAgYSA9IHBhclswXVtoZWFkW2FdXTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHdoaWxlIChoZWFkW2JdICE9IGhlYWRbbGNhXSkgewogICAgICAgICAgICAgICAgICAgIGFucyA9IG1heCAoYW5zLCBnZXRtYXgoMSwxLG4scG9zW2JdLHBvc1toZWFkW2JdXSkubWEpOwogICAgICAgICAgICAgICAgICAgIGIgPSBwYXJbMF1baGVhZFtiXV07CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoYSAhPSBsY2EpIGFucyA9IG1heCAoYW5zLCBnZXRtYXgoMSwxLG4scG9zW2FdLHBvc1tsY2FdICsgMSkubWEpOwogICAgICAgICAgICAgICAgaWYgKGIgIT0gbGNhKSBhbnMgPSBtYXggKGFucywgZ2V0bWF4KDEsMSxuLHBvc1tiXSxwb3NbbGNhXSArIDEpLm1hKTsKICAgICAgICAgICAgICAgIGNvdXQgPDwgYW5zIDw8ICdcbic7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKHNbMF0gPT0gJ04nKSB7CiAgICAgICAgICAgICAgICBpbnQgbGNhID0gTENBKGEsYik7CiAgICAgICAgICAgICAgICB3aGlsZSAoaGVhZFthXSAhPSBoZWFkW2xjYV0pIHsKICAgICAgICAgICAgICAgICAgICBuZWcoMSwxLG4scG9zW2FdLHBvc1toZWFkW2FdXSk7CiAgICAgICAgICAgICAgICAgICAgYSA9IHBhclswXVtoZWFkW2FdXTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHdoaWxlIChoZWFkW2JdICE9IGhlYWRbbGNhXSkgewogICAgICAgICAgICAgICAgICAgIG5lZygxLDEsbixwb3NbYl0scG9zW2hlYWRbYl1dKTsKICAgICAgICAgICAgICAgICAgICBiID0gcGFyWzBdW2hlYWRbYl1dOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYgKGEgIT0gbGNhKSBuZWcoMSwxLG4scG9zW2FdLHBvc1tsY2FdICsgMSk7CiAgICAgICAgICAgICAgICBpZiAoYiAhPSBsY2EpIG5lZygxLDEsbixwb3NbYl0scG9zW2xjYV0gKyAxKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAoc1swXSA9PSAnQycpIHsKICAgICAgICAgICAgICAgIGludCB1ID0gZWRnZXNbYV0udSwgdiA9IGVkZ2VzW2FdLnY7CiAgICAgICAgICAgICAgICBpZiAodSA9PSBwYXJbMF1bdl0pIGNoYW5nZSgxLDEsbixwb3Nbdl0sYik7CiAgICAgICAgICAgICAgICBlbHNlIGNoYW5nZSgxLDEsbixwb3NbdV0sYik7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gMDsKfQo=