#include <bits/stdc++.h>
using namespace std;
double eps = 0.00000001;
long long x, y, p, q, m, c, ans = -1;
void bp(long long l, long long r) {
  if (l == r || l + 1 == r) {
    if (0 <= p * (r + c) - x && p * (r + c) - x <= m + r * q)
      if (ans == -1 || r < ans) ans = r;
    if (0 <= p * (l + c) - x && p * (l + c) - x <= m + l * q)
      if (ans == -1 || l < ans) ans = l;
    return;
  }
  long long mid = (l + r) / 2;
  if (x <= 1LL * p * (mid + c) && 1LL * p * (mid + c) - x <= m + 1LL * mid * q)
    bp(l, mid);
  else
    bp(mid, r);
}
int main() {
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) {
    ans = -1;
    cin >> x >> y >> p >> q;
    m = (y * q - y) % q;
    c = (y + m) / q;
    bp(0, 1 << 30);
    if (ans != -1) ans = m + ans * q;
    cout << ans << endl;
  }
  return 0;
}
