CF2180B.Ashmal

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

Ashmal

You have an array aa of nn strings a1,a2,,ana_{1}, a_{2}, \ldots, a_{n}, each consisting of lowercase English letters, and an empty string ss.

In the ii-th (1in1 \le i \le n) step, you should do one of the following:

  • add aia_{i} to the beginning of ss, or
  • add aia_{i} to the end of ss.

For example, if before the ii-th step s=abas = \mathtt{aba} and ai=bbaa_{i} = \mathtt{bba}, after the ii-th step, ss will be equal to ababba or bbaaba.

What's the lexicographically smallest string ss you can reach after nn steps?

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

  • 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 the number of test cases tt (1t5001 \le t \le 500). The description of the test cases follows.

The first line of each test case contains a single integer nn (1n10001 \le n \le 1000) — size of array aa. The next line contains nn strings a1,a2,,ana_{1}, a_{2}, \ldots, a_{n} (1ai40001 \le |a_i| \le 4000), each consisting of lowercase English letters.

It is guaranteed that the sum of nn over all test cases does not exceed 10001000, and the total length of all strings in the input (over all test cases) does not exceed 40004000.

Output

For each test case, print the lexicographically minimum string ss you can reach after nn steps.

Note

In the first test case, one possible way to construct the lexicographically minimum string ss is as follows:

  1. After the first step, s=amirs = \mathtt{amir} regardless of whether we add it to the beginning or the end, since ss was initially empty.
  2. In the second step, we add a2=rimaa_2 = \mathtt{rima} to the end of ss. Now s=amirrimas = \mathtt{amirrima}.
  3. In the third step, we add a3=amina_3 = \mathtt{amin} to the beginning of ss. Now s=aminamirrimas = \mathtt{aminamirrima}.
  4. In the last step, we add a4=nimaa_4 = \mathtt{nima} to the end of ss. Therefore, the final value of ss is aminamirrimanima.

It can be proven that this resulting string is indeed the lexicographically smallest string obtainable after all steps.

Samples

3
4
amir rima amin nima
1
codeforces
3
a ab abc
aminamirrimanima
codeforces
aababc

在线编程 IDE

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