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: //package codeforces;
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.PrintWriter;
  import java.util.*;
  public class solution{ 
  	public static void main(String[] args){
      	FastScanner s=new FastScanner();
      	PrintWriter out=new PrintWriter(System.out);
      	int t=s.nextInt();
      	for(int tt=0;tt<t;tt++) {
      		
      		long n=s.nextLong(),k=s.nextLong();
      		
      		out.println(sol(n,k)%1000000007);
      	}
      	out.close();
  	}
  	public static long sol(long n,long k) {
  		
  		if(k==1) {
  			return (long)Math.pow(n, k-1)%1000000007;
  		}
  		if(k==2) {
  			return ((long)Math.pow(n, k-1))%1000000007;
  		}
  		int po=(int)(Math.log(k)/Math.log(2));
  		if((k&(k-1))==0) {
  			return (power(n, po,1000000007));
  		}else {
  			return ((power(n, po,1000000007))+sol(n,k-(long)Math.pow(2, po)))%1000000007;
  		}
  		
  	}
  	static long power(long x, long y, long p)
  	  {
  	    long res = 1; // Initialize result
  	 
  	    x = x % p; // Update x if it is more than or
  	    // equal to p
  	 
  	    if (x == 0)
  	      return 0; // In case x is divisible by p;
  	 
  	    while (y > 0)
  	    {
  	 
  	      // If y is odd, multiply x with result
  	      if ((y & 1) != 0)
  	        res = (res * x) % p;
  	 
  	      // y must be even now
  	      y = y >> 1; // y = y/2
  	      x = (x * x) % p;
  	    }
  	    return res;
  	  }
  	public static void printb(boolean ans) {
  		if(ans) {
  			System.out.println("Yes");
  		}else {
  			System.out.println("No");
  		}
  	}
  	static boolean isPrime(int n){
          if (n <= 1)
              return false;
          else if (n == 2)
              return true;
          else if (n % 2 == 0)
              return false;
          for (int i = 3; i <= Math.sqrt(n); i += 2){
              if (n % i == 0)
                  return false;
          }
          return true;
      }
  	static int gcd(int a, int b){
  	      if (b == 0)
  	        return a;
  	      return gcd(b, a % b);
  	    }
      static void sort(long [] a) {
  		ArrayList<Long> l=new ArrayList<>();
  		for (long i:a) l.add(i);
  		Collections.sort(l);
  		for (int i=0; i<a.length; i++) a[i]=l.get(i);
  	}
      static void sort(int [] a) {
  		ArrayList<Integer> l=new ArrayList<>();
  		for (int i:a) l.add(i);
  		Collections.sort(l);
  		for (int i=0; i<a.length; i++) a[i]=l.get(i);
  	}
      static class FastScanner {
  		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  		StringTokenizer st=new StringTokenizer("");
  		String next() {
  			while (!st.hasMoreTokens())
  				try {
  					st=new StringTokenizer(br.readLine());
  				} catch (IOException e) {
  					e.printStackTrace();
  				}
  			return st.nextToken();
  		}
  		double nextDouble() {
  			return Double.parseDouble(next());
  		}
  		int nextInt() {
  			return Integer.parseInt(next());
  		}
  		boolean nextBoolean() {
  			return Boolean.parseBoolean(next());
  		}
  		int[] readArray(int n) {
  			int[] a=new int[n];
  			for (int i=0; i<n; i++) a[i]=nextInt();
  			return a;
  		}
  		long nextLong() {
  			return Long.parseLong(next());
  		}
  		long[] readLongArray(int n) {
  			long[] a=new long[n];
  			for (int i=0; i<n; i++) a[i]=nextLong();
  			return a;
  		}
  	}
      static class Pair implements Comparable<Pair>{
      	int a,b ; 
      	Pair(int a,int b){
      		this.a=a;
      		this.b=b;
      	}
      	public int compareTo(Pair o) {
      		return a-o.a;
      	}
      }
  }
  