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

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

hide comments
2020-05-16 13:51:09
Using the Bottom-Up table you can store all the LCS. For storing LCS use SET.
2019-07-16 02:04:20
we can avoid bactracking, the bottom-up table stores a set of all the LCSs.
2018-09-24 08:45:39
lcs + backtracking gives Tle...wasted much time on that
2018-06-15 15:38:06
i am not getting what we have to give in output
2018-05-24 18:14:18 Simes
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
2018-05-09 15:32:52
Problem is simple just be clever during backtracking using recursion
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
2016-11-04 06:05:31
backtracking + lcs
2016-08-10 16:03:59 Nallagatla Manikanta
hell of code :(
2016-05-24 21:16:53 SUBHAJIT GORAI
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 ").
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.