#define rep(i,n) for(int i=0;i<(int)(n);i++) #define ALL(v) v.begin(),v.end() typedef long long ll; #include using namespace std; const int MOD=1e9+7; ll modpow(ll x,ll n){ ll ans=1; while(n){ if(n&1) ans=ans*x%MOD; x=x*x%MOD; n/=2; } return ans; } int main(){ ios::sync_with_stdio(false); std::cin.tie(nullptr); ll e=modpow(2,MOD-2),f=modpow(6,MOD-2); int t; cin>>t; while(t--){ ll n,m; cin>>n>>m; ll c=modpow(n,m),d=modpow(n,m/2); ll a=c*(c+1)%MOD*e%MOD; ll b=d*(d+1)%MOD*(2*d%MOD+1)%MOD*f%MOD; cout<<(a-b+MOD)%MOD<