CF1617A.Forbidden Subsequence

传统题 时间 2000 ms 内存 256 MiB 3 尝试 1 已通过 1 标签

Forbidden Subsequence

You are given strings SS and TT, consisting of lowercase English letters. It is guaranteed that TT is a permutation of the string abc.

Find string SS', the lexicographically smallest permutation of SS such that TT is not a subsequence of SS'.

String aa is a permutation of string bb if the number of occurrences of each distinct character is the same in both strings.

A string aa is a subsequence of a string bb if aa can be obtained from bb by deletion of several (possibly, zero or all) elements.

A string aa is lexicographically smaller than a string bb if and only if one of the following holds:

  • aa is a prefix of bb, but aba \ne b;
  • in the first position where aa and bb differ, the string aa has a letter that appears earlier in the alphabet than the corresponding letter in bb.

Input

Each test contains multiple test cases. The first line contains a single integer tt (1t10001 \le t \le 1000) — the number of test cases. Description of the test cases follows.

The first line of each test case contains a string SS (1S1001 \le |S| \le 100), consisting of lowercase English letters.

The second line of each test case contains a string TT that is a permutation of the string abc. (Hence, T=3|T| = 3).

Note that there is no limit on the sum of S|S| across all test cases.

Output

For each test case, output a single string SS', the lexicographically smallest permutation of SS such that TT is not a subsequence of SS'.

Note

In the first test case, both aaaabbc and aaaabcb are lexicographically smaller than aaaacbb, but they contain abc as a subsequence.

In the second test case, abccc is the smallest permutation of cccba and does not contain acb as a subsequence.

In the third test case, bcdis is the smallest permutation of dbsic and does not contain bac as a subsequence.

Samples

7
abacaba
abc
cccba
acb
dbsic
bac
abracadabra
abc
dddddddddddd
cba
bbc
abc
ac
abc
aaaacbb
abccc
bcdis
aaaaacbbdrr
dddddddddddd
bbc
ac

在线编程 IDE

建议全屏模式获得最佳体验