#include <bits/stdc++.h>
using namespace std;
long long x, y, xx, yy, p, q;
bool gd(long long mid) {
  yy = mid * q;
  xx = mid * p;
  if (xx < x || yy < y || xx - x > yy - y) return 0;
  return 1;
}
long long solve() {
  long long low = 0, hi = 1000000000, mid, ans = -1;
  while (low <= hi) {
    mid = (low + hi) >> 1;
    bool ok = gd(mid);
    if (ok) {
      hi = mid - 1;
      ans = mid * q - y;
    } else
      low = mid + 1;
  }
  return ans;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    cin >> x >> y >> p >> q;
    cout << solve() << "\n";
  }
  return 0;
}
