#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = 1e9 + 7;
using ld = long double;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  ll T;
  cin >> T;
  while (T--) {
    ll x, y, p, q;
    cin >> x >> y >> p >> q;
    if (p == 0)
      cout << (x == 0 ? 0 : -1);
    else if (p == q)
      cout << (x == y ? 0 : -1);
    else {
      ll a = y - x;
      ll b = q - p;
      ll kalu = (x + p - 1) / p;
      ll lalu = (a + b - 1) / b;
      cout << (q * 1ll * max(lalu, kalu) - y);
    }
    cout << "\n";
  }
}
