#include <bits/stdc++.h>
using namespace std;
long long x, y, p, q;
int t;
long long ans;
long long cnt;
bool check(long long mid) {
  long long A, B;
  A = mid * p - x;
  B = mid * q - y;
  if (A >= 0 && B >= 0 && A <= B) return true;
  return false;
}
void dfs(long long low, long long high) {
  long long mid;
  if (low >= high)
    return;
  else {
    mid = (low + high) >> 1;
    if (check(mid)) {
      ans = mid;
      dfs(low, mid);
    } else
      dfs(mid + 1, high);
  }
}
int main() {
  cin >> t;
  while (t--) {
    cin >> x >> y >> p >> q;
    ans = -1;
    dfs(0, 2e9);
    if (ans == -1)
      cout << ans << endl;
    else {
      cnt = ans * q - y;
      cout << cnt << endl;
    }
  }
  return 0;
}
