#include <bits/stdc++.h>
using namespace std;
int main() {
  int T;
  cin >> T;
  for (int t = 0; t < T; t++) {
    double x, y, p, q;
    cin >> x >> y >> p >> q;
    std::cout << std::fixed;
    if (x * q == y * p) {
      cout << 0 << endl;
    } else if (p == q || p == 0) {
      cout << -1 << endl;
    } else {
      long long Y = y, Q = q;
      long long a = ceil(x / p);
      long long b = ceil((y - x) / (q - p));
      long long f = Q * max(a, b) - Y;
      cout << f << endl;
    }
  }
  return 0;
}
