#include <bits/stdc++.h>
using namespace std;
long long gcd(long long x, long long y) { return y == 0 ? x : gcd(y, x % y); }
bool possible(long long p, long long q, long long x, long long y) {
  if (q < y) {
    return false;
  }
  if (p < x) {
    return false;
  }
  return q - y >= p - x;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long x, y, p, q;
    cin >> x >> y >> p >> q;
    long long l = 1, r = 1000000000;
    while (l < r) {
      long long mid = (l + r) / 2;
      if (possible(mid * p, mid * q, x, y)) {
        r = mid;
      } else {
        l = mid + 1;
      }
    }
    if (possible(l * p, l * q, x, y)) {
      cout << l * q - y << endl;
    } else {
      cout << -1 << endl;
    }
  }
  return 0;
}
