import sys import math def comb(x,y): n1=0 x2=x; while x2: n1 += x2/mo x2 /= mo y2=y n2=0 while y2: n2 += y2/mo y2 /= mo if n1 > n2: return 0 ret = 1 for i in range(y): ret = ret * (x-i) % mo return ret def hcomb(x,y): if x == 0: return 1 return comb(x+y-1,y) T=input() mo=1000000007 for i in range(T): C,P=map(int,raw_input().strip().split(" ")) if P*2-1>C: print 0 continue elif P==1: print C%mo continue else: ret = 0 ret += hcomb(C-(P+P-1),P-1) ret += hcomb(C-(P+P),P)*2 ret += hcomb(C-(P+P+1),P+1) print ret % mo