package q3xx; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class Q371_4F { static InputStream is; static PrintWriter out; static String INPUT = ""; static final int INTERVAL = 2000000; static int d(char[] a,int l){int r=0,j=l+4;for(;j>=l;j--)r=r*93+a[j]-(a[j]+1880)/58;return r;} static final char[] enigma = "! cTce(f$*L$NT-6%r9PB$0fO,*B5:;!U#$7#BJlN%QosC)6;TU'[?%Tl`?&o=7y$Ljeu O;6[#'Ss%$p*y2+!6)q+s?#C 0txk qrT7+km`/!dhrT%1]bB)Et$m+f?Wn(rvjo%9duz(*5P`#Y'bg(YMs-'Jbas&.)$#-Pb%s,Y.cp* |8#FnUF-^M2O-Sm8W(foqX !Ux[ u+Hi$:m*9*~wb2 O>_A&PeI]&~#&p).l+8#ztE:(dS m(zZz$+U63@);^J8$r&_l 4/2<&&lB-#]+ |-'XbQ'v}'*!3M6r'K@y! R>{N$V+o>%Gx*y&n C(!v8ft)9gRZ#&T8,' !fp#-|n&,71T&*CaTt YRkY#f6V`%F(l+!SWqb w;s>(!%/a%Gg?{$N#Z= GL] +nUTL!!U8G!P1C8(G4pA%H[ I)?.7M-u{)3$aDqJ(r*JA,EB1C-0({a*@b~1$F6c0-vZ{V''sw/,a?_x%Ameu*iK*j+X#}<#{7ST!;66X$jws8$7nmI'5&@:!-`PS!PH+n!SjXO*&POu%r2|Y$6'='!J`eO!YL,D!x~#S%$)_J!kkwF'5?Km!06mu(V6;e%[Z,6,[7Ap*OVKv#$8I:)SyN)&O1+!-%j4e >yBY&5DG.#IAy7.+2A#'nVp`)(FWb'A8TQ AOA/'u#^0'Cd2Y)x][x)Z3: **WM`#E.T8,=yjg'bhfd!HEy~$Pq>e(lD:@+|`&f#RWPG --Pq%Rz{>'<[%q(1/5Y+vS#}'Qy~M%*4n='1'xI%])Y4'wJ6-%/M?h(zu(i-yaJ|,-YA2,XZw/&:rAa*S(5U*vOW*,CQ%w a4n&_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[ReN,W,gR*x+w@-xz9,!o'/v#tw C#S]}F!M |- wCg4%w/OL*v*;^-9Fhh$&J0H NDbv-Ny/8-w'p#-qz>q'v)IZ!MyWW'ToZ4#1X)9-m+!w*igG1(Au*%&~z'O,0Y;~%[pk ,u1y| $vb?,2Fy])t0%h#pwmI+(=h~(N[R@*]\r\n".toCharArray(); static void solve() { for(int T = ni();T >= 1;T--){ String C = ns(), P = ns(); if(P.length() >= 11){ out.println(0); continue; } long pl = Long.parseLong(P); if(pl >= mod){ out.println(0); continue; } long cl = 0; if(C.length() >= 12){ for(char x : C.toCharArray())cl = (cl*10+x-'0')%mod; }else{ cl = Long.parseLong(C); if(cl-(2*pl-1)+1 <= 0){ out.println(0); continue; } } long sup = (cl-(pl-1))%mod; long inf = (cl-(2*pl-1))%mod; if(inf < 0)inf += mod; if(sup < 0)sup += mod; if(sup < inf){ out.println(0); continue; } out.println(f((int)sup)*invl(f((int)inf), mod)%mod); } } public static long invl(long a, long mod) { long b = mod; long p = 1, q = 0; while (b > 0) { long c = a / b; long d; d = a; a = b; b = d % b; d = p; p = q; q = d - c * q; } return p < 0 ? p + mod : p; } static long f(int n) { int h = n%INTERVAL; if(h < INTERVAL/2){ long mul = d(enigma, n/INTERVAL*5); int base = n/INTERVAL*INTERVAL; for(int i = base+1;i <= n;i++){ mul = mul * i % mod; } return mul; }else{ long mul = 1; int base = n/INTERVAL*INTERVAL+INTERVAL; for(int i = base;i > n;i--){ mul = mul * i % mod; } return invl(mul,mod)*d(enigma, n/INTERVAL*5+5)%mod; } } static int mod = 1000000007; public static void main(String[] args) throws Exception { long S = System.currentTimeMillis(); is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); solve(); out.flush(); long G = System.currentTimeMillis(); tr(G-S+"ms"); } private static byte[] inbuf = new byte[1024]; static int lenbuf = 0, ptrbuf = 0; private static int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private static String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private static int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); } }