#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 */
char getchar();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, ...);
int main() {
  int t, a, s, c, i;
  char ch = 'a';
  long long int b;
  scanf("%d", &t);
  while (t != 0) {
    while (ch != ' ') {
      a = ch - 48;
      ch = getchar();
    }
    scanf("%lld", &b);
    if (a == 0) {
      printf("0\n");
      t--;
      ch = 'a';
      continue;
    }
    if (b == 0) {
      printf("1\n");
      t--;
      ch = 'a';
      continue;
    }
    int last[4];
    s = 1;
    c = 0;
    for (i = 0; i < 4; i++) {
      s = (s * a) % 10;
      last[i] = s;
    }
    printf("%d\n", last[((b % 4) + 3) % 4]);
    t--;
    ch = 'a';
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
movb   $0x61,-0x19(%rbp)
lea    0xeb6(%rip),%rdi
lea    -0x8(%rbp),%rsi
mov    $0x0,%al
call   1050 <scanf@plt>
cmpl   $0x0,-0x8(%rbp)
je     127e <main+0x14e>
jmp    1164 <main+0x34>
movsbl -0x19(%rbp),%eax
cmp    $0x20,%eax
je     118a <main+0x5a>
movsbl -0x19(%rbp),%eax
sub    $0x30,%eax
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %al,-0x19(%rbp)
jmp    1164 <main+0x34>
lea    0xe72(%rip),%rdi
lea    -0x28(%rbp),%rsi
mov    $0x0,%al
call   1050 <scanf@plt>
cmpl   $0x0,-0xc(%rbp)
jne    11c6 <main+0x96>
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
movb   $0x61,-0x19(%rbp)
jmp    1155 <main+0x25>
cmpq   $0x0,-0x28(%rbp)
jne    11f1 <main+0xc1>
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
movb   $0x61,-0x19(%rbp)
jmp    1155 <main+0x25>
movl   $0x1,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x4,-0x18(%rbp)
jge    123b <main+0x10b>
mov    -0x10(%rbp),%eax
imul   -0xc(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %edx,-0x10(%rbp)
mov    -0x10(%rbp),%ecx
movslq -0x18(%rbp),%rax
mov    %ecx,-0x40(%rbp,%rax,4)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1206 <main+0xd6>
mov    -0x28(%rbp),%rax
mov    $0x4,%ecx
cqto
idiv   %rcx
mov    %rdx,%rax
add    $0x3,%rax
mov    $0x4,%ecx
cqto
idiv   %rcx
mov    -0x40(%rbp,%rdx,4),%esi
lea    0xda9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
movb   $0x61,-0x19(%rbp)
jmp    1155 <main+0x25>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
lea    0xeab(%rip),%rdi
lea    0x4(%rsp),%rsi
xor    %eax,%eax
call   1060 <scanf@plt>
cmpl   $0x0,0x4(%rsp)
je     128e <main+0x14e>
mov    $0x61,%al
lea    0xe8e(%rip),%r14
lea    0x8(%rsp),%r12
lea    0xe8d(%rip),%r15
lea    0xe80(%rip),%r13
jmp    11a7 <main+0x67>
mov    %r15,%rdi
call   1030 <puts@plt>
mov    0x4(%rsp),%ecx
add    $0xffffffff,%ecx
mov    %ecx,0x4(%rsp)
mov    $0x61,%al
test   %ecx,%ecx
je     128e <main+0x14e>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <getchar@plt>
cmp    $0x20,%al
jne    11a7 <main+0x67>
movsbl %bl,%ebp
mov    %r14,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1060 <scanf@plt>
add    $0xffffffd0,%ebp
je     118a <main+0x4a>
mov    0x8(%rsp),%rax
test   %rax,%rax
je     1282 <main+0x142>
movswl %bp,%ecx
imul   $0x6667,%ecx,%ecx
mov    %ecx,%edx
shr    $0x1f,%edx
sar    $0x12,%ecx
add    %edx,%ecx
add    %ecx,%ecx
lea    (%rcx,%rcx,4),%edx
mov    %ebp,%ecx
sub    %edx,%ecx
pinsrw $0x1,%ecx,%xmm0
imul   %ebp,%ecx
movswl %cx,%edx
imul   $0x6667,%edx,%edx
mov    %edx,%esi
shr    $0x1f,%esi
sar    $0x12,%edx
add    %esi,%edx
add    %edx,%edx
lea    (%rdx,%rdx,4),%edx
sub    %edx,%ecx
pinsrw $0x3,%ecx,%xmm0
imul   %ebp,%ecx
movswl %cx,%edx
imul   $0x6667,%edx,%edx
mov    %edx,%esi
shr    $0x1f,%esi
sar    $0x12,%edx
add    %esi,%edx
add    %edx,%edx
lea    (%rdx,%rdx,4),%edx
sub    %edx,%ecx
imul   %ecx,%ebp
movswl %bp,%edx
imul   $0x6667,%edx,%edx
mov    %edx,%esi
shr    $0x1f,%esi
sar    $0x12,%edx
add    %esi,%edx
add    %edx,%edx
lea    (%rdx,%rdx,4),%edx
pinsrw $0x5,%ecx,%xmm0
sub    %edx,%ebp
pinsrw $0x7,%ebp,%xmm0
psrad  $0x10,%xmm0
movdqa %xmm0,0x10(%rsp)
add    $0xffffffff,%eax
and    $0x3,%eax
mov    0x10(%rsp,%rax,4),%esi
mov    %r13,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1192 <main+0x52>
lea    0xd83(%rip),%rdi
jmp    118d <main+0x4d>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [5 x i8] c"%lld\00"
@.str.2 = [3 x i8] c"0\0A\00"
@.str.3 = [3 x i8] c"1\0A\00"
@.str.4 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
%8 = alloca i64
%9 = alloca [4 x i32]
store i32 0, i32* %1
store i8 97, i8* %7
%10 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
br label %11
11: ; preds = %55, %35, %28, %0
%12 = load i32, i32* %2
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %65
14: ; preds = %11
br label %15
15: ; preds = %19, %14
%16 = load i8, i8* %7
%17 = sext i8 %16 to i32
%18 = icmp ne i32 %17, 32
br i1 %18, label %19, label %24
19: ; preds = %15
%20 = load i8, i8* %7
%21 = sext i8 %20 to i32
%22 = sub nsw i32 %21, 48
store i32 %22, i32* %3
%23 = call signext i8 (...) @getchar()
store i8 %23, i8* %7
br label %15
24: ; preds = %15
%25 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64* %8)
%26 = load i32, i32* %3
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %24
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%30 = load i32, i32* %2
%31 = add nsw i32 %30, -1
store i32 %31, i32* %2
store i8 97, i8* %7
br label %11
32: ; preds = %24
%33 = load i64, i64* %8
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %32
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%37 = load i32, i32* %2
%38 = add nsw i32 %37, -1
store i32 %38, i32* %2
store i8 97, i8* %7
br label %11
39: ; preds = %32
store i32 1, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
br label %40
40: ; preds = %52, %39
%41 = load i32, i32* %6
%42 = icmp slt i32 %41, 4
br i1 %42, label %43, label %55
43: ; preds = %40
%44 = load i32, i32* %4
%45 = load i32, i32* %3
%46 = mul nsw i32 %44, %45
%47 = srem i32 %46, 10
store i32 %47, i32* %4
%48 = load i32, i32* %4
%49 = load i32, i32* %6
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [4 x i32], [4 x i32]* %9, i64 0, i64 %50
store i32 %48, i32* %51
br label %52
52: ; preds = %43
%53 = load i32, i32* %6
%54 = add nsw i32 %53, 1
store i32 %54, i32* %6
br label %40
55: ; preds = %40
%56 = load i64, i64* %8
%57 = srem i64 %56, 4
%58 = add nsw i64 %57, 3
%59 = srem i64 %58, 4
%60 = getelementptr inbounds [4 x i32], [4 x i32]* %9, i64 0, i64 %59
%61 = load i32, i32* %60
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %61)
%63 = load i32, i32* %2
%64 = add nsw i32 %63, -1
store i32 %64, i32* %2
store i8 97, i8* %7
br label %11
65: ; preds = %11
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [5 x i8] c"%lld\00"
@.str.4 = [4 x i8] c"%d\0A\00"
@str = [2 x i8] c"1\00"
@str.5 = [2 x i8] c"0\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca <4 x i32>
%4 = bitcast i32* %1 to i8*
%5 = bitcast i64* %2 to i8*
%6 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %1)
%7 = load i32, i32* %1
%8 = icmp eq i32 %7, 0
br i1 %8, label %60, label %9
9: ; preds = %0
%10 = bitcast <4 x i32>* %3 to i8*
br label %11
11: ; preds = %15, %9
%12 = phi i8 [ 97, %9 ], [ %16, %15 ]
%13 = call signext i8 (...) @getchar()
%14 = icmp eq i8 %13, 32
br i1 %14, label %17, label %15
15: ; preds = %11, %26
%16 = phi i8 [ %13, %11 ], [ 97, %26 ]
br label %11
17: ; preds = %11
%18 = sext i8 %12 to i32
%19 = add nsw i32 %18, -48
%20 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64* %2)
%21 = icmp eq i32 %19, 0
br i1 %21, label %22, label %29
22: ; preds = %17
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str.5, i64 0, i64 0))
%24 = load i32, i32* %1
%25 = add nsw i32 %24, -1
store i32 %25, i32* %1
br label %26
26: ; preds = %22, %32, %36
%27 = phi i32 [ %59, %36 ], [ %35, %32 ], [ %25, %22 ]
%28 = icmp eq i32 %27, 0
br i1 %28, label %60, label %15
29: ; preds = %17
%30 = load i64, i64* %2
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %29
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str, i64 0, i64 0))
%34 = load i32, i32* %1
%35 = add nsw i32 %34, -1
store i32 %35, i32* %1
br label %26
36: ; preds = %29
%37 = trunc i32 %19 to i16
%38 = srem i16 %37, 10
%39 = trunc i32 %19 to i16
%40 = mul i16 %38, %39
%41 = srem i16 %40, 10
%42 = trunc i32 %19 to i16
%43 = mul i16 %41, %42
%44 = srem i16 %43, 10
%45 = trunc i32 %19 to i16
%46 = mul i16 %44, %45
%47 = srem i16 %46, 10
%48 = insertelement <4 x i16> poison, i16 %38, i64 0
%49 = insertelement <4 x i16> %48, i16 %41, i64 1
%50 = insertelement <4 x i16> %49, i16 %44, i64 2
%51 = insertelement <4 x i16> %50, i16 %47, i64 3
%52 = sext <4 x i16> %51 to <4 x i32>
store <4 x i32> %52, <4 x i32>* %3
%53 = add i64 %30, 3
%54 = and i64 %53, 3
%55 = getelementptr inbounds <4 x i32>, <4 x i32>* %3, i64 0, i64 %54
%56 = load i32, i32* %55
%57 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %56)
%58 = load i32, i32* %1
%59 = add nsw i32 %58, -1
store i32 %59, i32* %1
br label %26
60: ; preds = %26, %0
ret i32 0
}