#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
long long modular_pow(long long, long long);
int /*<<< orphan*/ printf(char *, long long);
int /*<<< orphan*/ scanf(char *, long long *, ...);
int main(void) {
  long long int t, n, k, p = 1;
  ;
  scanf("%lld", &t);
  while (t--) {
    p = 1;
    scanf("%lld%lld", &n, &k);
    p = modular_pow(k - 1, n - 1);
    p = (p % 1000000007 * k % 1000000007) % 1000000007;
    printf("%lld\n", p);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x1,-0x28(%rbp)
lea    0xeb6(%rip),%rdi
lea    -0x10(%rbp),%rsi
mov    $0x0,%al
call   1050 <scanf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x10(%rbp)
cmp    $0x0,%rax
je     11ef <main+0xbf>
movq   $0x1,-0x28(%rbp)
lea    0xe7f(%rip),%rdi
lea    -0x18(%rbp),%rsi
lea    -0x20(%rbp),%rdx
mov    $0x0,%al
call   1050 <scanf@plt>
mov    -0x20(%rbp),%rdi
sub    $0x1,%rdi
mov    -0x18(%rbp),%rsi
sub    $0x1,%rsi
call   1040 <modular_pow@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    $0x3b9aca07,%ecx
cqto
idiv   %rcx
mov    %rdx,%rax
imul   -0x20(%rbp),%rax
mov    $0x3b9aca07,%ecx
cqto
idiv   %rcx
mov    %rdx,%rax
mov    $0x3b9aca07,%ecx
cqto
idiv   %rcx
mov    %rdx,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xe24(%rip),%rdi
call   1030 <printf@plt>
jmp    1159 <main+0x29>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x20,%rsp
lea    0xec0(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
subq   $0x1,0x10(%rsp)
jb     11ff <main+0xcf>
lea    0xe9d(%rip),%r14
lea    0x18(%rsp),%r15
lea    0x8(%rsp),%r12
movabs $0x89705f3112a28fe5,%rbx
5f 70 89
lea    0xe8b(%rip),%r13
xchg   %ax,%ax
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1050 <scanf@plt>
mov    0x8(%rsp),%rdi
add    $0xffffffffffffffff,%rdi
mov    0x18(%rsp),%rsi
add    $0xffffffffffffffff,%rsi
call   1040 <modular_pow@plt>
mov    %rax,%rsi
imul   %rbx
add    %rsi,%rdx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x1d,%rdx
add    %rax,%rdx
imul   $0x3b9aca07,%rdx,%rax
sub    %rax,%rsi
imul   0x8(%rsp),%rsi
mov    %rsi,%rax
imul   %rbx
add    %rsi,%rdx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x1d,%rdx
add    %rax,%rdx
imul   $0x3b9aca07,%rdx,%rax
sub    %rax,%rsi
mov    %r13,%rdi
call   1030 <printf@plt>
subq   $0x1,0x10(%rsp)
jae    1180 <main+0x50>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%lld\00"
@.str.1 = [9 x i8] c"%lld%lld\00"
@.str.2 = [6 x i8] c"%lld\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
store i32 0, i32* %1
store i64 1, i64* %5
%6 = call i32 (i8*, i64*, ...) @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64* %2)
br label %7
7: ; preds = %11, %0
%8 = load i64, i64* %2
%9 = add nsw i64 %8, -1
store i64 %9, i64* %2
%10 = icmp ne i64 %8, 0
br i1 %10, label %11, label %26
11: ; preds = %7
store i64 1, i64* %5
%12 = call i32 (i8*, i64*, ...) @scanf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i64* %3, i64* %4)
%13 = load i64, i64* %4
%14 = sub nsw i64 %13, 1
%15 = load i64, i64* %3
%16 = sub nsw i64 %15, 1
%17 = call i64 @modular_pow(i64 %14, i64 %16)
store i64 %17, i64* %5
%18 = load i64, i64* %5
%19 = srem i64 %18, 1000000007
%20 = load i64, i64* %4
%21 = mul nsw i64 %19, %20
%22 = srem i64 %21, 1000000007
%23 = srem i64 %22, 1000000007
store i64 %23, i64* %5
%24 = load i64, i64* %5
%25 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 %24)
br label %7
26: ; preds = %7
ret i32 0
}

LLVM IR (-O2):
@.str = [5 x i8] c"%lld\00"
@.str.1 = [9 x i8] c"%lld%lld\00"
@.str.2 = [6 x i8] c"%lld\0A\00"
define i32 @main() {
%1 = alloca i64
%2 = alloca i64
%3 = alloca i64
%4 = bitcast i64* %1 to i8*
%5 = bitcast i64* %2 to i8*
%6 = bitcast i64* %3 to i8*
%7 = call i32 (i8*, i64*, ...) @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64* %1)
%8 = load i64, i64* %1
%9 = add nsw i64 %8, -1
store i64 %9, i64* %1
%10 = icmp eq i64 %8, 0
br i1 %10, label %26, label %11
11: ; preds = %0, %11
%12 = call i32 (i8*, i64*, ...) @scanf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i64* %2, i64* %3)
%13 = load i64, i64* %3
%14 = add nsw i64 %13, -1
%15 = load i64, i64* %2
%16 = add nsw i64 %15, -1
%17 = call i64 @modular_pow(i64 %14, i64 %16)
%18 = srem i64 %17, 1000000007
%19 = load i64, i64* %3
%20 = mul nsw i64 %18, %19
%21 = srem i64 %20, 1000000007
%22 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 %21)
%23 = load i64, i64* %1
%24 = add nsw i64 %23, -1
store i64 %24, i64* %1
%25 = icmp eq i64 %23, 0
br i1 %25, label %26, label %11
26: ; preds = %11, %0
ret i32 0
}