{"trustable":true,"sections":[{"title":"","value":{"format":"MD","content":"\u003ch3\u003eRead problems statements in \u003ca target\u003d\"_blank\" href\u003d\"https://www.codechef.com/download/translated/APRIL18/mandarin/GOODPREF.pdf\" rel\u003d\"nofollow noreferrer noopener\"\u003eMandarin chinese\u003c/a\u003e, \u003ca target\u003d\"_blank\" href\u003d\"https://www.codechef.com/download/translated/APRIL18/russian/GOODPREF.pdf\" rel\u003d\"nofollow noreferrer noopener\"\u003eRussian\u003c/a\u003e and \u003ca target\u003d\"_blank\" href\u003d\"https://www.codechef.com/download/translated/APRIL18/vietnamese/GOODPREF.pdf\" rel\u003d\"nofollow noreferrer noopener\"\u003eVietnamese\u003c/a\u003e as well.\u003c/h3\u003e\n\nYou are given an integer $n$ and a string $s$ consisting only of characters \u0027a\u0027 and \u0027b\u0027. Consider a string $t \u003d \\underbrace{s + s + \\dots + s}_{n\\text{ times}}$, i.e. the string obtained by concatenating $n$ copies of $s$.\n\nFind the number of non-empty prefixes of $t$ in which the number of occurrences of \u0027a\u0027 is strictly greater than the number of occurrences of \u0027b\u0027.\n\n### Input\n- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.\n- The first and only line of each test case contains the string $s$ and the integer $n$, separated by a single space. \n\n### Output\nFor each test case, print a single line containing one integer — the number of valid prefixes.\n\n### Constraints \n- $1 \\le T \\le 100$\n- $1 \\le |s| \\le 10^3$\n- $1 \\le n \\le 10^9$\n- each character of $s$ is either \u0027a\u0027 or \u0027b\u0027\n\n### Subtasks\n**Subtask #1 (30 points):** $1 \\le n \\le 10^3$\n\n**Subtask #2 (70 points):** original constraints"}},{"title":"Sample 1","value":{"format":"MD","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\naba 2\nbaa 3\nbba 3\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e5\n6\n0\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n**Example case 1:** The string $t$ is \"abaaba\". It has five prefixes which contain more a-s than b-s: \"a\", \"aba\", \"abaa\", \"abaab\" and \"abaaba\".\n\n**Example case 2:** The string $t$ is \"baabaabaa\". The strings \"baa\", \"baaba\", \"baabaa\", \"baabaab\", \"baabaaba\" and \"baabaabaa\" are the six valid prefixes.\n\n**Example case 3:** The string $t$ is \"bbabbabba\". There is no prefix of this string which consists of more a-s than b-s. Therefore, the answer is zero.\n"}}]}