#include using namespace std; // sum_{i=0}^{n-1} floor((a * i + b) / m) // O(log(n + m + a + b)) long long floor_sum(long long n, long long a, long long b, long long m) { if (n == 0) return 0; long long res = 0; if (a >= m) { res += n * (n - 1) * (a / m) / 2; a %= m; } if (b >= m) { res += n * (b / m); b %= m; } if (a == 0) return res; long long ymax = (a * n + b) / m, xmax = ymax * m - b; if (ymax == 0) return res; res += (n - (xmax + a - 1) / a) * ymax; res += floor_sum(ymax, m, (a - xmax % a) % a, a); return res; } int main() { int CASE; cin >> CASE; while (CASE--) { long long L, R, K, C; cin >> L >> R >> K >> C; // 0 以上 N 以下の整数についての答えを求める auto solve = [&](long long N) -> long long { long long res = 0; long long beki = 1; for (int k = 0; k <= 10; ++k) { long long upper = floor_sum(N/K+1, K, beki*(10-C), beki*10); long long lower = floor_sum(N/K+1, K, beki*(9-C), beki*10); res += upper - lower; beki *= 10; } return res; }; cout << solve(R) - solve(L-1) << endl; } }