#include <bits/stdc++.h>
using namespace std;
long long z = 1000000007;
long long gcd(long long a, long long b) {
  if (a == 0) return b;
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long power(long long a, long long b) {
  long long res = 1;
  while (b) {
    if (b & 1) {
      res = (res * a) % z;
      b--;
    } else {
      a = (a * a) % z;
      b = b >> 1;
    }
  }
  return res;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  t = 1;
  cin >> t;
  while (t--) {
    long long x, y, p, q;
    cin >> x >> y >> p >> q;
    long long ans = -1;
    long long l = y / q, r = 1e9;
    if (y % q) l++;
    while (l <= r) {
      long long mid = l + (r - l) / 2;
      long long k = mid * q - y;
      if (x + k >= p * mid && p * mid >= x) {
        ans = k;
        r = mid - 1;
      } else
        l = mid + 1;
    }
    cout << ans << "\n";
  }
}
