#include <bits/stdc++.h>
using namespace std;
long long x, y, p, q, yu, ui;
int main() {
  int g;
  scanf("%d", &g);
  while (g--) {
    cin >> x >> y >> p >> q;
    if (p == q) {
      printf("%d\n", x == y ? 0 : -1);
      continue;
    }
    if (p == 0) {
      printf("%d\n", x == 0 ? 0 : -1);
      continue;
    }
    yu = ceil(x * 1.0 / p), ui = ceil((y - x) * 1.0 / (q - p));
    long long n = max(yu, ui);
    printf("%I64d\n", n * q - y);
  }
}
