description: 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...]
solution: #include <bits/stdc++.h>
  using namespace std;
  long long gcd(long long a, long long b) {
    if (b == 0) return a;
    return gcd(b, a % b);
  }
  long long lcm(long long a, long long b) { return (a / gcd(a, b) * b); }
  bool sorta(const pair<int, int> &a, const pair<int, int> &b) {
    return (a.second < b.second);
  }
  bool sortd(const pair<int, int> &a, const pair<int, int> &b) {
    return (a.second > b.second);
  }
  void printarr(long long arr[], long long n) {
    for (int i = 0; i < n; i++) cout << arr[i] << " ";
    cout << "\n";
  }
  string decToBinary(int n) {
    string s = "";
    int i = 0;
    while (n > 0) {
      s = to_string(n % 2) + s;
      n = n / 2;
      i++;
    }
    return s;
  }
  long long binaryToDecimal(string n) {
    string num = n;
    long long dec_value = 0;
    int base = 1;
    int len = num.length();
    for (int i = len - 1; i >= 0; i--) {
      if (num[i] == '1') dec_value += base;
      base = base * 2;
    }
    return dec_value;
  }
  bool isPrime(int n) {
    if (n <= 1) return false;
    if (n <= 3) return true;
    if (n % 2 == 0 || n % 3 == 0) return false;
    for (int i = 5; i * i <= n; i = i + 6)
      if (n % i == 0 || n % (i + 2) == 0) return false;
    return true;
  }
  bool isPowerOfTwo(int n) {
    if (n == 0) return false;
    return (ceil(log2(n)) == floor(log2(n)));
  }
  bool isPerfectSquare(long long x) {
    if (x >= 0) {
      long long sr = sqrt(x);
      return (sr * sr == x);
    }
    return false;
  }
  long long moduloMultiplication(long long a, long long b, long long mod) {
    long long res = 0;
    a %= mod;
    while (b) {
      if (b & 1) res = (res + a) % mod;
      b >>= 1;
    }
    return res;
  }
  long long powermod(long long x, long long y, long long p) {
    long long res = 1;
    x = x % p;
    if (x == 0) return 0;
    while (y > 0) {
      if (y & 1) res = (res * x) % p;
      y = y >> 1;
      x = (x * x) % p;
    }
    return res;
  }
  void asquare() {
    long long mod = 1000000007;
    long long n, k;
    cin >> n >> k;
    if (isPowerOfTwo(k)) {
      cout << powermod(n, ceil(log2(k)), mod) << "\n";
    } else {
      long long c = 0;
      while (k > 0) {
        if (k == 1) {
          c++;
          k = 0;
        } else if (k == 2) {
          c += n % mod;
          k = 0;
        } else if (k == 3) {
          c += (n + 1) % mod;
          k = 0;
        } else if (isPowerOfTwo(k)) {
          c += powermod(n, ceil(log2(k)), mod);
          k = 0;
        } else {
          long long x = pow(2, floor(log2(k)));
          c += powermod(n, floor(log2(k)), mod);
          k -= x;
        }
      }
      cout << c % mod << "\n";
    }
  }
  int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    long long t;
    cin >> t;
    while (t--) {
      asquare();
    }
    return 0;
  }
  