#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-7;
const double PI = acos(-1.0);
const long long MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const long long LL_INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 100007;
long long x, y, p, q;
int main() {
  int nCase;
  scanf("%d", &nCase);
  while (nCase--) {
    scanf("%I64d%I64d%I64d%I64d", &x, &y, &p, &q);
    if (p == 0) {
      if (x != 0) {
        printf("-1\n");
        continue;
      } else {
        printf("0\n");
        continue;
      }
    }
    if (p == q) {
      if (x != y) {
        printf("-1\n");
        continue;
      } else {
        printf("0\n");
        continue;
      }
    }
    long long k = max(ceil((double)x / (double)p),
                      ceil((double)(y - x) / (double)(q - p)));
    printf("%I64d\n", k * q - y);
  }
  return 0;
}
