#include <bits/stdc++.h>
using namespace std;
bool function(int x, int y) { return (x > y); }
int main() {
  long long int t, x, y, p, q, c1, c2, mult, flag, temp;
  cin >> t;
  for (int i = 0; i < t; i++) {
    cin >> x >> y >> p >> q;
    if (p == 0) {
      if (x == 0) {
        cout << "0" << endl;
      } else
        cout << "-1" << endl;
      continue;
    }
    if (q >= y) {
      mult = 1;
      flag = x / p;
      if (flag * p < x) flag++;
      if (flag > mult) mult = flag;
      long long int val2 = mult * q - y;
      if ((mult * p - x) > (mult * q - y)) {
        c2 = mult * p - x;
        if (p == q)
          cout << "-1" << endl;
        else {
          temp = c2 - val2;
          c2 = (c2 - val2) / (q - p);
          if (c2 * (q - p) < temp) c2++;
          mult = c2 + mult;
          c1 = (mult * q - y);
          cout << c1 << endl;
        }
      } else
        cout << val2 << endl;
    } else {
      mult = y / q;
      if (mult * q < y) mult++;
      flag = x / p;
      if (flag * p < x) flag++;
      if (flag > mult) mult = flag;
      flag = 0;
      long long int val2 = mult * q - y;
      if ((mult * p - x) > (mult * q - y)) {
        c2 = mult * p - x;
        if (p == q)
          cout << "-1" << endl;
        else {
          temp = c2 - val2;
          c2 = (c2 - val2) / (q - p);
          if (c2 * (q - p) < temp) c2++;
          mult = c2 + mult;
          c1 = (mult * q - y);
          cout << c1 << endl;
        }
      } else
        cout << val2 << endl;
    }
  }
  return 0;
}
