#include <bits/stdc++.h>
using namespace std;
int main() {
  int kase;
  scanf("%d", &kase);
  long long a, b, c, d;
  while (kase--) {
    cin >> a >> b >> c >> d;
    if (c == d && a != b) {
      cout << -1 << endl;
      continue;
    } else if (c == d && a == b) {
      cout << 0 << endl;
      continue;
    } else if (c == 0 && a == 0) {
      cout << 0 << endl;
      continue;
    } else if (c == 0 && a != 0) {
      cout << -1 << endl;
      continue;
    }
    b -= a;
    d -= c;
    long long left = a / c + (a % c != 0);
    long long new_a = left * c;
    long long new_b = left * d;
    long long ans = (new_a - a) + (new_b - b);
    if (new_a < a || new_b < b) {
      long long right = b / d + (b % d != 0);
      new_a = right * c;
      new_b = right * d;
      ans = (new_a - a) + (new_b - b);
      if (new_a < a || new_b < b) ans = -1;
    }
    cout << ans << endl;
  }
  return 0;
}
