The Stable Marriage Problem#796

The Stable Marriage Problem

1 second
64 megabytes
Medium

The stable marriage problem consists of matching members of two different sets according to the members' preferences for members of the other set. The input for the problem consists of:

A marriage is a one-to-one mapping between males and females. A marriage is called stable if there is no pair such that f \in F prefers m \in M to her current partner and prefers over his current partner. A stable marriage is called male-optimal if there is no other stable marriage where any male matches with a female he prefers more than the one assigned in .

Given preference lists for males and females, you must find the male-optimal stable marriage.

Input

The first line contains the number of test cases. The first line of each test case contains the integer (0 < n < 27). The next line contains the names of males and females. Male names begin with uppercase letters, female names with lowercase letters. Then follow lines describing the males' preference lists. The next lines describe the females' preference lists.

Output

For each test case, print the pairs of the stable marriage which is male-optimal. The pairs in each test case must be output in lexicographical order of the male names, as shown in the example. Output an empty line between test cases.

Examples

Example 1

Loading
Loading