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