Checksum

No attempts yetTime limit1sMemory limit128 MB

Problem

To guarantee the correctness of transmitted data, a checksum is sometimes attached to it. Imagine the following way of computing a checksum.

Given a message MM (as a bit string), we choose a checksum generator GG (also a bit string) and append to the end of MM a number of zeros equal to the number of bits of GG minus 11. This gives the extended message MM'. Interpreting the digits of MM' and GG as coefficients of polynomials over the field F2\mathbb{F}_2 (the most significant bit is the coefficient of the highest power of the variable), we compute the remainder of dividing the polynomial corresponding to MM' by the polynomial corresponding to GG. The sequence of coefficients of that remainder is called the checksum of the message MM.

Your task is to compute the checksum of the message MM. For certain reasons we trust the checksum more when the generator, interpreted as a binary number, is a prime, so compute the checksum only in that case. If the generator is not a prime, print the word ERROR.

Input

The first line contains an integer TT (1T1031 \le T \le 10^3), the number of test cases. Each of the next TT lines contains a single test case, consisting of two bit strings separated by a single space. The first bit string is the message MM (its length does not exceed 512512 bits); the second is the generator GG (its length does not exceed 4848 bits). Neither MM nor GG has leading zeros.

Output

For each test case, print on its own line the corresponding checksum as a decimal number (without leading zeros), unless the generator is not a prime — in that case print the word ERROR on that line.

Notes

About the field F2\mathbb{F}_2

By F2\mathbb{F}_2 we mean the set {0,1}\{0, 1\} with addition ++ and multiplication \cdot defined as follows.

  • 1+1=0+0=01 + 1 = 0 + 0 = 0 and 1+0=0+1=11 + 0 = 0 + 1 = 1
  • 10=01=00=01 \cdot 0 = 0 \cdot 1 = 0 \cdot 0 = 0 and 11=11 \cdot 1 = 1

Moreover, in F2\mathbb{F}_2 we have 1=1-1 = 1 and 0=0-0 = 0. This leads to the perhaps surprising conclusion that subtracting a number is equivalent to adding it, e.g. 01=0+1=10 - 1 = 0 + 1 = 1 and 11=1+1=01 - 1 = 1 + 1 = 0.

When operating on polynomials with coefficients in F2\mathbb{F}_2 we proceed analogously, e.g. (x3+x+1)(x1)=(x3+x+1)(x+1)=x4+x2+x+x3+x+1=x4+x3+x2+1(x^3 + x + 1)(-x - 1) = (x^3 + x + 1)(x + 1) = x^4 + x^2 + x + x^3 + x + 1 = x^4 + x^3 + x^2 + 1.

Worked examples

A generator whose binary value is composite yields the answer ERROR. For instance 100100, 110110, and 10001000 are the binary representations of the composite numbers 44, 66, and 88.

For a prime generator we compute the checksum. For example, with M=1101M = 1101 and G=10G = 10 we have M=11010M' = 11010, whose polynomial is WM(x)=x4+x3+xW_{M'}(x) = x^4 + x^3 + x and WG(x)=xW_G(x) = x. Dividing WMW_{M'} by WGW_G over F2\mathbb{F}_2 gives quotient x3+x2+1x^3 + x^2 + 1 and remainder 00, so the checksum is 00.

With M=1000M = 1000 and G=11G = 11 we have M=10000M' = 10000, whose polynomial is WM(x)=x4W_{M'}(x) = x^4 and WG(x)=x+1W_G(x) = x + 1. Dividing gives quotient x3+x2+x+1x^3 + x^2 + x + 1 and remainder 11, so the checksum is 11.