#include <bits/stdc++.h>
using namespace std;
long long used[10001], e, i, j, t, x, y, p, q, a, b, mn, l, r, w, v, m;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  cin >> t;
  for (i = 1; i <= t; i++) {
    cin >> x >> y >> p >> q;
    l = 1;
    r = 1e9;
    mn = 1e17;
    if (x == 0 && p == 0) {
      cout << 0 << '\n';
    } else {
      r = 1;
      if (p > 0) {
        r = x / p;
        if (x % p != 0) r++;
      }
      if (q - p > 0) {
        l = (y - x) / (q - p);
        if ((y - x) % (q - p) != 0) l++;
      }
      r = max(r, l);
      w = r * p;
      v = r * q;
      a = w - x;
      b = v - y - a;
      if (a >= 0 && b >= 0)
        cout << a + b << '\n';
      else
        cout << -1 << '\n';
    }
  }
}
