A common method of data compression, "dictionary coding", is to replace words in a text by numbers indicating their positions in a dictionary. Dynamic dictionary coding derives the dictionary from the text to be compressed. The text is processed from beginning to end, starting with an empty dictionary. Whenever a word is encountered that is in the dictionary, it is replaced by a number indicating its position in the dictionary. Whenever a word is encountered that is not in the dictionary, it appears as-is in the compressed text and is added to the end of the dictionary.
The first line of the input file contains a positive integer which is the number of cases to be compressed. Each case will consist of several lines containing text made of lower case letters and spaces only and will be separated from next case by a single blank line.
The output file should contain the cases compressed using dynamic dictionary coding as described above. Keep a single blank line between data sets as shown in input and keep all spacing and lines the same as input..
1 the cat chased the rat while the dog chased the cat into the rat house
the cat chased 1 rat while 1 dog 3 1 2 into 1 4 house