#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
long long can(long long first, long long second, long long p, long long q,
              long long r) {
  p *= r;
  q *= r;
  if (p < first || q < second) {
    return -1;
  }
  if (q - second < p - first) {
    return -1;
  }
  return q - second;
}
int main() {
  int T;
  cin >> T;
  while (T--) {
    long long first, second, p, q;
    cin >> first >> second >> p >> q;
    long long l = 1;
    long long r = inf;
    while (r - l > 1) {
      long long mid = (l + r) / 2;
      if (can(first, second, p, q, mid) != -1) {
        r = mid;
      } else {
        l = mid + 1;
      }
    }
    long long xx = can(first, second, p, q, l);
    if (xx == -1) {
      xx = can(first, second, p, q, r);
    }
    cout << xx << "\n";
  }
}
