#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define loop(i,j,k) for (int i = j;i != -1; i = k[i])
#define lson x << 1, l, mid
#define rson x << 1 | 1, mid + 1, r
#define ff first
#define ss second
#define mp(i,j) make_pair(i,j)
#define pb push_back
#define pii pair<int,int>
#define inone(x) scanf("%d", &x)
#define intwo(x,y) scanf("%d%d", &x, &y)
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const double eps = 1e-4;
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;
int T, n, x, y, z;
int ft[N], nt[N], u[N], v[N], sz, va[N];
int ct[N], mx[N], fa[N], o[N];
int top[N], bel[N], dep[N], cnt;
int f[N], g[N];
char s[N];
void dfs(int x, int f)
{
dep[x] = dep[f] + 1;
fa[x] = f; ct[x] = 1; mx[x] = 0;
loop(i, ft[x], nt)
{
if (u[i] == f) continue;
o[u[i]] = v[i];
dfs(u[i], x);
ct[x] += ct[u[i]];
mx[x] = ct[u[i]] > ct[mx[x]] ? u[i] : mx[x];
}
}
void Dfs(int x, int t)
{
bel[o[x]] = ++cnt;
g[cnt] = x;
top[x] = t ? top[fa[x]] : x;
if (mx[x]) Dfs(mx[x], 1);
loop(i, ft[x], nt)
{
if (u[i] == fa[x]) continue;
if (u[i] == mx[x]) continue;
Dfs(u[i], 0);
}
}
void build(int x, int l, int r)
{
if (l == r) { f[x] = va[o[g[l]]]; return; }
int mid = l + r >> 1;
build(lson); build(rson);
f[x] = max(f[x << 1], f[x << 1 | 1]);
}
void change(int x, int l, int r, int u, int v)
{
if (l == r) { f[x] = v; return; }
int mid = l + r >> 1;
if (u <= mid) change(lson, u, v);
else change(rson, u, v);
f[x] = max(f[x << 1], f[x << 1 | 1]);
}
int get(int x, int l, int r, int ll, int rr)
{
if (ll <= l&&r <= rr) return f[x];
int mid = l + r >> 1, ans = 0;
if (ll <= mid) ans = max(ans, get(lson, ll, rr));
if (rr > mid) ans = max(ans, get(rson, ll, rr));
return ans;
}
void query(int x, int y)
{
int ans = 0;
while (1)
{
if (top[x] == top[y])
{
if (x == y) break;
if (dep[x] > dep[y]) swap(x, y);
ans = max(ans, get(1, 1, n, bel[o[x]] + 1, bel[o[y]]));
break;
}
else
{
if (dep[top[x]] <= dep[top[y]]) swap(x, y);
ans = max(ans, get(1, 1, n, bel[o[top[x]]], bel[o[x]]));
x = fa[top[x]];
}
}
printf("%d\n", ans);
}
int main()
{
inone(T);
while (T--)
{
o[1] = va[0] = dep[0] = ct[0] = 0;
cnt = sz = 0;
inone(n);
rep(i, 1, n) ft[i] = -1;
rep(i, 1, n - 1)
{
intwo(x, y),inone(va[i]);
u[sz] = y; v[sz] = i; nt[sz] = ft[x]; ft[x] = sz++;
u[sz] = x; v[sz] = i; nt[sz] = ft[y]; ft[y] = sz++;
}
dfs(1, 0); Dfs(1, 0);
build(1, 1, n);
while (scanf("%s", s), s[0] != 'D')
{
intwo(x, y);
if (s[0] == 'Q') query(x, y);
else change(1, 1, n, bel[x], y);
}
}
return 0;
}