import math for _ in range(int(input())): a,n=map(int,input().split()) m=math.isqrt(a*n) c=[0]*(a+1) for i in range(1,a+1): c[i]=c[i-1]+(i*i-1)%a p,q=m//a,m%a ans=m*(2*m*m+3*m-5)//6-c[a]*p-c[q] print((n*m-ans//a)%(10**9+7))