#include <bits/stdc++.h>
using namespace std;
string a, b;
string c;
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    long long x, y, p, q;
    scanf("%lld%lld%lld%lld", &x, &y, &p, &q);
    long long l = 1, r = 1e18 / q, m;
    long long ans;
    while (1) {
      m = (l + r) >> 1;
      if (l == m) {
        long long del = l * q - y;
        if (del >= 0 && (x + del) >= l * p && x <= l * p) {
          ans = l;
        } else if ((r * q - y) >= 0 && (x + (r * q - y)) >= r * p &&
                   x <= r * p) {
          ans = r;
        } else {
          ans = -1;
        }
        break;
      }
      if (m * q < y) {
        l = m + 1;
        continue;
      }
      long long del = m * q - y;
      if ((x + del) >= m * p && x <= m * p) {
        r = m;
      } else {
        l = m + 1;
      }
    }
    printf("%lld\n", ans == -1 ? -1 : ans * q - y);
  }
}
