#include <bits/stdc++.h>
using namespace std;
long long x, y, q, p;
int main() {
  int t;
  cin >> t;
  while (t--) {
    cin >> x >> y >> p >> q;
    long long kb = 0, kk, m;
    if (q == p && y != x || !p && x) {
      cout << -1 << "\n";
      continue;
    } else if (q == p && y == x || !p && !x) {
      cout << 0 << "\n";
      continue;
    }
    if ((y - x) % (q - p))
      kk = (y - x) / (q - p) + 1;
    else
      kk = (y - x) / (q - p);
    if (x % p)
      kb = (x / p) + 1;
    else
      kb = x / p;
    if (kb > kk) kk = kb;
    m = q * kk - y;
    cout << m << "\n";
  }
  return 0;
}
