#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <cassert>
#include <string.h>

#include <time.h>
#include <random>
#include <iomanip>
using namespace std;

#define ll long long
#define N 100005
#define M 4100

ll memo[N];
ll jump[M];
ll loop[M];

void processParens(string str) {
    stack<ll> s;
    for (ll i = 0; i < str.size(); i++) {
        if (str[i] == '[') {
            s.push(i);
        }
        else if (str[i] == ']') {
            ll left = s.top();
            s.pop();
            jump[left] = i;
            jump[i] = left;
        }
    }
}

char getChar(string &input, ll inputIndex, ll sii) {
    if (inputIndex >= sii)
        return 255;
    return input[inputIndex];
}

void solve() {
    ll smm, scc, sii;
    cin >> smm >> scc >> sii;
    string scStr, input;
    cin >> scStr >> input;
    
    for (ll i = 0; i < N; i++)
        memo[i] = 0;
    for (ll i = 0; i < M; i++) {
        jump[i] = -1;
        loop[i] = 0;
    }
    
    processParens(scStr);
    
    ll inputIndex = 0;
    ll progPos = 0;
    ll memoPos = 0;
    ll counter = 0;
    
    while (progPos < scc && counter < 50000005) {
        switch(scStr[progPos]) {
            case '-':
                memo[memoPos] = (memo[memoPos]-1) % 256;
                if (memo[memoPos] < 0)
                    memo[memoPos] += 256;
                break;
            case '+':
                memo[memoPos] = (memo[memoPos]+1) % 256;
                break;
            case '<':
                memoPos = (memoPos-1) % smm;
                if (memoPos < 0)
                    memoPos += smm;
                break;
            case '>':
                memoPos = (memoPos+1) % smm;
                break;
            case '[':
                if (memo[memoPos] == 0)
                    progPos = jump[progPos];
                else {
                    if (loop[progPos]==0)
                        loop[progPos] = 1;
                }
                break;
            case ']':
                if (memo[memoPos] != 0)
                    progPos = jump[progPos];
                else
                    loop[jump[progPos]] = -1;
                break;
            case '.':
                break;
            case ',':
                memo[memoPos] = getChar(input, inputIndex, sii);
                inputIndex++;
                break;
            default:
                break;
        }
        counter++;
        progPos++;
    }
    
    if (progPos >= scc) {
        cout << "Terminates" << endl;
    }
    else {
        cout << "Loops ";
        
        for (ll i = 0; i < scc; i++)
            if (loop[i] == 1) {
                cout << i << " " << jump[i] << endl;
            }
    }
    
}

int main() {
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    ll t;
    cin >> t;
    for (ll i = 0; i < t; i++) {
        solve();
    }
}