{"trustable":true,"sections":[{"title":"","value":{"format":"MD","content":"A tree is a connected graph with no cycles. In this problem, you are given a rooted tree, where each node contains an integer value. And the value of a node is strictly greater than the value of its parent. Now you are given a node and an integer query. You have to find the greatest possible parent of this node (may include the node itself), whose value is greater than or equal to the given query integer."}},{"title":"Input","value":{"format":"MD","content":"Input starts with an integer **T (\u0026le; 5)**, denoting the number of test cases.\n\nThe first line of a case is a blank line. The next line contains two integers **N (1 \u0026le; N \u0026le; 10\u003csup\u003e5\u003c/sup\u003e)**, **q (1 \u0026le; q \u0026le; 50000)** where **N** denotes the number of nodes and **q** denotes the number of queries. The nodes are numbered from **0** to **N-1**.\n\nThen there will be **N-1** lines. The **i\u003csup\u003eth\u003c/sup\u003e (1 \u0026le; i \u0026lt; N)** line contains two integers **p\u003csub\u003ei\u003c/sub\u003e** and **s\u003csub\u003ei\u003c/sub\u003e (0 \u0026le; p\u003csub\u003ei\u003c/sub\u003e \u0026lt; i, 1 \u0026le; s\u003csub\u003ei\u003c/sub\u003e \u0026lt; 2\u003csup\u003e31\u003c/sup\u003e)**. **p\u003csub\u003ei\u003c/sub\u003e** denotes the parent and **s\u003csub\u003ei\u003c/sub\u003e** denotes the value of the **i\u003csup\u003eth\u003c/sup\u003e** node, respectively. Assume that the given tree is correct and follows the restrictions as stated. You can assume that the **0\u003csup\u003eth\u003c/sup\u003e** node is the root and its value is 1.\n\nEach of the next **q** lines contains a query. Each query contains two integers: **k** and **v (1 \u0026le; k \u0026lt; N, 1 \u0026le; v \u0026le; s\u003csub\u003ek\u003c/sub\u003e)**."}},{"title":"Output","value":{"format":"MD","content":"For each case, print the case number in a line. Then for each query, print the index of the greatest parent of node **k** with value greater than or equal to **v**. You can assume that a solution exists."}},{"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\u003e1\n\n7 4\n0 3\n0 4\n0 2\n1 4\n2 7\n2 10\n5 1\n4 2\n5 4\n6 10\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003eCase 1:\n0\n1\n2\n6\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n"}},{"title":"Note","value":{"format":"MD","content":"Dataset is huge. Use faster I/O methods."}}]}