TRIP - Trip


Alice and Bob want to go on holiday. Each of them has drawn up a list of cities to be visited in turn. A list may contain a city more than once. As they want to travel together, they have to agree upon a common route. No one wants to change the order of the cities on his list or add other cities. Therefore they have no choice but to remove some cities from the list. Of course the common route is to involve as much sight-seeing in cities as possible. There are exactly 26 cities in the region. Therefore they are encoded on the lists as lower case letters from 'a' to 'z'.

Input

The first line of input contains a number T <= 10 that indicates the number of test cases to follow. Each test case consists of two lines; the first line is the list of Alice, the second line is the list of Bob. Each list consists of 1 to 80 lower case letters.

Output

The output for each test case should contain all different trips exactly once that meet the conditions described above. There is at least one such trip, but never more than 1000 different ones. You should order the trips in lexicographic order. Print one blank line between the output of different test cases.

Example

Input

1
abcabcaa
acbacba

Output

ababa
abaca
abcba
acaba
acaca
acbaa
acbca

hide comments
inderjeet_777: 2018-09-24 08:45:39

lcs + backtracking gives Tle...wasted much time on that

tanishkbansal: 2018-06-15 15:38:06

i am not getting what we have to give in output

Simes: 2018-05-24 18:14:18

Hey @imkiller, why do you keep posting links to your code after you solve a problem? Not good imho.

=(Francky)=> Agree ! Comments cleaned ;-)

Last edit: 2018-05-24 20:59:44
horizon121: 2018-05-09 15:32:52

Problem is simple just be clever during backtracking using recursion

ahaunul_kader: 2018-01-29 07:37:24

why the don't start their journey from b/c???i mean why always from 1st index???

plz replay early..

Last edit: 2018-01-29 07:44:17
ashishranjan28: 2016-11-04 06:05:31

backtracking + lcs

Nallagatla Manikanta: 2016-08-10 16:03:59

hell of code :(

SUBHAJIT GORAI: 2016-05-24 21:16:53

MY solution passed using sets and vector , just use it in a proper way .HINT (in the question it is mentioned : " but never more than 1000 DIFFERENT ones ").

naruto09: 2016-02-23 18:38:47

tricky one..;) lcs + sets/maps in c++ will give tle..try something else

minhthai: 2016-02-23 16:53:35

in java, lcs + recursion is the right way but you also have to ignore unnecessary cases and use StringBuilder. At least, that worked for me :)


Added by:Adrian Kuegel
Date:2004-06-05
Time limit:3s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: NODEJS OBJC PERL6 SQLITE VB.NET
Resource:own problem, used in CEOI 2003