#include <bits/stdc++.h>
using namespace std;
inline void in(long long &now) {
  char Cget = getchar();
  now = 0;
  while (Cget > '9' || Cget < '0') Cget = getchar();
  while (Cget >= '0' && Cget <= '9') {
    now = now * 10 + Cget - '0';
    Cget = getchar();
  }
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
int main() {
  long long t, x, y, p, q;
  in(t);
  for (; t--;) {
    in(x), in(y), in(p), in(q);
    p /= gcd(p, q), q /= gcd(p, q);
    if (q == p) {
      if (x != y) {
        printf("-1\n");
        continue;
      }
    }
    if (p == 0) {
      if (x != 0) {
        printf("-1\n");
        continue;
      }
    }
    long long pos = (q - y % q) % q;
    double ki = (double)p / (double)q, li, ri;
    long long l = 0, r = 1000000000, ans;
    while (l <= r) {
      int mid = l + r >> 1;
      li = (double)x / (double)(y + pos + (mid * q));
      ri = (double)(x + pos + mid * q) / (double)(y + pos + mid * q);
      if (ki >= li && ki <= ri)
        ans = mid, r = mid - 1;
      else
        l = mid + 1;
    }
    printf("%lld\n", pos + ans * q);
  }
  return 0;
}
