#include <bits/stdc++.h>
using namespace std;
struct cww {
  cww() {
    if (1) {
      ios::sync_with_stdio(false);
      cin.tie(0);
    }
  }
} star;
template <typename T>
inline bool chmin(T &l, T r) {
  bool a = l > r;
  if (a) l = r;
  return a;
}
template <typename T>
inline bool chmax(T &l, T r) {
  bool a = l < r;
  if (a) l = r;
  return a;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (auto &it : v) is >> it;
  return is;
}
void solve() {
  long long x, y, p, q;
  cin >> x >> y >> p >> q;
  if (p == 0) {
    if (x > 0)
      cout << -1 << endl;
    else
      cout << 0 << endl;
    return;
  }
  if (p == q) {
    if (x < y)
      cout << -1 << endl;
    else
      cout << 0 << endl;
    return;
  }
  if (x == p && y == q) {
    cout << 0 << endl;
    return;
  }
  long long ub = 1e9;
  long long lb = 0;
  while (ub - lb > 1) {
    const long long mid = (ub + lb) / 2;
    const long long b = mid * p - x;
    const long long a = q * mid - y;
    if (b < 0 || a < b)
      lb = mid;
    else
      ub = mid;
  }
  cout << ub * q - y << endl;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
