#include <bits/stdc++.h>
using namespace std;
long long dvUp(long long x, long long d) {
  if (x % d == 0) {
    return x / d;
  }
  return x / d + 1;
}
long long slv() {
  long long x, y, q, p;
  cin >> x >> y >> p >> q;
  if (p == 0) {
    return x == 0 ? 0 : -1;
  }
  if (p == q) {
    return x == y ? 0 : -1;
  }
  auto mn = max(max(dvUp(x, p), dvUp(y, q)), dvUp(y - x, q - p));
  return mn * q - y;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    cout << slv() << endl;
  }
}
