#include <bits/stdc++.h>
using namespace std;
const long long mod = 10000007;
const int maxn = 1e4 + 10;
const int inf = 0x3f3f3f3f;
int main() {
  int x, y, p, q, t;
  scanf("%d", &t);
  while (t--) {
    scanf("%d %d %d %d", &x, &y, &p, &q);
    long long l = 0, r = 1e10 + 10;
    long long mid, ans = -1;
    while (l <= r) {
      mid = (l + r) / 2;
      long long a = q * mid - y;
      long long b = p * mid - x;
      if (a >= 0 && b >= 0 && a >= b) {
        ans = mid;
        r = mid - 1;
      } else
        l = mid + 1;
    }
    if (ans == -1)
      puts("-1");
    else
      printf("%lld\n", ans * q - y);
  }
}
