def naive(n): return n*(n+1)//2 def solve(s,mod): # s = str(n) m = len(s) sum = 0 # print(m) dp = [0] * (m+1) factor = [1] * m for i in range(m,1,-1): factor[i-2] = factor[i-1]*10%mod # print(factor) for i in range(m): last = sum + int(s[i])*factor[i] last %= mod # print(last) dp[i+1] = dp[i] + (last + sum + 1)*(last-sum)//2 dp[i+1] %= mod sum = last return dp[m] # random # for i in range(1,150): # assert(naive(i) == solve(i)) # stress t = int(input()) for _ in range(t): n,m = list(map(str, input().split(' '))) m = int(m) print(solve(n,m))