This page is still under construction.

Parts of this page are still being built. What you see may change.

All about that base

Time limit2sMemory limit256 MB

Summary
Test every base from 1 to 36 and list the bases where the given addition, subtraction, multiplication, or division holds.
Level

Medium5 of 10

Topics
Brute force, Math, Implementation
Solved
No attempts yet

Problem

The base (or radix) of a positional numeral system is the number of symbols the system uses to write a number. Base 10, also called decimal, uses 10 distinct symbols: 0, 1, ..., 9. For example, the number 72345 is read as

7×104+2×103+3×102+4×101+5×1007 \times 10^4 + 2 \times 10^3 + 3 \times 10^2 + 4 \times 10^1 + 5 \times 10^0

In base 10 the symbol at place P≥0P \ge 0, counting from the right, is multiplied by 10P10^P to get its value. In general, base BB uses BB symbols for the values 00 through B−1B-1, and the symbol at place PP is multiplied by BPB^P.

Other bases common in computing are base 2 (binary, symbols 0 and 1), base 8 (octal, symbols 0 to 7), and base 16 (hexadecimal, symbols 0 to 9 and a to f). In bases above 10, letters stand for the larger values. In hexadecimal a to f mean the decimal values 10 to 15, and in base 36 the letter z means the decimal value 35.

Decide which bases a given arithmetic expression holds in. An expression is valid in base BB when both of the following are true.

  • Every operand, read in base BB, has a value in the decimal range [1,232−1][1, 2^{32} - 1].
  • The expression is true. Division / is true only when it comes out even, with no remainder.

An expression may be valid in no base, in one base, or in several. This problem considers only bases 1 to 36, where base 1 is unary.

Applying the rule above literally, unary would use the single symbol 0. Here unary uses the symbol 1 instead, the way tally marks work. Unary 111 is decimal 3, and 1111111 is decimal 7.

Input

The first line has the number of expressions NN (0≤N≤20)(0 \le N \le 20). Each of the next NN lines holds one arithmetic expression in this form.

X op Y = Z

XX, YY, and ZZ are positive whole numbers written with 1 to 100 symbols from the set 0 to 9 and a to z, and op is one of +, -, *, /. For every expression there is at least one base BB (1≤B≤36)(1 \le B \le 36) in which XX, YY, and ZZ all have values in the decimal range [1,232−1][1, 2^{32} - 1].

Output

For each expression print one line listing the bases the expression is valid in, in increasing order of base. Print invalid if the expression holds in no base from 1 to 36. Write bases 1 to 9 as the symbols 1 to 9, bases 10 to 35 as a to z, and base 36 as 0.

Examples2

  1. Example 1

    Input
    8
    6ef + d1 = 7c0
    3 / 2 = 1
    444 / 2 = 222
    10111 * 11 = 1000101
    10111 * 11 = 111221
    5k - 1z = 46
    1111111111 - 1111111 = 111
    2048 - 512 = 1536
    
    Expected output
    g
    invalid
    56789abcdefghijklmnopqrstuvwxyz0
    2
    3456789abcdefghijklmnopqrstuvwxyz0
    invalid
    1
    a
    
  2. Example 2

    Input
    0
    
    Expected output