#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<algorithm>
#define N 20010
#define FOR(i,a,b) for(i=(a);i<=(b);i++)
#define ROF(i,a,b) for(i=(a);i>=(b);i--)
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long LL;
typedef long double LD;
int last[N],pre[N],e[N],W[N],len=0,size=0,n;
int w[N],b[N],a[N],sum[N],fa[N],son[N],dep[N],siz[N],top[N],c[N];
char s[1010];
void swap(int &A,int &B){int tmp=A;A=B;B=tmp;}
void add(int x,int y,int z){pre[++len]=last[x];last[x]=len;e[len]=y;W[len]=z;}
void DFS1(int x,int par,int Dep)
{
  dep[x]=Dep;siz[x]=1;son[x]=0;
  for(int p=last[x];p;p=pre[p])
  {
    int v=e[p];
    if (v==par) continue;
    fa[v]=x;b[v]=W[p];
    DFS1(v,x,Dep+1);
    if (son[x]==0||siz[v]>siz[son[x]]) son[x]=v;
    siz[x]+=siz[v];
  }
}
void DFS2(int x,int Top)
{
  w[x]=++size;a[size]=b[x];c[size]=x;top[x]=Top;
  if (son[x]!=0) DFS2(son[x],Top);
  for(int p=last[x];p;p=pre[p])
  {
    int v=e[p];
    if (fa[x]==v||v==son[x]) continue;
    DFS2(v,v);
  }
}
int getsum(int x,int y){return sum[y]-sum[x-1];}
int getans(int va,int vb)
{
  int f1=top[va],f2=top[vb],res=0;
  while (f1!=f2)
  {
    if (dep[f1]<dep[f2]){swap(va,vb);swap(f1,f2);}
    res+=getsum(w[f1],w[va]);
    va=fa[f1];f1=top[va];
  }
  if (va==vb) return res;
  if (dep[va]>dep[vb]) swap(va,vb);
  return res+getsum(w[son[va]],w[vb]);
}
int getlca(int va,int vb)
{
  int f1=top[va],f2=top[vb];
  while (f1!=f2)
  {
    if (dep[f1]<dep[f2]){swap(va,vb);swap(f1,f2);}
    va=fa[f1];f1=top[va];
  }
  if (dep[va]>dep[vb]) swap(va,vb);
  return va;
}
int jump(int x,int kth)
{
  int curdep=dep[x];
  int kthdep=curdep-kth+1;
  while (1)
  {
    if (dep[fa[top[x]]]<kthdep)
    {
      int fdep=dep[top[x]];
      int rank=curdep-fdep+1;
      rank-=curdep-kthdep;
      int st=w[top[x]];
      return c[st+rank-1];
    }else
    {
      x=fa[top[x]];
      curdep=dep[x];
    }
  }
}
int Main()
{
  scanf("%d",&n);int i;size=len=0;
  memset(last,0,sizeof(last));
  memset(pre,0,sizeof(pre));
  FOR(i,1,n-1)
  {
    int t1,t2,t3;
    scanf("%d%d%d",&t1,&t2,&t3);
    add(t1,t2,t3);add(t2,t1,t3);
  }
  DFS1(1,-1,1);
  DFS2(1,1);
  sum[0]=0;FOR(i,1,n) sum[i]=sum[i-1]+a[i];
  scanf("%s",s);
  while (strcmp(s,"DONE")!=0)
  {
    if (strcmp(s,"DIST")==0)
    {
      int a,b;
      scanf("%d%d",&a,&b);
      printf("%d\n",getans(a,b));
    }else
    {
      int a,b,kth,res;
      scanf("%d%d%d",&a,&b,&kth);
      int lca=getlca(a,b);
      int atot=dep[a]-dep[lca]+1;
      int pos=kth-atot+1;
      int btot=dep[b]-dep[lca]+1;
      pos=btot-pos+1;
      if (kth<=atot) res=jump(a,kth);else res=jump(b,pos);
      printf("%d\n",res);
    }
    scanf("%s",s);
  }
  return 0;
}
int main()
{
  int T;
  scanf("%d",&T);
  while (T--) Main();
}