#include <bits/stdc++.h>
#pragma GCC optimize("-O2")
using namespace std;
void err(istream_iterator<string> it) { cerr << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << "\t";
  err(++it, args...);
}
template <typename T1, typename T2>
ostream& operator<<(ostream& c, pair<T1, T2>& v) {
  c << "(" << v.first << "," << v.second << ")";
  return c;
}
template <template <class...> class TT, class... T>
ostream& operator<<(ostream& out, TT<T...>& c) {
  out << "{ ";
  for (auto& x : c) out << x << " ";
  out << "}";
  return out;
}
const long long LIM = 1e5 + 5, MOD = 1e9 + 7;
long long t, n, m, k, x, y, w;
vector<pair<long long, long long> > val1, val2;
long long best, ans;
void check(long long val, long long tar, long long posn) {
  if (abs(val - tar) < best)
    best = abs(val - tar), ans = posn;
  else if (best == abs(val - tar) and posn < ans)
    ans = posn;
}
void search(long long tar, long long pad) {
  auto it = lower_bound(val1.begin(), val1.end(), make_pair(tar, -1LL));
  check(it->first, tar, it->second + pad);
  it = lower_bound(val2.begin(), val2.end(), make_pair(tar, -MOD));
  --it;
  check(it->first, tar, -it->second + pad);
}
void solve(long long testcase) {
  long long a, b, p, q;
  cin >> a >> b >> p >> q;
  long long t = sqrt(b - a + 1);
  long long mod = 2 * q, tar = q;
  val1.clear(), val2.clear();
  for (long long i = a; i < a + t; ++i)
    val1.push_back({(2 * p * i) % mod, i}),
        val2.push_back({(2 * p * i) % mod, -i}),
        val1.push_back({mod + (2 * p * i) % mod, i}),
        val2.push_back({-mod + (2 * p * i) % mod, -i});
  sort(val1.begin(), val1.end());
  sort(val2.begin(), val2.end());
  best = ans = MOD;
  long long id = 0;
  search(tar, t * id++);
  a += t;
  while (a + t <= b + 1) {
    tar = (tar - (2 * t * p) % mod + mod) % mod;
    search(tar, t * id++);
    a += t;
  }
  for (long long i = a; i < b + 1; ++i) check((2 * p * i) % mod, q, i);
  cout << ans << '\n';
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long testcase = 1;
  cin >> testcase;
  for (long long i = 1; i < testcase + 1; ++i) solve(i);
  return 0;
}

### Prompt

Construct a CPP code solution to the problem outlined:

You have given integers a, b, p, and q. Let f(x) = abs(sin(p/q π x)).

Find minimum possible integer x that maximizes f(x) where a ≤ x ≤ b.

Input

Each test contains multiple test cases. 

The first line contains the number of test cases t (1 ≤ t ≤ 100) — the number of test cases.

The first line of each test case contains four integers a, b, p, and q (0 ≤ a ≤ b ≤ 10^{9}, 1 ≤ p, q ≤ 10^{9}).

Output

Print the minimum possible integer x for each test cases, separated by newline.

Example

Input


2
0 3 1 3
17 86 389 995


Output


1
55

Note

In the first test case, f(0) = 0, f(1) = f(2) ≈ 0.866, f(3) = 0.

In the second test case, f(55) ≈ 0.999969, which is the largest among all possible values.

### Response

```cpp
#include <bits/stdc++.h>
#pragma GCC optimize("-O2")
using namespace std;
void err(istream_iterator<string> it) { cerr << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << "\t";
  err(++it, args...);
}
template <typename T1, typename T2>
ostream& operator<<(ostream& c, pair<T1, T2>& v) {
  c << "(" << v.first << "," << v.second << ")";
  return c;
}
template <template <class...> class TT, class... T>
ostream& operator<<(ostream& out, TT<T...>& c) {
  out << "{ ";
  for (auto& x : c) out << x << " ";
  out << "}";
  return out;
}
const long long LIM = 1e5 + 5, MOD = 1e9 + 7;
long long t, n, m, k, x, y, w;
vector<pair<long long, long long> > val1, val2;
long long best, ans;
void check(long long val, long long tar, long long posn) {
  if (abs(val - tar) < best)
    best = abs(val - tar), ans = posn;
  else if (best == abs(val - tar) and posn < ans)
    ans = posn;
}
void search(long long tar, long long pad) {
  auto it = lower_bound(val1.begin(), val1.end(), make_pair(tar, -1LL));
  check(it->first, tar, it->second + pad);
  it = lower_bound(val2.begin(), val2.end(), make_pair(tar, -MOD));
  --it;
  check(it->first, tar, -it->second + pad);
}
void solve(long long testcase) {
  long long a, b, p, q;
  cin >> a >> b >> p >> q;
  long long t = sqrt(b - a + 1);
  long long mod = 2 * q, tar = q;
  val1.clear(), val2.clear();
  for (long long i = a; i < a + t; ++i)
    val1.push_back({(2 * p * i) % mod, i}),
        val2.push_back({(2 * p * i) % mod, -i}),
        val1.push_back({mod + (2 * p * i) % mod, i}),
        val2.push_back({-mod + (2 * p * i) % mod, -i});
  sort(val1.begin(), val1.end());
  sort(val2.begin(), val2.end());
  best = ans = MOD;
  long long id = 0;
  search(tar, t * id++);
  a += t;
  while (a + t <= b + 1) {
    tar = (tar - (2 * t * p) % mod + mod) % mod;
    search(tar, t * id++);
    a += t;
  }
  for (long long i = a; i < b + 1; ++i) check((2 * p * i) % mod, q, i);
  cout << ans << '\n';
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long testcase = 1;
  cin >> testcase;
  for (long long i = 1; i < testcase + 1; ++i) solve(i);
  return 0;
}

```