#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  long long t, lo, hi;
  cin >> t;
  long long x, y, p, q;
  while (t--) {
    cin >> x >> y >> p >> q;
    lo = 0;
    hi = (long long)1e9;
    long double a;
    long long Ans = -1;
    long long mid;
    if (!(p * hi >= x && q * hi - p * hi >= y - x))
      cout << -1 << endl;
    else {
      while (hi - lo > 1) {
        mid = (lo + hi) / 2;
        bool Poss = false;
        if (p * mid >= x && q * mid - p * mid >= y - x) Poss = true;
        if (Poss)
          hi = mid;
        else
          lo = mid;
      }
      cout << q * hi - y << endl;
    }
  }
  return 0;
}
