#include <bits/stdc++.h>
using namespace std;
long long solve(long long x, long long y, long long p, long long q) {
  long long ans = q * (max((long long)((x + p - 1) / p),
                           (long long)((y - x + q - p - 1) / (q - p)))) -
                  y;
  return ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t;
  cin >> t;
  while (t--) {
    long long x, y, p, q;
    cin >> x >> y >> p >> q;
    if (p == 0) {
      if (x != 0)
        cout << "-1\n";
      else
        cout << "0\n";
    } else {
      if (p == q and x != y)
        cout << "-1\n";
      else if (p == q and x == y)
        cout << "0\n";
      else
        cout << solve(x, y, p, q) << "\n";
    }
  }
}
