#include <bits/stdc++.h>
using namespace std;
int main() {
  long long tc, x, y, p, q;
  cin >> tc;
  while (tc--) {
    cin >> x >> y >> p >> q;
    if (p == 0) {
      if (x == 0)
        cout << 0 << endl;
      else
        cout << (-1) << endl;
    } else {
      long long l = 1, h = 10000000000, m, ans = -1;
      while (l <= h) {
        m = (l + h) / 2;
        if ((p * m) >= x && (q * m) >= y && (q * m - y) >= (p * m - x))
          ans = q * m - y, h = m - 1;
        else
          l = m + 1;
      }
      cout << ans << endl;
    }
  }
  return 0;
}
