#include <bits/stdc++.h>
using namespace std;
long long x, y, p, q;
bool ischeck(long long t) { return (p * t >= x && (q - p) * t >= y - x); }
int main() {
  int t;
  cin >> t;
  while (t--) {
    cin >> x >> y >> p >> q;
    if (!ischeck(y)) {
      cout << "-1" << endl;
      continue;
    }
    long long s = 0;
    long long e = 1e9 + 7;
    while (e - s > 1) {
      long long mid = (s + e) / 2;
      if (ischeck(mid))
        e = mid;
      else
        s = mid;
    }
    cout << q * e - y << endl;
  }
  return 0;
}
