#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
int T;
long long x, y, p, q;
long long gcd(long long a, long long b) {
  while (b) {
    long long temp = a % b;
    a = b;
    b = temp;
  }
  return a;
}
int main() {
  cin >> T;
  while (T--) {
    scanf("%lld%lld%lld%lld", &x, &y, &p, &q);
    long long temp = gcd(p, q);
    p /= temp;
    q /= temp;
    long long L = 0;
    long long R = inf;
    if (p == q && x != y) {
      printf("-1\n");
    } else if (p == 0 && x != 0) {
      printf("-1\n");
    } else {
      long long ans;
      while (L <= R) {
        long long mid = L + (R - L) / 2;
        long long A = mid * p - x;
        long long B = mid * q - y;
        if (A >= 0 && B >= 0 && A <= B) {
          R = mid - 1;
          ans = mid;
        } else {
          L = mid + 1;
        }
      }
      printf("%lld\n", ans * q - y);
    }
  }
  return 0;
}
