Read problems statements in Mandarin Chinese  and Russian. 

As you have probably realized up to now that Devu is not a normal guy, he is a very weird and abnormal guy. Normally people have two hands, but Devu has three of them. So he wears three wrist watches on his hands. 

Devu loves to write name of his friends on his wrist watches. So he want to attach a string corresponding to name of his friends on each wrist watch. He is a quite particular about natural beauty, so he will not attach strings such that one of the string on a watch is prefix (not necessarily proper prefix) of some other string on other watch. Also alphabet size of characters in the string is equal to first K English Alphabets. (i.e. 1 ≤ K ≤ 26).

Now Devu wonders in how many ways he can select three non empty strings of length less than or equal to N to attach with his wrist watches. As answer could be very large, he wants you to print answer modulo 10^{9} + 7.

------ Input ------ 

First line of test case contains a single integer T corresponding to the number of test cases.
For each test case, there will be a single line containing two space separated integers N, K. 

------ Output ------ 

For each test case, print a single line corresponding to the answer of the problem.

------ Constraints ------ 

$Subtask #1: (3 points)   1 ≤ T ≤ 9, 1 ≤ N ≤ 3, 1 ≤ K ≤ 3$
$Subtask #2: (20 points)  1 ≤ T ≤ 200, 1 ≤ N ≤ 100, 1 ≤ K ≤ 26$
$Subtask #3: (30 points)  1 ≤ T ≤ 200, 1 ≤ N ≤ 10^{5}, 1 ≤ K ≤ 26$
$Subtask #4: (47 points)  1 ≤ T ≤ 2000, 1 ≤ N ≤ 10^{9}, 1 ≤ K ≤ 26$

----- Sample Input 1 ------ 
3

1 2

1 3

2 2
----- Sample Output 1 ------ 
0

6

36
----- explanation 1 ------ 
Example #1. 
There is no valid arrangement of three strings to the watches, because at least one of the string will be equal to other which will violate the property stated in the problem. 

Example #2.  
There are 6 possible arrangements of the strings to the watches. 

{"a", "b", "c"} 
{"a", "c", "b"} 
{"b", "a", "c"} 
{"b", "c", "a"} 
{"c", "a", "b"} 
{"c", "b", "a"}

Source: codechef | Difficulty: VERY_HARD
Tags: Combinatorics

Mathematics


Solutions:
from itertools import *
MOD = 10 ** 9 + 7

def isPrefix(a, b):
	if len(a) > len(b):
		return False
	if a == b[:len(a)]:
		return True
	return False

def bruteforce(N, K):
	S = []
	for i in range(1, N + 1):
		for p in product(range(K), repeat=i):
			s = ''.join((chr(j + ord('a')) for j in p))
			S.append(s)
	ans = 0
	for a in S:
		for b in S:
			if isPrefix(a, b) or isPrefix(b, a):
				continue
			for c in S:
				if isPrefix(a, c) or isPrefix(c, a):
					continue
				if isPrefix(b, c) or isPrefix(c, b):
					continue
				print(a, b, c)
				ans += 1
	print(ans)
	return ans

def countPrefixesFor2(n, k):
	modinv = pow(k - 1, MOD - 2, MOD)
	tmp1 = modinv * k * (pow(k, n, MOD) + MOD - 1)
	tmp2 = modinv * n * (pow(k, n + 1, MOD) + MOD - 1)
	tmp3 = (modinv * (pow(k, n + 2, MOD) + MOD - 1) + MOD - (n + 1 + k) % MOD) * modinv
	tmp2 = tmp2 + MOD - tmp3
	return (tmp1 + 2 * tmp2) % MOD

