Under construction

Still being built. Things may change

Palindrome in base n

Interview

Time limit1sMemory limit256 MB

Summary
For each query, convert the decimal number A to base n and report whether its digit sequence reads the same forwards and backwards.
Level

Easy3 of 10

Topics
Math, Implementation, Two pointers, Brute force
Solved
No attempts yet

Problem

Base n notation writes a number with radix nn. Decimal notation, for example, uses radix 10. A number AmAm−1Am−2…A1A0A_m A_{m-1} A_{m-2} \dots A_1 A_0 written in base n has the value

AmAm−1Am−2…A1A0=Am×nm+Am−1×nm−1+Am−2×nm−2+⋯+A1×n1+A0×n0A_m A_{m-1} A_{m-2} \dots A_1 A_0 = A_m \times n^m + A_{m-1} \times n^{m-1} + A_{m-2} \times n^{m-2} + \dots + A_1 \times n^1 + A_0 \times n^0

For example, the decimal number 12468 equals 1×104+2×103+4×102+6×101+8×1001 \times 10^4 + 2 \times 10^3 + 4 \times 10^2 + 6 \times 10^1 + 8 \times 10^0.

A palindrome is text that reads the same forwards and backwards. madam, level, and 12321 are palindromes. Chung-ang, university, and 54899 are not.

Given a decimal number AA and a radix nn, write a program that decides whether the base n representation of AA is a palindrome. Each position counts as a single symbol, and the representation carries no leading zero. In other words, the representation is a palindrome when reversing the sequence of digit values leaves it unchanged.

Input

The first line contains the number of test cases TT (1≤T≤1 0001 \le T \le 1\,000).

Each of the next TT lines contains a decimal number AA (1≤A≤100 000 000 0001 \le A \le 100\,000\,000\,000) and a radix nn (2≤n≤162 \le n \le 16), separated by a space.

Output

For each test case, print 1 on its own line if the base n representation of AA is a palindrome, and 0 otherwise.

Examples2

  1. Example 1

    Input
    5
    100 3
    30 2
    25 4
    58 8
    342633 16
    
    Expected output
    1
    0
    1
    0
    0
    
  2. Example 2

    Input
    1
    1 2
    
    Expected output
    1