#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long x, y, p, q;
    cin >> x >> y >> p >> q;
    long long k = ceil(x / (double)p);
    if ((x == y && q == p)) {
      cout << "0\n";
      continue;
    }
    if (q - p == 0 || (p == 0 && x > 0)) {
      cout << "-1\n";
      continue;
    }
    k = max(k, (long long)ceil((y - x) / (double)(q - p)));
    long long res = max((long long)0, q * k - y);
    cout << res << "\n";
  }
}
