#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int mod = (int)1e9 + 7;
const long long INF = 1000000000000000000LL;
const int WASTE = (int)2e5;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int x, y, p, q;
    cin >> x >> y >> p >> q;
    if (!p) {
      cout << (x == 0 ? 0 : -1) << "\n";
      continue;
    }
    if (p == q) {
      if (x == y)
        cout << 0 << "\n";
      else
        cout << -1 << "\n";
      continue;
    }
    int i = (x + p - 1) / p;
    int i1 = (y - x - p + q - 1) / (q - p);
    cout << (q * 1LL * max(i, i1) - y) << "\n";
  }
  return 0;
}
