#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  long long x, y, p, q;
  cin >> t;
  while (t--) {
    cin >> x >> y >> p >> q;
    long long l = 0, h = 1e9, ans = -1, mid, a, b;
    while (l <= h) {
      mid = (l + h) / 2;
      a = p * mid;
      b = q * mid;
      if (b >= y && a >= x && b - a >= y - x) {
        ans = b - y;
        h = mid - 1;
      } else
        l = mid + 1;
    }
    cout << ans << endl;
  }
  return 0;
}
