#include <bits/stdc++.h>
using namespace std;
int main() {
  unsigned long long t, x, y, p, q, lo, hi, mid, k;
  cin >> t;
  while (t--) {
    cin >> x >> y >> p >> q;
    if (p == 1 and q == 1) {
      if (x != y)
        cout << -1 << endl;
      else
        cout << 0 << endl;
    } else if (x > 0 and p == 0)
      cout << -1 << endl;
    else {
      lo = ((y - 1) / q);
      hi = 1e9;
      while (hi - lo > 1) {
        mid = (lo + hi) / 2;
        if ((q * mid) - y >= (p * mid) - x and x <= p * mid)
          hi = mid;
        else
          lo = mid;
      }
      cout << q * hi - y << endl;
    }
  }
  return 0;
}
