#include <bits/stdc++.h>
using namespace std;
template <typename T>
void maxtt(T& t1, T t2) {
  t1 = max(t1, t2);
}
template <typename T>
void mintt(T& t1, T t2) {
  t1 = min(t1, t2);
}
bool debug = 0;
int n, m, k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
string direc = "URDL";
long long ln, lk, lm;
void etp(bool f = 0) {
  puts(f ? "YES" : "NO");
  exit(0);
}
void addmod(int& x, int y, int mod = 998244353) {
  assert(y >= 0);
  x += y;
  if (x >= mod) x -= mod;
  assert(x >= 0 && x < mod);
}
void et(int x = -1) {
  printf("%d\n", x);
  exit(0);
}
long long fastPow(long long x, long long y, int mod = 998244353) {
  long long ans = 1;
  while (y > 0) {
    if (y & 1) ans = (x * ans) % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return ans;
}
long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; }
int a[55][55], id, w[55][55];
vector<pair<int, int> > pl[55], pr[55];
int cx[55], cy[55];
int idl[55], idr[55];
void maxmatch() {
  memset(cx, 0xff, sizeof(cx));
  memset(cy, 0xff, sizeof(cy));
  queue<int> q;
  for (int(i) = 1; (i) <= (int)(n); (i)++) q.push(i);
  while (!q.empty()) {
    int x = q.front();
    int tar = pl[x][idl[x]++].second;
    if (cy[tar] == -1) {
      cy[tar] = x;
      cx[x] = tar;
      q.pop();
      continue;
    } else {
      if (w[tar][x] < w[tar][cy[tar]]) {
        int z = cy[tar];
        cy[tar] = x;
        cx[x] = tar;
        q.pop();
        cx[z] = -1;
        q.push(z);
      }
    }
  }
}
void fmain(int tid) {
  scanf("%d", &n);
  for (int(i) = 1; (i) <= (int)(n); (i)++)
    for (int(j) = 1; (j) <= (int)(n); (j)++) scanf("%d", &a[i][j]);
  puts("B");
  fflush(stdout);
  char ope[2];
  scanf("%s%d", ope, &id);
  if (ope[0] == 'D')
    for (int(i) = 1; (i) <= (int)(n); (i)++)
      for (int(j) = 1; (j) <= (int)(n); (j)++) a[i][j] *= -1;
  for (int(i) = 1; (i) <= (int)(n); (i)++) {
    pl[i].clear();
    pr[i].clear();
  }
  int fg = id <= n ? 1 : -1;
  for (int(i) = 1; (i) <= (int)(n); (i)++) {
    for (int(j) = 1; (j) <= (int)(n); (j)++) {
      pl[i].push_back({a[i][j] * fg, j});
      pr[j].push_back({-a[i][j] * fg, i});
    }
  }
  for (int(i) = 1; (i) <= (int)(n); (i)++) {
    sort(pl[i].begin(), pl[i].end());
    sort(pr[i].begin(), pr[i].end());
    for (int(j) = 0; (j) < (int)(n); (j)++) w[i][pr[i][j].second] = j;
    idl[i] = 0;
    idr[i] = 0;
  }
  maxmatch();
  if (id <= n) {
    while (id != -1) {
      printf("%d\n", cx[id] + n);
      fflush(stdout);
      scanf("%d", &id);
      if (id == -2) exit(0);
    }
  } else {
    while (id != -1) {
      printf("%d\n", cy[id - n]);
      fflush(stdout);
      scanf("%d", &id);
      if (id == -2) exit(0);
    }
  }
}
int main() {
  int t = 1;
  scanf("%d", &t);
  for (int(i) = 1; (i) <= (int)(t); (i)++) {
    fmain(i);
  }
  return 0;
}
