#include <bits/stdc++.h>
using namespace std;
bool cal(long long x, long long y, long long p, long long q, long long t) {
  return ((p * t) >= x && ((q * t - p * t) >= (y - x)));
}
int main() {
  long long i, j, k, l, m, n, x, y, p, q, low, high, mid;
  int tst;
  cin >> tst;
  while (tst--) {
    cin >> x >> y >> p >> q;
    low = -1, high = 1e9;
    long long ans = 1e10;
    while (low <= high) {
      mid = (low + high) / 2;
      if (cal(x, y, p, q, mid)) {
        ans = min(ans, mid);
        high = mid - 1;
      } else
        low = mid + 1;
    }
    if (ans == 1e10) {
      cout << -1 << '\n';
    } else
      cout << (ans * q - y) << '\n';
  }
}
