#include <bits/stdc++.h>
using namespace std;
long long read() {
  int f = 1;
  long long res = 0;
  char ch;
  do {
    ch = getchar();
    if (ch == '-') f = -f;
  } while (ch < '0' || ch > '9');
  do {
    res = res * 10 + ch - '0';
    ch = getchar();
  } while (ch >= '0' && ch <= '9');
  return f == 1 ? res : -res;
}
void fast_io() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
const int N = 100010;
const int M = 3010;
const int mod = 1e9 + 7;
const long long INF = 1e18;
int n, m;
int A[N], B[N];
char str[N];
vector<vector<int>> graph(N);
int head[N], to[N * 2], nxt[N * 2], tot;
void addEdge(int u, int v) {
  tot++;
  nxt[tot] = head[u];
  to[tot] = v;
  head[u] = tot;
}
template <class T>
T mmax(T a, T b) {
  return a < b ? b : a;
}
template <class T>
T mmin(T a, T b) {
  return a < b ? a : b;
}
int countOne(long long set) {
  int res = 0;
  while (set) {
    res++;
    set &= set - 1;
  }
  return res;
}
bool contain(long long set, int i) { return (set & (1LL << i)) > 0; }
long long myPow(long long a, long long p) {
  if (p == 0) return 1;
  long long res = myPow(a, p / 2);
  res *= res;
  res %= mod;
  if (p % 2 == 1) {
    res *= a;
    res %= mod;
  }
  return res % mod;
}
void addMode(long long &a, long long b) { a = (a + b) % mod; }
long long mul(long long a, long long b) { return a * b % mod; }
template <class T>
void mySwap(T &a, T &b) {
  T tmp = a;
  a = b;
  b = tmp;
}
long long solve(long long start, long long end, long long p, long long q) {
  long long interval = 1;
  while (interval * interval < end - start) interval++;
  vector<pair<long long, long long>> remainder, remainder2;
  for (long long i = start; i < start + interval; i++) {
    remainder2.push_back(make_pair(i * 2LL * p % (2LL * q), i));
  }
  sort(remainder2.begin(), remainder2.end());
  remainder.push_back(remainder2[0]);
  for (int i = 1; i < remainder2.size(); i++)
    if (remainder2[i - 1].first != remainder2[i].first) {
      remainder.push_back(remainder2[i]);
    }
  remainder2.clear();
  pair<long long, long long> ans = make_pair(INF, -1);
  long long iterate = 0;
  for (iterate = 0; start + (iterate + 1) * interval <= end; iterate++) {
    long long look = q - iterate * interval * 2 * p;
    look %= 2 * q;
    if (look < 0) look += 2 * q;
    int baseIdx =
        lower_bound(remainder.begin(), remainder.end(), make_pair(look, 0LL)) -
        remainder.begin();
    for (int i = 0; i >= -1; i--) {
      long long idx = baseIdx + i;
      idx %= remainder.size();
      if (idx < 0) idx += remainder.size();
      long long dis = look - remainder[idx].first;
      if (dis < 0) dis = -dis;
      if (dis > q) dis = 2 * q - dis;
      long long j = iterate * interval + remainder[idx].second;
      if (make_pair(dis, j) < ans) {
        ans = make_pair(dis, j);
      }
    }
  }
  for (long long i = start + iterate * interval; i < end; i++) {
    long long v = i * 2 * p % (2 * q);
    v -= q;
    if (v < 0) v = -v;
    if (make_pair(v, i) < ans) {
      ans = make_pair(v, i);
    }
  }
  return ans.second;
}
int main() {
  fast_io();
  int T;
  cin >> T;
  for (int t = 0; t < T; t++) {
    long long a, b, p, q;
    cin >> a >> b >> p >> q;
    cout << solve(a, b + 1, p, q) << endl;
  }
  return 0;
}
