PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000192. Dãy chữ số – phiên bản khó (Numerical Sequence, Hard Version)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Dãy chữ số – phiên bản khó (Numerical Sequence, Hard Version)

Numerical Sequence (Hard Version)

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

An infinite string of digits is formed by concatenating blocks 1, 12, 123, 1234, ... . Block tt is obtained by concatenating decimal representations of the integers from 11 through tt, without spaces.

The infinite string begins with 112123123412345.... Individual digits, rather than whole integers, are indexed starting at 11.

Answer qq independent queries: for each position kk, output the single digit at that position. In this hard version, kk can be as large as 101810^{18}, so explicit generation is infeasible.

Input

  • First line: integer qq.
  • Each of the next qq lines contains one query position kk.

Output

  • Print one decimal digit per query, each on its own line.

Subtasks

  • Subtask 1 (20 points): 1≤q≤121\le q\le 12; 1≤ki≤1000001\le k_i\le 100000.
  • Subtask 2 (30 points): 1≤q≤1001\le q\le 100; 1≤ki≤10000000000001\le k_i\le 1000000000000.
  • Subtask 3 (50 points): 1≤q≤5001\le q\le 500; 1≤ki≤10000000000000000001\le k_i\le 1000000000000000000.

Examples

Input

5
1
3
20
38
56

Output

1
2
5
2
0

Explanation

The first six blocks are 1, 12, 123, 1234, 12345, 123456 with lengths 1,2,3,4,5,61,2,3,4,5,6. Their cumulative length is 2121.

Position Digit Reason
1 1 Only digit in block 1
3 2 Second digit of block 2
20 5 Fifth digit of block 6
38 2 Found from block-length prefixes
56 0 Last digit of 10 at the end of block 10

Large positions are handled arithmetically, without building the string.