#include <bits/stdc++.h>
using namespace std;
long long n, a, b, p, q;
long long gcd(long long a, long long b) {
  if (a % b == 0) return b;
  return gcd(b, a % b);
}
bool check(long long x) { return x * p - a >= 0 && x * q - b >= x * p - a; }
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a >> b >> p >> q;
    long long first = 0, last = 2e9, mid;
    if (a / gcd(a, b) == p && b / gcd(a, b) == q) {
      cout << "0" << endl;
      continue;
    }
    if ((p == 1 && q == 1) || p == 0) {
      cout << "-1" << endl;
      continue;
    }
    while (first < last) {
      mid = first + (last - first) / 2;
      if (check(mid) == true) {
        last = mid;
      } else
        first = mid + 1;
    }
    if (first != 0 && last != 2e9)
      cout << first * q - b << endl;
    else
      cout << "-1" << endl;
  }
  return 0;
}
