#include <bits/stdc++.h>
using namespace std;
long long x, y, p, q;
bool check(long long t) {
  return ((p * t >= x) && ((q * t - p * t) >= (y - x)));
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    scanf("%I64d%I64d%I64d%I64d", &x, &y, &p, &q);
    long long l = 0, r = 1e9, mid;
    long long ans = -1;
    while (l <= r) {
      mid = (l + r) / 2;
      if (check(mid)) {
        ans = mid;
        r = mid - 1;
      } else
        l = mid + 1;
    }
    if (ans == -1)
      printf("-1\n");
    else
      printf("%I64d\n", ans * q - y);
  }
  return 0;
}
