#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int t;
  cin >> t;
  while (t--) {
    long long int a, b, c, d;
    float f, l;
    cin >> a >> b >> c >> d;
    f = (float)a / b;
    l = (float)c / d;
    if (f == l)
      cout << "0" << endl;
    else {
      if (l == 1 or l == 0)
        cout << "-1" << endl;
      else {
        long long int x = ((a + c - 1) / c);
        long long int i = ((b - a) + (d - c) - 1) / (d - c);
        cout << max(x, i) * d - b << endl;
      }
    }
  }
}
