#include #include #define rep(i, n) for (int i = 0; i < n; i++) #define ALL(a) a.begin(), a.end() #define ll long long using namespace std; using namespace atcoder; ll ten[11]; ll f(ll r, ll k, ll c) { ll ans = 0; rep(i, 9) { ans += floor_sum(r, ten[i + 1], k, ten[i + 1] - c * ten[i] + k); ans -= floor_sum(r, ten[i + 1], k, k); } return ans; } void solve() { ll l, r, k, c; cin >> l >> r >> k >> c; l = (l - 1) / k; r = r / k; ll ans = f(r, k, c) - f(r, k, c + 1) - f(l, k, c) + f(l, k, c + 1); cout << ans << '\n'; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(13); ten[0] = 1; rep(i, 10) ten[i + 1] = 10 * ten[i]; int t; cin >> t; rep(i, t) solve(); return 0; }