#include <bits/stdc++.h>
using namespace std;
const long long INF = 0x3f3f3f3f;
long long gcd(long long a, long long b) { return a % b ? gcd(b, a % b) : b; }
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    long long x, y, p, q;
    scanf("%lld%lld%lld%lld", &x, &y, &p, &q);
    long long g = gcd(p, q);
    long long ans = -1;
    long long l = 0, r = INF;
    while (r >= l) {
      long long mid = (l + r) >> 1;
      long long m = q * mid - y;
      long long n = p * mid - x;
      if (m >= n && m >= 0 && n >= 0) {
        ans = mid;
        r = mid - 1;
      } else
        l = mid + 1;
    }
    if (ans == -1)
      printf("-1\n");
    else
      printf("%lld\n", ans * q - y);
  }
}
