{"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 string $$$s$$$ of length $$$n$$$ consisting of lowercase English letters. This string can be used to build other strings. The cost of each letter in $$$s$$$ is given by another string $$$c$$$ of length $$$n$$$ consisting of digits, such that the cost of using the letter $$$s_i$$$ is $$$c_i$$$ coins.\u003c/p\u003e\u003cp\u003eAlso, you are given another string $$$p$$$ of length $$$m$$$ consisting of \u003cspan class\u003d\"tex-font-style-bf\"\u003eunique\u003c/span\u003e lowercase English letters. Your task is to find the minimum cost to build string $$$p$$$ by using the letters of $$$s$$$. Can you?\u003c/p\u003e"}},{"title":"Input","value":{"format":"HTML","content":"\u003cp\u003eThe first line contains an integer $$$T$$$ ($$$1 \\le T \\le 500$$$) specifying the number of test cases.\u003c/p\u003e\u003cp\u003eThe first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n \\le 10^3,\\,1 \\le m \\le 26$$$), in which $$$n$$$ is the length of strings $$$s$$$ and $$$c$$$, and $$$m$$$ is the length of string $$$p$$$.\u003c/p\u003e\u003cp\u003eThen $$$3$$$ lines follow, each line contains a string, giving the string $$$s$$$, $$$c$$$, and $$$p$$$, respectively. Both strings $$$s$$$ and $$$p$$$ contains only lowercase English letters, while string $$$c$$$ contains only digits. Also, string $$$p$$$ is consisting of \u003cspan class\u003d\"tex-font-style-bf\"\u003eunique\u003c/span\u003e letters.\u003c/p\u003e"}},{"title":"Output","value":{"format":"HTML","content":"\u003cp\u003eFor each test case, print a single line containing the minimum cost of building the string $$$p$$$ by using the letters of string $$$s$$$. If string $$$p$$$ cannot be built using string $$$s$$$, print $$$-1$$$.\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\u003e3\n4 2\nabcd\n1234\nac\n4 4\nabcd\n1234\nabec\n5 3\nabcba\n24513\nacb\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e4\n-1\n8\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, you have to use the $$$1^{st}$$$ and $$$3^{rd}$$$ letters of string $$$s$$$ to build string $$$p$$$. So, the total cost is $$$1 + 3 \u003d 4$$$.\u003c/p\u003e\u003cp\u003eIn the second test case, you cannot build string $$$p$$$ using $$$s$$$ because the letter \u0027\u003cspan class\u003d\"tex-font-style-tt\"\u003ee\u003c/span\u003e\u0027 from $$$p$$$ does not exist in $$$s$$$. So, the answer is $$$-1$$$.\u003c/p\u003e\u003cp\u003eIn the third test case, the optimal way is to use the $$$1^{st}$$$, $$$3^{rd}$$$, and $$$4^{th}$$$ letters of string $$$s$$$ to build $$$p$$$. So, the total cost is $$$2 + 5 + 1 \u003d 8$$$.\u003c/p\u003e"}}]}