Under construction

Still being built. Things may change

Palindrome Game

Time limit1sMemory limit1024 MB

Summary
With a pile of N stones, players alternately remove a palindromic number of stones; determine whether the first player wins.
Level

Hard8 of 10

Topics
Game theory, Dynamic programming, Math, Implementation
Solved
No attempts yet

Problem

Sangyoon and Seungwoo are fierce rivals with a record of 500 wins and 500 losses in 1000 games.

To settle who is better at games, they decide to play one final game.

The rules are as follows.

  1. The game is played with a pile of NN stones.
  2. Sangyoon moves first. After that, Seungwoo and Sangyoon alternate turns.
  3. On your turn, you may take xx stones from the pile. Here xx must be a palindrome number.
  4. If you have no stones to take on your turn, you lose.

A palindrome number is a positive integer that reads the same when reversed, such as 33, 1111, and 929929. Numbers with leading zeros, such as 010010, do not count.

Both Sangyoon and Seungwoo have played 1000 games and always make the best move.

Given the number of stones NN, print who wins.

Input

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

Each of the next TT lines gives the number of stones NN in the pile. (1≤N≤10181 \le N \le 10^{18})

Output

For each game, print 0 if Sangyoon wins and 1 if Seungwoo wins.

Examples1

  1. Example 1

    Input
    4
    3
    10
    12
    27
    
    Expected output
    0
    1
    0
    0