cswork / OAOpen practice workspace

AmazonOA #130

Custom Sort String

Estimated EasyJudge-ready

Topics (AI-labelled)StringsIn-place Arrays

The alphabet gives the order of distinct characters from smallest to largest. Sort all strings lexicographically by this order; a shorter prefix comes first, and identical strings keep all their copies. The standard I/O uses - to represent the empty string, and - is not part of the allowed alphabet.

Input

The first line contains order, the second line contains n, followed by n strings (an empty string is written as -). order consists of uppercase and lowercase letters and digits, 1≤|order|≤62, 1≤n≤100000, and the total length of the original strings is ≤1000000.

Output

Output the n strings in sorted order, using - for the empty string and keeping duplicates.

Example 1

Input:

9AacB
7
BBBBa
BBBB9
B9ca
Aa999
B9A
B
B9A

Output:

Aa999 B B9A B9A B9ca BBBB9 BBBBa

Example 2

Input:

yYaAbBl
6
Yay
yaY
lyab
lyab
b
bay

Output:

yaY Yay b bay lyab lyab

Example 3

Input:

ba
5
-
a
b
ba
-

Output:

- - b ba a

Example notes

Example 1: the order is Aa999, B, B9A, B9A, B9ca, BBBB9, BBBBa; the source's BBB89 is a typo that altered a character. Example 2: yaY, Yay, b, bay, lyab, lyab; the duplicate lyab must not be changed to lyaB. Example 3: the two empty strings come first, followed by b, ba, a, and each empty string is output as -.

Editorial

Loading solution…