#include <bits/stdc++.h>
using namespace std;
int x, y, p, q, t;
bool can(long long m) {
  long long newX = p * m, newY = m * q;
  if (newY == y && newX > x || newX - x > newY - y || newX < x || newY < y)
    return false;
  return true;
}
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%d%d%d%d", &x, &y, &p, &q);
    long long r = 1e9, l = 1, m, ans = 1e18;
    while (r >= l) {
      m = (r + l) / 2;
      if (can(m)) {
        ans = (q * m) - y;
        r = m - 1;
      } else
        l = m + 1;
    }
    if (ans == 1e18)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
  return 0;
}
