#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
int32_t main() {
  int t;
  cin >> t;
  for (int z = 0; z < t; z += 1) {
    long long x, y, p, q;
    cin >> x >> y >> p >> q;
    if (p == 0) {
      if (x == 0)
        cout << 0 << '\n';
      else
        cout << -1 << '\n';
      continue;
    }
    long long left = 0;
    long long right = 1e9;
    long long ans = -1;
    long long cur_mod = abs(x - y);
    while (left <= right) {
      long long mid = (left + right) / 2;
      long long zc = (mid * q);
      if (zc < y) {
        left = mid + 1;
        continue;
      }
      long long wow = p * (mid);
      if (wow < x) {
        left = mid + 1;
        continue;
      }
      if (abs(wow - zc) < cur_mod) {
        left = mid + 1;
        continue;
      } else {
        ans = mid;
        right = mid - 1;
      }
    }
    if (ans < 0)
      cout << ans << '\n';
    else
      cout << (q * ans) - y << '\n';
  }
}
