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