python program for following Lexicographical order
Write python program for following
Lexicographical order
Assume that we have alphabet A and the list of words written in this alphabet. Sort this words by lexicographical order consistent with the alphabet A.
Let S1 and S2 will be the words written in A and k will be the number of the first character which differ S1 and S2. S1 is lexicographicaly smaller than S2 if the k-th character in S1 occures earlier than the k-th character in S2 in A or the k-th character from S1, is the last one in this word.
Input
In the first line is given the number of tests t. For each test, in separated lines, are written characters from alphabet A which consists of some small letters of the Latin alphabet. Succession of letters defines order of the alphabet. The next line contains the number n (n <100000) - the number of words to sort. Words are placed in the next n lines. Each word consists of only letters from defined alphabet and its length not exceed 1000 letters. You can assume that the total number of letters in the words of each test does not exceed 4e6 (4 000 000). Test cases are separated by empty line.
Output
For each test case print sorted words in succeeding lines. After each test case add empty line.
Example
Input:
2
re
3
ere
rer
re
balujemy
5
bel
luba
lej
bal
leje
Output:
re
rer
ere
bal
bel
luba
lej
leje
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images