import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int i = 0; i < t; i++){ long l = sc.nextLong(); long r = sc.nextLong(); long k = sc.nextLong(); long c = sc.nextLong(); long ans = f(r, k, c) - f(l - 1, k, c); System.out.println(ans); } } public static long floorSum(long n, long m, long a, long b){ long ret = 0; if(a >= m){ ret += (n - 1) * n * (a/m)/2; a %= m; } if(b >= m){ ret += n * (b/m); b %= m; } long ym = (a * n + b) / m; long xm = (ym * m - b); if(ym == 0) return ret; ret += (n - (xm + a - 1)/a) * ym; ret += floorSum(ym, a, m, (a - xm % a) % a); return ret; } public static long f(long r, long k, long c){ long n = r / k; long ans = 0; long d = 1; for(int i = 0; i < 9; i++){ long x = c * d; long y = (c + 1) * d; d *= 10; ans += floorSum(n, d, k, d + k - x) - floorSum(n, d, k, d + k - y); } return ans; } }