Shirts

Interview

Time limit1sMemory limit128 MB

Summary
Merge two already sorted lists of shirts and print one merged list ordered by size, then by colour letter. Multiple scenarios end with a 0.
Level

Easy3 of 10

Topics
Sorting, Two pointers, Implementation, String
Solved
No attempts yet

Problem

Jane and Tim are getting married. Both of them keep the shirts in their wardrobes arranged in the same order: first by size, then by colour. After the wedding Tim moves in, so his shirts must be merged with Jane's and arranged together, again sorted by size and then by colour.

Because several newly-wed couples share these habits, your program must be able to process more than one set of data.

Input

The input consists of several scenarios.

Each scenario begins with a single integer WW (0<W≤300 < W \le 30) on a line of its own, the number of shirts belonging to the wife. A value of 00 in place of WW signals the end of the input.

The next WW lines each contain two uppercase letters describing one shirt: the first is its size and the second is its colour. The sizes are S, M and L. The colours are B (Blue), K (blacK), N (browN), O (Orange), P (Purple), R (Red) and W (White).

The following line contains a single integer HH (0<H≤300 < H \le 30), the number of shirts belonging to the husband, followed by HH lines describing the husband's shirts in the same two-letter format.

Output

For each scenario print a single line containing all of that scenario's shirts separated by single spaces. Order the shirts first by size (small before medium before large) and then, within the same size, by colour in alphabetical order of the colour letter.

Examples1

  1. Example 1

    Input
    5
    SB
    SB
    SP
    MP
    MR
    6
    MB
    MK
    MP
    LK
    LN
    LW
    0
    
    Expected output
    SB SB SP MB MK MP MP MR LK LN LW