#include <bits/stdc++.h>
using namespace std;
long long gcd(long long x, long long y) {
  if (!y) return x;
  return gcd(y, x % y);
}
int main() {
  int tt;
  scanf("%d", &tt);
  while (tt--) {
    long long x, y, a, b;
    scanf("%I64d %I64d %I64d %I64d", &x, &y, &a, &b);
    if (a == b)
      if (x != y)
        printf("-1\n");
      else
        printf("0\n");
    else if (a == 0) {
      if (x == 0)
        printf("0\n");
      else
        printf("-1\n");
    } else {
      long long d = gcd(a, b);
      long long now1 = a / d;
      long long now2 = b / d;
      long long k1, k2;
      k1 = x / now1;
      if (x % now1) k1++;
      k2 = (y - x) / (now2 - now1);
      if ((y - x) % (now2 - now1)) k2++;
      printf("%I64d\n", max(d, max(k1, k2)) * now2 - y);
    }
  }
  return 0;
}
