#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long x, y, p, q, tmp;
  cin >> x >> y >> p >> q;
  if (p == q) {
    if (x == y)
      cout << 0 << endl;
    else
      cout << -1 << endl;
    return;
  }
  if (p == 0) {
    if (x == 0)
      cout << 0 << endl;
    else
      cout << -1 << endl;
    return;
  }
  tmp = max(((y - x) + (q - p) - 1) / (q - p),
            max((x + p - 1) / p, (y + q - 1) / q));
  while (1) {
    if (tmp * q < y || tmp * p < x) {
      tmp++;
      continue;
    }
    if (tmp * p - x <= tmp * q - y) {
      cout << tmp * q - y << endl;
      return;
    } else
      tmp++;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    solve();
  }
  return 0;
}
