#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;
    if (p == 0) {
      if (x == 0)
        cout << 0 << endl;
      else
        cout << -1 << endl;
      continue;
    }
    if (p == q) {
      if (x == y)
        cout << 0 << endl;
      else
        cout << -1 << endl;
      continue;
    }
    long long k1, k2, k;
    k1 = (y - x) / (q - p) + ((y - x) % (q - p) ? 1 : 0);
    k2 = x / p + ((x % p) ? 1 : 0);
    k = max(k1, k2);
    cout << k * q - y << endl;
  }
  return 0;
}
