#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const int INF = 213456789;
const double eps = 1e-7;
template <class T>
T gcd(T a, T b) {
  if (!b) return a;
  return gcd(b, a % b);
}
long long x, y, p, q;
bool check(long long mid) {
  long long np = p * mid, nq = q * mid;
  if (nq < y) return 0;
  long long now = nq - y;
  if (x <= np && x + now >= np) return 1;
  return 0;
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    scanf("%lld%lld%lld%lld", &x, &y, &p, &q);
    if (p == q) {
      if (x == y)
        printf("0\n");
      else
        printf("-1\n");
    } else if (p == 0) {
      if (x != 0)
        printf("-1\n");
      else
        printf("0\n");
    } else {
      long long left = 0, right = 1000000001;
      long long ans = 0;
      while (left <= right) {
        long long mid = left + (right - left) / 2;
        if (check(mid)) {
          ans = q * mid - y;
          right = mid - 1;
        } else
          left = mid + 1;
      }
      printf("%lld\n", ans);
    }
  }
  return 0;
}
