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: #!/usr/bin/env python3
  import sys
  import getpass  # not available on codechef
  import math, random
  import functools, itertools, collections, heapq, bisect
  from collections import Counter, defaultdict, deque
  input = sys.stdin.readline  # to read input quickly
  
  # available on Google, AtCoder Python3, not available on Codeforces
  # import numpy as np
  # import scipy
  
  M9 = 10**9 + 7  # 998244353
  yes, no = "YES", "NO"
  # d4 = [(1,0),(0,1),(-1,0),(0,-1)]
  # d8 = [(1,0),(1,1),(0,1),(-1,1),(-1,0),(-1,-1),(0,-1),(1,-1)]
  # d6 = [(2,0),(1,1),(-1,1),(-2,0),(-1,-1),(1,-1)]  # hexagonal layout
  MAXINT = sys.maxsize
  
  # if testing locally, print to terminal with a different color
  OFFLINE_TEST = getpass.getuser() == "hkmac"
  # OFFLINE_TEST = False  # codechef does not allow getpass
  def log(*args):
      if OFFLINE_TEST:
          print('\033[36m', *args, '\033[0m', file=sys.stderr)
  
  def solve(*args):
      # screen input
      if OFFLINE_TEST:
          log("----- solving ------")
          log(*args)
          log("----- ------- ------")
      return solve_(*args)
  
  def read_matrix(rows):
      return [list(map(int,input().split())) for _ in range(rows)]
  
  def read_strings(rows):
      return [input().strip() for _ in range(rows)]
  
  def minus_one(arr):
      return [x-1 for x in arr]
  
  def minus_one_matrix(mrr):
      return [[x-1 for x in row] for row in mrr]
  
  # ---------------------------- template ends here ----------------------------
  
  
  def solve_(n,k):
      # your solution here
  
      # k in base n
      # k -= 1
      binarr = bin(k)[2:]
  
      # log(binarr)
  
      res = 0
      for c in binarr:
          res *= n
          if c == "1":
              res += 1
          res = res%M9
          # log(res)
  
      return res
  
  
  # for case_num in [0]:  # no loop over test case
  # for case_num in range(100):  # if the number of test cases is specified
  for case_num in range(int(input())):
  
      # read line as an integer
      # k = int(input())
  
      # read line as a string
      # srr = input().strip()
  
      # read one line and parse each word as a string
      # lst = input().split()
  
      # read one line and parse each word as an integer
      n,k = list(map(int,input().split()))
      # lst = list(map(int,input().split()))
      # lst = minus_one(lst)
  
      # read multiple rows
      # arr = read_strings(k)  # and return as a list of str
      # mrr = read_matrix(k)  # and return as a list of list of int
      # mrr = minus_one_matrix(mrr)
  
      res = solve(n,k)  # include input here
  
      # print length if applicable
      # print(len(res))
  
      # parse result
      # res = " ".join(str(x) for x in res)
      # res = "\n".join(str(x) for x in res)
      # res = "\n".join(" ".join(str(x) for x in row) for row in res)
  
      # print result
      # print("Case #{}: {}".format(case_num+1, res))   # Google and Facebook - case number required
  
      print(res)
  