#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
long long int x, y, p, q;
int main() {
  int t;
  cin >> t;
  while (t--) {
    cin >> x >> y >> p >> q;
    long long int r = INF;
    long long int l = 0;
    long long int temp = -1;
    while (l <= r) {
      long long int mid = (l + r) / 2;
      long long int a = mid * p - x;
      long long int b = mid * q - y;
      if (a >= 0 && b >= 0 && a <= b) {
        temp = b;
        r = mid - 1;
      } else {
        l = mid + 1;
      }
    }
    if (temp == -1)
      cout << "-1" << endl;
    else
      cout << temp << endl;
  }
  return 0;
}
