PermRLE (Large)

Find the permutation of positions 1 to k applied to every block of the string that minimizes the number of runs after run-length encoding.

Hard8Dynamic programmingBit manipulationGreedyNo attempts yetTime limit5sMemory limit512 MB

Problem

You have built a slightly modified run-length encoding (RLE) compression algorithm called PermRLE.

To compress a string, the algorithm first picks a permutation of the integers from 1 to kk. It applies that permutation to the first kk letters of the string, then to the next block of kk letters, and so on to the end of the string. The length of the string is divisible by kk. After every block is permuted, the new string is compressed with RLE.

Applying a permutation pp to a block of kk letters means putting the p[1]p[1]-th letter of the block in the first position, the p[2]p[2]-th letter in the second position, and so on up to the kk-th position. For example, applying the permutation {3,1,4,2} to the block "abcd" gives "cadb". Applying the same permutation block by block to the longer string "abcdefghijkl" gives "cadbgehfkilj".

The permuted string is then compressed with run-length encoding. Here the compressed size is the number of runs of consecutive equal letters. For example, the compressed size of "aabcaaaa" is 4. It splits into a run of two letters 'a', a run holding one 'b', a run holding one 'c', and finally a run of four letters 'a'.

The compressed size depends on the permutation you pick. Choose the permutation that makes the compressed size as small as possible and print that size.

Input

The first line contains the number of test cases NN. NN test cases follow.

The first line of each test case contains kk. The second line contains the string SS to be compressed.

Limits

  • 1N201 \le N \le 20
  • SS contains only lowercase letters 'a' through 'z'
  • The length of SS is divisible by kk
  • 2k162 \le k \le 16
  • 1S500001 \le |S| \le 50000

Output

For each test case print one line containing "Case #XX: YY", where XX is the number of the test case and YY is the minimum compressed size of SS.