


The first line of input contains an integer ‘T’, denoting the number of test cases.
The first line of each test case contains one integer ‘N’, denoting the number of strings in the array.
The following line contains an array ‘S’ of ‘N’ spaced strings denoting the commonly used strings by Bob.
The following line contains an integer ‘L’ denoting the length of the string Bob is going to type.
The following line contains a string ‘P’ denoting the string Bob is going to type.
For each test case, print ‘L’ lines containing at most three single space-separated words that the Alices program will suggest after each character of string ‘P’ is typed.
You are not required to print the expected output. It has already been taken care of. Just implement the function.
1 <= T <= 10
1 <= N <= 1000
1 <= L <= 1000
1 <= Sum of lengths of all ‘S[i]’ <= 2 * 10 ^ 4.
There are no repeated elements in the array ‘S’.
All characters in ‘S[i]’ and ‘P’ are lower-case English letters.
Time Limit: 1 sec
We will need to ask ‘L’ questions where ‘L’ is the length of the string Bob is going to type. In each question, we will have to find 3 lexicographically minimum words which have the same prefix as that of the word typed.
If we sort all the strings, words with the same prefix will get aligned one after another lexicographically. So we will only need to find the first string that has the same prefix as the word we are searching for and take that string along with the next 2 strings if they have the same prefix.
After sorting we need to find the first index that has same prefix as that of the word we are searching for. Since the array of strings is sorted, we can use binary search to find the first index with the same prefix and check the next two strings if they have the same prefix.
We need a data structure that allows us to search for all the words with the given prefix. We can use the trie data structure to store all the words. The question also asks for sorted results. If you look closely, a trie word is represented by its preorder traversal. It is also worth noting that a preorder traversal of a trie will always result in a sorted traversal of results. Thus all we need to do is limit the word traversal to 3.