#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define mk make_pair
#define ll long long
#define fastio ios_base::sync_with_stdio(0); cin.tie();
using namespace std;
typedef pair<int, int> pii;
const ll MOD=1e9+7;
const int N=0;
int par[1];
int find(int x){return x==par[x]?x:par[x]=find(par[x]);}
void join(int x,int y){par[find(x)]=y;}
ll expo(ll a,ll b,ll mod){ll ans=1; while(b){if(b&1) ans=(ans*a)%mod; a=(a*a)%mod; b>>=1;} return ans;}
int gcd(int a,int b,int& x,int& y) {if(a==0){x=0;y=1;return b;}int x1, y1;int d=gcd(b%a, a, x1, y1);x=y1-(b/a)*x1;y=x1;return d;}
ll c[20][20],p[10][20];
int a[10],b[10];
int fix[10];
int dig[20];
int fix_n,dig_n;
ll ways_to_place(int places){
ll ans=1;
for(int i=0;i<fix_n;++i){
if(b[fix[i]]<0){
return 0;
}
ans*=c[places][b[fix[i]]];
places-=b[fix[i]];
if(places<0){
return 0;
}
}
ans*=p[10-fix_n][places];
//cout<<ans<<"\n";
return ans;
}
ll calc(ll x){
if(x==0) return 0;
//cout << x << ", ";
for(dig_n=0;x;x/=10){
dig[dig_n++]=x%10;
}
//cout << dig_n << " -- ";
int i,j,be;
for(i=0;i<10;++i){
b[i]=a[i];
}
ll ans=0;
for(i=0;i<dig_n-1;++i){
for(j=1;j<10;++j){
b[j]--;
ans+=ways_to_place(i);
b[j]++;
}
}
for(i=dig_n-1;i>=0;--i){
be=(i==dig_n-1 ? 1 : 0);
for(j=be;j<dig[i];++j){
b[j]--;
ans+=ways_to_place(i);
b[j]++;
}
b[dig[i]]--;
}
ans+=ways_to_place(0);
return ans;
}
ll solve(ll x){
int i,end=(1<<10);
ll ans=x;
for(int mask=1;mask<end;++mask){
fix_n=0;
for(i=0;(1<<i)<=mask;++i){
if(mask&(1<<i)){
fix[fix_n++]=i;
}
}
if(fix_n&1){
ans-=calc(x);
} else{
ans+=calc(x);
}
}
return ans;
}
void init(){
int i,j;
for(i=0;i<20;++i){
c[i][0]=1;
}
for(i=1;i<20;++i){
for(j=1;j<=i;++j){
c[i][j]=c[i-1][j-1]+c[i-1][j];
}
}
p[0][0]=1;
for(i=1;i<=10;++i){
p[i][0]=1;
for(j=1;j<20;++j){
p[i][j]=p[i][j-1]*i;
}
}
//for(int i = 0; i <= 5; i++){
// for(int j = 0; j <= 5; j++){
// cout << p[i][j] << " ";
// }
// cout << endl;
//}
}
int main(){
int test,i;
ll r,l;
ll res;
init();
scanf("%d",&test);
while(test--){
scanf("%lld",&l);scanf("%lld",&r);
for(i=0;i<10;++i){
scanf("%d",&a[i]);
}
res=solve(r)-solve(l-1);
printf("%lld\n", res);
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgRiBmaXJzdAojZGVmaW5lIFMgc2Vjb25kCiNkZWZpbmUgcGIgcHVzaF9iYWNrCiNkZWZpbmUgbWsgbWFrZV9wYWlyCiNkZWZpbmUgbGwgbG9uZyBsb25nCiNkZWZpbmUgZmFzdGlvIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oMCk7IGNpbi50aWUoKTsKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKdHlwZWRlZiBwYWlyPGludCwgaW50PiBwaWk7CmNvbnN0IGxsIE1PRD0xZTkrNzsKY29uc3QgaW50IE49MDsKIAppbnQgcGFyWzFdOwppbnQgZmluZChpbnQgeCl7cmV0dXJuIHg9PXBhclt4XT94OnBhclt4XT1maW5kKHBhclt4XSk7fQp2b2lkIGpvaW4oaW50IHgsaW50IHkpe3BhcltmaW5kKHgpXT15O30KbGwgZXhwbyhsbCBhLGxsIGIsbGwgbW9kKXtsbCBhbnM9MTsgd2hpbGUoYil7aWYoYiYxKSBhbnM9KGFucyphKSVtb2Q7IGE9KGEqYSklbW9kOyBiPj49MTt9IHJldHVybiBhbnM7fQppbnQgZ2NkKGludCBhLGludCBiLGludCYgeCxpbnQmIHkpIHtpZihhPT0wKXt4PTA7eT0xO3JldHVybiBiO31pbnQge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