from functools import lru_cache import sys ii = lambda: int(input()) mi = lambda: map(int, input().split()) li = lambda: list(mi()) INF = 2 ** 63 - 1 mod = 998244353 def floor_sum(N, M, a, b): res = 0 while N: q, a = divmod(a, M) res += N * (N - 1) // 2 * q q, b = divmod(b, M) res += N * q N, b = divmod(a * N + b, M) a, M = M, a return res def f(r, k, c): n = r // k ans = 0 for i in range(1, 10): d = 10 ** i x = c * (d // 10) y = (c + 1) * (d // 10) ans += n - floor_sum(n, d, k, d + k) + floor_sum(n, d, k, d + k - x) if c < 9: ans -= n - floor_sum(n, d, k, d + k) + floor_sum(n, d, k, d + k - y) return ans def solve(l, r, k, c): return f(r, k, c) - f(l - 1, k, c) def solve_gu(l, r, k, c): s = (l + k - 1) // k t = r // k ans = 0 for i in range(s, t + 1): now = k * i while now > 0: if now % 10 == c: ans += 1 now //= 10 return ans for _ in range(ii()): l, r, k, c = mi() print(solve(l, r, k, c))