#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long x, y, p, q;
  cin >> x >> y >> p >> q;
  if (p == 0) {
    if (x == 0) {
      cout << 0 << endl;
      return;
    }
    cout << -1 << endl;
    return;
  }
  if (p == q) {
    if (x == y) {
      cout << 0 << endl;
      return;
    }
    cout << -1 << endl;
    return;
  }
  long long k = max((x + p - 1) / p, (y - x + (q - p) - 1) / (q - p));
  0;
  cout << k * q - y << endl;
}
int main() {
  cin.tie(0);
  cin.sync_with_stdio(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
