{"trustable":true,"prependHtml":"\u003cstyle type\u003d\u0027text/css\u0027\u003e\n .input, .output {\n border: 1px solid #888888;\n }\n .output {\n margin-bottom: 1em;\n position: relative;\n top: -1px;\n }\n .output pre, .input pre {\n background-color: #EFEFEF;\n line-height: 1.25em;\n margin: 0;\n padding: 0.25em;\n }\n \u003c/style\u003e\n \u003clink rel\u003d\"stylesheet\" href\u003d\"//codeforces.org/s/96598/css/problem-statement.css\" type\u003d\"text/css\" /\u003e\u003cscript\u003e window.katexOptions \u003d { disable: true }; \u003c/script\u003e\n\u003cscript type\u003d\"text/x-mathjax-config\"\u003e\n MathJax.Hub.Config({\n tex2jax: {\n inlineMath: [[\u0027$$$\u0027,\u0027$$$\u0027], [\u0027$\u0027,\u0027$\u0027]],\n displayMath: [[\u0027$$$$$$\u0027,\u0027$$$$$$\u0027], [\u0027$$\u0027,\u0027$$\u0027]]\n }\n });\n\u003c/script\u003e\n\u003cscript type\u003d\"text/javascript\" async src\u003d\"https://mathjax.codeforces.org/MathJax.js?config\u003dTeX-AMS_HTML-full\"\u003e\u003c/script\u003e","sections":[{"title":"","value":{"format":"HTML","content":"\u003cp\u003eYou are given a permutation $$$p_1, p_2, \\ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$. Each position from $$$1$$$ to $$$n$$$ is colored in one of $$$k$$$ colors. We want to sort the permutation, and for that, we can apply any number of operations of the following types:\u003c/p\u003e\u003cul\u003e\u003cli\u003e Swap any two elements. This operation costs $$$S$$$ coins;\u003c/li\u003e\u003cli\u003e Choose any color $$$i$$$, and permute the elements on positions of color $$$i$$$ as you wish. This operation costs $$$C_i$$$ coins.\u003c/li\u003e\u003c/ul\u003e\u003cp\u003eNote that the positions are colored, not the elements, so when you swap two elements, the positions won\u0027t change their colors.\u003c/p\u003e\u003cp\u003eFind the minimum number of coins you need to spend to sort the permutation.\u003c/p\u003e"}},{"title":"Input","value":{"format":"HTML","content":"\u003cp\u003eThe first line of the input contains a single integer $$$T$$$ ($$$1 \\le T \\le 10^{3}$$$)\u0026nbsp;— the number of independent test cases you need to process. The description of the test cases follows.\u003c/p\u003e\u003cp\u003eThe first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le n \\le 10^{5}$$$, $$$1 \\le k \\le 5$$$)\u0026nbsp;— the size of the permutation and the number of colors. \u003c/p\u003e\u003cp\u003eThe second line of each test case contains $$$(k+1)$$$ integers $$$S, C_1, C_2, \\ldots, C_k$$$ ($$$0 \\le S, C_i \\le 10^9$$$)\u0026nbsp;— the costs of the operations. \u003c/p\u003e\u003cp\u003eThe third line of each test case contains $$$n$$$ integers $$$p_1, p_2, \\ldots, p_n$$$ ($$$1 \\le p_i \\le n$$$, all $$$p_i$$$ are distinct)\u0026nbsp;— the permutation. \u003c/p\u003e\u003cp\u003eThe fourth line of each test case contains $$$n$$$ integers $$$col_i$$$ ($$$1 \\le col_i \\le k$$$)\u0026nbsp;— the colors of the positions.\u003c/p\u003e\u003cp\u003eThe sum of $$$n$$$ over all test cases in one file does not exceed $$$10^{5}$$$.\u003c/p\u003e"}},{"title":"Output","value":{"format":"HTML","content":"\u003cp\u003eFor each test case print a single integer\u0026nbsp;— the minimum number of coins you need to spend to sort the permutation.\u003c/p\u003e"}},{"title":"Examples","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\u003e4\n4 1\n1 10\n2 3 4 1\n1 1 1 1\n4 1\n10 1\n2 3 4 1\n1 1 1 1\n6 2\n10 1 1\n5 2 4 6 1 3\n1 2 1 2 1 2\n4 3\n6 7 8 9\n1 2 3 4\n2 2 3 2\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e3\n1\n12\n0\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n"}},{"title":"Note","value":{"format":"HTML","content":"\u003cp\u003eIn the first test case, we can sort the permutation by applying the \"Swap\" operation $$$3$$$ times: $$$(2, 3, 4, 1) \\to (4, 3, 2, 1) \\to (4, 2, 3, 1) \\to (1, 2, 3, 4)$$$. This way you will spend $$$3$$$ coins.\u003c/p\u003e\u003cp\u003eAnother way to sort it would be to permute all elements on positions of color $$$1$$$, but this would cost $$$10$$$ coins, and we can do cheaper.\u003c/p\u003e\u003cp\u003eIn the second test case (which differs from the first one only in the costs of operations), however, it\u0027s cheaper to just permute all elements on positions of color $$$1$$$, spending $$$1$$$ coin on this.\u003c/p\u003e\u003cp\u003eIn the third test case, one of the optimal sequences of operations would be the following:\u003c/p\u003e\u003cul\u003e\u003cli\u003e\u003cp\u003ePermute the elements on positions of color $$$2$$$ to obtain the permutation $$$(5, 2, 4, 3, 1, 6)$$$. This operation costs $$$1$$$ coin.\u003c/p\u003e\u003c/li\u003e\u003cli\u003e\u003cp\u003eSwap elements $$$p_3, p_4$$$. The permutation is now $$$(5, 2, 3, 4, 1, 6)$$$. This operation costs $$$10$$$ coins.\u003c/p\u003e\u003c/li\u003e\u003cli\u003e\u003cp\u003ePermute the elements on positions of color $$$1$$$ to obtain the permutation $$$(1, 2, 3, 4, 5, 6)$$$. This operation costs $$$1$$$ coin.\u003c/p\u003e\u003c/li\u003e\u003c/ul\u003e\u003cp\u003eIn total, we spent $$$12$$$ coins.\u003c/p\u003e\u003cp\u003eIn the fourth test case, the permutation is already sorted, so we don\u0027t have to spend anything.\u003c/p\u003e"}}]}