#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  long long int x, y, p, q;
  long long int i, m1, m2, m, r;
  for (i = 1; i <= t; i++) {
    cin >> x >> y >> p >> q;
    if (p == 0) {
      cout << (x == 0 ? 0 : -1) << endl;
    } else if (p == q) {
      cout << (x == y ? 0 : -1) << endl;
    } else {
      m1 = (x + p - 1) / p;
      m2 = ((y - x) + (q - p) - 1) / (q - p);
      m = max(m1, m2);
      r = (q * m) - y;
      cout << r << endl;
    }
  }
  return 0;
}
