A balanced number is a non-negative integer that can be balanced if a pivot is placed at some digit. More specifically, imagine each digit as a box with weight indicated by the digit. When a pivot is placed at some digit of the number, the distance from a digit to the pivot is the offset between it and the pivot. Then the torques of left part and right part can be calculated. It is balanced if they are the same. A balanced number must be balanced with the pivot at some of its digits. For example, 4139 is a balanced number with pivot fixed at 3. The torqueses are 4*2 + 1*1 = 9 and 9*1 = 9, for left part and right part, respectively. It's your job 
to calculate the number of balanced numbers in a given range [x, y].

Input

The input contains multiple test cases. The first line is the total number of cases T (0 < T ≤ 30). For each case, there are two integers separated by a space in a line, x and y. (0 ≤ x ≤ y ≤ 10 18).

Output

For each case, print the number of balanced numbers in the range [x, y] in a line.

Sample Input

2
0 9
7604 24324

Sample Output

10
897

 题意:求一个区间内的平衡数有多少个,平衡数就是以这个数的某一个位为中心,左边的数的大小乘上这个数与中心的距离的和与右边的数的大小乘上这个数与中心的距离的和相等,那么这个数就是平衡数

思路:是否为平衡数可以用中心左边的和减去中心右边的和是否等于零来判断

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
ll dp[25][25][4000];
ll x,y;
ll a[25];
ll dfs(int pos,int cen,ll temp,int limit)
{
    if(pos<0)return temp==0;
    if(!limit&&dp[pos][cen][temp]!=-1)return dp[pos][cen][temp];
    int up=limit?a[pos]:9;
    ll ans=0;
    for(int i=0;i<=up;i++) ans+=dfs(pos-1,cen,temp+(pos-cen)*i,limit&&i==up);
    if(!limit) dp[pos][cen][temp]=ans;
    return ans;
}
ll solve(ll x)
{
    int t=0;
    ll ans=0;
    do{
        a[t++]=x%10;
        x/=10;
    }while(x);
    for(int i=0;i<t;i++)ans+=dfs(t-1,i,0,1);
    return ans-t+1;
}
int main()
{
    int T;
    scanf("%d",&T);
    memset(dp,-1,sizeof(dp));
    while(T--)
    {
        scanf("%lld%lld",&x,&y);
        printf("%lld\n",solve(y)-solve(x-1));
    }  
    return 0;
}