#include <bits/stdc++.h>
using namespace std;
inline long long ll_ceil_of_div(long long a, long long b) {
  if (a % b == 0LL)
    return a / b;
  else
    return 1LL + (a / b);
}
inline long long ll_ceil_without_div(long long a, long long b) {
  long long rem = a % b;
  if (rem == 0LL)
    return a;
  else
    return a + b - rem;
}
int main(int argc, char* argv[]) {
  cout.sync_with_stdio(false);
  int TEST_NUM;
  scanf("%d", &TEST_NUM);
  for (int the_test = 0; the_test < TEST_NUM; the_test++) {
    int x_int, y_int, p_int, q_int;
    scanf("%d %d %d %d", &x_int, &y_int, &p_int, &q_int);
    long long x = x_int;
    long long y = y_int;
    long long p = p_int;
    long long q = q_int;
    if (p == 0) {
      if (x == 0)
        cout << "0\n";
      else
        cout << "-1\n";
    } else if (p == q) {
      if (x == y)
        cout << "0\n";
      else
        cout << "-1\n";
    } else if (x * q == y * p) {
      cout << "0\n";
    } else {
      long long res_1, res_2;
      if (x * q < y * p) {
        res_1 = ll_ceil_of_div(p * y - x * q, q - p);
      } else {
        res_1 = ll_ceil_of_div(x * q - p * y, p);
      }
      res_2 = ll_ceil_without_div(y + res_1, q) - y;
      cout << res_2 << "\n";
    }
  }
  return 0;
}
