#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}
signed main() {
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    long long a, b, c, d;
    cin >> a >> b >> c >> d;
    long long x = gcd(c, d);
    c /= x;
    d /= x;
    if (c == 0 && a != 0) {
      cout << -1 << '\n';
      continue;
    }
    if (c == 0 && a == 0) {
      cout << "0\n";
      continue;
    }
    if (c == d && a != b) {
      cout << -1 << '\n';
      continue;
    }
    if (c == d && a == b) {
      cout << "0\n";
      continue;
    }
    long long l = (a + c - 1) / c - 1;
    long long r = (long long)2e18 / d;
    while (r - l > 1) {
      long long m = (r + l) / 2;
      if (m * d >= b + m * c - a) {
        r = m;
      } else {
        l = m;
      }
    }
    cout << r * d - b << '\n';
  }
}
