#include <bits/stdc++.h>
using namespace std;
const int N = 55;
int num[N][N], id[N][N], st[N], r[N], link[N * 2], top;
int cur;
bool val;
inline bool cmp(int x, int y) { return (num[cur][x] < num[cur][y]) != val; }
void solve(int n) {
  memset(link, 0, sizeof(link));
  memset(r, 0, sizeof(r));
  for (int i = 1; i <= n; i++) {
    cur = i;
    for (int j = 1; j <= n; j++) id[i][j] = j;
    sort(id[i] + 1, id[i] + n + 1, cmp);
    st[++top] = i;
  }
  while (top) {
    int u = st[top], v = id[u][++r[u]];
    if (!link[n + v] || (num[u][v] > num[link[n + v]][v]) != val) {
      --top;
      if (link[n + v]) st[++top] = link[n + v];
      link[u] = n + v;
      link[n + v] = u;
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++) cin >> num[i][j];
    cout << "B" << endl;
    char s[5];
    int x;
    cin >> s >> x;
    val = (s[0] == 'I') != (x <= n);
    solve(n);
    while (x != -1) {
      cout << link[x] << endl;
      cin >> x;
    }
  }
  return 0;
}
