#include using namespace std; using ll = long long; void solve() { string s; string t; cin >> s >> t; ll ans = 0; int n = s.size(), nk = t.size(); ll val[20]; for (int i = 0; i < n - 1; i ++) { val[i] = stoll(s.substr(i + 1, n - i - 1)) + 1; } val[n - 1] = 0; ll tm[20]; tm[0] = 1; for (int i = 1; i < 18; i ++) { tm[i] = tm[i - 1] * 10; } bool eq = true; for (int pkj = 0; pkj < nk; pkj ++) { // ll sm = 0; int i = (pkj == 0); for (; i < t[pkj] - '0'; i ++) { ll nm = 0; for (int x = 0; x < n - pkj; x ++) { if (!eq) { nm += tm[x]; continue; } if (i == s[pkj] - '0' && x == n - pkj - 1) { nm += val[pkj]; } else if (i > s[pkj] - '0' && x == n - pkj - 1) { break; } else { nm += tm[x]; } } ans += nm; } eq = eq && s[pkj] == t[pkj]; ans ++; } cout << ans << endl; } int main () { int T; cin >> T; while (T--) { solve(); } }