#include <bits/stdc++.h>
const long double PI = acos(-1);
const long double eps = 0.0000000001;
const long long INF = 0x3fffffffffffffff;
long long T, n, m, a[2005][2005], st, nxt[4005], rk[2005][2005];
char c;
std::vector<long long> pos[2005];
std::queue<long long> que;
void init(bool t) {
  memset(nxt, 0, sizeof(nxt));
  for (long long i = 1; i <= n; ++i) {
    que.push(i);
    pos[i].clear();
    for (long long j = 1; j <= n; ++j) pos[i].push_back(j);
    std::sort(pos[i].begin(), pos[i].end(),
              [&](const long long &A, const long long &B) {
                return (a[i][A] > a[i][B]) ^ t;
              });
  }
  for (long long i = 1; i <= n; ++i) {
    static std::vector<long long> sa;
    sa.clear();
    for (long long j = 1; j <= n; ++j) sa.push_back(j);
    std::sort(sa.begin(), sa.end(),
              [&](const long long &A, const long long &B) {
                return (a[A][i] > a[B][i]) ^ t;
              });
    for (long long j = 1; j <= n; ++j) rk[i][sa[j - 1]] = j;
  }
  while (!que.empty()) {
    long long now = que.front();
    que.pop();
    while (!nxt[now]) {
      long long to = pos[now].back();
      pos[now].pop_back();
      if (!nxt[to + n] || rk[to][now] < rk[to][nxt[to + n]]) {
        if (nxt[to + n]) {
          nxt[nxt[to + n]] = 0;
          que.push(nxt[to + n]);
        }
        nxt[to + n] = now;
        nxt[now] = to + n;
      }
    }
  }
}
signed main() {
  std::ios::sync_with_stdio(false);
  std::cin >> T;
  while (T--) {
    std::cin >> n;
    for (long long i = 1; i <= n; ++i) {
      for (long long j = 1; j <= n; ++j) {
        std::cin >> a[i][j];
      }
    }
    std::cout << "B" << std::endl;
    std::cin >> c >> st;
    if (c == 'D')
      for (long long i = 1; i <= n; ++i) {
        for (long long j = 1; j <= n; ++j) {
          a[i][j] = -a[i][j];
        }
      }
    init(st > n);
    while (~st) {
      std::cout << nxt[st] << std::endl;
      std::cin >> st;
    }
  }
  return 0;
}
