#include <iostream>
#include <cstring>

using namespace std;
const int M = 50000000;

int dat[1000000];
int match[4096];
int stack[4096]; int ssz = 0;
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int ncase; cin >> ncase;
	for (int csnum = 1; csnum <= ncase; csnum++) {
		int sm, sc, si; cin >> sm >> sc >> si;
		string tmp; getline(cin, tmp);
		string code; getline(cin, code);
		string input; getline(cin, input);
		//string output;

		for (int i = 0; i < sc; i++) {
			if (code[i] == '[') {
				stack[ssz++] = i;
			} else if (code[i] == ']') {
				int start = stack[--ssz];
				match[start] = i;
				match[i] = start;
			}
		}

		int idx = 0;
		int iptr = 0;
		int inptr = 0;
		bool ended = false;
		memset(dat, 0, sizeof dat);
		for (int t = 0; t < M; t++) {
			char c = code[iptr];
			if (c == '-') {
				dat[idx] = (dat[idx]+256-1)%256;
			} else if (c == '+') {
				dat[idx] = (dat[idx]+1)%256;
			} else if (c == '<') {
				idx = (idx+sm-1)%sm;
			} else if (c == '>') {
				idx = (idx+1)%sm;
			} else if (c == '[') {
				if (dat[idx] == 0) iptr = match[iptr]-1;
			} else if (c == ']') {
				if (dat[idx] != 0) iptr = match[iptr]-1;
			} else if (c == '.') {
				//output += (char)(dat[idx]);
			} else if (c == ',') {
				if (inptr >= si) {
					dat[idx] = 255;
				} else {
					dat[idx] = (int)(input[inptr++]);
				}
			}
			iptr++;
			if (iptr >= sc) {
				ended = true;
				break;
			}
		}

		//cerr << output << endl;

		if (ended) {
			cout << "Terminates" << endl;
		} else {
			ssz = 0;
			for (int i = 0; i <= iptr; i++) {
				if (code[i] == '[') {
					stack[ssz++] = i;
				} else if (code[i] == ']' && i < iptr) {
					--ssz;
				}
			}
			for (int t = 0; t < M; t++) {
				char c = code[iptr];
				if (c == '-') {
					dat[idx] = (dat[idx]+256-1)%256;
				} else if (c == '+') {
					dat[idx] = (dat[idx]+1)%256;
				} else if (c == '<') {
					idx = (idx+sm-1)%sm;
				} else if (c == '>') {
					idx = (idx+1)%sm;
				} else if (c == '[') {
					if (dat[idx] == 0) iptr = match[iptr]-1;
				} else if (c == ']') {
					if (dat[idx] != 0) iptr = match[iptr]-1;
					else {
						if (match[iptr] == stack[ssz-1]) {
							--ssz;
						}
					}
				} else if (c == '.') {
					//output += (char)(dat[idx]);
				} else if (c == ',') {
					if (inptr == si) {
						dat[idx] = 255;
					} else {
						dat[idx] = (int)(input[inptr++]);
					}
				}
				iptr++;
			}
			cout << "Loops " << stack[ssz-1] << " " << match[stack[ssz-1]] << endl;
		}
	}
}