#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long s = 0, e = 1e9 + 2;
  long long t, a, b, c, d;
  cin >> t;
  while (t--) {
    cin >> a >> b >> c >> d;
    s = 0;
    e = 1e9 + 1;
    long long min1 = 1e9 + 4;
    long long mid;
    while (s <= e) {
      mid = (s + e) / 2;
      if (mid * c >= a && mid * d - mid * c >= b - a) {
        min1 = min(min1, mid);
        e = mid - 1;
      } else {
        s = mid + 1;
      }
    }
    if (min1 == 1e9 + 4) {
      cout << "-1" << endl;
      ;
    } else {
      cout << min1 * d - b << endl;
    }
  }
}
