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.util.*;
  import java.io.*;
  
  public class Main {
  	static Scanner sc = new Scanner(System.in);
  	static PrintWriter pw = new PrintWriter(System.out);
     static int mod=(int)1e9+7;
  	public static void main(String[] args) throws IOException, InterruptedException {
          int t=sc.nextInt();
          while(t-->0) {
          long n=sc.nextLong(),k=sc.nextLong();
          long ans=0;
          String s=Long.toBinaryString(k);
          for(int i=s.length()-1,c=0;i>-1;i--,c++) {
          	if(s.charAt(i)=='1') {
          		ans+=(int)mod(n,c);
          		ans%=mod;
          	}
          }
          	pw.println(ans);
          }
  
  		pw.flush();
  	}
      public static long mod(long n,long pow) {
      	long ans=1;
      	for(int i=0;i<pow;i++) {
      		ans*=n;
      		ans%=mod;
      	}
      	return ans;
      }
  	static class pair implements Comparable<pair> {
  		long x;
  		long y;
   
  		public pair(long x, long y) {
  			this.x = x;
  			this.y = y;
  		}
   
  		public String toString() {
  			return x + " " + y;
  		}
   
  		public boolean equals(Object o) {
  			if (o instanceof pair) {
  				pair p = (pair) o;
  				return p.x == x && p.y == y;
  			}
  			return false;
  		}
   
  		public int hashCode() {
  			return new Long(x).hashCode() * 31 + new Long(y).hashCode();
  		}
   
  		public int compareTo(pair other) {
  			if (this.x == other.x) {
  				return Long.compare(this.y, other.y);
  			}
  			return Long.compare(this.x, other.x);
  		}
  	}
   
  	static class tuble implements Comparable<tuble> {
  		int x;
  		int y;
  		int z;
   
  		public tuble(int x, int y, int z) {
  			this.x = x;
  			this.y = y;
  			this.z = z;
  		}
   
  		public String toString() {
  			return x + " " + y + " " + z;
  		}
   
  		public int compareTo(tuble other) {
  			if (this.x == other.x) {
  				if (this.y == other.y) {
  					return this.z - other.z;
  				}
  				return this.y - other.y;
  			} else {
  				return this.x - other.x;
  			}
  		}
  	}
   
  
  	static class PairPoint {
  		pair p1;
  		pair p2;
  
  		public PairPoint(pair p1, pair p2) {
  			this.p1 = p1;
  			this.p2 = p2;
  
  		}
  
  		public String toString() {
  			return p1.toString() + p2.toString();
  		}
  
  		public boolean equals(PairPoint pp) {
  			if (p1.equals(pp.p1) && p2.equals(pp.p2))
  				return true;
  			return false;
  		}
  
  	}
  
  	static class Scanner {
  		StringTokenizer st;
  		BufferedReader br;
  
  		public Scanner(InputStream s) {
  			br = new BufferedReader(new InputStreamReader(s));
  		}
  
  		public Scanner(FileReader r) {
  			br = new BufferedReader(r);
  		}
  
  		public String next() throws IOException {
  			while (st == null || !st.hasMoreTokens())
  				st = new StringTokenizer(br.readLine());
  			return st.nextToken();
  		}
  
  		public String readAllLines(BufferedReader reader) throws IOException {
  			StringBuilder content = new StringBuilder();
  			String line;
  
  			while ((line = reader.readLine()) != null) {
  				content.append(line);
  				content.append(System.lineSeparator());
  			}
  
  			return content.toString();
  		}
  
  		public int nextInt() throws IOException {
  			return Integer.parseInt(next());
  		}
  
  		public long nextLong() throws IOException {
  			return Long.parseLong(next());
  		}
  
  		public String nextLine() throws IOException {
  			return br.readLine();
  		}
  
  		public double nextDouble() throws IOException {
  			String x = next();
  			StringBuilder sb = new StringBuilder("0");
  			double res = 0, f = 1;
  			boolean dec = false, neg = false;
  			int start = 0;
  			if (x.charAt(0) == '-') {
  				neg = true;
  				start++;
  			}
  			for (int i = start; i < x.length(); i++)
  				if (x.charAt(i) == '.') {
  					res = Long.parseLong(sb.toString());
  					sb = new StringBuilder("0");
  					dec = true;
  				} else {
  					sb.append(x.charAt(i));
  					if (dec)
  						f *= 10;
  				}
  			res += Long.parseLong(sb.toString()) / f;
  			return res * (neg ? -1 : 1);
  		}
  
  		public long[] nextlongArray(int n) throws IOException {
  			long[] a = new long[n];
  			for (int i = 0; i < n; i++)
  				a[i] = nextLong();
  			return a;
  		}
  
  		public Long[] nextLongArray(int n) throws IOException {
  			Long[] a = new Long[n];
  			for (int i = 0; i < n; i++)
  				a[i] = nextLong();
  			return a;
  		}
  
  		public int[] nextIntArray(int n) throws IOException {
  			int[] a = new int[n];
  			for (int i = 0; i < n; i++)
  				a[i] = nextInt();
  			return a;
  		}
  
  		public Integer[] nextIntegerArray(int n) throws IOException {
  			Integer[] a = new Integer[n];
  			for (int i = 0; i < n; i++)
  				a[i] = nextInt();
  			return a;
  		}
  
  		public boolean ready() throws IOException {
  			return br.ready();
  		}
  
  	}
  
  }
  