#include <bits/stdc++.h>
using namespace std;
long long x, y, p, q;
long long m = 1e18;
long long can(long long mid) {
  long long upore = p * mid;
  long long niche = q * mid;
  if (upore >= x && niche >= y) {
    if ((upore - x) <= (niche - y)) {
      return 1;
    }
  }
  return 0;
}
int main() {
  long long tes;
  scanf("%lld", &tes);
  for (long long cas = 1; cas <= tes; cas++) {
    scanf("%lld%lld%lld%lld", &x, &y, &p, &q);
    if (p == 0) {
      if (x == 0) {
        cout << "0" << endl;
      } else {
        cout << "-1" << endl;
      }
      continue;
    } else if (p == q && x != y) {
      cout << "-1" << endl;
      continue;
    }
    long long ans;
    long long low = 1;
    long long high = m / q;
    while (low <= high) {
      long long mid = (low + high) / 2;
      if (can(mid)) {
        ans = mid;
        high = mid - 1;
      } else {
        low = mid + 1;
      }
    }
    cout << ans * q - y << endl;
  }
  return 0;
}
