Codeforces Round 761 (Div. 2)
A
Forbidden Subsequence
Codeforces
800
原题
←B →
翻译中…

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

翻译中…

Find string $S'$, the lexicographically smallest permutation of $S$ such that $T$ is not a subsequence of $S'$.

翻译中…

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

翻译中…

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

翻译中…

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

翻译中…
  • $a$ is a prefix of $b$, but $a \ne b$;
  • in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$.
翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \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 $S$ ($1 \le |S| \le 100$), consisting of lowercase English letters.

翻译中…

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

翻译中…

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

翻译中…

Output

翻译中…

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

翻译中…

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.

翻译中…

样例

Input #1
7
abacaba
abc
cccba
acb
dbsic
bac
abracadabra
abc
dddddddddddd
cba
bbc
abc
ac
abc
Output #1
aaaacbb
abccc
bcdis
aaaaacbbdrr
dddddddddddd
bbc
ac
InputCopy OutputCopy
题目信息
题号
1617A
时间限制
1 s
内存限制
256 MB
标签
构造
贪心
排序
字符串

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。