#include <bits/stdc++.h>
long long read() {
  long long r = 0, t = 1, c = getchar();
  while (c < '0' || c > '9') {
    t = c == '-' ? -1 : 1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    r = r * 10 + c - 48;
    c = getchar();
  }
  return r * t;
}
const int mod = 1000000007, i2 = (mod + 1) / 2;
long long power(long long a, long long b, long long p) {
  long long r = 1;
  a %= mod;
  for (; b; b >>= 1, a = a * a % p)
    if (b & 1) r = r * a % p;
  return r;
}
long long inv(long long a) { return power(a, mod - 2, mod); }
std::array<long long, 6> R, U, E;
std::array<long long, 6> operator+(std::array<long long, 6> a,
                                   std::array<long long, 6> b) {
  return {(a[0] + b[0] * a[1]) % mod,
          a[1] * b[1] % mod,
          (a[2] + (b[2] + b[0] * a[3]) % mod * a[1]) % mod,
          (a[3] + b[3]) % mod,
          (a[4] + (b[4] + b[0] * a[5]) % mod * a[1]) % mod,
          (a[5] + b[5]) % mod};
}
std::array<long long, 6> operator*(std::array<long long, 6> a, long long b) {
  if (b <= 0) assert(0);
  std::array<long long, 6> r = a;
  b--;
  for (; b; b >>= 1, a = a + a)
    if (b & 1) r = r + a;
  return r;
}
long long p[1000], q;
void gcd(long long n, long long m) {
  if (!m) return;
  if (n > m) {
    gcd(m, n);
    p[++q] = -1;
    return;
  }
  long long k = m / n;
  gcd(n, m - n * k);
  p[++q] = k;
}
void solve(long long n, long long m) {
  R = {1, 1, 0, 1, 0, 0};
  U = {0, i2, 0, 0, 0, 1};
  E = {0, 1, 0, 0, 0, 0};
  q = 0;
  gcd(n, m);
  std::array<std::array<long long, 6>, 3> f = {R, U, E};
  p[1]--;
  if (p[1]) {
    for (int i = (q), end_i = (1); i >= end_i; i--)
      if (p[i] == -1)
        f = {f[1], f[0], f[2]};
      else
        f = {f[0] + f[1] * p[i], f[1], f[2] + f[1] * p[i]};
  }
  std::array<long long, 6> ans = R + f[2] + U;
  long long a1 = ans[0], a2 = ans[2], a3 = ans[4], t = ans[1],
            I = inv((t - 1) * (t - 1));
  a2 = ((a2 - a2 * t + a1 * t % mod * (n % mod)) % mod * I % mod + mod) % mod;
  a3 = ((a3 - a3 * t + a1 * t % mod * (m % mod)) % mod * I % mod + mod) % mod;
  a1 = a1 * inv(1 - t + mod) % mod;
  printf("%lld\n", inv(n % mod) *
                       (n % mod * a1 % mod -
                        (m % mod * a2 - n % mod * a3) % mod * i2 % mod + mod) %
                       mod);
}
int main() {
  for (int T = (1), end_T = (read()); T <= end_T; T++) {
    long long n = read(), m = read();
    solve(n, m);
  }
  return 0;
}

### Prompt

Please provide a cpp coded solution to the problem described below:

The prehistoric caves of El Toll are located in Moià (Barcelona). You have heard that there is a treasure hidden in one of n possible spots in the caves. You assume that each of the spots has probability 1 / n to contain a treasure.

You cannot get into the caves yourself, so you have constructed a robot that can search the caves for treasure. Each day you can instruct the robot to visit exactly k distinct spots in the caves. If none of these spots contain treasure, then the robot will obviously return with empty hands. However, the caves are dark, and the robot may miss the treasure even when visiting the right spot. Formally, if one of the visited spots does contain a treasure, the robot will obtain it with probability 1 / 2, otherwise it will return empty. Each time the robot searches the spot with the treasure, his success probability is independent of all previous tries (that is, the probability to miss the treasure after searching the right spot x times is 1 / 2x).

