#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  long long x, y, p, q;
  while (t--) {
    cin >> x >> y >> p >> q;
    if (p == q) {
      if (x == y)
        cout << "0" << endl;
      else
        cout << "-1" << endl;
      continue;
    }
    if (p == 0) {
      if (x == 0)
        cout << "0" << endl;
      else
        cout << "-1" << endl;
      continue;
    }
    long long t1 = (x + p - 1) / p;
    long long t2 = ((y - x) + (q - p) - 1) / (q - p);
    long long t = max(t1, t2);
    long long ans = q * 1LL * t - y;
    cout << ans << endl;
  }
  return 0;
}
