#include #include using namespace std; using namespace atcoder; using mint = modint998244353; using ll = long long; #define all(x) x.begin(), x.end() #define rep(i, n) for (int i = 0; i < n; i++) ll f(ll r, ll k, ll c){ ll n = r / k; ll ans = 0; ll d = 1; for(int i = 0; i < 9; i++){ ll x = c * d; ll y = (c + 1) * d; d *= 10; ans += floor_sum(n, d, k, d + k - x) - floor_sum(n, d, k, d + k - y); } return ans; } ll gu(ll l, ll r, ll k, ll c){ ll s = (l + k - 1) / k; ll t = r / k; ll ans = 0; for(ll i = s; i <= t; i++){ ll now = k * i; while(now > 0){ if (now % 10 == c) ans++; now /= 10; } } return ans; } ll solve(){ ll l, r, k, c; cin >> l >> r >> k >> c; if(k > 31622){ return gu(l, r, k, c); } return f(r, k, c) - f(l - 1, k, c); } int main(void) { int t; cin >> t; rep(i, t){ cout << solve() << endl; } return 0; }