#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long x, y, p, q;
  cin >> x >> y >> p >> q;
  if (x != y && p == q) {
    cout << "-1\n";
    return;
  }
  if (x != 0 && p == 0) {
    cout << "-1\n";
    return;
  }
  if ((x == 0 && p == 0) || (x == y && p == q)) {
    cout << "0\n";
    return;
  }
  long long ratio = max((x + p - 1) / p, (y + q - 1) / q);
  long long scale_x = ratio * p;
  long long scale_y = ratio * q;
  long long diff = scale_x - x - (scale_y - y);
  if (diff > 0) {
    long long ratio2 = (diff + (q - p) - 1) / (q - p);
    scale_x += ratio2 * p;
    scale_y += ratio2 * q;
  }
  cout << scale_y - y << "\n";
}
int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) solve();
}
