#include <bits/stdc++.h>
using namespace std;
long long n;
int main() {
  cin >> n;
  for (int p = 0; p < n; p++) {
    long long x, y, a, b;
    cin >> x >> y >> a >> b;
    long long left, right, middle;
    left = 0;
    right = 1e18 / b;
    right++;
    middle = (left + right) / 2;
    if (!(right * b - y >= right * a - x && right * a >= x && right * b >= y)) {
      cout << -1 << '\n';
      continue;
    }
    while (left != right) {
      if (middle * b - y >= middle * a - x && middle * a >= x &&
          middle * b >= y) {
        right = middle;
      } else {
        left = middle + 1;
      }
      middle = (left + right) / 2;
    }
    cout << middle * b - y << '\n';
  }
}
