#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T read(register T& t) {
  register T f = 1;
  register char ch = getchar();
  t = 0;
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -f;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') t = t * 10 + ch - '0', ch = getchar();
  t *= f;
  return t;
}
template <typename T, typename... Args>
inline void read(T& t, Args&... args) {
  read(t);
  read(args...);
}
int n;
char t[102];
int w[102][102], rnk[102][102], pos[102];
vector<int> p[102];
queue<int> q;
void solve(int t) {
  for (int i = 1; i <= n; ++i) {
    p[i].clear(), p[i + n].clear();
    for (int j = 1; j <= n; ++j) p[i].push_back(j + n), p[i + n].push_back(j);
    sort(p[i].begin(), p[i].end(),
         [&](int a, int b) { return w[i][a - n] > w[i][b - n] ^ t; });
    sort(p[i + n].begin(), p[i + n].end(),
         [&](int a, int b) { return w[a][i] < w[b][i] ^ t; });
    q.push(i), pos[i] = 0, pos[i + n] = n;
    for (int j = 0; j < n; ++j)
      rnk[j][p[i][j]] = j, rnk[i + n][p[i + n][j]] = j;
  }
  while (!q.empty()) {
    int u = q.front(), v;
    while (pos[v = p[u][pos[u]]] < rnk[v][u]) ++pos[u];
    if (pos[v] < n) q.push(p[v][pos[v]]);
    pos[v] = rnk[v][u], q.pop();
  }
}
int main() {
  int T;
  read(T);
  while (T--) {
    read(n);
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= n; ++j) read(w[i][j]);
    puts("B"), fflush(stdout), scanf("%s", t);
    if (t[0] == 'I')
      for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= n; ++j) w[i][j] = -w[i][j];
    int u;
    for (solve((read(u)) > n); ~u; read(u))
      printf("%d\n", p[u][pos[u]]), fflush(stdout);
  }
  return 0;
}
