Under construction

Still being built. Things may change

Sharkovski's Ordering

Time limit2sMemory limit128 MB

Summary
Sort each test case of up to 255 integers into Sharkovski ordering and print them space-separated on one line.
Level

Easy3 of 10

Topics
Sorting, Math
Solved
No attempts yet

Problem

In a 1964 paper on continuous mappings of the reals into the reals, Alexandr Sharkovski used the following ordering of the positive integers.

3◃5◃7◃9◃⋯◃3×2◃5×2◃7×2◃⋯◃3×22◃5×22◃⋯◃23◃22◃2◃13 \triangleleft 5 \triangleleft 7 \triangleleft 9 \triangleleft \cdots \triangleleft 3 \times 2 \triangleleft 5 \times 2 \triangleleft 7 \times 2 \triangleleft \cdots \triangleleft 3 \times 2^2 \triangleleft 5 \times 2^2 \triangleleft \cdots \triangleleft 2^3 \triangleleft 2^2 \triangleleft 2 \triangleleft 1

Here a◃ba \triangleleft b means that aa comes before bb.

Ciesielski and Pogoda (2008) describe it this way.

First come the odd numbers, beginning with 3, arranged in increasing order. This sequence is repeated with each odd integer multiplied by 22. The initial sequence is repeated again with each odd integer multiplied by 222^2, and so on. The terminal sequence consists of the nonnegative powers of 22 arranged in decreasing order (note that 1=201 = 2^0).

Write a program that reads a list of at most 255 unsigned integers, not necessarily distinct, with values at most 65,535, separated by white space and terminated by 0. Print the numbers arranged in Sharkovski's ordering on one line. The numbers on the line are separated by exactly one space.

Input

The first line contains an integer NN (0≤N≤2550 \le N \le 255). Then NN input cases follow. Each input case is a non-empty list of at most 255 unsigned integers, not necessarily distinct, with values at most 65,535. Numbers are separated by white space, and each input case is terminated by 0.

Output

For every input case, print the numbers of that case arranged in Sharkovski's ordering on one line. The numbers on a line are separated by exactly one space.

Examples1

  1. Example 1

    Input
    2
    3 0
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 1 0
    
    Expected output
    3
    3 5 7 9 11 13 15 17 19 6 10 14 18 12 16 8 4 2 1 1