#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 f(long long ll, long long rr, long long k, long long d, long long x) { long long p = floor_sum(rr, d, k, 0) - floor_sum(ll, d, k, 0); long long q = floor_sum(rr, d, k, d - x) - floor_sum(ll, d, k, d - x) - (rr - ll); return (rr - ll) - (p - q); } int main(void) { int T; scanf("%d", &T); while (T--) { long long l, r, k, c; scanf("%lld %lld %lld %lld", &l, &r, &k, &c); long long ll = (l + k - 1) / k; long long rr = r / k + 1; long long ans = 0, now = 0; for (long long i = 0; i < 9; ++i) { ans += f(ll, rr, k, now * 10, now * c) - f(ll, rr, k, now * 10, now * (c + 1)); now *= 10; } printf("%lld\n", ans); } return 0; }