#include <bits/stdc++.h>
using namespace std;
long long p, q, x, y, l, r, mid;
bool check(long long O) { return (p * O >= x && q * O - p * O >= y - x); }
void BS() {
  l = 0;
  r = 1e9;
  while (r - l > 1) {
    mid = l + (r - l) / 2;
    if (check(mid))
      r = mid;
    else
      l = mid;
  }
  cout << r * q - y << endl;
}
int t;
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  ;
  cin >> t;
  while (t--) {
    cin >> x >> y >> p >> q;
    if (!check(1e9)) {
      cout << "-1\n";
      continue;
    }
    BS();
  }
  return 0;
}
