#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2")
using namespace std;
using vi = vector<long long>;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
long long powmod(long long a, long long b, long long mod) {
  if (b == 0 || a == 1) {
    if (mod == 1)
      return 0;
    else
      return 1;
  }
  if (b % 2 == 0) {
    long long k = powmod(a, b / 2, mod);
    return (k * k) % mod;
  } else {
    long long k = powmod(a, b / 2, mod);
    return ((k * k) % mod * a) % mod;
  }
}
long long gcd(long long a, long long b) {
  if (a == 0) return b;
  if (b == 0) return a;
  if (a > b)
    return gcd(a % b, b);
  else
    return gcd(b % a, a);
}
long long prime(long long p) {
  if (p == 1) return 0;
  for (long long i = 2; i * i <= p; i++) {
    if (p % i == 0 && i < p) return i;
  }
  return 1;
}
long long inv(long long a, long long mod) { return powmod(a, mod - 2, mod); }
long long random_modul() {
  uniform_int_distribution<long long> u1(1e9, 2e9);
  long long pepega = u1(rnd);
  while (prime(pepega) != 1) pepega = u1(rnd);
  return pepega;
}
template <typename T1>
void ifmax(T1& max, T1 kek) {
  if (kek > max) max = kek;
  return;
}
template <typename T1>
void ifmin(T1& min, T1 kek) {
  if (kek < min) min = kek;
  return;
}
template <typename T1>
istream& operator>>(std::istream& in, pair<T1, T1>& a) {
  in >> a.first >> a.second;
  return in;
}
template <typename T1>
istream& operator>>(std::istream& in, vector<T1>& a) {
  for (long long i = 0; i < a.size(); i++) in >> a[i];
  return in;
}
template <typename T1>
ostream& operator<<(std::ostream& out, vector<T1>& b) {
  for (long long i = 0; i < b.size(); i++) out << b[i] << " ";
  out << "\n";
  return out;
}
template <typename T1>
void sort(vector<T1>& m) {
  sort(m.begin(), m.end());
}
template <typename T1>
void reverse(vector<T1>& m) {
  reverse(m.begin(), m.end());
}
long long mod = 1000000007;
long long fac[200005];
long long inv_fac[200005];
long long co(long long a, long long b) {
  long long ans = 1;
  ans *= fac[b];
  ans *= inv_fac[b - a];
  ans %= mod;
  ans *= inv_fac[a];
  return ans % mod;
}
bool is_sorted(vector<long long> a) {
  long long check = 1;
  for (long long i = 0; i + 1 < a.size(); i++)
    if (a[i + 1] < a[i]) check = 0;
  if (check)
    return true;
  else
    return false;
}
void solve() {
  long long a, k;
  cin >> a >> k;
  vector<long long> lul;
  long long q = k;
  while (q) {
    lul.push_back(q % 2);
    q /= 2;
  }
  long long cur = 1;
  long long ans = 0;
  for (long long i = 0; i < lul.size(); i++) {
    ans += lul[i] * cur;
    ans %= mod;
    cur *= a;
    cur %= mod;
  }
  cout << ans << "\n";
  return;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(10);
  long long multitest = 1;
  long long tututu;
  tututu = 1;
  long long combinatorics = 0;
  if (combinatorics) {
    fac[1] = 1;
    fac[0] = 1;
    for (long long i = 2; i < 200005; i++) {
      fac[i] = fac[i - 1] * i;
      fac[i] %= mod;
    }
    for (long long i = 0; i < 200005; i++) {
      inv_fac[i] = inv(fac[i], mod);
    }
  }
  if (multitest) cin >> tututu;
  for (long long qwerty = 0; qwerty < tututu; qwerty++) solve();
  return 0;
}

### 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>
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2")
using namespace std;
using vi = vector<long long>;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
long long powmod(long long a, long long b, long long mod) {
  if (b == 0 || a == 1) {
    if (mod == 1)
      return 0;
    else
      return 1;
  }
  if (b % 2 == 0) {
    long long k = powmod(a, b / 2, mod);
    return (k * k) % mod;
  } else {
    long long k = powmod(a, b / 2, mod);
    return ((k * k) % mod * a) % mod;
  }
}
long long gcd(long long a, long long b) {
  if (a == 0) return b;
  if (b == 0) return a;
  if (a > b)
    return gcd(a % b, b);
  else
    return gcd(b % a, a);
}
long long prime(long long p) {
  if (p == 1) return 0;
  for (long long i = 2; i * i <= p; i++) {
    if (p % i == 0 && i < p) return i;
  }
  return 1;
}
long long inv(long long a, long long mod) { return powmod(a, mod - 2, mod); }
long long random_modul() {
  uniform_int_distribution<long long> u1(1e9, 2e9);
  long long pepega = u1(rnd);
  while (prime(pepega) != 1) pepega = u1(rnd);
  return pepega;
}
template <typename T1>
void ifmax(T1& max, T1 kek) {
  if (kek > max) max = kek;
  return;
}
template <typename T1>
void ifmin(T1& min, T1 kek) {
  if (kek < min) min = kek;
  return;
}
template <typename T1>
istream& operator>>(std::istream& in, pair<T1, T1>& a) {
  in >> a.first >> a.second;
  return in;
}
template <typename T1>
istream& operator>>(std::istream& in, vector<T1>& a) {
  for (long long i = 0; i < a.size(); i++) in >> a[i];
  return in;
}
template <typename T1>
ostream& operator<<(std::ostream& out, vector<T1>& b) {
  for (long long i = 0; i < b.size(); i++) out << b[i] << " ";
  out << "\n";
  return out;
}
template <typename T1>
void sort(vector<T1>& m) {
  sort(m.begin(), m.end());
}
template <typename T1>
void reverse(vector<T1>& m) {
  reverse(m.begin(), m.end());
}
long long mod = 1000000007;
long long fac[200005];
long long inv_fac[200005];
long long co(long long a, long long b) {
  long long ans = 1;
  ans *= fac[b];
  ans *= inv_fac[b - a];
  ans %= mod;
  ans *= inv_fac[a];
  return ans % mod;
}
bool is_sorted(vector<long long> a) {
  long long check = 1;
  for (long long i = 0; i + 1 < a.size(); i++)
    if (a[i + 1] < a[i]) check = 0;
  if (check)
    return true;
  else
    return false;
}
void solve() {
  long long a, k;
  cin >> a >> k;
  vector<long long> lul;
  long long q = k;
  while (q) {
    lul.push_back(q % 2);
    q /= 2;
  }
  long long cur = 1;
  long long ans = 0;
  for (long long i = 0; i < lul.size(); i++) {
    ans += lul[i] * cur;
    ans %= mod;
    cur *= a;
    cur %= mod;
  }
  cout << ans << "\n";
  return;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(10);
  long long multitest = 1;
  long long tututu;
  tututu = 1;
  long long combinatorics = 0;
  if (combinatorics) {
    fac[1] = 1;
    fac[0] = 1;
    for (long long i = 2; i < 200005; i++) {
      fac[i] = fac[i - 1] * i;
      fac[i] %= mod;
    }
    for (long long i = 0; i < 200005; i++) {
      inv_fac[i] = inv(fac[i], mod);
    }
  }
  if (multitest) cin >> tututu;
  for (long long qwerty = 0; qwerty < tututu; qwerty++) solve();
  return 0;
}

```