#include <bits/stdc++.h>
using namespace std;
template <class T>
void dbg(const char* xn, T x) {
  if (0) cout << xn << ": " << x << "\n";
}
template <class T, class... TA>
void dbg(const char* xn, T x, TA... t) {
  while (*xn != ',')
    if (0) cout << *xn++;
  if (0) cout << ": " << x << ",";
  dbg(xn + 1, t...);
}
template <class T>
inline bool upd_max(T& x, T y) {
  return y > x ? x = y, true : false;
}
template <class T>
inline bool upd_min(T& x, T y) {
  return y < x ? x = y, true : false;
}
const int N = 1000007;
long long x, y, p, q;
long long solve() {
  cin >> x >> y >> p >> q;
  long long t = 1000000007LL;
  if (!(p * t >= x && q * t - p * t >= y - x)) return -1;
  if (p == 0) {
    if (x == 0)
      return 0;
    else
      return -1;
  }
  if (p == q) return 0;
  long long mx = max((x + p - 1) / p, (y - x + q - p - 1) / (q - p));
  long long rst = q * mx - y;
  return rst;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout << fixed << setprecision(10);
  long long tests;
  cin >> tests;
  while (tests--) {
    cout << solve() << '\n';
  }
  return 0;
}
