#include <bits/stdc++.h>
using namespace std;
int x, y, p, q;
long long getans() {
  long long f = 1, r = 1e9;
  while (r - f > 1) {
    long long mid = (f + r) / 2;
    if (mid * (q - p) >= y - x && mid * p >= x)
      r = mid;
    else
      f = mid;
  }
  if (!(f * (q - p) >= y - x && f * p >= x)) f = r;
  return f * q - y;
}
int main() {
  int i, j, k;
  int T;
  scanf("%d", &T);
  while (T--) {
    scanf("%d%d%d%d", &x, &y, &p, &q);
    if ((x < y && p == q) || (x != 0 && p == 0)) {
      printf("-1\n");
      continue;
    }
    printf("%I64d\n", getans());
  }
  return 0;
}
