#include <bits/stdc++.h>
using namespace std;
long long x, y, p, q;
bool can(long long ans) {
  return (x - y + q * ans >= ans * p and p * ans >= x);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  int tt;
  cin >> tt;
  for (int cs = 1; cs <= tt; cs++) {
    cin >> x >> y >> p >> q;
    long long l = 1, r = 1e9, ans = -1;
    while (l <= r) {
      long long m = (l + r) / 2;
      if (can(m)) {
        if (!can(m - 1)) {
          ans = m;
          break;
        }
        r = m - 1;
      } else {
        l = m + 1;
      }
    }
    if (ans == -1) {
      cout << ans << endl;
    } else {
      cout << ans * q - y << endl;
    }
  }
}
