SLEXSORT - Lexicographic sort

Given alphabet A and a list of words, sort the list according to the lexicographic order induced by A.

Input

The first line of input contains t, the number of tests.

Each test begins with a line with alphabet A, which consists of lowercase letters arbitrary chosen from the Latin alphabet. The next line contains an integer n<100 000 - the number of words. The subsequent n contain one word each, which is not longer than 1 000 letters. Additionally, you can assume that the total number of letters in all words of each test does not exceed 4*106.

There is an empty line after each test.

Output

For each test output the sorted list of words in successive lines.

Score

The score is equal to the length of the source code of your program.

Example

Input:
2
re
3
ere
rer
re

balujemy
5
bel
luba
lej
bal
leje

Output:
re
rer
ere

bal
bel
luba
lej
leje
Warning: large Input/Output data, be careful with certain languages

Added by:kuszi
Date:2007-04-10
Time limit:30s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 SCM qobi VB.NET

hide comments
2012-12-27 12:03:51 Aditya Pande
submission ID 8356382 giving TLE in RUBY
i got AC in C++ in 1.2 seconds
Please help
2012-03-18 16:47:45 Hallvard Norheim Bø
Can there be duplicate words in a test set? Or are all words distinct?
2011-09-23 11:32:54 HWK
@Piotr Kakol: What's wrong with this task? Everytime 'Internal Error' even on old AC-solutions. :-(

Edit: Seems that SPOJ has solved the problem.

Last edit: 2011-09-23 11:51:58
2010-11-08 20:40:52 Piotr KÄ…kol
You would have to ask the author because I have access only to my tasks so I'm not able to look up Your code. Sorry.

Last edit: 2011-01-04 19:41:13
2010-11-04 16:04:35 Piotr Fusik
For what input does my #4256038 fail ? Thanks.
2010-07-02 13:40:57 Piotr KÄ…kol
Congratulations!
Impossible is nothing. :-)
2010-06-30 18:37:59 Zoltán Zámbori
No, the shortest length is 71 (in Perl). :-)
2010-04-26 14:09:59 Piotr KÄ…kol
You can also submit here Your solutions.

// Shortest length is 73 (in Perl). ;-)

Last edit: 2010-05-03 17:51:44
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.