#include<iostream>
using namespace std;
const int o=1e9+7;
unsigned long long int func(int k)
{
unsigned long long int sum=0;
sum=(k%o)*((k+1)%o);
return sum;
}
int main()
{
long long int t;
cin>>t;
while(t--){
unsigned long long int n,k;
cin>>n>>k;
if(n==0){
k=k-1;
cout<<func(k)<<endl;
}
else{unsigned long long int time=0;
if(k%2==1)
{
k=k-1;
time=time+k%o;
}
unsigned long long int c=((k/2)+n-1)%o;
time=time+func(c)+n;
cout<<time<<endl;
}
}
return 0;
}