#include <bits/stdc++.h>
using namespace std;
const int big = ((int)1 << 30);
int timecount = 0;
int main(void) {
  long long int N, M, i, j, x, y, p, q, w, e, ans;
  cin >> N;
  for (; N > 0; N--) {
    ans = 0;
    cin >> x >> y >> p >> q;
    if ((x != 0 && p == 0) || (x != y && p == 1 && q == 1)) {
      cout << -1 << endl;
      continue;
    }
    if ((x == 0 && p == 0) || (x == y && p == q)) {
      cout << 0 << endl;
      continue;
    }
    i = max(max((x + p - 1) / p, (y + q - 1) / q),
            (y - x + q - p - 1) / (q - p));
    cout << q * i - y << endl;
  }
  return 0;
}
