Define function f(x)=∏i=1len(x%10i)%(x+1), where len represents the digit length of x.
For example, f(1023)=(3∗23∗23∗1023)%1024.
Define function g(n,m)={f(g(n,m−1))f(n)m>1m=1. For example, g(n,2)=f(f(n)).
You are given n and m, please calculate ∑i=1mg(n,i).
Input
The input consists of multiple test cases.
The first line contains an integer T (1≤T≤20) — the number of test cases. The description of the test cases follows.
The only line contains two integers n,m (1≤n,m≤109) .
Output
For each test case, print the answer.
Example
Input
2
3 4
4102 642
Output
12
21262
#include<bits/stdc++.h>
using namespace std;
long long f(int n)
{
if(n==0)
return 0;
long long t=10;
long long ans=1;
int m=n;
while(m)
{
ans*=n%t;
ans%=(n+1);
t*=10;
m/=10;
}
return ans;
}
int main()
{
long long n,m,t,i;
scanf("%lld",&t);
while(t--)
{
long long k1,k2,ans=0;
scanf("%lld %lld",&n,&m);
k1=f(n);
ans+=k1;
for(i=2; i<=m; i++)
{
k2=f(k1);
ans+=k2;
if(k2==k1) //继续下去都会是相同数值,不再进入函数
break; //利用乘法算出答案,节省时间
else
k1=k2;
}
if(i<m)
ans+=k1*(m-i);
if(m==0)
ans=0;
printf("%lld\n",ans);
}
return 0;
}