#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int t;
  long long int x, y, p, q, c, ans, l, m, a, b, n, d;
  cin >> t;
  while (t > 0) {
    t--;
    cin >> x >> y >> p >> q;
    if (p == q) {
      if (x != y)
        cout << "-1" << endl;
      else
        cout << "0" << endl;
    } else if (p == 0) {
      if (x == 0)
        cout << "0" << endl;
      else
        cout << "-1" << endl;
    } else {
      ans = ((p - (x % p)) % p);
      a = x + ans;
      b = y + ans;
      n = (b * p) - (a * q);
      d = (q - p) * p;
      if (n > 0) {
        c = 0;
        if (n % d != 0) c = 1;
        n = (n / d) + c;
      } else {
        n = 0;
      }
      a = a + (n * p);
      ans = ans + (n * p);
      b = b + (n * p);
      c = ((q - (b % q)) % q);
      b = b + c;
      ans = ans + c;
      l = a / p;
      m = b / q;
      b = b + ((l - m) * q);
      ans = ans + ((l - m) * q);
      cout << ans << endl;
    }
  }
  return 0;
}
