RTREE - Roger and tree


Roger is a computer science student who likes connected undirected acyclic graphs, also known as trees. He especially likes solving problems about trees. Recently Roger found a piece of paper with a rooted tree with 'N' vertices drawn on it (numbered from 1 to 'N'). He also found 'Q' queries on the same piece of paper, where each query was an integer 'Sbetween 1 to 'N'. But the paper said nothing about the description of the queries. So he decided to find the longest path of each of the subtree 'S'.

Roger spent two sleepless nights trying to solve this problem efficiently. He is still trying and won't sleep until he knows the answer to each query. Write a program which answers all the queries correctly.

Input

The first line contains an integer 'N', then N-1 lines follow.

Each of the next 'N-1' line contains two integer 'U' and 'V' which means that vertex 'U' and 'V' are connected.

Next line contains an integer 'R' which denotes the root of the tree.

Next line contains another integer 'Q' which denotes the number of queries.

Each of the next 'Q' line contains an integer 'S' between (1 to N).

Output

For each query print the longest path of the subtree 'S' rooted at vertex 'R'.

Output exactly 'Q' lines, each line containing the output of the ith query.

Example

SAMPLE INPUT
3
1 2
1 3
1
2
1
2

SAMPLE OUTPUT
2
0

Constraints

1 ≤ N ≤ 10^5
1 ≤ U,V ≤ N
1 ≤ R ≤ N
1 ≤ Q ≤ 10^5
1 ≤ S ≤ N

 

Like Trees? Try the problems: RTREE2, RTREE3 as well


hide comments
Abhilash: 2015-01-31 21:45:29

50th :)

:D: 2015-01-31 21:45:29

There's exactly ONE test case per input. Don't read until EOF or you will run into some "garbage" in the file.

Vignesh Manoharan: 2015-01-31 21:45:29

@dunnohyet
case 1:longest path 2->1->3
case 2:no path , subtree is a leaf

Martijn Muijsers: 2015-01-31 21:45:29

I'm getting SIGABRT. This has never happened before, and my memory usage should not be excessive. Furthermore I clear all STL-used memory. Can anyone explain possible causes?

dunnohyet: 2015-01-31 21:45:29

test case is not matching the explanation ... can sum1 explain??


Added by:Rana Saha
Date:2014-08-26
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All
Resource:Own problem (Codecracker 2014)