{"trustable":true,"prependHtml":"\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 async src\u003d\"https://mathjax.codeforces.org/MathJax.js?config\u003dTeX-AMS-MML_HTMLorMML\" type\u003d\"text/javascript\"\u003e\u003c/script\u003e","sections":[{"title":"","value":{"format":"HTML","content":"\u003cdiv class\u003d\"panel_content\"\u003e In the kingdom of string, people like palindromic strings very much. They like only palindromic strings and dislike all other strings. There is a unified formula to calculate the score of a palindromic string. The score is calculated by applying the following three steps.\u003cbr\u003e \u003cstrong\u003e1.\u003c/strong\u003e Since a palindromic string is symmetric, the second half (excluding the middle of the string if the length is odd) is got rid of, and only the rest is considered. For example, \"abba\" becomes \"ab\", \"aba\" becomes \"ab\" and \"abacaba\" becomes \"abac\".\u003cbr\u003e \u003cstrong\u003e2.\u003c/strong\u003e Define some integer values for \u0027a\u0027 to \u0027z\u0027.\u003cbr\u003e \u003cstrong\u003e3.\u003c/strong\u003e Treat the rest part as a 26-based number M and the score is M modulo 777,777,777.\u003cbr\u003e However, different person may have different values for \u0027a\u0027 to \u0027z\u0027. For example, if \u0027a\u0027 is defined as 3, \u0027b\u0027 is defined as 1 and c is defined as 4, then the string \"accbcca\" has the score (3×26\u003csup\u003e3\u003c/sup\u003e+4×26\u003csup\u003e2\u003c/sup\u003e+4×26+1) modulo 777777777\u003d55537.\u003cbr\u003e One day, a very long string S is discovered and everyone in the kingdom wants to know that among all the palindromic substrings of S, what the one with the K-th smallest score is.\u003cbr\u003e\u003c/div\u003e"}},{"title":"Input","value":{"format":"HTML","content":" The first line contains an integer T(1 ≤ T ≤ 20), the number of test cases.\u003cbr\u003e The first line in each case contains two integers n, m (1 ≤ n ≤ 100000, 1 ≤ m ≤ 20) where n is the length of S and m is the number of people in the kingdom. The second line is the string S consisting of only lowercase letters. The next m lines each containing 27 integers describes a person in the following format.\u003cbr\u003e\u003ccenter\u003eK\u003csub\u003ei\u003c/sub\u003e va vb ... vz\u003c/center\u003e\u003cbr\u003e Where va is the value of \u0027a\u0027 for the person, vb is the value of \u0027b\u0027 and so on. It is ensured that the K\u003csub\u003ei\u003c/sub\u003e-th smallest palindromic substring exists and va, vb, ..., vz are in the range of [0, 26). But the values may coincide.\u003cbr\u003e"}},{"title":"Output","value":{"format":"HTML","content":" For each person, output the score of the K-th smallest palindromic substring in one line. Print a blank line after each case."}},{"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\u003e3\r\n6 2\r\nabcdca\r\n3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n7 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0\r\n4 10\r\nzzzz\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 14\r\n2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 14\r\n3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 14\r\n4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 14\r\n5 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 14\r\n6 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 14\r\n7 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 14\r\n8 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 14\r\n9 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 14\r\n10 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 14\r\n51 4\r\nabcdefghijklmnopqrstuvwxyzyxwvutsrqponmlkjihgfedcba\r\n1 1 3 3 25 20 25 21 7 0 9 7 3 16 15 14 19 5 19 19 19 22 8 23 2 4 1\r\n25 1 3 3 25 20 25 21 7 0 9 7 3 16 15 14 19 5 19 19 19 22 8 23 2 4 1\r\n26 1 3 3 25 20 25 21 7 0 9 7 3 16 15 14 19 5 19 19 19 22 8 23 2 4 1\r\n76 1 3 3 25 20 25 21 7 0 9 7 3 16 15 14 19 5 19 19 19 22 8 23 2 4 1\r\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e1\r\n620\r\n\r\n14\r\n14\r\n14\r\n14\r\n14\r\n14\r\n14\r\n378\r\n378\r\n378\r\n\r\n0\r\n9\r\n14\r\n733665286\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n"}},{"title":"Hint","value":{"format":"HTML","content":"\u003cbr\u003eThere are 7 palindromic substrings {\"a\", \"a\", \"b\", \"c\", \"c\", \"d\", \"cdc\"} in the first case. For \u003cbr\u003ethe first person, the corresponding scores are {1, 1, 1, 1, 1, 1, 27}. For the second person, the corresponding scores are {25, 25, 24, 23, 23, 22, 620}.\u003cbr\u003e"}}]}