Codeforces Round 1069 (Div. 1)
A
Needle in a Haystack
Codeforces
1200
原题
←B →
翻译中…

You got lucky to know the answer to all important questions in the world. This time, the answer is the string $s$, consisting of lowercase English letters only. You want to hide this string.

翻译中…

You have another string $t$ also consisting of lowercase English letters only. You need to shuffle the letters in $t$ so that the string $s$ appears at least once in $t$ as a subsequence$^{\text{∗}}$. Among all possible reorderings of $t$ containing $s$ as a subsequence, find the lexicographically smallest$^{\text{†}}$ one.

翻译中…

$^{\text{∗}}$A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by the deletion of several (possibly, zero or all) element from arbitrary positions. $^{\text{†}}$A string $a$ is lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a \ne b$; or 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 the number of test cases $T$ ($1 \le T \le 10^4$). The description of the test cases follows.

翻译中…

The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$), where $|s|$ is the length of the string $s$.

翻译中…

The second line of each test case contains the string $t$ ($|s| \le |t| \le 10^5$).

翻译中…

Both strings consist of lowercase English letters only.

翻译中…

The sum of $|t|$ over all test cases does not exceed $10^5$.

翻译中…

Output

翻译中…

For each test case, print a single string: the lexicographically smallest reordering of letters in the string $t$ that contains $s$ as a subsequence. If no such string exists, print "Impossible" instead.

翻译中…

Note

翻译中…

In the first test case, $\mathtt{abc}\,\mathtt{dcbe}\,\mathtt{ef}$ contains $\mathtt{dcbe}$.

翻译中…

In the second test case, $\mathtt{aaaaa}\,\mathtt{baba}\,\mathtt{bcc}\,\mathtt{dab}$ also contains $\mathtt{babadab}$.

翻译中…

It can be proven that these are the lexicographically smallest strings satisfying the given condition.

翻译中…

In the third test case, no rearrangement of letters in $t$ contains $s$.

翻译中…

样例

Input #1
3
dcbe
bedbaecfc
babadab
abacabadabacaba
babaisyou
flagiswin
Output #1
abcdcbeef
aaaaabababccdab
Impossible
InputCopy OutputCopy
题目信息
题号
2174A
时间限制
2 s
内存限制
256 MB
标签
贪心
字符串
双指针

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