#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t;
  cin >> t;
  while (t--) {
    long long x, y, p, q;
    cin >> x >> y >> p >> q;
    if (p == q && x == y) {
      printf("0\n");
      continue;
    }
    if (p == 0 && x == 0) {
      printf("0\n");
      continue;
    }
    if (p == q || p == 0 || q == 0) {
      printf("-1\n");
      continue;
    }
    long long i = max(
        (x - p * ((y + q - 1) / q) + p - 1) / p,
        (p * ((y + q - 1) / q) - (x + ((y + q - 1) / q) * q - y) + q - p - 1) /
            (q - p));
    printf("%lld\n", i * q + ((y + q - 1) / q) * q - y);
  }
  return 0;
}
