def solve(): n, k = map(int, input().split()) ans = 0 k = str(k) tot = 0 for s in k: tot *= 10 tot += int(s) if tot <= n: ans += 1 for i in range(tot // 10 * 10, tot): if i == 0: continue di = 1 t = i while t <= n: ans += max(0, min(n + 1, t + di) - t) t *= 10 di *= 10 print(ans) for _ in range(int(input())): solve()