#include using namespace std; long long f(long long n, long long a, long long b) { return a * n + b; } long long floorSum(long long n, long long m, long long a, long long b) { if (n == 0) return 0; long long toAdd = (a / m) * n * (n - 1) / 2 + (b / m) * n; a %= m; b %= m; return toAdd + f(n - 1, a, b) / m + floorSum(f(n - 1, a, b) / m, a, m, f(n - 1, a, b) % m); } int L, R, K, C; void read() { cin >> L >> R >> K >> C; } long long calc(int upto) { int N = upto / K; long long ret = 0; int p10 = 1; for (int nTh = 0; nTh < 9; ++nTh) { ret += floorSum(N + 1, p10 * 10, K, (10 - C) * p10) - floorSum(N + 1, p10 * 10, K, (9 - C) * p10); p10 *= 10; } return ret; } void work() { cout << calc(R) - calc(L - 1) << endl; } int main() { int T; cin >> T; for (int i = 0; i < T; ++i) { read(); work(); } return 0; }