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: import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.PrintWriter;
  import java.util.*;
  
  public class Practice {
  	public static long mod = (long) Math.pow(10, 9) + 7;
  	public static long mod2 = 998244353;
  	public static int tt = 0;
  	public static ArrayList<Integer> prime;
  
  	public static void main(String[] args) throws Exception {
  		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  		PrintWriter pw = new PrintWriter(System.out);
  		int t = Integer.parseInt(br.readLine());
  		TreeSet<Long> set = new TreeSet<Long>();
  		long curr = 1;
  		int c = 0;
  		HashMap<Long, Integer> map = new HashMap<Long, Integer>();
  		while (curr < Integer.MAX_VALUE) {
  			set.add(curr);
  			map.put(curr, c);
  			c++;
  			curr = curr * 2;
  		}
  
  		while (t-- > 0) {
  			String[] s1 = br.readLine().split(" ");
  			long n = Integer.valueOf(s1[0]);
  			long k = Integer.valueOf(s1[1]);
  			long[] arr = new long[c];
  			long j = 1;
  			for (int i = 0; i < c; i++) {
  				arr[i] = j;
  				j = (j * n) % mod;
  			}
  			long ans = 0;
  			while (k > 0) {
  				long h = set.floor(k);
  				ans = (ans + arr[map.get(h)]) % mod;
  				k = k - h;
  			}
  			pw.println(ans);
  		}
  		pw.close();
  	}
  
  }
  
  //	private static void getFac(long n, PrintWriter pw) {
  //		// TODO Auto-generated method stub
  //		int a = 0;
  //		while (n % 2 == 0) {
  //			a++;
  //			n = n / 2;
  //		}
  //		if (n == 1) {
  //			a--;
  //		}
  //		for (int i = 3; i <= Math.sqrt(n); i += 2) {
  //			while (n % i == 0) {
  //				n = n / i;
  //				a++;
  //			}
  //		}
  //		if (n > 1) {
  //			a++;
  //		}
  //		if (a % 2 == 0) {
  //			pw.println("Bob");
  //		} else {
  //			pw.println("Alice");
  //		}
  //		//System.out.println(a);
  //		return;
  //	}
  
  //	private static long power(long a, long p) {
  //		// TODO Auto-generated method stub
  //		long res = 1;
  //		while (p > 0) {
  //			if (p % 2 == 1) {
  //				res = (res * a) % mod;
  //			}
  //			p = p / 2;
  //			a = (a * a) % mod;
  //		}
  //		return res;
  //	}
  //
  //	private static void fac() {
  //		fac[0] = 1;
  //		// TODO Auto-generated method stub
  //		for (int i = 1; i < fac.length; i++) {
  //			if (i == 1) {
  //				fac[i] = 1;
  //			} else {
  //				fac[i] = i * fac[i - 1];
  //			}
  //			if (fac[i] > mod) {
  //				fac[i] = fac[i] % mod;
  //			}
  //		}
  //	}
  //
  //	private static int getLower(Long long1, Long[] st) {
  //		// TODO Auto-generated method stub
  //		int left = 0, right = st.length - 1;
  //		int ans = -1;
  //		while (left <= right) {
  //			int mid = (left + right) / 2;
  //			if (st[mid] <= long1) {
  //				ans = mid;
  //				left = mid + 1;
  //			} else {
  //				right = mid - 1;
  //			}
  //		}
  //		return ans;
  //	}
  
  //	private static long getGCD(long l, long m) {
  //
  //		long t1 = Math.min(l, m);
  //		long t2 = Math.max(l, m);
  //		while (true) {
  //			long temp = t2 % t1;
  //			if (temp == 0) {
  //				return t1;
  //			}
  //			t2 = t1;
  //			t1 = temp;
  //		}
  //	}