Quaternion Dijkstra

Decide whether a repeated i/j/k string splits into three nonempty parts reducing to i, j, and k under quaternion multiplication.

Medium5SimulationBrute forceMathNo attempts yetTime limit5sMemory limit512 MB

Problem

The Dutch computer scientist Edsger Dijkstra left many results behind, among them the shortest path algorithm that carries his name. This problem is not about that algorithm.

You lost one point on an algorithms exam for misspelling "Dijkstra". Between D and stra you wrote some number of characters, each of which was i, j, or k. To win the point back you appeal to quaternions, a number system extended from the complex numbers. Quaternion multiplication follows this table.

a×ba \times b11iijjkk
1111iijjkk
iiii1-1kkj-j
jjjjk-k1-1ii
kkkkjji-i1-1

Look up the first quaternion in the rows and the second one in the columns, then read the cell where they meet. For example, in a×ba \times b with a=ia = i and b=jb = j the cell holds kk, and with a=ja = j and b=ib = i the cell holds k-k.

As those examples show, quaternion multiplication is not commutative: there are aa and bb with a×bb×aa \times b \neq b \times a. It is associative, so a×(b×c)=(a×b)×ca \times (b \times c) = (a \times b) \times c holds for all aa, bb, and cc.

Signs behave the usual way. For all quaternions aa and bb, (a)×(b)=a×b(-a) \times (-b) = a \times b, and (a)×b=a×(b)=(a×b)(-a) \times b = a \times (-b) = -(a \times b).

To argue that your misspelling equals the correct spelling ijk, cut the string in two places into three substrings so that the leftmost one reduces to ii, the middle one reduces to jj, and the rightmost one reduces to kk. None of the three substrings may be empty. For example, jij is evaluated as j×i×jj \times i \times j. Here j×i=kj \times i = -k and k×j=i-k \times j = i, so jij reduces to ii. Decide whether the given string admits such a split.

Input

The first line contains the number of test cases TT. TT test cases follow. Each test case has two lines. The first line contains two space separated integers LL and XX, and the second line contains a string of LL characters, each of which is i, j, or k. The string never contains a minus sign, a 1, or any other character. The string you must evaluate is the given string repeated XX times. For example, with L=4L = 4, X=3X = 3, and the given string kiij, the string to evaluate is kiijkiijkiij.

Limits

  • 1T1001 \le T \le 100
  • 1L100001 \le L \le 10000
  • 1X100001 \le X \le 10000
  • 1L×X100001 \le L \times X \le 10000

Output

For each test case, print one line in the form Case #x: y, where xx is the test case number starting from 1 and yy is YES if the evaluated string can be split into three parts under the rule above, and NO otherwise.

Sample explanation

The test cases of the first example work out as follows.

  • Case 1: the string is too short to be split into three parts.
  • Case 2: split it into i, j, and k.
  • Case 3: the only split into three parts is k, j, i, and it does not meet the condition.
  • Case 4: the evaluated string is jijijijijiji. It splits into jij, which reduces to ii, then iji, which reduces to jj, then jijiji, which reduces to kk.
  • Case 5: no split ever produces a part that reduces to jj or to kk.