#include <bits/stdc++.h>
using namespace std;
const int N = 1004;
int main() {
  int T;
  long long p, q, x, y;
  scanf("%d", &T);
  while (T--) {
    scanf("%lld %lld %lld %lld", &x, &y, &p, &q);
    bool f = 0;
    long long l = 0, r = 8000000000;
    while (l < r) {
      long long mid = (l + r) / 2;
      long long tx = p * mid - x, ty = q * mid - y;
      if (tx >= 0 && tx <= ty) {
        r = mid;
        f = 1;
      } else
        l = mid + 1;
    }
    printf("%lld\n", f ? q * l - y : -1);
  }
}
