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.BufferedWriter;
      import java.math.*;
      import java.io.IOException;
      import java.io.InputStreamReader;
      import java.io.OutputStream;
      import java.io.OutputStreamWriter;
      import java.io.PrintWriter;
      import java.io.Writer;
      import java.math.BigInteger;
      import java.util.*;
      import java.util.Map.Entry;
  
       
  								///////////////		/////////  	  //////////
  							          ///		   //     //   	    //     //
  								     ///		  //     //    	   //     //
  									///			 //     //    	  //     //
  								   ///			//     //    	 //     //
  								  ///		   //     //    	//	   //
  							///  ///		  //     //    	   //	  //
  							 /////			 ///////// 	 	//////////
  
      
      public class CodeForces
      {
      	final static String no = "NO";
      	final static String yes = "YES";
      	static int count = 0;
      	static public OutputWriter w = new OutputWriter(System.out);
      	static public FastScanner sc = new FastScanner();
      	class Pair{
      		int x ,y;
      		public Pair(int x,int y) {
      			this.x = x;
      			this.y = y;
      		}
      	}
      	
      	
      	
          public static void main(String[] args) {
          	int t = sc.nextInt();
          	in:
          	while(t-->0) {
          		int n = sc.nextInt();
          		int k = sc.nextInt();
          		long ans = 0;
          		for(int i=0;i<31;i++) {
          			if((k>>i)%2==1) {
          				ans += pow(n,i);
          			}
          			ans%=(long)1e9+7;
          		}
          		w.writer.println(ans);
          	}
          	w.writer.flush();
          	
          }
          public static long pow(long a,long p)
          {
              long res=1;
              while(p>0)
              {
                  if(p%2==1)
                  {
                      p--;
                      res*=a;
                      res%=(long)1e9+7;
                  }
                  else
                  {
                      a*=a;
                      a%=(long)1e9+7;
                      p/=2;
                  }
              }
              return res;
          }
          static int gcd(int a, int b){
              if (a == 0)
                  return b;
              return gcd(b % a, a);
          }
          static long gcd(long a, long b){
              if (a == 0)
                  return b;
              return gcd(b % a, a);
          }
           
          // method to return LCM of two numbers
          static long lcm(long a, long b)
          {
              return (a / gcd(a, b)) * b;
          }
          static long getPairsCount(long[] arr, long sum)
          {
              Map<Long, Long> hm = new HashMap<>();
              int n = arr.length;
              long count = 0;
              for(int i =0;i<n;i++) {
              	if(hm.containsKey(sum - arr[i])) {
              		count+=hm.get(sum-arr[i]);
              	}
              	if(hm.get(arr[i])!=null) {
              		hm.put(arr[i], hm.get(arr[i])+1);
              	}
              	else {
              		hm.put(arr[i], 1l);
              	}
              }
              return count;
          }
          static long sumLong(int[]arr) {
          	long sum =0;
          	for(int x:arr) {
          		sum+=x;
          	}
          	return sum;
          }
          static boolean isEqualList(ArrayList<Integer> arr,ArrayList<Integer> temp){
              for(int i =0;i<arr.size();i++){
                  if(arr.get(i)!=temp.get(i)){
                      return true;
                  }
              }
              return false;
          }
      	static void sort(int[] arr) {
              Random rand = new Random();
              int n = arr.length;
              for (int i = 0; i < n; i++) {
                  int idx = rand.nextInt(n);
                  if (idx == i) continue;
                  arr[i] ^= arr[idx];
                  arr[idx] ^= arr[i];
                  arr[i] ^= arr[idx];
              }
              Arrays.sort(arr);
          }
       
          static void sort(long[] arr) {
              Random rand = new Random();
              int n = arr.length;
              for (int i = 0; i < n; i++) {
                  int idx = rand.nextInt(n);
                  if (idx == i) continue;
                  arr[i] ^= arr[idx];
                  arr[idx] ^= arr[i];
                  arr[i] ^= arr[idx];
              }
              Arrays.sort(arr);
          }
       
          static void sortDec(int[] arr) {
              Random rand = new Random();
              int n = arr.length;
              for (int i = 0; i < n; i++) {
                  int idx = rand.nextInt(n);
                  if (idx == i) continue;
                  arr[i] ^= arr[idx];
                  arr[idx] ^= arr[i];
                  arr[i] ^= arr[idx];
              }
              Arrays.sort(arr);
              int l = 0;
              int r = n - 1;
              while (l < r) {
                  arr[l] ^= arr[r];
                  arr[r] ^= arr[l];
                  arr[l] ^= arr[r];
                  l++;
                  r--;
              }
          }
       
          static void sortDec(long[] arr) {
              Random rand = new Random();
              int n = arr.length;
              for (int i = 0; i < n; i++) {
                  int idx = rand.nextInt(n);
                  if (idx == i) continue;
                  arr[i] ^= arr[idx];
                  arr[idx] ^= arr[i];
                  arr[i] ^= arr[idx];
              }
              Arrays.sort(arr);
              int l = 0;
              int r = n - 1;
              while (l < r) {
                  arr[l] ^= arr[r];
                  arr[r] ^= arr[l];
                  arr[l] ^= arr[r];
                  l++;
                  r--;
              }
          }
      	
      	
      	static int[] countInversions(int[]arr,int low,int high){
      		if(low==high) {
      			int base[] = new int[1];
      			base[0] = arr[low];
      			return base;
      		}
      		int mid = low+(high-low)/2;
      		int left[] = countInversions(arr,low,mid);
      		int right[] = countInversions(arr,mid+1,high);
      		int[]merged = merge(left,right);
      		return merged;
      	}
      	
      	
      	static int[] merge(int first[], int[]next) {
      		
      		int i = 0;
      		int j = 0;
      		int k = 0;
      		int[]merged = new int[first.length+next.length];
      		while(j<first.length && k<next.length) {
      			if(first[j]>=next[k]) {
      				merged[i++] = first[j++];
      			}else {
      				merged[i++] = next[k++];
      				count += first.length -j;
      			}
      		}
      		while(j<first.length) {
      			merged[i++] = first[j++];
      		}
      		while(k<next.length) {
      			merged[i++] = next[k++];
      		}
      		
      		return merged;
      	}
      	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();
      		}
      		
      		int nextInt() {
      			return Integer.parseInt(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 [] longArray(int n) {
      			long[] a=new long[n];
      			for(int i=0 ; i<n ; i++) a[i]=nextLong();
      			return a;
      		}
      		double nextDouble() {
      			return Double.parseDouble(next());
      		}
      	}
          static public class OutputWriter {
              private  final PrintWriter writer;
       
              public OutputWriter(OutputStream outputStream) {
                  writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
              }
       
              public OutputWriter(Writer writer) {
                  this.writer = new PrintWriter(writer);
              }
       
              public void print(Object... objects) {
                  for (int i = 0; i < objects.length; i++) {
                      if (i != 0)
                          writer.print(' ');
                      writer.print(objects[i]);
                  }
              }
       
              public void printLine(Object... objects) {
                  print(objects);
                  writer.println();
              }
       
              public void close() {
                  writer.close();
              }
          }
       
       
      }