#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e9 + 7;
const long long mxN = 1e5 + 1;
long long x, y, p, q;
void solve() {
  cin >> x >> y >> p >> q;
  if (p == 0) {
    if (x == 0) {
      cout << 0 << "\n";
      return;
    }
    cout << -1 << "\n";
    return;
  }
  if (p == 1 && q == 1) {
    if (x == y) {
      cout << 0 << "\n";
      return;
    }
    cout << -1 << "\n";
    return;
  }
  long long res =
      max({(x + p - 1) / p, (y + q - 1) / q, (y - x + q - p - 1) / (q - p)});
  res = res * q - y;
  cout << res << "\n";
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  long long T = 1, t = 0;
  cin >> T;
  while (t++ < T) {
    solve();
  }
}
