INS14I - Infinite Sequence

Digo and Sharry like to play with numbers and sequences. They decide to write an infinite sequence. For this they start by choosing a number each. They start making the sequence by following these rules:

  1. Digo writes his number on a piece of paper. This is the 0th number of the sequence.
  2. Then Sharry writes his number on the paper, as many times as Digo’s number. e.g. if Digo wrote 2, then Sharry would write his number 2 times, making the total sequence of length 3.
  3. Then Digo writes his number on the paper, which becomes the next element of the sequence.
  4. Then Sharry writes his number as many times as the number indexed 1 in the sequence.
  5. Then Digo writes his number on the paper, which becomes the next element of the sequence.
  6. Then Sharry writes his number as many times as the number indexed 2 in the sequence.
  7. ... and the cycle goes on.

Now Digo starts to feel that the sequence that they are generating is very trivial. He claims he can tell the digit at the nth place without having to play the game. So Sharry challenges him to tell the digit which will come at the nth place.

Help Digo complete Sharry's challenge.

It is given that Digo chooses 4 as his number and Sharry chooses 5 as his number.

Input

First line of the input contains an integer T - the number of test cases.

T lines follow, each containing an integer N.

Output

Print T lines. For every test case, print the digit that appears in the Nth place.

Constraints

1 < T < 100

0 ≤ N < 1000000000

Sample

Input
3
0
4
7

Output
4
5
5

Explanation

The sequence is 4, 5, 5, 5, 5, 4, 5, 5, 5, 5, 5 ...


Added by:Surya Kiran
Date:2014-03-20
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Insomnia 2014

hide comments
2018-06-06 14:14:03 Sergej
Check number of testcases, please: T > 100 ?
2015-06-17 05:38:37 vishrut mishra
really enjoyed to solve it , learn a lot ;)
2014-12-22 11:59:47 vivek
AC in one go :)
2014-08-22 14:00:55 Pragya
How to declare an array with 10^9 size in C???
Please if someone could tell me another technique except for the run time allocation using malloc.
(Comment=>) When in such doubts look at the best solutions used memory. With that you can have an idea whether such large memory allocation is at all required.

Last edit: 2014-08-29 07:16:52
2014-06-30 11:03:53 Shantanu Singh
Learned something new .Its hard to get AC without that.
2014-06-22 19:23:55 kiran pilli
please provide some tricky test cases
2014-04-13 19:37:56 Surya kiran
@DEVENDRA : No more test cases will be provided
2014-03-30 05:47:11 lovecode
plz in running 2 i m getting wa plz someone give me more test cases plz

Last edit: 2014-03-31 21:22:44
2014-03-26 22:27:41 Jacob Plachta
There's a typo in the problem name.

Edit: Thanks for pointing it out. Changes made.

Last edit: 2014-03-26 22:28:20
2014-03-26 22:27:41 Surya kiran
@Rivu : No more test cases will be provided
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.