#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e4 + 5;
int n;
int sum[N][20], fa[N][20], dep[N];
vector <pair <int, int> > e[N];
inline void dfs (int x, int f) {
dep[x] = dep[f] + 1;
for (int j = 1; j <= 19; ++ j) fa[x][j] = fa[fa[x][j - 1]][j - 1];
for (int j = 1; j <= 19; ++ j) sum[x][j] = sum[x][j - 1] + sum[fa[x][j - 1]][j - 1];
for (auto k : e[x]) {
int v = k.first, w = k.second;
if (v == f) continue;
sum[v][0] = w;
fa[v][0] = x;
dfs (v, x);
}
return ;
}
inline pair <int, int> query1 (int x, int y) {
int res = 0;
if (dep[x] < dep[y]) swap (x, y);
for (int j = 19; j >= 0; -- j) if (dep[fa[x][j]] >= dep[y] && fa[x][j]) res += sum[x][j], x = fa[x][j];
for (int j = 19; j >= 0; -- j) if (fa[x][j] != fa[y][j]) {
res += sum[x][j], res += sum[y][j];
x = fa[x][j], y = fa[y][j];
}
res += sum[x][0] + sum[y][0], x = fa[x][0];
return {res, x};
}
inline int query2 (int x, int y, int k) {
int LCA = query1 (x, y).second;
if (k == 1) return x;
if (dep[x] - dep[LCA] + 1 >= k) {
for (int j = 19; j >= 0; -- j) if (dep[x] - dep[fa[x][j]] + 1 <= k && fa[x][j]) k -= (dep[x] - dep[fa[x][j]]), x = fa[x][j];
return x;
}
k -= (dep[x] - dep[LCA] + 1);
for (int j = 19; j >= 0; -- j) if (dep[fa[y][j]] >= dep[LCA] + k && fa[y][j]) y = fa[y][j];
return y;
}
inline void init () {
for (int i = 0; i < N; ++ i)
for (int j = 0; j <= 19; ++ j)
fa[i][j] = sum[i][j] = 0;
for (int i = 0; i < N; ++i) e[i].clear (), dep[i] = 0;
return ;
}
inline void solve () {
cin >> n;
init ();
for (int u, v, w, i = 1; i < n; ++ i) {
cin >> u >> v >> w;
e[u].push_back ({v, w});
e[v].push_back ({u, w});
}
dfs (1, 1);
string opt;
for (int x, y, k; ;) {
cin >> opt;
if (opt == "DONE") break;
if (opt == "DIST") {
cin >> x >> y;
cout << query1 (x, y).first << "\n";
} else {
cin >> x >> y >> k;
cout << query2 (x, y, k) << "\n";
}
}
return void (cout << "\n");
}
signed main () {
ios::sync_with_stdio (false), cin.tie (nullptr), cout.tie (nullptr);
int T;
cin >> T;
while (T --) solve ();
return 0;
}