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 static java.lang.Math.max;
  import static java.lang.Math.min;
  import static java.lang.Math.abs;
  import static java.lang.System.out;
  import java.util.*;
  import java.io.*;
  import java.math.*;
  import java.util.Arrays;
  import java.util.Collections;
  
  public class b747
  {
  	public static void main(String args[]) throws IOException
  	{
  	    // long start = System.currentTimeMillis();		
  		int u = 0;
  		BufferedReader br = new BufferedReader( new InputStreamReader(System.in));
  		// System.out.println("Enter t");
  		int t = Integer.parseInt(br.readLine());
  		do
  		{
  			
  			// System.out.println("Enter n");
  			
  			long[] input = Arrays.stream(br.readLine().split("\\s+")).mapToLong(Long::parseLong).toArray();
              long n = input[0];
              long k = input[1];
  
             
              StringBuffer a1 = new StringBuffer();
              a1 = dec_to_bin(k);
              //long b = f(a,n);
  
  
  
  
              BigInteger b = new BigInteger(f(a1,n).toString());
              //b.add(f(a1,n));
              //System.out.println(a);
  
              //System.out.println(b);
              BigInteger q = new BigInteger("1000000007");
              System.out.println(b.mod(q));
  
  
  			
  			
  			
  
  
  
              
              u++;
              
          }
          while(u<t);
          // long end = System.currentTimeMillis();
          // System.out.println("time taken is "+ (end - start) + "ms");
  
  	}
  	public static StringBuffer dec_to_bin(long a)
  	{
  		long temp =a;
  		StringBuffer bin =new StringBuffer("");
  		int val =-1;
  		while(val!=0)
  		{
  			if(temp==0)
  			{
  				// bin = bin +"0";
  				bin.append("0");
  				val =0;
  			}
  			else if (temp==1)
  			{
  				// bin = bin+ "1";
  				bin.append("1");
  				val =0;
  			}
  			else
  			{
  				// bin = bin + String.valueOf(temp%2);
  				bin.append(Long.toString(temp%2));
  				temp = temp/2;//bin has the reverse of correct ans.
  				// System.out.println("temp is " +temp);
  			}
  			// System.out.println("val is " + val);
  		}
  		
  		// System.out.println("bin is " + bin);
  		
  		// StringBuilder input1 = new StringBuilder();
  		// input1.append(bin);
  		// String ans = input1.reverse().toString();
  		bin.reverse();
  		return bin;
  	}
  	//below method changes 0 to 1 and 1 to 0.
  	public static BigInteger f(StringBuffer s,long n)
  	{
  		
  		BigInteger ans = new BigInteger("0");
  		int j =0;
  		for(int i =s.length() -1;i>=0;i--)
  		{
  			if(s.charAt(i) == '0')
  			{
  				j++;
  			}
  			else if(s.charAt(i) == '1')
  			{
  				BigInteger a = new BigInteger("1");
  				for(int x =1;x<= j;x++)
  				{
  					a = a.multiply(BigInteger.valueOf(n));
  				}
  				ans = ans.add(a);
  
  				
  				j++;
  				//System.out.println(i);
  				
  			}
  			else
  			{
  				System.out.println("input string is not in binary , error from digit_complement method");
  			}
  		}
  		//System.out.println("ans is "+ ans);
  		return ans;
  	}
      
  	
  
  }