LearnToCP
Sign in
Navigation
HomeRoadmapProblemsAbout Us
Theory
Contest Knowledge
Selecting an IDEInteractive TasksOutput-Only Tasks
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 FindSqrt Decomposition
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 DPDP problemsTree DPBitmask 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

The Cable Car

MediumProblem #45
Time LimitMemoryInputOutput
1 s64 MBstdinstdout

Sorting by the x coordinate looks right until the line stands straight up.

A cable car runs up a mountain in a perfectly straight line. Along the way it passes n pylons, and every pylon stands somewhere on that same line.

The engineer who surveyed the mountain wrote the pylons down in whatever order he walked into them, which is not the order the cabin passes them. He did note one thing though: the first two pylons in his list are written in the order the cabin meets them, so together they tell you which way the cabin travels.

Your task is to put the whole list back into travel order.

Input

The first line contains a single integer t - the number of testcases.

  • The first line of each testcase contains a single integer n - the number of pylons.
  • Each of the next n lines contains two integers xi​ and yi​ - the position of one pylon.

All n pylons in a testcase are distinct and lie on one straight line. The cabin travels from the first pylon in the list towards the second one.

Output

For every testcase print n lines, the positions of the pylons in the order the cabin passes them, two integers per line.

Example

Input
2
5
9 4
5 2
15 7
7 3
13 6
4
0 0
0 5
0 -3
0 9
Output
15 7
13 6
9 4
7 3
5 2
0 -3
0 0
0 5
0 9

In the first testcase the cabin goes from (9,4) towards (5,2), so it travels down and to the left and the pylon at (15,7) is the one it meets first. In the second testcase the line is vertical, so the order has nothing to do with x at all.

Constraints

1≤t≤10
3≤n≤5⋅104
−106≤xi​,yi​≤106
n1​+n2​+…+nt​≤105


This problem was adapted, with permission, from Sortiranje duž linije, 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