#include <bits/stdc++.h>
using namespace std;
long long x, y, p, q;
bool valid(long long a) {
  if (a * q - y >= a * p - x && a * p >= x) return 1;
  return 0;
}
long long bs() {
  long long st = 1, ed = 3e9 + 1, md;
  while (st < ed) {
    md = (st + ed) / 2;
    if (valid(md))
      ed = md;
    else
      st = md + 1;
  }
  return (valid(st)) ? st * q - y : -1;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    cin >> x >> y >> p >> q;
    if (p == 0 && x != 0) {
      puts("-1");
    } else if (x == 0 && p == 0) {
      puts("0");
    } else if (x == p && y == q) {
      puts("0");
    } else if (p == 1 && q == 1 && x != y) {
      puts("-1");
    } else {
      cout << bs() << endl;
    }
  }
}
