#include <bits/stdc++.h>
int read() {
  int x;
  scanf("%d", &x);
  return x;
}
int n, e[51][51], rank[51][51], next[101], pos[51];
std::vector<int> vec[51], t;
std::queue<int> q;
void work(int f) {
  memset(pos + 1, 0, n << 2), memset(next + 1, 0, n << 3);
  for (int i = 1, j; i <= n; ++i)
    for (q.push(i), vec[i].clear(), j = 1; j <= n; ++j) vec[i].push_back(j);
  for (int i = 1; i <= n; ++i)
    std::sort(vec[i].begin(), vec[i].end(),
              [&](int a, int b) { return (e[i][a] < e[i][b]) ^ f; });
  for (int i = 1; i <= n; ++i) {
    t.clear();
    for (int j = 1; j <= n; ++j) t.push_back(j);
    std::sort(t.begin(), t.end(),
              [&](int a, int b) { return (e[a][i] > e[b][i]) ^ f; });
    for (int j = 0; j < (int)t.size(); ++j) rank[i][t[j]] = j + 1;
  }
  for (int u, v; !q.empty();)
    for (u = q.front(), q.pop(); !next[u];) {
      v = vec[u][pos[u]++];
      if (!next[v + n] || rank[v][next[v + n]] > rank[v][u]) {
        next[next[v + n]] = 0;
        if (next[v + n]) q.push(next[v + n]);
        next[v + n] = u, next[u] = v + n;
      }
    }
}
int main() {
  for (int t = read(), p; t; --t) {
    n = read();
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= n; ++j) e[i][j] = read();
    printf("B\n"), fflush(stdout);
    if (getchar(), getchar() == 'D')
      for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= n; ++j) e[i][j] = -e[i][j];
    for (work((p = read()) > n); ~p; p = read())
      printf("%d\n", next[p]), fflush(stdout);
  }
}
