#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
long long x, y, p, q;
long long ya;
long long gcd(long long a, long long b) {
  if (!b)
    return a;
  else
    return gcd(b, a % b);
}
bool go(long long val) {
  long long yy = y + q * val;
  long long xx = yy / q;
  xx *= p;
  if (xx < x) return false;
  return (yy - ya >= xx - x);
}
int main(void) {
  ios_base::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) {
    cin >> x >> y >> p >> q;
    ya = y;
    if (!p) {
      if (!x)
        cout << 0 << endl;
      else
        cout << -1 << endl;
      continue;
    } else if (p == q) {
      if (x == y)
        cout << 0 << endl;
      else
        cout << -1 << endl;
      continue;
    }
    long long g = gcd(p, q);
    p /= g;
    q /= g;
    y += (q - (y % q)) % q;
    long long bot = 0, top = 1000000099;
    long long ans;
    while (bot <= top) {
      long long mid = (bot + top) / 2LL;
      if (go(mid)) {
        ans = mid;
        top = mid - 1;
      } else {
        bot = mid + 1;
      }
    }
    cout << y - ya + ans * q << endl;
  }
  return 0;
}
