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.*;
  import java.util.*;
  
  
  public class CodeForcesTest {
  	static class Pair{
  		int f;
  		int s;
  		
  		Pair(int f,int s){
  			this.f=f;
  			this.s=s;
  		}
  	}
  	public static void main(String[] args) throws IOException {
          FastReader sc=new FastReader();
          BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(java.io.FileDescriptor.out), "ASCII"), 512);
  		int T=sc.nextInt();
  		long x = 1000000007;
          for(int t=0;t<T;t++){
  			int n = sc.nextInt(),k=sc.nextInt();
  			ArrayList<Integer> a = new ArrayList<>();
  			while(k!=0){
  				a.add(k);
  				k=k/2;
  			}
  			int m=1;
  			long r = 1;
  			for(int i=a.size()-2;i>=0;i--){
  				if(2*m==a.get(i)){
  					m=2*m;
  					r=(r%x*n%x)%x;
  				}
  				else{
  					m=2*m+1;
  					r=(r%x*n%x)%x+1;
  				}
  			}
  			System.out.println(r);
  		}  
  
          out.close();
  	}
  	
      static class FastReader 
  	{ 
  		BufferedReader br; 
  		StringTokenizer st; 
  
  		public FastReader() 
  		{ 
  			br = new BufferedReader(new InputStreamReader(System.in)); 
  		} 
  
  		String next() 
  		{ 
  			while (st == null || !st.hasMoreElements()) 
  			{ 
  				try
  				{ 
  					st = new StringTokenizer(br.readLine()); 
  				} 
  				catch (IOException e) 
  				{ 
  					e.printStackTrace(); 
  				} 
  			} 
  			return st.nextToken(); 
  		} 
  
  		int nextInt() 
  		{ 
  			return Integer.parseInt(next()); 
  		} 
  
  		long nextLong() 
  		{ 
  			return Long.parseLong(next()); 
  		} 
  
  		double nextDouble() 
  		{ 
  			return Double.parseDouble(next()); 
  		} 
  
  		String nextLine() 
  		{ 
  			String str = ""; 
  			try
  			{ 
  				str = br.readLine(); 
  			} 
  			catch (IOException e) 
  			{ 
  				e.printStackTrace(); 
  			} 
  			return str; 
  		}
  		int[] readArray(int n){
  			FastReader sc = new FastReader();
  			int[] a = new int[n];
  			for(int i=0;i<n;i++){
  				a[i]=sc.nextInt();
  			}
  			return a;
  		} 
  	} 
  	static int Mex(int[] a){
          Arrays.sort(a);
          if(a[0]!=1){
              return 1;
          }
          for(int i=0;i<a.length-1;i++){
              if(a[i]!=a[i+1] && a[i+1]>a[i]+1){
                  return a[i]+1;
              }
          }
          return a[a.length-1]+1;
      }
  	static int gcd(Integer a,Integer b)
      {
          if(b==0)
              return a;
          else
              return gcd(b,a%b);
      }
  	static boolean isPrime(long n)
      {
   
          // Check if number is less than
          // equal to 1
          if (n <= 1)
              return false;
   
          // Check if number is 2
          else if (n == 2)
              return true;
   
          // Check if n is a multiple of 2
          else if (n % 2 == 0)
              return false;
   
  		// If not, then just check the odds
  		double l = Math.sqrt(n);
          for (int i = 3; i <= l; i += 2) 
          {
              if (n % i == 0)
                  return false;
          }
          return true;
      }
  	static class Edge{
  		int to,weight;
  		public Edge(int to,int weight){
  			this.to=to;
  			this.weight=weight;
  		}
  	}
  }
  