#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<stack>
#include<cstring>
#include<set>
#include<iterator>
#include<list>
#include<deque>
#include<queue>
#include<map>
#include<cmath>
#include<sstream>
#include<cstdio>
#include<stdio.h>
#include<string.h>
using namespace std;
typedef long long ll;
const int N = 1E4 + 5;
const ll inf = 1e18;
struct Sementtree
{
int l, r;
ll min, max, add;
#define l(x) tree[x].l
#define r(x) tree[x].r
#define Max(x) tree[x].max
#define Min(x) tree[x].min
#define add(x) tree[x].add
}tree[N << 2];
void build(int p, int l, int r)
{
l(p) = l, r(p) = r;
Max(p) = Min(p) = add(p) = 0;
if (l == r)
return;
int mid = (l + r) / 2;
build(p * 2, l, mid);
build(p * 2 + 1, mid + 1, r);
}
void spread(int p)
{
if (l(p) == r(p))
return;
if (add(p))
{
Max(p * 2) = -Max(p * 2);
Min(p * 2) = -Min(p * 2);
swap(Max(p * 2), Min(p * 2));
Max(p * 2 + 1) = -Max(p * 2 + 1);
Min(p * 2 + 1) = -Min(p * 2 + 1);
swap(Max(p * 2 + 1), Min(p * 2 + 1));
add(p * 2) ^= 1;
add(p * 2 + 1) ^= 1;
add(p) = 0;
}
}
void pushup(int p)
{
Max(p) = max(Max(p * 2), Max(p * 2 + 1));
Min(p) = min(Min(p * 2), Min(p * 2 + 1));
}
void change(int p, int l, int r)
{
if (l <= l(p) && r >= r(p))
{
Max(p) = -Max(p);
Min(p) = -Min(p);
swap(Max(p), Min(p));
add(p) ^= 1;
return;
}
spread(p);
int mid = (l(p) + r(p)) / 2;
if (l <= mid)
change(p * 2, l, r);
if (r > mid)
change(p * 2 + 1, l, r);
pushup(p);
}
void update(int p, int x, int val)
{
if (l(p) == x && r(p) == x)
{
Max(p) = Min(p) = val;
add(p) = 0;
return;
}
spread(p);
int mid = (l(p) + r(p)) / 2;
if (x <= mid)
{
update(p * 2, x, val);
}
else
{
update(p * 2 + 1, x, val);
}
pushup(p);
}
ll ask(int p, int l, int r)
{
if (l <= l(p) && r >= r(p))
return Max(p);
spread(p);
int mid = (l(p) + r(p)) / 2;
ll maxans = -inf;
if (l <= mid)
maxans = max(maxans, ask(p * 2, l, r));
if (r > mid)
maxans = max(maxans, ask(p * 2 + 1, l, r));
return maxans;
}
int head[N], ver[2 * N], Next[2 * N], tot;
int top[N];
int deep[N];
int num[N];
int p[N];
int fp[N];
int fa[N];
int son[N];
int pos = 1;
void init()
{
pos = 1;
tot = 0;
memset(head, 0, sizeof(head));
memset(son, -1, sizeof(son));
}
void addedge(int a, int b)
{
ver[++tot] = b, Next[tot] = head[a], head[a] = tot;
ver[++tot] = a, Next[tot] = head[b], head[b] = tot;
}
void dfs1(int u, int pre, int d)
{
deep[u] = d;
fa[u] = pre;
num[u] = 1;
for (int i = head[u]; i; i = Next[i])
{
int v = ver[i];
if (v != pre)
{
dfs1(v, u, d + 1);
num[u] += num[v];
if (son[u] == -1 || num[v] > num[son[u]])
{
son[u] = v;
}
}
}
}
void getpos(int u, int sp)
{
top[u] = sp;
p[u] = pos++;
fp[p[u]] = u;
if (son[u] == -1)
return;
//cout << u << " " << son[u] << endl;
getpos(son[u], sp);
for (int i = head[u]; i; i = Next[i])
{
int v = ver[i];
if (v != son[u] && v != fa[u])
{
getpos(v, v);
}
}
}
void Query(int u, int v, int flag)
{
int f1 = top[u], f2 = top[v];
ll maxans = -inf;
while (f1 != f2)
{
if (deep[f1] < deep[f2])
{
swap(f1, f2);
swap(u, v);
}
if (flag)
maxans = max(maxans, ask(1, p[f1], p[u]));
else
change(1, p[f1], p[u]);
u = fa[f1];
f1 = top[u];
}
if (deep[u] > deep[v])
swap(u, v);
if (flag)
{
if (u == v)
cout << maxans << endl;
else
{
maxans = max(maxans, ask(1, p[son[u]], p[v]));
cout << maxans << endl;
}
}
else
{
if (u == v)
return;
change(1, p[son[u]], p[v]);
}
}
int e[N][3];
int n;
int main()
{
//ios::sync_with_stdio(false);
//cin.tie(0), cout.tie(0);
int t;
scanf("%d", &t);
while (t--)
{
init();
scanf("%d", &n);
for (int i = 0; i < n-1; i++)
{
scanf("%d%d%d", &e[i][0], &e[i][1], &e[i][2]);
//cin >> e[i][0] >> e[i][1] >> e[i][2];
addedge(e[i][0], e[i][1]);
}
build(1, 1, n);
dfs1(1, 0, 0);
getpos(1, 1);
for (int i = 0; i < n-1; i++)
{
if (deep[e[i][0]] > deep[e[i][1]])
swap(e[i][0], e[i][1]);
update(1, p[e[i][1]], e[i][2]);
}
char op[10];
while (scanf("%s", op) != EOF)
{
if (op[0] == 'D')
{
break;
}
int x, y;
scanf("%d%d", &x, &y);
if (op[0] == 'Q')
{
Query(x, y, 1);
}
else if (op[0] == 'C')
{
update(1, p[e[x-1][1]], y);
}
else if (op[0] == 'N')
{
Query(x, y, 0);
}
}
}
return 0;
}