public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t > 0) {
int a = sc.nextInt();
int b = sc.nextInt();
int p = sc.nextInt();
int q = sc.nextInt();
int total = 0;
while (a < b) {
if (a + p >= b) {
a = a + p;
} else {
p *= q;
}
total++;
}
System.out.println(total);
}
sc.close();
}