#include <bits/stdc++.h>
using namespace std;
inline long long flr(long long p, long long v) {
  if (p % v == 0) return p;
  return (p / v + 1) * v;
}
int main() {
  long long te, a, b, c, d, x, y, t;
  cin >> te;
  while (te--) {
    cin >> a >> b >> c >> d;
    if (c == 0 && d == 1) {
      if (a != 0) {
        cout << -1 << endl;
        continue;
      }
      cout << 0 << endl;
      continue;
    }
    if (c == d) {
      if (a < b)
        cout << -1 << endl;
      else
        cout << 0 << endl;
      continue;
    } else {
      long long l = -1, r = 1000000000, md;
      while (l < r) {
        if (l == r - 1) break;
        md = (l + r) / 2;
        if (md * c >= a && md * (d - c) >= b - a)
          r = md;
        else
          l = md;
      }
      cout << r * d - b << endl;
    }
  }
}
