def Gauss(n): inv2 = pow(2, mod - 2, mod) return n * (n + 1) * inv2 % mod def Gauss2(n): inv6 = pow(6, mod - 2, mod) return n * (n + 1) * (2 * n + 1) * inv6 % mod T = int(input()) mod = 10 ** 9 + 7 for _ in range(T): N, M = map(int, input().split()) ans = Gauss(pow(N, M, mod)) - Gauss2(pow(N, M//2, mod)) print(ans % mod)