import std.bigint,std.conv,std.stdio,std.string;int M=10^^9+7,S=4700000,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|` !=T^$N7Q ?5" )351S;,]M&3G V/FYT#,%LJCD5SSZ9K6 ?G1ANE)NYLP*D.F.Y>YJHN.[*%)G!C=C.HI'3-93/[F4F!:C9XZWV'^F[5S6-NT"_;'KP<_YLGC="#!\?[*C7?H!AB0Q#L#4XPD8UM"ZPID@R+"] ^WQSY6%:MW[-F.*JBC$S/8B$BFDG@NC50Q@-P_#V=8'"=GJV3U0(%)(3$=&)CWZ$KM&^/JZ1XN9NS?\'Z6+&NT*%EGA4 0!Q.=#M@D7IZ$PX,18W_X?.;X;6P@MFHN)?Q7H(LOU4VQ]["+/3&UP.2 MMFEV"FT9U%#.U(ET;03<6VHN R&X) J(0PR@G#"(8!,CC :ZW8N)/QJ$SJND6DK\1,U1*Q'G>B^V#!E%CV)5W$;?)J5"A>.8'V,7/CKQ=1HYM/U.O?S8@E".=O2$@1@A\G$V`[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;}}