#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int x, y, p, q;
    scanf("%d %d %d %d", &x, &y, &p, &q);
    long long lower = 1, upper = 2e9;
    long long result = -1;
    while (lower <= upper) {
      long long mid = (lower + upper) / 2;
      long long c = mid;
      long long newP = p * c;
      long long newQ = q * c;
      long long addX = newP - x;
      long long addY = newQ - y;
      if (x + addX <= y + addY && addX >= 0 && addY >= 0 && addY >= addX) {
        result = addY;
        upper = mid - 1;
      } else {
        lower = mid + 1;
      }
    }
    printf("%lld\n", result);
  }
  return 0;
}
