#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int rez, t, p, q, a, b, x, y, k;
  cin >> t;
  while (t--) {
    k = 0;
    cin >> x >> y >> p >> q;
    if (q == 1 or p == 0) {
      if (p == 1 and q == 1) {
        if (x < y) cout << -1 << '\n';
        if (x == y) cout << 0 << '\n';
      }
      if (p == 0) {
        if (x > 0) cout << -1 << '\n';
        if (x == 0) cout << 0 << '\n';
      }
    } else {
      if (x % p == 0 and k < x / p) k = x / p;
      if (x % p != 0 and k <= x / p) k = x / p + 1;
      if (y % q == 0 and k < y / q) k = y / q;
      if (y % q != 0 and k <= y / q) k = y / q + 1;
      a = y - x;
      b = q - p;
      if (a % b == 0 and k < a / b) k = a / b;
      if (a % b != 0 and k <= a / b) k = a / b + 1;
      rez = q * k - y;
      cout << rez << '\n';
    }
  }
  return 0;
}
