#include <bits/stdc++.h>
#include <atcoder/math>
using namespace std;
using ll = long long;


ll solve(){
    ll L, R, K, C;
    cin >> L >> R >> K >> C;
    R /= K;
    --L /= K;
    ll ans = 0;
    ll pow10 = 1;
    for(ll d = 0; d < 9; d++){
        ans += atcoder::floor_sum(R - L, pow10 * 10, K, (L + 1) * K - C * pow10);
        ans -= atcoder::floor_sum(R - L, pow10 * 10, K, (L + 1) * K - (C + 1) * pow10);
        pow10 *= 10;
    }
    return ans;
}
int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    ll T;
    cin >> T;
    while(T--) cout << solve() << '\n';
}