#include <cstring>
#include <iostream>
#include <sstream>
using namespace std;
long long memo[10][10][10][8][27][20];
long long step_found(int a, int b, int c, int found, int next_digit) {
// computes the next "found" bitmask if I'm looking for the digits "a", "b", "c"
// and see the next digit "next_digit"
if (next_digit == a && !(found&4)) return found|4;
if (next_digit == b && !(found&2)) return found|2;
if (next_digit == c && !(found&1)) return found|1;
if (next_digit > c) return -1;
return found;
}
long long go(int a, int b, int c, int found, int rem, int left) {
// a,b,c = the three largest digits we want to have (a >= b >= c)
// found = bitmask saying which of them we already have
// rem = the remainder so far
// left = the number of digits still left to fill in
if (left == 0) return (found==7 && rem==0); // we are at the end -- did we construct a valid number?
long long &result = memo[a][b][c][found][rem][left];
if (result != -1) return result;
result = 0;
for (int d=0; d<=9; ++d) {
// try appending the next digit
int next_rem = (10*rem + d) % (a+b+c);
int next_found = step_found(a, b, c, found, d);
if (next_found != -1) result += go(a, b, c, next_found, next_rem, left-1);
}
return result;
}
long long solve(long long UB) {
// count the good numbers in [1,UB)
// convert UB to string to count its digits and get its prefixes easily
stringstream ss; ss << UB;
string S = ss.str();
int D = S.size();
long long answer = 0;
// try all possibilities for the three largest digits
for (int a=1; a<=9; ++a) for (int b=0; b<=a; ++b) for (int c=0; c<=b; ++c) {
// first, count all numbers shorter than UB: iterate over their length and first digit
for (int len=3; len<D; ++len) {
for (int first=1; first<=9; ++first) {
int found = step_found(a, b, c, 0, first);
if (found != -1) answer += go(a, b, c, found, first%(a+b+c), len-1);
}
}
// next, for each prefix of UB count all numbers with a smaller last digit
int prefix_rem = 0, prefix_found = 0;
for (int p=1; p<=D; ++p) {
int pd = S[p-1]-'0';
for (int d=0; d<pd; ++d) {
if (p==1 && d==0) continue; // we don't want to count numbers with a leading zero
int next_found = step_found(a, b, c, prefix_found, d);
if (next_found != -1) answer += go(a, b, c, next_found, (10*prefix_rem+d)%(a+b+c), D-p);
}
prefix_rem = (10*prefix_rem + pd) % (a+b+c);
prefix_found = step_found(a, b, c, prefix_found, pd);
if (prefix_found == -1) break;
}
}
return answer;
}
int main() {
memset(memo,-1,sizeof(memo));
int T; cin >> T;
while (T--) { long long A, B; cin >> A >> B; cout << (solve(B+1) - solve(A)) << endl; }
}
I2luY2x1ZGUgPGNzdHJpbmc+CiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPHNzdHJlYW0+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgpsb25nIGxvbmcgbWVtb1sxMF1bMTBdWzEwXVs4XVsyN11bMjBdOwoKbG9uZyBsb25nIHN0ZXBfZm91bmQoaW50IGEsIGludCBiLCBpbnQgYywgaW50IGZvdW5kLCBpbnQgbmV4dF9kaWdpdCkgewogICAgLy8gY29tcHV0ZXMgdGhlIG5leHQgImZvdW5kIiBiaXRtYXNrIGlmIEknbSBsb29raW5nIGZvciB0aGUgZGlnaXRzICJhIiwgImIiLCAiYyIKICAgIC8vIGFuZCBzZWUgdGhlIG5leHQgZGlnaXQgIm5leHRfZGlnaXQiCiAgICBpZiAobmV4dF9kaWdpdCA9PSBhICYmICEoZm91bmQmNCkpIHJldHVybiBmb3VuZHw0OwogICAgaWYgKG5leHRfZGlnaXQgPT0gYiAmJiAhKGZvdW5kJjIpKSByZXR1cm4gZm91bmR8MjsKICAgIGlmIChuZXh0X2RpZ2l0ID09IGMgJiYgIShmb3VuZCYxKSkgcmV0dXJuIGZvdW5kfDE7CiAgICBpZiAobmV4dF9kaWdpdCA+IGMpIHJldHVybiAtMTsKICAgIHJldHVybiBmb3VuZDsKfQoKbG9uZyBsb25nIGdvKGludCBhLCBpbnQgYiwgaW50IGMsIGludCBmb3VuZCwgaW50IHJlbSwgaW50IGxlZnQpIHsKICAgIC8vIGEsYixjID0gdGhlIHRocmVlIGxhcmdlc3QgZGlnaXRzIHdlIHdhbnQgdG8gaGF2ZSAoYSA+PSBiID49IGMpCiAgICAvLyBmb3VuZCA9IGJpdG1hc2sgc2F5aW5nIHdoaWNoIG9mIHRoZW0gd2UgYWxyZWFkeSBoYXZlC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