import std.bigint,std.conv,std.stdio,std.string;int M=10^^9+7,S=4600000,q;long y,z,a,b,e;void i(long n){y=1;for(e=M-2;e;e/=2,n=n*n%M)if(e&1)y=y*n%M;}void f(int n){if(n>M/2){f(M-1-n);i(z);z=n%2?y:M-y;}else{z=1;if(n%S>S/2){for(;n%S;)z=z*++n%M;i(z);}else{for(;n%S;)z=z*n--%M;y=z;}z=0;foreach(j;0..5)z=z<<6|` !9)KE"DUX8B>30# U[O$..MV)WVR>^QX>C$BJ'D\72-I[-7PZUN=C?V(:E'$E-"/-Z.6(0>0#WV?#W/MUVC%9GCZG_U6KYMS9R]T%E1Q.DFYHIM28MFJ;M[1.P^6;#?JE$(@?::$'?L8W6ZIV^F1L+(,>E.DN(JTJ$PIT7?[G:=[;-O3PYZMS+JG _ML+;',_:IQ \WFA&?S$J,#C7%/'GPM,Y+C1]^18/P<05]<2JUZ4.SAUZ[V[R/5:NU#0UDU(1EV\09<==3LJ$2 ';U4;#!]>3\+[IS$B\!K.KE`[n/S*5+j]-32;z=z*y%M;}}void main(){foreach(t;0..readln.chomp.to!int){auto s=readln.split;BigInt C=s[0],P=s[1],r=C+1-P,l=r-P;a=0;if(l>=0&&l/M==r/M){f(r%M);a=z;f(l%M);i(z);a=a*y%M;}a.writeln;}}