LearnToCP
Sign in
Navigation
HomeRoadmapProblemsAbout Us
Theory
Basics
Your First ProgramData types and IOC++ syntaxModuloFunctionsVectorsMatricesTime Complexity
Sorting
SortingCounting sortRadix Sort
Optimization Techniques
Two PointersSum of numbers 1 to nPrefix sumBinary SearchGreedyBinary Search Functions
Binary Numbers
Binary NumbersNumbers in codeBitwise Operations
Math
Binary ExponentiationPrime NumbersPrime FactorizationGCD and LCMSieve of Eratosthenes
Data Structures
StringsStackQueue
Dynamic Programming
About DPDP problems

Smallest Concatenation

MediumProblem #29
Time LimitMemoryInputOutput
1 s64 MBstdinstdout

The result is too long to fit in any number type.

Concatenating two numbers x and y means writing the digits of y right after the digits of x: concatenating 123 and 45 gives 12345. Given an array of numbers, your task is to find the smallest number that can be obtained by concatenating all of them, each used exactly once, in some order.

Input

First line of input will be a single integer t - the number of testcases.
Each testcase consists of two lines: the first contains an integer n - the number of elements, and the second contains n integers a0​,a1​,…,an−1​.

Output

For every testcase print a single line with the smallest number obtainable by concatenating all the given numbers.

Example

Input
2
5
32 11 987 12 3
2
91919 919191
Output
1112323987
91919191919

In the first testcase the order 11,12,32,3,987 gives the smallest result - note that 32 comes before 3. In the second, starting with 919191 beats starting with 91919.

Constraints

1≤t≤1000
1≤n≤105
1≤ai​≤109
n1​+n2​+…+nt​≤105


This problem was adapted, with permission, from Najmanji broj nadovezivanjem više brojeva, authored by Društvo matematičara Srbije and Fondacija Petlja.

Submit your solution

Sign in to submit your solution and track your progress.

Sign in to submit