{"trustable":true,"sections":[{"title":"","value":{"format":"HTML","content":"A rooted tree is a well-known data structure in computer science and engineering. An example is shown below: \r\u003cbr\u003e\r\u003cbr\u003e\u003cimg src\u003d\"CDN_BASE_URL/a4cf40017a91cfbd4f81b13b267ec9ee?v\u003d1695922427\"\u003e\r\u003cbr\u003eIn the figure, each node is labeled with an integer from {1, 2,...,16}. Node 8 is the root of the tree. Node x is an ancestor of node y if node x is in the path between the root and node y. For example, node 4 is an ancestor of node 16. Node 10 is also an ancestor of node 16. As a matter of fact, nodes 8, 4, 10, and 16 are the ancestors of node 16. Remember that a node is an ancestor of itself. Nodes 8, 4, 6, and 7 are the ancestors of node 7. A node x is called a common ancestor of two different nodes y and z if node x is an ancestor of node y and an ancestor of node z. Thus, nodes 8 and 4 are the common ancestors of nodes 16 and 7. A node x is called the nearest common ancestor of nodes y and z if x is a common ancestor of y and z and nearest to y and z among their common ancestors. Hence, the nearest common ancestor of nodes 16 and 7 is node 4. Node 4 is nearer to nodes 16 and 7 than node 8 is. \r\u003cbr\u003e\r\u003cbr\u003eFor other examples, the nearest common ancestor of nodes 2 and 3 is node 10, the nearest common ancestor of nodes 6 and 13 is node 8, and the nearest common ancestor of nodes 4 and 12 is node 4. In the last example, if y is an ancestor of z, then the nearest common ancestor of y and z is y. \r\u003cbr\u003e\r\u003cbr\u003eWrite a program that finds the nearest common ancestor of two distinct nodes in a tree. \r\u003cbr\u003e\r\u003cbr\u003e"}},{"title":"Input","value":{"format":"HTML","content":"The input consists of T test cases. The number of test cases (T) is given in the first line of the input file. Each test case starts with a line containing an integer N , the number of nodes in a tree, 2\u0026lt;\u003dN\u0026lt;\u003d10,000. The nodes are labeled with integers 1, 2,..., N. Each of the next N -1 lines contains a pair of integers that represent an edge --the first integer is the parent node of the second integer. Note that a tree with N nodes has exactly N - 1 edges. The last line of each test case contains two distinct integers whose nearest common ancestor is to be computed. "}},{"title":"Output","value":{"format":"HTML","content":"Print exactly one line for each test case. The line should contain the integer that is the nearest common ancestor. "}},{"title":"Sample","value":{"format":"HTML","content":"\u003ctable class\u003d\u0027vjudge_sample\u0027\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e2\r\n16\r\n1 14\r\n8 5\r\n10 16\r\n5 9\r\n4 6\r\n8 4\r\n4 10\r\n1 13\r\n6 15\r\n10 11\r\n6 7\r\n10 2\r\n16 3\r\n8 1\r\n16 12\r\n16 7\r\n5\r\n2 3\r\n3 4\r\n3 1\r\n1 5\r\n3 5\r\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e4\r\n3\r\n\r\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n"}}]}