#include <bits/stdc++.h>
using namespace std;
long long T, x, y, p, q, l, r, mid;
bool check(long long a) {
  if (a * q < y) return 0;
  if (x <= p * a && p * a <= a * q - y + x)
    return 1;
  else
    return 0;
}
int main() {
  for (cin >> T; T; --T) {
    cin >> x >> y >> p >> q;
    if (p == 0 && x != 0) {
      cout << -1 << endl;
      continue;
    }
    if (p == q && x != y) {
      cout << -1 << endl;
      continue;
    }
    l = 1;
    r = 1e9 + 10;
    while (l < r) {
      mid = (l + r) / 2;
      if (check(mid))
        r = mid;
      else
        l = mid + 1;
    }
    cout << l * q - y << endl;
  }
  return 0;
}
