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