> File Name: 5863.cpp
> Author: jiangyuzhu
> Mail: 834138558@qq.com
> Created Time: 2016/10/10 19:58:36
************************************************************************/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int N = 15;
struct Matrix
{
int row,cal;
long long m[N][N];
};
Matrix init(Matrix a, long long t)
{
for(int i = 0; i < a.row; i++)
for(int j = 0; j < a.cal; j++)
a.m[i][j] = t;
return a;
}
Matrix operator * (const Matrix& a, const Matrix& b)
{
Matrix ans;
ans.row = a.row, ans.cal = b.cal;
ans = init(ans,0);
for(int i = 0; i < a.row; i++)
for(int j = 0; j < b.cal; j++)
for(int k = 0; k < a.cal; k++)
ans.m[i][j] = (ans.m[i][j] + a.m[i][k] * b.m[k][j])%mod;
return ans;
}
int m, k;
void print(Matrix I)
{
for(int i = 0; i < I.row; ++i){
for(int j = 0; j < I.cal; ++j){
printf("%d ", I.m[i][j]);
}
puts("");
}
}
ll gao(int m, long long b)
{
Matrix A;
A.row = A.cal = m + 1;
A = init(A, 0);
for(int i = 0; i <= m; ++i){
for(int j = 0; j <= m; ++j){
if(i == 0) A.m[i][j] = k * (k - 1);
else{
if(i == j + 1) A.m[i][j] = k;
}
}
}
Matrix I;
I.row = m + 1, I.cal = 1;
I = init(I, 0);
I.m[0][0] = 1;
for(; b; b >>= 1, A = A * A){
if(b & 1) I = A * I;
}
ll ans = 0;
for(int i = 0; i <= m; ++i){
ans = (ans + I.m[i][0]) % mod;
}
return ans;
}
int main (void)
{
int T;scanf("%d", &T);
while(T--){
int n;scanf("%d%d%d", &n, &m, &k);
printf("%lld\n", (gao(m, n) - gao(m - 1, n) + mod) % mod);
}
return 0;
}