#include <bits/stdc++.h>
using namespace std;
long long max(long long a, long long b) { return a > b ? a : b; }
int main() {
  long long T;
  cin >> T;
  while (T--) {
    long long x, y, p, q;
    long long u;
    cin >> x >> y >> p >> q;
    if (p == 0) {
      if (x == 0) {
        cout << 0 << endl;
        continue;
      } else {
        cout << -1 << endl;
        continue;
      }
    }
    if (p == q && x != y) {
      cout << -1 << endl;
      continue;
    }
    if (p == q && x == y) {
      cout << 0 << endl;
      continue;
    }
    long long u1, u2;
    u1 = (x / p);
    u2 = (y - x) / (q - p);
    if (x % p != 0) {
      u1++;
    }
    if ((y - x) % (q - p) != 0) {
      u2++;
    }
    u = max(u1, u2);
    cout << u * q - y << endl;
  }
  return 0;
}
