#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 */
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ scanf(char *, ...);
int strlen(char *);
int main() {
  int i, T, ball, len;
  char str[510];
  scanf("%d", &T);
  while (T--) {
    scanf("%s", str);
    len = strlen(str);
    ball = 0;
    for (i = 0; i < len; i++) {
      if (str[i] == '0') {
        ball = ball - 1;
      }
      if (str[i] == '1') {
        ball = ball + 1;
      }
      if (ball < 0) {
        break;
      }
    }
    if (ball < 0)
      printf("MAGIC\n");
    if (ball >= 0)
      printf("NORMAL\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeb7(%rip),%rdi
lea    -0xc(%rbp),%rsi
mov    $0x0,%al
call   1050 <scanf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0xc(%rbp)
cmp    $0x0,%eax
je     1235 <main+0x105>
lea    -0x220(%rbp),%rsi
lea    0xe8d(%rip),%rdi
mov    $0x0,%al
call   1050 <scanf@plt>
lea    -0x220(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1204 <main+0xd4>
movslq -0x8(%rbp),%rax
movsbl -0x220(%rbp,%rax,1),%eax
ff
cmp    $0x30,%eax
jne    11c4 <main+0x94>
mov    -0x10(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
movslq -0x8(%rbp),%rax
movsbl -0x220(%rbp,%rax,1),%eax
ff
cmp    $0x31,%eax
jne    11e2 <main+0xb2>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11f1 <main+0xc1>
jmp    1204 <main+0xd4>
jmp    11f6 <main+0xc6>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    119a <main+0x6a>
cmpl   $0x0,-0x10(%rbp)
jge    121a <main+0xea>
lea    0xdf1(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0x10(%rbp)
jl     1230 <main+0x100>
lea    0xde2(%rip),%rdi
call   1040 <printf@plt>
jmp    1154 <main+0x24>
xor    %eax,%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x210,%rsp
lea    0xeb9(%rip),%rdi
lea    0xc(%rsp),%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
subl   $0x1,0xc(%rsp)
jae    116d <main+0x3d>
xor    %eax,%eax
add    $0x210,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xe8f(%rip),%r14
lea    0x10(%rsp),%r13
lea    0xe86(%rip),%r15
lea    0xe86(%rip),%r12
jmp    119f <main+0x6f>
nopl   0x0(%rax)
mov    %r12,%rdi
call   1030 <puts@plt>
subl   $0x1,0xc(%rsp)
jb     115a <main+0x2a>
mov    %r14,%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
mov    %r13,%rdi
call   1040 <strlen@plt>
mov    %r15,%rdi
test   %eax,%eax
jle    1193 <main+0x63>
mov    %eax,%eax
xor    %ecx,%ecx
xor    %edx,%edx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movzbl 0x10(%rsp,%rcx,1),%ebx
xor    %esi,%esi
cmp    $0x30,%bl
sete   %sil
mov    %edx,%edi
sub    %esi,%edi
xor    %edx,%edx
cmp    $0x31,%bl
sete   %dl
add    %edi,%edx
js     1190 <main+0x60>
add    $0x1,%rcx
cmp    %rcx,%rax
jne    11d0 <main+0xa0>
mov    %r15,%rdi
jmp    1193 <main+0x63>

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

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [3 x i8] c"%s\00"
@str = [7 x i8] c"NORMAL\00"
@str.4 = [6 x i8] c"MAGIC\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [510 x i8]
%3 = bitcast i32* %1 to i8*
%4 = getelementptr inbounds [510 x i8], [510 x i8]* %2, i64 0, i64 0
%5 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %1)
%6 = load i32, i32* %1
%7 = add nsw i32 %6, -1
store i32 %7, i32* %1
%8 = icmp eq i32 %6, 0
br i1 %8, label %36, label %9
9: ; preds = %0, %30
%10 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%11 = call i32 @strlen(i8* %4)
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %30
13: ; preds = %9
%14 = zext i32 %11 to i64
br label %17
15: ; preds = %17
%16 = icmp eq i64 %29, %14
br i1 %16, label %30, label %17
17: ; preds = %13, %15
%18 = phi i64 [ 0, %13 ], [ %29, %15 ]
%19 = phi i32 [ 0, %13 ], [ %27, %15 ]
%20 = getelementptr inbounds [510 x i8], [510 x i8]* %2, i64 0, i64 %18
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 48
%23 = sext i1 %22 to i32
%24 = add nsw i32 %19, %23
%25 = icmp eq i8 %21, 49
%26 = zext i1 %25 to i32
%27 = add nsw i32 %24, %26
%28 = icmp slt i32 %27, 0
%29 = add nuw nsw i64 %18, 1
br i1 %28, label %30, label %15
30: ; preds = %15, %17, %9
%31 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @str, i64 0, i64 0), %9 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @str.4, i64 0, i64 0), %17 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @str, i64 0, i64 0), %15 ]
%32 = call i32 @puts(i8* dereferenceable(1) %31)
%33 = load i32, i32* %1
%34 = add nsw i32 %33, -1
store i32 %34, i32* %1
%35 = icmp eq i32 %33, 0
br i1 %35, label %36, label %9
36: ; preds = %30, %0
ret i32 0
}