def countPrefixesFor3(n, k):
	modinv = pow(k - 1, MOD - 2, MOD)
	ans = k * (pow(k, n, MOD) + MOD - 1) * modinv
	tmp01 = n * (pow(k, n + 1, MOD) + MOD - 1) * modinv
	tmp02 = modinv * ((pow(k, n + 2, MOD) + MOD - 1) * modinv + (2 * MOD - n - 1 - k))
	tmp02 %= MOD
	tmp0 = tmp01 + (MOD - tmp02)
	ans = (ans + 3 * tmp0) % MOD
	tmp1 = modinv * (pow(k, 2 * n, MOD) + MOD - pow(k, n + 1, MOD))
	tmp2 = MOD - 2 * (n - 1) * pow(k, n, MOD) % MOD
	tmp3 = k * (pow(k, n - 1, MOD) + MOD - 1) * modinv
	tmp4 = (tmp1 + tmp2 + tmp3) * k * k * modinv * modinv
	ans += 3 * tmp4
	ans %= MOD
	return ans

def bruteforce2(N, K):
	S = []
	for i in range(1, N + 1):
		for p in product(range(K), repeat=i):
			s = ''.join((chr(j + ord('a')) for j in p))
			S.append(s)
	ans = 0
	for a in S:
		for b in S:
			if isPrefix(a, b) or isPrefix(b, a):
				ans += 1
	print(ans, countPrefixesFor2(N, K))

def computeAns2(n, k):
	ans2 = 0
	for i in range(3, n + 1):
		tmp = (i - 2) * (i - 1) // 2 * k ** i
		ans2 += tmp
	return 6 * ans2
dp_fastAns2 = [[]]

def computeFastAns2():
	for k in range(1, 26 + 1):
		row = [0, 0, 0]
		tmp = k ** 2
		for n in range(3, 10 ** 5 + 1):
			tmp = tmp * k % MOD
			tmp2 = (n - 2) * (n - 1) // 2
			row.append((row[-1] + tmp * tmp2) % MOD)
		dp_fastAns2.append(row)

def computeAns2Fastest(n, k):
	modinv = pow(2, MOD - 2, MOD)
	tmp0 = modinv * zz(n, k)
	tmp1 = MOD - 3 * modinv * y(n, k) % MOD
	inv2 = pow(k - 1, MOD - 2, MOD)
	tmp3 = k * inv2 * (pow(k, n, MOD) + MOD - 1)
	return (tmp0 + tmp1 + tmp3) % MOD

def y(n, k):
	modinv = pow(k - 1, MOD - 2, MOD)
	ans = modinv ** 2 * k
	tmp = ((k - 1) * n + MOD - 1) * pow(k, n, MOD) + 1
	ans = ans * tmp % MOD
	return ans
	ans = 0
	for i in range(1, n + 1):
		ans += i * k ** i
	return ans

def z(n, k):
	ans = 0
	for i in range(1, n + 1):
		ans += i ** 2 * k ** i
	return ans / k

def zz(n, k):
	modinv = pow(k - 1, MOD - 2, MOD)
	ans = k * pow(modinv, 3, MOD)
	tmp1 = n ** 2 * pow(k, n + 2, MOD)
	tmp2 = MOD - (2 * n * (n + 1) - 1) * pow(k, n + 1, MOD) % MOD
	tmp3 = (n + 1) ** 2 * pow(k, n, MOD) + (MOD - k - 1)
	ans *= (tmp1 + tmp2 + tmp3) % MOD
	ans %= MOD
	return ans
	tmp = k * (n ** 2 * k ** (n + 2) - (2 * n * (n + 1) - 1) * k ** (n + 1) + (n + 1) ** 2 * k ** n - k - 1) // (k - 1) ** 3
	return tmp % MOD

def bruteforce3(n, k):
	S = []
	for i in range(1, n + 1):
		for p in product(range(k), repeat=i):
			s = ''.join((chr(j + ord('a')) for j in p))
			S.append(s)
	ans = 0
	ans2 = 0
	ans3 = 0
	for a in S:
		for b in S:
			for c in S:
				tmp = 0
				if isPrefix(a, b) or isPrefix(b, a):
					tmp += 1
				if isPrefix(a, c) or isPrefix(c, a):
					tmp += 1
				if isPrefix(b, c) or isPrefix(c, b):
					tmp += 1
				if tmp == 3:
					if a == b and a == c and (b == c):
						ans3 += 1
					elif a == b and a != c or (a == c and a != b) or (b == c and b != a):
						ans3 += 1
					else:
						print(a, b, c)
						ans2 += 1
				if not (isPrefix(a, b) or isPrefix(b, a) or isPrefix(a, c) or isPrefix(c, a) or isPrefix(b, c) or isPrefix(c, b)):
					ans += 1
	print(ans, f(n, k) + ans2)
	print('ans2:', ans2, ans2 / 6)
	print('cans2:', computeAns2(n, k))

