#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    long long x, y, p, q;
    cin >> x >> y >> p >> q;
    long long low, mid, ans, upr, val1 = 0, val = 0, i, j;
    upr = 1e9, low = 0;
    while (low <= upr) {
      val = 0;
      mid = (low + upr) / 2;
      i = (p * mid) - x;
      j = (q * mid) - y;
      if (i >= 0 && j >= 0 && i <= j) {
        val = 1;
      }
      if (val == 1) {
        val1 = mid;
        upr = mid - 1;
      } else {
        low = mid + 1;
      }
    }
    if (val1 == 0) {
      ans = -1;
    } else {
      ans = (val1 * q) - y;
    }
    cout << ans << "\n";
  }
}
