Fast squarer

All submissions for this problem are available.
Read problems statements in Mandarin Chinese and Russian
A squarer is a simple and convenient device. You give it some positive integer X and it calculates its square.
Leha is implementing a module of this device which is responsible for squaring the numbers consisting of multiple repetitions of one digit. But it turned out that it's not as simple as he thought.
Please help him now!
Input
The first line contains one integer T denoting the number of testcases. The descriptions of T test cases follow.
Each of the following T lines contain 2 spaceseparated integers  N and D, respectively. It means that the number X in the corresponding testcase consists of the digit D repeated N times (in decimal representation).
Output
As the answer can be very large, we ask you to output its hash which is computed in the following way:
Let's consider the integer answer Y as a 0indexed array starting from its leftmost digit. The hash function is calculated as:
p^{0}*Y[0] + p^{1}*Y[1] + ... + p^{M1}*Y[M1] modulo 10^{9} + 7
where M is the length of the array representation of Y and p equals 23.
Constraints
 1 ≤ T ≤ 20
 1 ≤ D ≤ 9
 Subtask 1 (16 points): 1 ≤ N ≤ 9
 Subtask 2 (25 points): 1 ≤ N ≤ 100
 Subtask 3 (27 points): 1 ≤ N ≤ 2 × 10^{4}
 Subtask 4 (32 points): 1 ≤ N ≤ 10^{6}
Example
Input: 3 1 4 3 6 3 5 Output: 139 40079781 32745632
Explanation
In the first test case, X = 4 and Y = 16. Its hash equals 1*1 + 23*6 = 139.
Author:  pavel1996 
Tester:  xcwgf666 
Editorial  http://discuss.codechef.com/problems/FSTSQ 
Tags  adhoc, easy, implementation, ltime29, pavel1996 
Date Added:  27092015 
Time Limit:  2 sec 
Source Limit:  50000 Bytes 
Languages:  C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, SCALA, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, PERL6, TEXT, SCM chicken, CLOJ, FS 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions
HELP
If you are still having problems, see a sample solution here. 