#include <bits/stdc++.h>
using namespace std;
template <typename T>
T INF(T a) {
  a = numeric_limits<T>::max();
  return a;
}
template <typename T>
T mINF(T a) {
  a = numeric_limits<T>::min();
  return a;
}
long long int v;
long long int x, y, p, q;
bool certo(long long int vi) {
  bool ans = 1;
  long long int pl = p * vi;
  long long int ql = q * vi;
  long long int nedx = pl - x;
  long long int nedy = ql - y;
  if (nedx < 0 or nedy < 0) {
    ans = 0;
  }
  if (nedx > nedy) {
    ans = 0;
  }
  return ans;
}
int main() {
  int t;
  cin >> t;
  for (int ii = 0; ii < (t); ii++) {
    scanf("%lld %lld %lld %lld", &x, &y, &p, &q);
    if (p == 0) {
      x == 0 ? cout << 0 : cout << -1;
      cout << endl;
      continue;
    }
    if (p == q) {
      x == y ? cout << 0 : cout << -1;
      cout << endl;
      continue;
    }
    long long int obj;
    long long int lo = 1, hi = 1e9 + 1;
    while (hi > lo) {
      long long int mid = (hi + lo) / 2;
      if (certo(mid)) {
        hi = mid;
      } else {
        lo = mid + 1;
      }
    }
    v = lo;
    long long int pl = p * v;
    long long int ql = q * v;
    long long int nedx = pl - x;
    long long int nedy = ql - y;
    long long int ans = nedy;
    if (nedx > nedy) {
      ans = -1;
    }
    cout << ans << endl;
  }
  return 0;
}
