#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const long long MOD = 2e9 + 7;
const long long INF64 = 1e18;
const long double EPS = 1e-7;
mt19937 myrand(time(NULL));
long long a, b, c, d;
long long ans;
bool read() {
  if (scanf("%lld%lld%lld%lld", &a, &b, &c, &d) != 4) return 0;
  return 1;
}
bool check(long long mid) {
  long long _b = b + mid * d;
  long long _c = c * (_b / d);
  return (a <= _c && (a + mid * d + ans) >= _c);
}
void print(long long mid) { printf("%lld\n", ans + mid * d); }
void solve() {
  if (c == 0) {
    printf("%d\n", a == c ? 0 : -1);
    return;
  }
  ans = d - (b % d == 0 ? d : b % d);
  b += ans;
  long long l = 0, r = MOD;
  while (l < r - 1) {
    long long mid = (l + r) / 2;
    if (check(mid))
      r = mid;
    else
      l = mid;
  }
  for (long long i = l; i <= r; ++i)
    if (check(i)) {
      print(i);
      return;
    }
  printf("-1\n");
}
int main() {
  int t;
  scanf("%d", &t);
  for (int i = 0; i < (t); i++) {
    read();
    solve();
  }
  return 0;
}
