import sys input = sys.stdin.readline def calc(digits, N): if digits < N[:len(digits)]: cnt = 0 for d in range(len(N)-len(digits)+1): cnt += 10**d return cnt if digits > N[:len(digits)]: cnt = 0 for d in range(len(N)-len(digits)): cnt += 10**d return cnt res = 0 cnt = 1 for d in range(len(digits), len(N)): res = res * 10 + N[d] cnt += res + 1 return cnt T = int(input()) for _ in range(T): N, K = map(int, input().split()) N = list(map(int, str(N))) K = list(map(int, str(K))) ans = 0 for i, x in enumerate(K): for y in range(x): if i == 0 and y == 0: continue ans += calc(K[:i] + [y], N) ans += 1 print(ans)