Huffman Tree

No attempts yetTime limit3sMemory limit128 MB

Problem

One relatively simple way to compress data is to use a Huffman tree. With a Huffman tree you can easily compress the data in a file and later decompress it.

Many programs use a binary Huffman tree, in which every node is either a leaf or has exactly two children. This problem generalizes that idea to an NN-ary Huffman tree, in which every internal node has exactly NN children.

If a file contains ZZ distinct characters, the tree has exactly ZZ leaves. The sequence of numbers written along the path from the root down to a leaf is the encoding of that character. Each edge (a step from a node to one of its children) is labeled with a number from 00 to N1N-1.

Placing frequently used characters close to the root and rarely used characters far from it improves the compression ratio. In other words, a Huffman tree is a tree that minimizes the total number of NN-ary symbols needed to encode the file.

In this problem, every node of the tree is either an internal node or a leaf that encodes exactly one character. There are no dangling leaves that encode no character, so every internal node has exactly NN children.

For example, when N=3N=3, a frequently used character may be encoded with a single symbol, a less common one with two symbols, and a rare one with three symbols.

To decode a file you must know the tree that was used to encode it, so the tree has to be stored. In this problem the tree is stored as follows. The ZZ distinct characters are denoted by the integers 0,1,,Z10, 1, \dots, Z-1. Write these characters once each in increasing order to form a file, encode that file, and store the resulting string. That is, the stored string is the encoding of character 00, followed by the encoding of character 11, and so on up to the encoding of character Z1Z-1, all concatenated together.

Given NN and the string stored as above, write a program that determines the symbol sequence that encodes each character.

Input

The first line contains the number of test cases TT. Each test case consists of three lines:

  • Line 1: the number of distinct characters in the file, ZZ (2Z202 \le Z \le 20).
  • Line 2: the arity NN of the Huffman tree (2N102 \le N \le 10). The tree is NN-ary: N=2N=2 is binary and N=3N=3 is ternary.
  • Line 3: the string obtained by encoding the file that lists every character exactly once in increasing order. Its length does not exceed 200200, and every character is a digit from 00 to N1N-1.

The same string may correspond to more than one tree. For example, when Z=5Z=5 and N=2N=2, the string 010011101100010011101100 can come from several different trees. However, only inputs whose answer is uniquely determined are given in this problem.

Output

For each test case, print ZZ lines. Each line has the form character->encoding, where the character is an integer from 00 to Z1Z-1 and the encoding is the symbol sequence assigned to that character by the Huffman tree. Print the characters in the order 0,1,,Z10, 1, \dots, Z-1.