#include <bits/stdc++.h>
using namespace std;
vector<int> v[55];
int c[55], d[55], f[55], edge[55][55], nxt[105];
void gx(int w) {
  c[w]++;
  int u = v[w][c[w]];
  if (f[u] == 0) {
    f[u] = w;
    d[w] = u;
  } else {
    if (edge[w][u] > edge[f[u]][u]) {
      int cun = f[u];
      f[u] = w;
      d[w] = u;
      gx(cun);
    } else
      gx(w);
  }
}
int n;
void work(int w) {
  if (w == 1) {
    for (int i = 1; i <= n; i++)
      for (int j = i + 1; j <= n; j++) {
        swap(edge[i][j], edge[j][i]);
      }
  }
  for (int i = 1; i <= n; i++) {
    v[i].clear();
    vector<pair<int, int> > vec;
    for (int j = 1; j <= n; j++) {
      vec.push_back(make_pair(edge[i][j], j));
    }
    sort(vec.begin(), vec.end());
    for (int j = 0; j < n; j++) v[i].push_back(vec[j].second);
  }
  for (int i = 1; i <= n; i++) c[i] = -1, d[i] = 0, f[i] = 0;
  for (int i = 1; i <= n; i++) {
    if (d[i] == 0) gx(i);
  }
  if (w == 1) {
    for (int i = 1; i <= n; i++) nxt[n + i] = d[i];
  } else {
    for (int i = 1; i <= n; i++) nxt[i] = n + d[i];
  }
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    fflush(stdout);
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++) scanf("%d", &edge[i][j]);
    printf("B\n");
    fflush(stdout);
    char c;
    cin >> c;
    if (c == 'D') {
      for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++) edge[i][j] = n * n + 1 - edge[i][j];
    }
    int k;
    scanf("%d", &k);
    if (k > n)
      work(1);
    else
      work(0);
    while (1) {
      if (k == -1) break;
      printf("%d\n", nxt[k]);
      fflush(stdout);
      scanf("%d", &k);
    }
  }
}
