#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long add() { return 0; }
template <typename T, typename... Types>
T add(T var1, Types... var2) {
  return (((((long long)(var1)) % mod + (long long)(add(var2...))) % mod) +
          mod) %
         mod;
}
long long mul() { return 1; }
template <typename T, typename... Types>
T mul(T var1, Types... var2) {
  return (((long long)(var1)) % mod * (long long)(mul(var2...))) % mod;
}
long long T;
signed main() {
  cin >> T;
  while (T--) {
    long long x, y, p, q;
    cin >> x >> y >> p >> q;
    if (p == 0) {
      if (x == 0) {
        cout << 0 << '\n';
      } else {
        cout << -1 << '\n';
      }
      continue;
    }
    if (p == q) {
      if (x == y) {
        cout << 0 << '\n';
      } else {
        cout << -1 << '\n';
      }
      continue;
    }
    long long first = (x + p - 1) / p;
    long long second = ((y - x) + (q - p) - 1) / (q - p);
    cout << q * max(first, second) - y << '\n';
  }
  return 0;
}
