problems / 255

Palindromic in Which Bases?

easy math palindromes two-pointers brute-force 1000 ms · 256 MB

Palindromic in Which Bases?

A number is a palindrome in base b if its digits in base b, written without leading zeros, read the same from left to right and from right to left. For example, 9 is 1001 in base 2, so it is a palindrome in base 2, while in base 10 it is the single digit 9, which is a palindrome too.

You are given q positive integers. For each of them, find every base b with 2 ≤ b ≤ 36 in which it is a palindrome.

A digit in base b is a value from 0 to b − 1. Compare digit values, not their printed forms: in base 12, the number 133 has the two digits 11 and 1, so it is not a palindrome.

Input

The first line contains an integer q (1 ≤ q ≤ 10^4), the number of queries.

Each of the next q lines contains one integer n (1 ≤ n ≤ 10^18). Note that n does not fit in a 32-bit integer.

Output

For each query, print one line: the bases in which n is a palindrome, in increasing order and separated by single spaces, or the word NONE if there are no such bases.

Notes

In the first example, 1 is a single digit in every base, so all 35 bases are listed. 5 is 101 in base 2, 12 in base 3, 11 in base 4, and a single digit 5 in every base from 6 upward.

Example 1

Input

4
1
5
9
133

Output

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
2 4 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
2 8 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
11 18

Example 2

Input

3
36
37
47

Output

5 8 11 17 35
6 36
NONE

Hints · reveal one at a time if you're stuck

Hint 1

There are only 35 bases to try, so you can check each one separately. How do you get the digits of n in base b?

Hint 2

The last digit of n in base b is n mod b, and dividing n by b drops that digit. Keep the digits as numbers, not text: above base 10, a digit like 11 is one digit, not two.

report an issue Prefer your own editor? sooks prep 255 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.