def make_roop_pair(instructions):
dictionary = {}
temps = []
for idx, ins in enumerate(instructions):
if ins == '[':
temps.append(idx)
elif ins == ']':
post_index = temps.pop()
dictionary[idx] = post_index
dictionary[post_index] = idx
return dictionary
def minus_operation(array, array_pointer):
array[array_pointer] = (array[array_pointer] - 1) % 256
def plus_operation(array, array_pointer):
array[array_pointer] = (array[array_pointer] + 1) % 256
def move_array_pointer(array_pointer, sm, dir):
if dir == 'left':
array_pointer = (array_pointer - 1) % sm
elif dir == 'right':
array_pointer = (array_pointer + 1) % sm
return array_pointer
def insert_character(characters, character_pointer, array, array_pointer):
if character_pointer >= len(characters):
array[array_pointer] = 255
else:
array[array_pointer] = ord(characters[character_pointer])
character_pointer += 1
return character_pointer
def instruction_jump(type, instruction_pointer, array, array_pointer, loop_stack, loop_dict, visited):
visited[instruction_pointer] = True
if type == '[':
if array[array_pointer] == 0:
instruction_pointer = loop_dict[instruction_pointer]
else:
loop_stack.append(instruction_pointer)
elif type == ']':
if array[array_pointer] != 0:
instruction_pointer = loop_dict[instruction_pointer]
else:
loop_stack.pop()
return instruction_pointer
# 50000만번을 돌려서 빠져나오지 못한 가장 바깥 괄호
def run(sm, array, instructions, characters, loop_dict):
visited = [False]*len(instructions)
loop_stack = []
array_pointer = 0 # 배열을 가르키는 포인터
character_pointer = 0 # 입력 문자를 가르키는 포인터
instruction_pointer = 0
instruction_count = 0
closed = 0
# 명령어 실행
while True:
if instruction_count >= 50000000:
break
if instruction_pointer >= len(instructions):
break
if instructions[instruction_pointer] == '-': minus_operation(array, array_pointer)
elif instructions[instruction_pointer] == '+': plus_operation(array, array_pointer)
elif instructions[instruction_pointer] == '<': array_pointer = move_array_pointer(array_pointer, sm, 'left')
elif instructions[instruction_pointer] == '>': array_pointer = move_array_pointer(array_pointer, sm, 'right')
elif instructions[instruction_pointer] == ',': character_pointer = insert_character(characters, character_pointer, array, array_pointer)
elif instructions[instruction_pointer] in ['[', ']']:
if instructions[instruction_pointer] == ']':
closed = max(closed, instruction_pointer)
instruction_pointer = instruction_jump(instructions[instruction_pointer], instruction_pointer, array, array_pointer, loop_stack, loop_dict, visited)
instruction_pointer += 1
instruction_count += 1
if instruction_pointer == len(instructions):
return [True]
else:
return [False, loop_dict[closed], closed]
# if instruction_count >= 50000000 and loop_stack:
# if visited[loop_stack[0]] and visited[loop_dict[loop_stack[0]]]:
# return [False, loop_stack[0], loop_dict[loop_stack[0]]]
# else:
# return [False, loop_stack[-1], loop_dict[loop_stack[-1]]]
# else:
# return [True]
if __name__=='__main__':
answers = []
# 0. 테스트 개수
Tc = int(input())
for i in range(Tc):
# 1. 입력받기
sm, sc, si = map(int, input().split())
array = [0 for _ in range(sm)]
instructions = input()
characters = input()
# 2. loop pair 만들기
loop_dict = make_roop_pair(instructions)
# 3. 테스트 수행
results = run(sm, array, instructions, characters, loop_dict)
if results[0]:
answers.append('Terminates')
else:
answers.append(f'Loops {results[1]} {results[2]}')
for answer in answers:
print(answer)