#include <bits/stdtr1c++.h>
using namespace std;

unsigned char m[100042];
char c[4200], in[4200];
int mbi[4200];
int ex[4200];

int main() {
  int t; scanf("%d\n", &t);
  while (t--) {
    int sm, sc, si;
    scanf("%d%d%d\n%s\n%s\n", &sm, &sc, &si, c, in);
    memset(m, 0, sizeof m);
    memset(mbi, -1, sizeof mbi);
    memset(ex, 0, sizeof ex);
    for (int i = 0; i < sc; i++) {
      if (c[i] == '[') {
        int ind = i, b = 1;
        while (b) {
          ind++;
          if (c[ind] == ']')
            b--;
          else if (c[ind] == '[')
            b++;
        }
        mbi[i] = ind+1;
        mbi[ind] = i+1;
      }
    }
    int ops = 50000042, mi = 0, ci = 0, inind = 0;
    while (ops) {
      if (ci == sc)
        break;
      switch (c[ci]) {
        case '-':
          m[mi]--;
          ci++;
          break;
        case '+':
          m[mi]++;
          ci++;
          break;
        case '<':
          mi--;
          if (mi < 0)
            mi = sm-1;
          ci++;
          break;
        case '>':
          mi++;
          if (mi == sm)
            mi = 0;
          ci++;
          break;
        case '[':
          if (!m[mi])
            ci = mbi[ci];
          else {
            if (!ex[ci])
              ex[ci] = 1;
            ci++;
          }
          break;
        case ']':
          if (m[mi])
            ci = mbi[ci];
          else {
            ex[mbi[ci]-1] = 2;
            ci++;
          }
          break;
        case '.':
          ci++;
          break;
        case ',':
          if (inind == si)
            m[mi] = 255;
          else
            m[mi] = in[inind];
          inind++;
          ci++;
          break;
        default:
          break;
      }
      ops--;
    }
    if (ops)
      printf("Terminates\n");
    else {
      for (int i = 0; i < sc; i++) {
        if (ex[i] == 1) {
          printf("Loops %d %d\n", i, mbi[i]-1);
          break;
        }
      }
    }
  }
  return 0;
}