#include <bits/stdc++.h>
using namespace std;
long long power_modulo(long long base, long long exp, long long mod) {
  long long result = 1;
  base %= mod;
  while (exp > 0) {
    if (exp % 2 == 1) result = (result * base) % mod;
    exp = exp >> 1;
    base = (base * base) % mod;
  }
  return result;
}
bool check(long long mid, long long x, long long y, long long p, long long q,
           long long &best) {
  long long num = mid * p, den = mid * q;
  if (num >= x && den >= y) {
    if (num - x <= den - y) {
      best = den - y;
      return true;
    }
  }
  return false;
}
int main() {
  int t;
  cin >> t;
  for (long long i = 0; i < (t); i++) {
    long long x, y, p, q, best;
    cin >> x >> y >> p >> q;
    long long l = 0, r = 2e9;
    best = -1;
    while (l <= r) {
      long long m = (l + r) / 2;
      if (check(m, x, y, p, q, best))
        r = m - 1;
      else
        l = m + 1;
    }
    cout << best << endl;
  }
}
