#include <bits/stdc++.h>
using namespace std;
template <typename T>
T gcd(T a, T b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
template <typename T>
T pow(T a, T b, long long int m) {
  T ans = 1;
  while (b > 0) {
    if (b % 2 == 1) ans = (ans * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return ans % m;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t;
  cin >> t;
  for (int T = 0; T < t; T++) {
    long double x, y, p, q;
    cin >> x >> y >> p >> q;
    long long int ans = -1, solved = 0;
    if (p / q == x / y) {
      ans = 0;
      solved = 1;
    } else if (p == q == 1) {
      ans = -1;
      solved = 1;
    } else if (p == 0) {
      ans = -1;
      solved = 1;
    }
    if (!solved) {
      long long int f = max((y - x + q - p - 1) / (q - p), (x + p - 1) / p);
      ans = q * f - y;
    }
    cout << ans << "\n";
  }
  return 0;
}
