#d2955. 函数迭代求和

函数迭代求和

Iterated Function Summation

Problem Description

Define the function f(x)f(x):

$$f(x)=\prod_{i=1}^{k}\bigl(x\bmod 10^i\bigr)\bmod (x+1),$$

where kk is the number of digits of xx. For example, f(2013)=(2013×13×13×3) mod 2014f(2013)=(2013\times 13\times 13\times 3)\bmod 2014.

Define the function g(x,m)g(x,m):

$$g(x,m)= \begin{cases} f\bigl(g(x,m-1)\bigr), & m>1,\\ f(x), & m=1. \end{cases}$$

For example, g(x,2)=f(f(x))g(x,2)=f(f(x)).

You are given TT groups of x,mx, m. For each group, compute the value of ∑i=1mg(x,i)\displaystyle\sum_{i=1}^{m} g(x,i).

Input Format

The first line contains an integer TT (1≤T≤201\le T\le 20), the number of test cases.

The next TT lines each contain two integers x,mx, m (1≤x,m≤1091\le x,m\le 10^9).

Output Format

For each test case, output one integer on a single line.

Sample Input

2
3 4
4102 642

Sample Output

12
21262