#include <bits/stdc++.h>
using namespace std;
long long int x, y, p, q;
int t;
int main() {
  cin >> t;
  while (t--) {
    cin >> x >> y >> p >> q;
    long long a, b, c, d;
    if (x && p == 0) {
      cout << "-1\n";
      continue;
    }
    if (x == 0 && p == 0) {
      if (y != 0 && q != 0) {
        cout << 0 << "\n";
      } else {
        cout << "-1\n";
      }
      continue;
    }
    if (p == q) {
      if (x == y) {
        cout << "0\n";
        continue;
      } else {
        cout << "-1\n";
        continue;
      }
    }
    a = x;
    b = y - x;
    c = p;
    d = q - p;
    long long int m1, m2;
    m1 = a / c;
    if (a % c) m1++;
    m2 = b / d;
    if (b % d) m2++;
    c = c * max(m1, m2);
    d = d * max(m1, m2);
    cout << q * max(m1, m2) - y << "\n";
  }
}
