#include #include unsigned long long int floor_sum(long long a, long long b, long long m, long long n) { unsigned long long int ans = a / m * ((n - 1) * n / 2) + b / m * n; a %= m; b %= m; if (!a) return ans; unsigned long long int n2 = (a * n + b) / m; return ans + (n - 1) * n2 - floor_sum(n2, a, m, m - 1 - b); } long long solver_inner(long long n, long long x, long long k, long long d) { return n - (floor_sum(k, k, d, n) - floor_sum(k, k - x, d, n)); } long long solver(long long lim, long long k, long long c) { long long n = lim / k; if (n <= 0) return 0; long long ans = 0; for (long long d = 1; d <= 9; ++d) ans += solver_inner(n, c * pow(10, (d - 1)), k, pow(10, d)) - solver_inner(n, (c + 1) * pow(10, d - 1), k, pow(10, d)); return ans; } void solve(void) { long long L, R, K, C; scanf("%lld%lld%lld%lld", &L, &R, &K, &C); printf("%lld\n", solver(R, K, C) - solver(L - 1, K, C)); } int main(void) { int T; scanf("%d", &T); while (T--) { solve(); } return 0; }