You are given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, 3…N-1. Each edge has an integer value assigned to it, representing its length.

We will ask you to perfrom some instructions of the following form:

DIST a b : ask for the distance between node a and node b
or
KTH a b k : ask for the k-th node on the path from node a to node b
Example:
N = 6
1 2 1 // edge connects node 1 and node 2 has cost 1
2 4 1
2 5 2
1 3 1
3 6 2

Path from node 4 to node 6 is 4 -> 2 -> 1 -> 3 -> 6
DIST 4 6 : answer is 5 (1 + 1 + 1 + 2 = 5)
KTH 4 6 4 : answer is 3 (the 4-th node on the path from node 4 to node 6 is 3)
Input

The first line of input contains an integer t, the number of test cases (t <= 25). t test cases follow.

For each test case:

In the first line there is an integer N (N <= 10000)
In the next N-1 lines, the i-th line describes the i-th edge: a line with three integers a b c denotes an edge between a, b of cost c (c <= 100000)
The next lines contain instructions “DIST a b” or “KTH a b k”
The end of each test case is signified by the string “DONE”.
There is one blank line between successive tests.

Output

For each “DIST” or “KTH” operation, write one integer representing its result.

Print one blank line after each test.

Example

Input:
1

6
1 2 1
2 4 1
2 5 2
1 3 1
3 6 2
DIST 4 6
KTH 4 6 4
DONE

Output:
5
3

第一问。。。!
第二问找这个点直接倍增就好了嘛。。。。

#include <cstdio> 
#include <cstring> 
#include <algorithm> 
#define LOG 20
#define N 10010
#define M 20010
using namespace std;  

typedef long long LL;

#define logn(i, a, b) for(int i =(a);(1 << i)<=(b); ++ i) 
#define repp(e, H, u) for(Edge* e = H[u]; e; e = e -> next) 
#define rep(i, a, b) for(int i =(a); i < (b); ++ i) 
#define rev(i, a, b) for(int i =(a); i >=(b); -- i) 
#define FOR(i, a, b) for(int i =(a); i <=(b); ++ i) 
#define clr(a, x) memset(a, x, sizeof a) 
#define cpy(a, x) memcpy(a, x, sizeof a) 
#define root 1, 2, n 
#define mid ((l + r) >> 1) 

struct Edge
{  
    int v, c;  
    Edge* next;  
} E[M], *H[N], *e;  

int anc[N][LOG];  
int dist[N];  
int pre[N];  
int dep[N];  
int n;  

void clear()
{
    e = E;  
    clr(H, 0);  
    clr(dep, 0);  
    clr(dist, 0);  
}  

void adde(int u, int v, int c)
{
    e -> v = v;  
    e -> c = c;  
    e -> next = H[u];  
    H[u] = e ++;  
    e -> v = u;  
    e -> c = c;  
    e -> next = H[v];  
    H[v] = e ++;  
}  

void dfs(int u, int fa = -1)
{  
    pre[u] = fa;  
    repp(e, H, u)
    {
        int v = e -> v;  
        if(v == fa)continue;  
        dist[v] = dist[u] + e -> c;  
        dep[v] = dep[u] + 1;  
        dfs(v, u);
    }  
}  

void preprocess()
{
    FOR(i, 1, n)
        anc[i][0] = pre[i];  
    FOR(i, 1, n)
        logn(j, 1, n)
            anc[i][j] = -1;  
    logn(j, 1, n)
        FOR(i, 1, n)
            if(~anc[i][j - 1])
                anc[i][j] = anc[anc[i][j - 1]][j - 1];  
}  

int lca(int x, int y)
{
    if(dep[x] < dep[y])swap(x, y);  
    int log = 0;  
    logn(i, 1, dep[x])++ log;  
    rev(i, log, 0)
        if(dep[x] -(1 << i)>= dep[y])x = anc[x][i];  
    if(x == y)return x;  
    rev(i, log, 0)
    if(~anc[x][i] && anc[x][i] != anc[y][i])
    {  
        x = anc[x][i];  
        y = anc[y][i];  
    }  
    return pre[x];  
}  

int kth(int x, int y, int k)
{
    int cp = lca(x, y);  
    if(dep[x] - dep[cp] + 1 >= k)
    {
        int deep = dep[x] - k + 1, log = 0;  
        logn(i, 1, dep[x])++ log;  
        rev(i, log, 0)if(dep[x] -(1 << i)>= deep)x = anc[x][i];  
        return x;  
    } 
    else
    {  
        int deep = k -(dep[x] - dep[cp])+ dep[cp] - 1, log = 0;  
        logn(i, 1, dep[y])++ log;  
        rev(i, log, 0)if(dep[y] -(1 << i)>= deep)y = anc[y][i];  
        return y;  
    }  
}  

void solve()
{  
    char s[10];  
    int x, y, c;  
    clear();  
    scanf("%d",&n);  
    rep(i, 1, n)
    {  
        scanf ("%d%d%d", &x, &y, &c);  
        adde(x, y, c);  
    }  
    dfs(1);  
    preprocess();  
    while(scanf("%s", s)&& s[1] != 'O')  
        if(s[0] == 'D')
        {  
            scanf("%d%d", &x, &y);  
            int cp = lca(x, y);  
            printf("%d\n", dist[x] + dist[y] - 2 * dist[cp]);  
        } 
        else 
        {  
            scanf("%d%d%d", &x, &y, &c);  
            printf("%d\n", kth(x, y, c));  
        }  
}  

int main()
{  
    int T;  
    scanf("%d", &T);  
    while(T --)solve();  
    return 0;  
}