#include <bits/stdc++.h>
using namespace std;
int IN() {
  int c, f, x;
  while (!isdigit(c = getchar()) && c != '-')
    ;
  c == '-' ? (f = 1, x = 0) : (f = 0, x = c - '0');
  while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + c - '0';
  return !f ? x : -x;
}
int x, y, p, q;
int cal(int a, int b) { return a / b + (a % b != 0); }
long long Main() {
  x = IN(), y = IN(), p = IN(), q = IN();
  if (1ll * x * q == 1ll * p * y) return 0;
  if (p == 0 || p == q) return -1;
  int k = max(cal(x, p), max(cal(y, q), cal(y - x, q - p)));
  return 1ll * k * q - y;
}
int main() {
  for (int T = IN(); T--;) {
    printf("%I64d\n", Main());
  }
}
