Swapping Game
Time limit1sMemory limit128 MB
Rearrange the letters of the given string into the lexicographically smallest string that fits the per-position allowed letters, or report NO SOLUTION.
Problem
Dongil came up with a game he can play by himself.
The game starts from one string of lowercase letters.
Dongil may swap any two letters of the string. He may repeat this operation as many times as he likes, zero times included.
The goal is to build the lexicographically smallest string.
After a few rounds the game felt too easy, so Dongil added one more rule. For every position of the finished string he decides in advance which letters may sit there, like this.
- The first letter must be a or b.
- The second letter must be b or c.
The rule applies only to the final string. A string that appears in the middle of the game does not have to obey it.
The rule made the game much harder. Help Dongil and find the lexicographically smallest string he can build while obeying the rule.
Input
The first line has the number of test cases . ()
The first line of each test case has the starting string , which consists of lowercase letters. ()
The next lines each have a string . consists of letters and lists the letters allowed at position of the result. ()
Every letter that appears in appears at least once in the starting string .
Output
For each test case print, on its own line, the lexicographically smallest string that obeys the rule. If no string obeys the rule, print NO SOLUTION.