import sys input = sys.stdin.readline mod = 10**9+7 def matprod(A, B): x = len(A) y = len(B) z = len(B[0]) C = [[0]*z for _ in range(x)] for i in range(x): for j in range(y): for k in range(z): C[i][j] += A[i][k] * B[k][j] C[i][j] %= mod return C def matpow(A, n): if n == 1: return A if n%2 == 0: B = matpow(A, n/2) return matprod(B, B) return matprod(A, matpow(A, n-1)) def f(): n, m, x = map(int, input().split()) X = [[1, m], [m, 1]] Y = matpow(X, n) print(Y[0][x]) s = int(input()) for _ in range(s): f()