#include <bits/stdc++.h>
using namespace std;
int a[1000], b[10000];
int main() {
  long long int t, k, i, j, c, n, m, x, y, p, q, a, b;
  cin >> t;
  while (t--) {
    cin >> x >> y >> p >> q;
    long long int low = 0, high = 2e9, mid;
    while (low < high) {
      mid = (low + high) / 2;
      b = mid * p - x;
      a = mid * (q - p) + x - y;
      if (a < 0 || b < 0)
        low = mid + 1;
      else
        high = mid;
    }
    mid = (low + high) / 2;
    b = mid * p - x;
    a = mid * (q - p) + x - y;
    if (a < 0 || b < 0)
      cout << -1 << endl;
    else
      cout << a + b << endl;
  }
  return 0;
}
