Devu and his three Hands

All submissions for this problem are available.
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
Example
Input: 3 1 2 1 3 2 2 Output: 0 6 36
Explanation
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"}
Author:  admin2 
Tester:  kevinsogo 
Editorial  http://discuss.codechef.com/problems/DEVHAND 
Tags  admin2, arithmetic, combinatorics, may15, medium, modular 
Date Added:  7022015 
Time Limit:  3 sec 
Source Limit:  50000 Bytes 
Languages:  C, CPP14, JAVA, PYTH, PYTH 3.5, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, SCALA, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, PERL6, TEXT, SCM chicken, CLOJ, FS 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions
HELP
If you are still having problems, see a sample solution here. 