#include <bits/stdc++.h>
using namespace std;
long long a[1001];
bool moguce(int x, int y, int p, int q, int s) {
  long long p1 = (long long)p * s, p2 = (long long)q * s;
  if (p1 >= (long long)x && p2 >= (long long)y &&
      p1 - (long long)x <= (long long)p2 - y)
    return true;
  else
    return false;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, x, y, p, q, i, poc, kraj, s;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> x >> y >> p >> q;
    if (moguce(x, y, p, q, 1000000001) == false)
      a[i] = -1;
    else {
      poc = 1;
      kraj = 1000000001;
      while (poc <= kraj) {
        s = (poc + kraj) / 2;
        if (moguce(x, y, p, q, s) == true && moguce(x, y, p, q, s - 1) == false)
          break;
        else {
          if (moguce(x, y, p, q, s) == false)
            poc = s + 1;
          else
            kraj = s - 1;
        }
      }
      a[i] = (long long)q * s - y;
    }
  }
  for (i = 0; i < n; i++) cout << a[i] << endl;
  return 0;
}
