{"trustable":true,"prependHtml":"\u003cscript\u003e\n window.katexOptions \u003d {\n delimiters: [\n {left: \u0027\\\\(\u0027, right: \u0027\\\\)\u0027, display: false},\n ]\n };\n\u003c/script\u003e\n","sections":[{"title":"","value":{"format":"HTML","content":"\u003cp\u003eAt the beginning of the semester in kindergarten, the \u003cem\u003en\u003c/em\u003e little kids (indexed from 1 to \u003cem\u003en\u003c/em\u003e, for convenience) in class need to elect their new leader.\u003c/p\u003e\n\n\u003cp\u003eThe \u003cem\u003ei\u003csup\u003eth\u003c/sup\u003e\u003c/em\u003e kid will vote for his best friend \u003cem\u003ef\u003csub\u003ei\u003c/sub\u003e\u003c/em\u003e (where 1 \u003cem\u003e≤ f\u003csub\u003ei\u003c/sub\u003e ≤ n\u003c/em\u003e, and it\u0027s too shame to vote for yourself, so \u003cem\u003ef\u003csub\u003ei\u003c/sub\u003e ≠ i\u003c/em\u003e). And the kid who gets the most votes will be the leader. If more than one kids who get the largest number of votes, there will be multiple leaders in the new semester.\u003c/p\u003e\n\n\u003cp\u003eLittle Sheldon (the kid with index 1) is extremely vain, and he would like to be the \u003cb\u003eONLY\u003c/b\u003e leader. (That means the number of votes he gets should strictly larger than any other.) Soon Sheldon found that if he give \u003cem\u003ec\u003csub\u003ei\u003c/sub\u003e\u003c/em\u003e candies to the \u003cem\u003ei\u003csup\u003eth\u003c/sup\u003e\u003c/em\u003e kid, the \u003cem\u003ei\u003csup\u003eth\u003c/sup\u003e\u003c/em\u003e kid would regard Sheldon as the new best friend, and of course vote for Sheldon.\u003c/p\u003e\n\n\u003cp\u003eEvery kid including Sheldon loves candies. As an evil programmer, please help the evil Sheldon become the \u003cb\u003eONLY\u003c/b\u003e leader with minimum cost of candies. By the way, Sheldon should vote for any one he wants \u003cb\u003eEXCEPT\u003c/b\u003e himself.\u003c/p\u003e\n\n\u003ch4\u003eInput\u003c/h4\u003e\n\u003cp\u003eThere are multiple test cases. The first line of input contains an integer \u003cem\u003eT\u003c/em\u003e (\u003cem\u003eT ≤\u003c/em\u003e 100) indicating the number of test cases. Then \u003cem\u003eT\u003c/em\u003e test cases follow.\u003c/p\u003e\n\n\u003cp\u003eThe first line of each case contains one integer: \u003cem\u003en\u003c/em\u003e (3 \u003cem\u003e≤ n ≤\u003c/em\u003e 100) -- the number of kids in class.\n\n\u003c/p\u003e\u003cp\u003eThe second line contains \u003cem\u003en-1\u003c/em\u003e integers: \u003cem\u003ef\u003csub\u003ei\u003c/sub\u003e\u003c/em\u003e (1 \u003cem\u003e≤ f\u003csub\u003ei\u003c/sub\u003e ≤ n\u003c/em\u003e, \u003cem\u003ef\u003csub\u003ei\u003c/sub\u003e ≠ i\u003c/em\u003e, and 2 \u003cem\u003e≤ i ≤ n\u003c/em\u003e) -- represents that the best friend of \u003cem\u003ei\u003csup\u003eth\u003c/sup\u003e\u003c/em\u003e kid is indexed with \u003cem\u003ef\u003csub\u003ei\u003c/sub\u003e\u003c/em\u003e.\u003c/p\u003e\n\n\u003cp\u003eThe third line contains \u003cem\u003en-1\u003c/em\u003e integers: \u003cem\u003ec\u003csub\u003ei\u003c/sub\u003e\u003c/em\u003e (1 \u003cem\u003e≤ c\u003csub\u003ei\u003c/sub\u003e ≤\u003c/em\u003e 1000, and 2 \u003cem\u003e≤ i ≤ n\u003c/em\u003e) -- represents that if Sheldon gave \u003cem\u003ec\u003csub\u003ei\u003c/sub\u003e\u003c/em\u003e candies to the \u003cem\u003ei\u003csup\u003eth\u003c/sup\u003e\u003c/em\u003e kid, the \u003cem\u003ei\u003csup\u003eth\u003c/sup\u003e\u003c/em\u003e kid would vote Sheldon, instead of their old best friend \u003cem\u003ef\u003csub\u003ei\u003c/sub\u003e\u003c/em\u003e, as the new semester leader.\u003c/p\u003e\n\n\u003ch4\u003eOutput\u003c/h4\u003e\n\u003cp\u003e\nFor each test case, print the minimal cost of candies to help Sheldon become the \u003cb\u003eONLY\u003c/b\u003e leader.\n\u003c/p\u003e\n\n\u003ch4\u003eSample\u003c/h4\u003e\n\u003ctable class\u003d\"vjudge_sample\"\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\n4\n1 1 2\n1 10 100\n3\n3 2\n1 10\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e0\n11\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n\n\n\u003ch4\u003eHint\u003c/h4\u003e\n\u003cp\u003eIn the first case, \n\u003c/p\u003e\u003cul\u003e\n\t\u003cli\u003eIf Sheldon vote for 2\u003csup\u003end\u003c/sup\u003e kid, the 2\u003csup\u003end\u003c/sup\u003e kid and Sheldon will both have 2 votes. In this case, Sheldon have to pay 100 candies to the 4\u003csup\u003eth\u003c/sup\u003e kid, and get 3 votes to win;\u003c/li\u003e\n\t\u003cli\u003eIf Sheldon vote for 3\u003csup\u003erd\u003c/sup\u003e or 4\u003csup\u003eth\u003c/sup\u003e kid, Sheldon will win with 2 votes without sacrifice any candy.\u003c/li\u003e\n\u003c/ul\u003e\u003cp\u003e\u003c/p\u003e\n"}}]}