#include <bits/stdc++.h>
using namespace std;
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    long long x, y, p, q;
    cin >> x >> y >> p >> q;
    long long l = 0, r = 1000000000, ans = -1;
    while (l <= r) {
      long long mid = (l + r) >> 1;
      if (p * mid >= x && q * mid - p * mid >= y - x) {
        ans = mid;
        r = mid - 1;
      } else
        l = mid + 1;
    }
    if (ans == -1)
      puts("-1");
    else
      cout << q * ans - y << endl;
  }
}
