LearnToCP
Sign in
Navigation
HomeRoadmapProblemsAbout Us
Theory
Contest Knowledge
Selecting an IDE
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 FunctionsBinary Search by AnswerDivide and Conquer
Binary Numbers
Binary NumbersNumbers in codeBitwise OperationsBitmasks
Math
Binary ExponentiationPrime NumbersPrime FactorizationGCD and LCMSieve of EratosthenesModified Sieve
Data Structures
StringsStackQueueMapsSetsPriority QueueCustom Criteria for FunctionsSegment TreesFenwick TreesSparse TablesUnion Find
Combinatorics
Addition PrincipleMultiplication PrincipleCombinatoric ObjectsInclusion Exclusion Principle
Geometry
Geometry BasicsVectorsCross and Dot ProductLinesPolygonsAnglesPoint in PolygonDistances and Intersection PointsConvex HullCircles
Recursion
PointersRecursionGenerating Combinatoric Objects
Dynamic Programming
About DPTree DPBitmask DPDigit DP
Graph Theory
GraphsDFS and BFSShortest PathsTreesTopological SortingDijkstra's AlgorithmMinimum Spanning TreesShortest Path Algorithms
Advanced Graph Theory
BiconnectivityStrongly Connected ComponentsBipartite GraphGraph FlowAugmenting PathsFlow - Minimum Cut DualityHeavy-Light DecompositionCentroid Decomposition
Advanced Data Structures
2D and 3D Segment TreesLazy PropagationImplicit Segment TreesPersistent Segment TreesLowest Common AncestorTrieBalanced Binary Search TreesMo's Algorithm

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