#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long x, y, p, q;
    cin >> x >> y >> p >> q;
    if (y == 0) {
      cout << "-1" << endl;
      continue;
    }
    long long low = 0;
    long long high = 1000000001;
    int cnt = 150;
    while (cnt--) {
      int mid = (low + high) / 2;
      long long xx = p * mid - x;
      long long yy = q * mid - y;
      if (xx < 0 || yy < 0 || xx > yy)
        low = mid;
      else
        high = mid;
    }
    long long xx = p * high - x;
    long long yy = q * high - y;
    if (xx < 0 || yy < 0 || xx > yy) {
      cout << "-1" << endl;
      continue;
    } else {
      cout << q * high - y << endl;
      continue;
    }
  }
}
