#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
inline T1 max(T1 a, T2 b) {
  return a < b ? b : a;
}
template <typename T1, typename T2>
inline T1 min(T1 a, T2 b) {
  return a < b ? a : b;
}
const int _ = 53;
inline void failure() { exit(0); }
template <typename T>
inline void py(T val) {
  cout << val << endl, cout.flush();
}
template <typename T>
inline T ty() {
  cout.flush();
  T a;
  cin >> a;
  return a;
}
int n, val[_][_], rk[_][_], now[_], tr[_], tl[_];
void make() {
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) rk[i][j] = j;
    sort(rk[i] + 1, rk[i] + n + 1,
         [&](int a, int b) { return val[i][a] > val[i][b]; });
    tl[i] = tr[i] = now[i] = 0;
  }
  int finale = 0;
  while (!finale) {
    finale = 1;
    for (int i = 1; i <= n; i++) {
      if (tr[i]) continue;
      finale = 0, now[i]++;
      int b = rk[i][now[i]];
      if (!tl[b])
        tr[i] = b, tl[b] = i;
      else {
        int a = tl[b];
        if (-val[i][b] > -val[a][b]) tr[a] = 0, tl[b] = i, tr[i] = b;
      }
    }
  }
}
void finder() {
  n = ty<int>();
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) val[i][j] = ty<int>();
  py("B");
  if (ty<char>() == 'D')
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++) val[i][j] = -val[i][j];
  int now = ty<int>(), rev = 0;
  if (now <= n) {
    rev = 1;
    for (int i = 1; i <= n; i++)
      for (int j = i + 1; j <= n; j++) swap(val[i][j], val[j][i]);
  }
  make();
  while (now > 0) {
    if (now > n) now -= n;
    py(tl[now] + rev * n), now = ty<int>();
    if (now == -2) failure();
    if (now == -1) return;
  }
}
int main() {
  int t;
  cin >> t;
  while (t--) finder();
  return 0;
}
