As we all know, F.C. Barcelona is the best soccer team of our era! Their entangling and mesmerizing game style usually translates into very high ball possession, consecutive counter-attack plays and goals. Lots of goals, thanks to the natural talent of their attacker and best player in history, Lionel Andres Messi.
However, at the most prestigious tournament of individual teams, the UEFA Champions League, there are no guarantees and believe it or not, Barcelona is in trouble.... They are tied versus Chelsea, which is a very defending team that usually relies on counter-strike to catch opposing teams off guard and we are in the last minute of the match. So Messi decided to settle things down for good and now he is conducting the ball on his teams' midfield and he will start a lethal counter-attack :D
After dribbling the 2 strikers from Chelsea, he now finds himself near the center of the field and he won't be able to dribble the entire team on his own, so he will need to pass the ball to one of his teammates, run forward and receive the ball once again to score the final goal.
Exactly K players are with him on his counter-attack and the coach, Tito Villanova knows that this counter-attack will end in a goal only if after exactly N passes are performed between the players, Messi ends up with the ball.
(Note that the ball only needs to end with Messi after exactly N passes are performed between all the K+1 players, i.e. Messi can receive the ball several times during the N passes. See the 2nd test case explanation for further clarification. ) 
However, he realized that there are many scenarios possible for this, so he asked you, his assistant coach, to tell him in how many ways can Messi score the important victory goal. So help him!!

-----Input-----
Input will contain a number T denoting the number of test cases.
Then T test cases follow, each one consisting of two space-sparated integers N and K.

-----Output-----
For each test case, output a single integer, the number of ways the winning play might happen modulo 1000000007 (109+7).

-----Constraints-----

- 1 ≤ T ≤ 100
- 2 ≤ N ≤ 1000
- 1 ≤ K ≤ 10

-----Example-----
Input:
2
2 4
4 2

Output:
4
6

-----Explanation-----
In the first test case, say four players with Messi are Xavi, Busquets, Iniesta and Jordi Alba. Then the ways of the winning play to happen when exactly  2 passes are to be performed are:
1) Messi - Xavi - Messi
2) Messi - Busquets - Messi
3) Messi - Iniesta - Messi
4) Messi - Alba - Messi

In the second test case, also say that two players with Messi are Xavi and Iniesta. There are 6 ways for the winning play to happen when exactly 4 passes are performed. All the examples of such winning play are:
1) Messi - Xavi - Messi - Iniesta - Messi
2) Messi - Xavi - Iniesta - Xavi - Messi
3) Messi - Xavi - Messi - Xavi - Messi
4) Messi - Iniesta - Messi - Iniesta - Messi
5) Messi - Iniesta - Messi - Xavi - Messi
6) Messi - Iniesta - Xavi - Iniesta - Messi

Starter code:

Solutions:
T = int(input())
for _ in range(T):
 p,n=map(int,input().split())
 mod = 1000000007
 if p == 2:
  print(n)
 else:
  f=n
  t=n
  for i in range(p-2):
   f=(f%mod*n)%mod
   a=(f-t+mod)%mod
   t=a
  print(a)

T = int(input())
for _ in range(T):
 p,n=map(int,input().split())
 mod = 1000000007
 if p == 2:
  print(n)
 else:
  f=n
  t=n
  for i in range(p-2):
   f=(f%mod*n)%mod
   a=(f-t+mod)%mod
   t=a
  print(a)

# cook your dish here

for _ in range(int(input())):
 n, k = [int(i) for i in input().split()]
 A = [[1],[0]]
 for i in range(1, n + 1):
  A[0].append(k * A[1][i - 1])
  A[1].append(A[0][i - 1] + (k - 1) * A[1][i - 1])
 print(A[0][n] % (10**9+7))





e=10**9+7
t=int(input())
for _ in range(t):
 n,k=list(map(int,input().split()))
 messi=0
 non_messi=k
 for i in range(n-2):
  z = non_messi
  non_messi =(messi*k+z*(k-1))%e
  messi=z
 
 print(non_messi)


# cook your dish here
s=pow(10,9)+7
a=[[0 for i in range(11)]for j in range(1001)]
for i in range(1,11):
 a[2][i]=i
for n in range(3,1001):
 for k in range(1,11):
  a[n][k]=pow(k,n-1,s)-a[n-1][k]
  if(a[n][k]<0):
   a[n][k]+=s
t=int(input())
for i in range(t):
 inp = list(map(int,input().split()))
 n=inp[0]
 k=inp[1]
 print(a[n][k])

t=int(input())
while(t>0):
 n,k=map(int,input().split())
 j=(k**n+k*((-1) ** n))//(k+1)

 j=j%1000000007
 print(j)
 t-=1

# cook your dish here
t=int(input())
for i in range(t):
 n,k=list(map(int,input().split(" ")))
 sum=0
 z=0
 while(n>1):
  sum+=(k**(n-1))*((-1)**z)
  n-=1
  z+=1
 print(sum%1000000007)



t=int(input())
while(t>0):
 n,k=list(map(int,input().split()))
 j=(k**n+k*((-1) ** n))//(k+1)
 # j=(k**n+k*((-1)**n))//k+1
 #print(j)
 j=j%1000000007
 print(j)
 t-=1


mod = int(1e9+7)

for _ in range(int(input())):
 n, k = map(int, input().split())
 ans = (k ** n + k * ((-1) ** n)) // (k + 1)
 print(ans % mod)

mod = int(1e9+7)

for _ in range(int(input())):
 n, k = map(int, input().split())
 ls = [0] * (n + 1)
 ls[0], ls[1] = 1, 0
 for i in range(2, n+1):
  ls[i] = ((k-1) * ls[i-1] + k * ls[i-2]) % mod
 print(ls[n])

t=int(input())

for i in range(t):
 arr = input()
 l = list(map(int,arr.split(' ')))
 n=l[0]
 k=l[1]
 f=[]
 f.append(k)
 f.append(k*(k-1))
 if(n==2):
  print(f[0]%1000000007)
 if(n==3):
  print(f[-1]%1000000007)
 if(n>3):
  for j in range(n-3):
   f.append((f[-2]*k)+((k-1)*f[-1]))
   #print(f)
  print(f[-1]%1000000007)