#include <bits/stdc++.h>
using namespace std;
bool check(long long x, long long y, long long p, long long q, long long mid) {
  if (p * mid >= x && q * mid - p * mid >= y - x)
    return true;
  else
    return false;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    long long x, y, p, q;
    cin >> x >> y >> p >> q;
    long long l = 0;
    long long r = 1e9;
    long long ans = -1;
    while (l <= r) {
      long long mid = (l + r) / 2;
      if (check(x, y, p, q, mid)) {
        ans = mid;
        r = mid - 1;
      } else
        l = mid + 1;
    }
    if (ans == -1)
      cout << -1 << endl;
    else
      cout << q * ans - y << endl;
  }
}
