for _ in range(int(input())): a = 0 n, k = map(int, input().split()) l = len(str(k)) for i in range(18 - l): a += max(10 ** (l + i), min(k * 10 ** (i + 1), n + 1)) - 10 ** (l + i) for i in range(l): a += max(10 ** (l - i - 1), min(k // (10 ** i), n)) - 10 ** (l - i - 1) + 1 print(a)