What is the expected number of days it will take to obtain the treasure if you choose optimal scheduling for the robot? Output the answer as a rational number modulo 109 + 7. Formally, let the answer be an irreducible fraction P / Q, then you have to output <image>. It is guaranteed that Q is not divisible by 109 + 7.

Input

The first line contains the number of test cases T (1 ≤ T ≤ 1000).

Each of the next T lines contains two integers n and k (1 ≤ k ≤ n ≤ 5·108).

Output

For each test case output the answer in a separate line.

Example

Input

3
1 1
2 1
3 2


Output

2
500000007
777777786

Note

In the first case the robot will repeatedly search in the only spot. The expected number of days in this case is 2. Note that in spite of the fact that we know the treasure spot from the start, the robot still has to search there until he succesfully recovers the treasure.

In the second case the answer can be shown to be equal to 7 / 2 if we search the two spots alternatively. In the third case the answer is 25 / 9.

### Response

```cpp
#include <bits/stdc++.h>
long long read() {
  long long r = 0, t = 1, c = getchar();
  while (c < '0' || c > '9') {
    t = c == '-' ? -1 : 1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    r = r * 10 + c - 48;
    c = getchar();
  }
  return r * t;
}
const int mod = 1000000007, i2 = (mod + 1) / 2;
long long power(long long a, long long b, long long p) {
  long long r = 1;
  a %= mod;
  for (; b; b >>= 1, a = a * a % p)
    if (b & 1) r = r * a % p;
  return r;
}
long long inv(long long a) { return power(a, mod - 2, mod); }
std::array<long long, 6> R, U, E;
std::array<long long, 6> operator+(std::array<long long, 6> a,
                                   std::array<long long, 6> b) {
  return {(a[0] + b[0] * a[1]) % mod,
          a[1] * b[1] % mod,
          (a[2] + (b[2] + b[0] * a[3]) % mod * a[1]) % mod,
          (a[3] + b[3]) % mod,
          (a[4] + (b[4] + b[0] * a[5]) % mod * a[1]) % mod,
          (a[5] + b[5]) % mod};
}
std::array<long long, 6> operator*(std::array<long long, 6> a, long long b) {
  if (b <= 0) assert(0);
  std::array<long long, 6> r = a;
  b--;
  for (; b; b >>= 1, a = a + a)
    if (b & 1) r = r + a;
  return r;
}
long long p[1000], q;
void gcd(long long n, long long m) {
  if (!m) return;
  if (n > m) {
    gcd(m, n);
    p[++q] = -1;
    return;
  }
  long long k = m / n;
  gcd(n, m - n * k);
  p[++q] = k;
}
void solve(long long n, long long m) {
  R = {1, 1, 0, 1, 0, 0};
  U = {0, i2, 0, 0, 0, 1};
  E = {0, 1, 0, 0, 0, 0};
  q = 0;
  gcd(n, m);
  std::array<std::array<long long, 6>, 3> f = {R, U, E};
  p[1]--;
  if (p[1]) {
    for (int i = (q), end_i = (1); i >= end_i; i--)
      if (p[i] == -1)
        f = {f[1], f[0], f[2]};
      else
        f = {f[0] + f[1] * p[i], f[1], f[2] + f[1] * p[i]};
  }
  std::array<long long, 6> ans = R + f[2] + U;
  long long a1 = ans[0], a2 = ans[2], a3 = ans[4], t = ans[1],
            I = inv((t - 1) * (t - 1));
  a2 = ((a2 - a2 * t + a1 * t % mod * (n % mod)) % mod * I % mod + mod) % mod;
  a3 = ((a3 - a3 * t + a1 * t % mod * (m % mod)) % mod * I % mod + mod) % mod;
  a1 = a1 * inv(1 - t + mod) % mod;
  printf("%lld\n", inv(n % mod) *
                       (n % mod * a1 % mod -
                        (m % mod * a2 - n % mod * a3) % mod * i2 % mod + mod) %
                       mod);
}
int main() {
  for (int T = (1), end_T = (read()); T <= end_T; T++) {
    long long n = read(), m = read();
    solve(n, m);
  }
  return 0;
}

```