#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long e_gcd(long long a, long long b, long long &x, long long &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  long long ans = e_gcd(b, a % b, y, x);
  y -= x * (a / b);
  return ans;
}
int main() {
  int n;
  long long x, y, p, q;
  scanf("%d", &n);
  while (n--) {
    scanf("%lld%lld%lld%lld", &x, &y, &p, &q);
    if (x * q == p * y) {
      printf("0\n");
      continue;
    }
    if (p == q && x != y) {
      printf("-1\n");
      continue;
    }
    long long l = 0, r = 1e9, ans = -1;
    long long a, b;
    while (l <= r) {
      long long mid = (l + r) / 2;
      a = mid * p - x;
      b = mid * q - y;
      if (a >= 0 && b >= 0 && a <= b) {
        ans = mid;
        r = mid - 1;
      } else
        l = mid + 1;
    }
    if (ans == -1)
      printf("-1\n");
    else
      printf("%lld\n", ans * q - y);
  }
  return 0;
}