def f(n, k):
	modinv = pow(k - 1, MOD - 2, MOD)
	tmp = k * (pow(k, n, MOD) + MOD - 1) * modinv
	ans = pow(tmp, 3, MOD)
	COUNT2 = countPrefixesFor2(n, k)
	tmp0 = 3 * COUNT2 * tmp % MOD
	ans = ans + MOD - tmp0
	ans += countPrefixesFor3(n, k)
	ans += 3 * COUNT2
	ans = ans + MOD - 2 * tmp % MOD
	ans += 6 * computeAns2Fastest(n, k)
	ans %= MOD
	return ans

def main():
	T = int(input())
	for t in range(T):
		(n, k) = [int(x) for x in input().split()]
		if k == 1:
			print(0)
		else:
			print(f(n, k))
main()


def powm(x, y):
	if y == 1:
		return x
	elif y == 0:
		return 1
	elif y % 2 == 0:
		ans = powm(x, y // 2)
		ans *= ans
		ans %= 1000000007
		return ans
	else:
		return powm(x, y - 1) * x % 1000000007

def findMulInv(n):
	nowCheck = 1000000007 + 1
	while nowCheck % n != 0:
		nowCheck += 1000000007
	return nowCheck // n

def solve():
	str = input().split()
	n = int(str[0])
	k = int(str[1])
	ans = 0
	if k != 1:
		muli = findMulInv(k - 1)
		ans += k * (-2 * (2 * n * n + 2 * n + 1) * powm(k, n + 1) + n * (2 * n + 1) * powm(k, n + 2) + (4 * n + 3) * powm(k, 2 * n + 1) - (4 * n + 1) * powm(k, 2 * n + 2) + powm(k, 3 * n + 2) + (2 * n * n + 3 * n + 1) * powm(k, n) - k - 1)
		ans -= powm(k, n + 1) * (-2 * n * powm(k, n + 1) - powm(k, n + 1) + 2 * n * powm(k, n + 2) - powm(k, n + 2) - k * k * n * n + k * k + 2 * k * n * n + k - n * n)
		ans -= k * ((2 * n + 1) * powm(k, n + 1) + n * powm(k, n + 2) - 2 * powm(k, 2 * n + 1) - (3 * n + 1) * powm(k, n) + k + 1)
		ans %= 1000000007
		ans *= muli ** 3
	ans %= 1000000007
	print(int(ans))
for tc in range(int(input())):
	solve()


mod = 1000000007

def minv(a):
	return pow(a, mod - 2, mod)

def ans(n, k):
	if k == 1:
		return 0
	m = (6 * n - 3) * k * k * k
	m += (6 * n - 12) * k * k
	m += (-6 * n - 15) * k
	m += -6 * n - 6
	m = m * pow(k, 2 * n + 2, mod)
	s = pow(k, 4) * (4 - 3 * n * n)
	s += k * k * k * (12 + 6 * n)
	s += k * k * (6 * n * n + 6 * n + 10)
	s += k * (-6 * n)
	s += -3 * n * n - 6 * n - 2
	s = s * pow(k, n + 1, mod)
	p = -1 * pow(k, 5) + 5 * pow(k, 3) + 6 * pow(k, 2) + 2 * k
	l = (m + s + p) % mod
	r = minv((k - 1) * (k - 1) * (k - 1) * (k + 1) * (k + 1))
	l = l * r % mod
	totr = (pow(k, n + 1, mod) - k) * minv(k - 1) % mod
	tot = pow(totr, 3, mod)
	return (tot - l) % mod
t = eval(input())
while t:
	(n, k) = map(int, input().split())
	print(ans(n, k))
	t -= 1