#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
const int MAX = 1e6;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    long long int x, y, p, q;
    cin >> x >> y >> p >> q;
    long long int l = 0ll, r = INT_MAX, ans = -1;
    while (l <= r) {
      long long int mid = (l + r) >> 1;
      long long int X = p * mid - x;
      long long int Y = q * mid - y;
      if (X >= 0 && Y >= 0 && X <= Y) {
        ans = Y;
        r = mid - 1;
      } else
        l = mid + 1;
    }
    cout << ans << endl;
  }
  return 0;
}
