#include <bits/stdc++.h>
using namespace std;
bool check(long long x, long long y, long long p, long long q, long long t) {
  return p * t >= x && q * t - p * t >= y - x;
}
int main() {
  int t;
  cin >> t;
  for (int i = 1; i <= t; i++) {
    long long x, y, p, q;
    cin >> x >> y >> p >> q;
    long long l = -1;
    long long r = (long long)1e9;
    if (!check(x, y, p, q, r)) {
      cout << -1 << endl;
      continue;
    }
    while (r - l > 1) {
      long long m = (l + r) / 2;
      if (check(x, y, p, q, m)) {
        r = m;
      } else
        l = m;
    }
    long long ans = r * q - y;
    cout << ans << endl;
  }
}
