#include <bits/stdc++.h>
long long x, y, ix, iy, p, q, t;
bool judge(long long k) { return k * q - y >= k * p - x and k * p - x >= 0; }
void solve() {
  long long l = 1, r = 1000000007;
  while (l < r) {
    long long mid = l + (r - l) / 2;
    if (judge(mid))
      r = mid;
    else
      l = mid + 1;
  }
  printf("%lld\n", l == 1000000007 ? -1 : l * q - y);
}
int main() {
  scanf("%lld", &t);
  while (t--) {
    scanf("%lld%lld%lld%lld", &x, &y, &p, &q);
    solve();
  }
  return 0;
}
