#include <bits/stdc++.h>
using namespace std;
const int MAXN = 0;
long long gcd(long long A, long long B) {
  if (B > A) swap(A, B);
  while (B > 0) {
    A %= B;
    swap(A, B);
  }
  return A;
}
long long divUp(long long x, long long y) {
  assert(y > 0);
  return (x + y - 1) / y;
}
int main() {
  int T;
  cin >> T;
  for (int i = 1; i <= T; i++) {
    long long x, y, p, q;
    cin >> x >> y >> p >> q;
    long long g = gcd(p, q);
    p /= g;
    q /= g;
    if (p * y == q * x) {
      cout << 0 << endl;
      continue;
    }
    if (p == q || p == 0) {
      cout << -1 << endl;
      continue;
    }
    long long tA = divUp(x, p);
    long long tB = divUp(y, q);
    long long tC = divUp(y - x, q - p);
    cout << q * max(tA, max(tB, tC)) - y << endl;
  }
  return 0;
}
