#include <bits/stdc++.h>
using namespace std;
template <class t>
t mag(t a) {
  return a > 0 ? a : -a;
}
template <class T>
T cdiv(T num, T den) {
  return (num / den) + (num % den > 0);
}
std::ifstream term("/dev/tty");
std::string tempstring;
using ll = int_fast64_t;
using ii = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
using vii = vector<ii>;
int tc = 1;
const int sij = 1000010;
const ll mod = 1000000007ll;
ll x, y, p, q;
int escape(int tcase) {
  cin >> x >> y >> p >> q;
  if (p == 0) {
    if (x == 0)
      cout << 0 << '\n';
    else
      cout << -1 << '\n';
    return 0;
  }
  if (p == 1 and q == 1) {
    if (x == y)
      cout << 0 << '\n';
    else
      cout << -1 << '\n';
    return 0;
  }
  ll prt = max({cdiv(y, q), cdiv(x, p), cdiv(y - x, q - p)});
  prt = prt * q - y;
  cout << prt << '\n';
  return 0;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> tc;
  for (int i = 1; i <= tc; i++) escape(i);
}
