In the stable marriage problem you match the members of two equal-sized groups according to their preferences for members of the other group. You are given:
A marriage is a one-to-one matching between the males and the females. A marriage is stable if there is no pair $(m, f)$ such that $f$ prefers $m$ to her current partner and $m$ prefers $f$ to his current partner. A stable marriage is male-optimal if there is no other stable marriage in which some male is matched to a female he prefers to the one assigned here.
Given the preference lists of the males and the females, find the male-optimal stable marriage.
The first line contains the number of test cases. Each test case is given as:
x:P, where x is a male name and P is the string of all $n$ female names in his order of preference, most preferred first;X:p, where X is a female name and p is the string of all $n$ male names in her order of preference, most preferred first.For each test case, print the pairs of the male-optimal stable marriage, one per line as m F (a male name, a single space, then his partner's name), sorted in ascending order of the male name. Print one blank line between consecutive test cases.