#include <bits/stdc++.h>
using namespace std;
long long p, q, l, m;
bool check(long long t, long long x) {
  if ((q - p) * t - (m - l) >= 0 && p * t - l >= 0) return true;
  return false;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    cin >> l >> m >> p >> q;
    long long low = 0, high = 1e9 + 3, mid, ans = -1;
    long long x = q * l - p * m;
    while (low <= high) {
      mid = low + high >> 1;
      if (check(mid, x)) {
        ans = mid;
        high = mid - 1;
      } else {
        low = mid + 1;
      }
    }
    if (ans == -1)
      cout << ans << '\n';
    else
      cout << q * ans - m << '\n';
  }
  return 0;
}
