#include <bits/stdc++.h>
using namespace std;
void readfile() {
  ios_base ::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
const long long nmax = (long long)5e5 + 20;
long long x, y, p, q;
signed main() {
  readfile();
  long long t;
  cin >> t;
  while (t--) {
    cin >> x >> y >> p >> q;
    if (p == 0 && x == 0) {
      cout << 0 << endl;
      continue;
    }
    if (p == 0) {
      cout << -1 << endl;
      continue;
    }
    long long lo = 1LL;
    long long hi = (long long)1e9;
    long long iter = 64;
    while (iter--) {
      long long mid = (lo + hi) / 2;
      long long q1 = q * mid;
      long long p1 = p * mid;
      long long yy = q1 - y;
      long long xx = p1 - x;
      if (yy < 0 || xx < 0 || xx > yy) {
        lo = mid;
      } else {
        hi = mid;
      }
    }
    long long ans = -1;
    for (long long i = (long long)max(0LL, lo - 10); i <= (long long)hi + 10;
         i++) {
      long long q1 = q * i;
      long long p1 = p * i;
      long long yy = q1 - y;
      long long xx = p1 - x;
      if (yy < 0 || xx < 0 || xx > yy) continue;
      ans = yy;
      break;
    }
    cout << ans << endl;
  }
  return 0;
}
