import java.io.PrintWriter;import java.util.Scanner;class Z{static int M=1000000007;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 char[]e="! CLc!-|k$k-d{e$#OZL0 @x7?$`e4/&1j50)i?(C,'?jW-<0FN'#*7}$5sMq HFc%#ZWcR!9ZM^'c{u|'5.ev*['x]&S^>Z'dhrT%1{-k%2KLX gZ=E-Np6e u*O.({>=H&U3#T'`'Fa!.J=2*^M2O-vQ+]-VT.,&!2($&4kSc$T#^y#PB}j*]r-l*]DSa$M 'W*4/2<&r[;*)ii|n(c'BS*RBRj(E/Q~*o]8--3)>X)VRm=&Fo39)CaTt *^/h(TC%(';3iS)4|of'+XSB'QK.%+>O4}*@@ Q-#>b#&-`PS!dhn@'E e)-f>GA Lrlo-VEw6#c|G1$gOrx%@O;s NF2>%$8I:)=)jz*Eus<-!WtU!_vvC$K-*}- (u>!6^]K%S;H{*7wa+.E.T8,9kR=*yh6K #|^/,^Q54+=u5K$?DRO#GRE!) Sn]%CR'+%wJ6-%^o{u%)o2L%Bw&;+ofo&2D0h&:D&D*&a_u)!V^ %x'X&+PU17$+**t(R?lT#Y-3+Cb ,h*p,6 .[ pZYP%J>Qn,`%'Q&ubw%$QV%',7tJi!:U5$+QZB9!?/Na%)%Sk#_G G !VF~!$yya)#=b{,-]vR,IX0R-~tUH,z@jK,VLO>!9y;s&>y ?'./PR+Buxe)lf6l+/4Em+(`AW'^$2t!^Y%c&ry3d,DyZc,puBB)8Q>z yzI['*^` ,OTf6(m*cj,E}{r'-KS;#@zt|,w#Zm!Z;9I#1h[F!#(*@$mf7A)e96!,dvk#'7tpV$-1Eu)dW&G%tk~T)H0xD**wMc-([@%&bRM%'|6Aw+tM{8)SR_0+0){long c=a/b,d=a;a=b;b=d%b;d=p;p=q;q=d-c*q;}return p<0?p+M:p;}static long f(int n){int I=3400000,h=n%I,i;long u;if(hn;u=u*i%M,i--);return i(u)*d(e,n/I*5+5)%M;}}public static void main(String[]g)throws Exception{Scanner y=new Scanner(System.in);PrintWriter o=new PrintWriter(System.out);for(int T=Integer.parseInt(y.next());T>=1;T--){String C=y.next(),P=y.next();if(P.length()>=11){o.println(0);}else{long q=Long.parseLong(P),d=0;if(q>=M){o.println(0);}else{if(C.length()>=12){for(char x:C.toCharArray())d=(d*10+x-'0')%M;}else{d=Long.parseLong(C);if(d-(2*q-1)+1<=0){o.println(0);continue;}}long U=(d-(q-1)+M)%M,F=(d-(2*q-1)+M+M)%M;o.println(U