#include <bits/stdc++.h>
using namespace std;
long long x, y, p, q, n, m, d, ans, g;
int T;
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%lld%lld%lld%lld", &x, &y, &p, &q);
    if (p == q) {
      if (x != y)
        printf("-1\n");
      else
        printf("0\n");
    } else if (p == 0) {
      if (x != 0)
        printf("-1\n");
      else
        printf("0\n");
    } else {
      if (x % p == 0)
        n = x / p;
      else
        n = x / p + 1;
      if ((y - x) % (q - p) == 0)
        m = (y - x) / (q - p);
      else
        m = (y - x) / (q - p) + 1;
      d = max(n, m);
      ans = d * q - y;
      printf("%lld\n", ans);
    }
  }
  return 0;
}
