#include <bits/stdc++.h>
using namespace std;
const int block_size = 320;
const long long mod = 1e9 + 7;
const long long inf = 1e9 + 7;
const long double eps = 1e-9;
const double PI = atan(1) * 4;
template <typename T>
inline int sign(const T &a) {
  if (a < 0) return -1;
  if (a > 0) return 1;
  return 0;
}
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
template <typename T, typename S>
inline bool upmin(T &a, const S &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T, typename S>
inline bool upmax(T &a, const S &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T>
inline void in(T &x) {
  x = 0;
  T f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  x *= f;
}
long long twop(int x) { return 1LL << x; }
template <typename T>
T MOD(T a, T m) {
  a %= m;
  if (a < 0) a += m;
  return a;
}
template <typename T>
T inverse(T a, T m) {
  a = MOD(a, m);
  if (a <= 1) return a;
  return MOD((1 - inverse(m, a) * m) / a, m);
}
template <typename A, typename B>
inline void in(A &x, B &y) {
  in(x);
  in(y);
}
template <typename A, typename B, typename C>
inline void in(A &x, B &y, C &z) {
  in(x);
  in(y);
  in(z);
}
template <typename A, typename B, typename C, typename D>
inline void in(A &x, B &y, C &z, D &d) {
  in(x);
  in(y);
  in(z);
  in(d);
}
template <typename T>
T sqr(T x) {
  return x * x;
}
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long fast(long long a, long long b, long long mod) {
  long long ans = 1;
  while (b) {
    if (b & 1) {
      b--;
      ans = ans * a % mod;
    } else {
      a = a * a % mod;
      b /= 2;
    }
  }
  return ans % mod;
}
namespace SOLVE {
long long a, b, p, q;
long long g(long long x) {
  return min(MOD(2 * p * x - q, 2 * q), MOD(q - 2 * p * x, 2 * q));
}
long long ans = a;
void update(long long candidate) {
  if (g(candidate) < g(ans))
    ans = candidate;
  else if (g(candidate) == g(ans) and candidate < ans)
    ans = candidate;
}
void main() {
  cin >> a >> b >> p >> q;
  ans = a;
  if (b - a <= 100000) {
    for (long long i = a; i < b + 1; i++) {
      update(i);
    }
    cout << ans << endl;
  } else {
    const long long block = 30000;
    {};
    map<long long, long long> mp;
    for (long long i = a; i < a + block; i++) {
      int tmp = MOD(2 * p * i - q, 2 * q);
      if (!mp.count(tmp)) mp[tmp] = i;
      update(i);
    }
    for (long long i = b; i >= b - block; i--) update(i);
    vector<pair<long long, long long> > v;
    for (auto i : mp) v.push_back(i);
    {};
    for (long long i = 0; a + (i + 1) * block - 1 <= b; i++) {
      {};
      long long add = MOD(2 * i * block * p, 2 * q);
      {};
      update(v[0].second + i * block);
      update(v.back().second + i * block);
      auto ptr =
          lower_bound(begin(v), end(v), make_pair(MOD(-add, 2 * q), -1ll));
      if (v.begin() <= ptr and ptr < v.end()) {
        {};
        update(ptr->second + i * block);
      }
      --ptr;
      if (v.begin() <= ptr and ptr < v.end()) {
        {};
        update(ptr->second + i * block);
      }
    }
    cout << ans << endl;
  }
}
}  // namespace SOLVE
signed main() {
  int t = 1;
  in(t);
  while (t--) {
    SOLVE::main();
  }
  return 0;
}

### Prompt

In CPP, your task is to solve the following problem:

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>
using namespace std;
const int block_size = 320;
const long long mod = 1e9 + 7;
const long long inf = 1e9 + 7;
const long double eps = 1e-9;
const double PI = atan(1) * 4;
template <typename T>
inline int sign(const T &a) {
  if (a < 0) return -1;
  if (a > 0) return 1;
  return 0;
}
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
template <typename T, typename S>
inline bool upmin(T &a, const S &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T, typename S>
inline bool upmax(T &a, const S &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T>
inline void in(T &x) {
  x = 0;
  T f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  x *= f;
}
long long twop(int x) { return 1LL << x; }
template <typename T>
T MOD(T a, T m) {
  a %= m;
  if (a < 0) a += m;
  return a;
}
template <typename T>
T inverse(T a, T m) {
  a = MOD(a, m);
  if (a <= 1) return a;
  return MOD((1 - inverse(m, a) * m) / a, m);
}
template <typename A, typename B>
inline void in(A &x, B &y) {
  in(x);
  in(y);
}
template <typename A, typename B, typename C>
inline void in(A &x, B &y, C &z) {
  in(x);
  in(y);
  in(z);
}
template <typename A, typename B, typename C, typename D>
inline void in(A &x, B &y, C &z, D &d) {
  in(x);
  in(y);
  in(z);
  in(d);
}
template <typename T>
T sqr(T x) {
  return x * x;
}
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long fast(long long a, long long b, long long mod) {
  long long ans = 1;
  while (b) {
    if (b & 1) {
      b--;
      ans = ans * a % mod;
    } else {
      a = a * a % mod;
      b /= 2;
    }
  }
  return ans % mod;
}
namespace SOLVE {
long long a, b, p, q;
long long g(long long x) {
  return min(MOD(2 * p * x - q, 2 * q), MOD(q - 2 * p * x, 2 * q));
}
long long ans = a;
void update(long long candidate) {
  if (g(candidate) < g(ans))
    ans = candidate;
  else if (g(candidate) == g(ans) and candidate < ans)
    ans = candidate;
}
void main() {
  cin >> a >> b >> p >> q;
  ans = a;
  if (b - a <= 100000) {
    for (long long i = a; i < b + 1; i++) {
      update(i);
    }
    cout << ans << endl;
  } else {
    const long long block = 30000;
    {};
    map<long long, long long> mp;
    for (long long i = a; i < a + block; i++) {
      int tmp = MOD(2 * p * i - q, 2 * q);
      if (!mp.count(tmp)) mp[tmp] = i;
      update(i);
    }
    for (long long i = b; i >= b - block; i--) update(i);
    vector<pair<long long, long long> > v;
    for (auto i : mp) v.push_back(i);
    {};
    for (long long i = 0; a + (i + 1) * block - 1 <= b; i++) {
      {};
      long long add = MOD(2 * i * block * p, 2 * q);
      {};
      update(v[0].second + i * block);
      update(v.back().second + i * block);
      auto ptr =
          lower_bound(begin(v), end(v), make_pair(MOD(-add, 2 * q), -1ll));
      if (v.begin() <= ptr and ptr < v.end()) {
        {};
        update(ptr->second + i * block);
      }
      --ptr;
      if (v.begin() <= ptr and ptr < v.end()) {
        {};
        update(ptr->second + i * block);
      }
    }
    cout << ans << endl;
  }
}
}  // namespace SOLVE
signed main() {
  int t = 1;
  in(t);
  while (t--) {
    SOLVE::main();
  }
  return 0;
}

```