#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > adj;
int e1, e2;
int N, M;
long long int gcd(long long int a, long long int b);
long long int lcm(long long int a, long long int b);
long long int CEIL(long long int a, long long int b);
long long int power_mod(long long int a, long long int b, long long int mod);
vector<long long int> powers;
void pre() {
  long long int p = 1;
  while (p <= 1e9) {
    powers.push_back(p);
    p *= 2;
  }
}
void solve(long long int input) {
  long long n, k;
  cin >> n >> k;
  long long p = 1;
  long long int z = 0;
  while (p <= k) p *= 2, ++z;
  if (p == k) {
    cout << power_mod(n, z, 1e9 + 7) << "\n";
    return;
  }
  long long ans = 0;
  const long long int mod = 1e9 + 7;
  long long int pos;
  while (1) {
    pos = upper_bound(powers.begin(), powers.end(), k) - powers.begin();
    pos--;
    (ans += power_mod(n, pos, mod)) %= mod;
    k -= powers[pos];
    if (k <= 0) break;
  }
  cout << ans << endl;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int x = 1;
  pre();
  cin >> x;
  for (long long int i = 1; i <= x; i++) {
    solve(i);
  }
  return 0;
}
long long int gcd(long long int a, long long int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long int lcm(long long int a, long long int b) {
  return (a / gcd(a, b)) * b;
}
long long int CEIL(long long int a, long long int b) { return (a + b - 1) / b; }
long long int power_mod(long long int x, long long int y, long long int mod) {
  long long int res = 1;
  x = x % mod;
  while (y > 0) {
    if (y & 1) res = (res * x) % mod;
    y = y >> 1;
    x = (x * x) % mod;
  }
  return res;
}

### Prompt

Your task is to create a Cpp solution to the following problem:

Theofanis really likes sequences of positive integers, thus his teacher (Yeltsa Kcir) gave him a problem about a sequence that consists of only special numbers.

Let's call a positive number special if it can be written as a sum of different non-negative powers of n. For example, for n = 4 number 17 is special, because it can be written as 4^0 + 4^2 = 1 + 16 = 17, but 9 is not.

Theofanis asks you to help him find the k-th special number if they are sorted in increasing order. Since this number may be too large, output it modulo 10^9+7.

Input

The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.

The first and only line of each test case contains two integers n and k (2 ≤ n ≤ 10^9; 1 ≤ k ≤ 10^9).

Output

For each test case, print one integer — the k-th special number in increasing order modulo 10^9+7.

Example

Input


3
3 4
2 12
105 564


Output


9
12
3595374

Note

For n = 3 the sequence is [1,3,4,9...]

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > adj;
int e1, e2;
int N, M;
long long int gcd(long long int a, long long int b);
long long int lcm(long long int a, long long int b);
long long int CEIL(long long int a, long long int b);
long long int power_mod(long long int a, long long int b, long long int mod);
vector<long long int> powers;
void pre() {
  long long int p = 1;
  while (p <= 1e9) {
    powers.push_back(p);
    p *= 2;
  }
}
void solve(long long int input) {
  long long n, k;
  cin >> n >> k;
  long long p = 1;
  long long int z = 0;
  while (p <= k) p *= 2, ++z;
  if (p == k) {
    cout << power_mod(n, z, 1e9 + 7) << "\n";
    return;
  }
  long long ans = 0;
  const long long int mod = 1e9 + 7;
  long long int pos;
  while (1) {
    pos = upper_bound(powers.begin(), powers.end(), k) - powers.begin();
    pos--;
    (ans += power_mod(n, pos, mod)) %= mod;
    k -= powers[pos];
    if (k <= 0) break;
  }
  cout << ans << endl;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int x = 1;
  pre();
  cin >> x;
  for (long long int i = 1; i <= x; i++) {
    solve(i);
  }
  return 0;
}
long long int gcd(long long int a, long long int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long int lcm(long long int a, long long int b) {
  return (a / gcd(a, b)) * b;
}
long long int CEIL(long long int a, long long int b) { return (a + b - 1) / b; }
long long int power_mod(long long int x, long long int y, long long int mod) {
  long long int res = 1;
  x = x % mod;
  while (y > 0) {
    if (y & 1) res = (res * x) % mod;
    y = y >> 1;
    x = (x * x) % mod;
  }
  return res;
}

```