#include <bits/stdc++.h>
using namespace std;
int main() {
  long long test;
  ios::sync_with_stdio(0);
  cin >> test;
  while (test--) {
    long long a, b, p, q;
    cin >> a >> b >> p >> q;
    if (p == q) {
      if (a != b) {
        cout << -1;
      } else {
        cout << 0;
      }
      cout << '\n';
      continue;
    }
    if (p == 0) {
      if (a == 0) {
        cout << 0;
      } else {
        cout << -1;
      }
      cout << '\n';
      continue;
    }
    long long k = max(a / p + (a % p != 0), b / q + (b % q != 0));
    k = max(k, (b - a) / (q - p) + ((b - a) % (q - p) != 0));
    k = k * q - b;
    cout << k << '\n';
  }
  return 0;
}
