import std.bigint,std.conv,std.stdio,std.string;int M=10^^9+7,S=4000000,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| ` !/<4.V.^Y,'%M^!94!^%%=19Q\ #LDG!*&U)R,_QD@;?!1K)(_ 32="G1CPSE+M8OAM.@!C]7F-4>YT=]K$Y-(7 %\S!30]LA$S];R+K0=A,L2[IH90UC+H^40$^?+-@I+>H1J+7ULGC="&2O"#3:-(OWH#+%A-[^!W)+,G.,.M>=5O'95_5S\P"T\L&[@1I.A6FH%K%^*(&YF!5UI89$PL@N&)(7Z&=+9N($XD\5#XC3KXH4WDXZ3+UCMNI8_OK.[$#DBFKM*)"4^1V;@*5:GTL&%+Z;<+>&1Y^1U0')N%C)G!V*QT<,&R!O2%@<3_@PG3S O02:]PWXZ;0MQX,'4K*G_=Q@768+1X]4P5[O-#R2;03:N?O9&)1<\7:YVC\G!)6]H/Z&M09<==/#:&EL]JV[JX[:7# _,N%Z:F6=HI^&1J!E3KKJ\/,Z@]HKM<=;` [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; }}