You are given a complete bipartite graph with $$$2n$$$ nodes, with $$$n$$$ nodes on each side of the bipartition. Nodes $$$1$$$ through $$$n$$$ are on one side of the bipartition, and nodes $$$n+1$$$ to $$$2n$$$ are on the other side. You are also given an $$$n \times n$$$ matrix $$$a$$$ describing the edge weights. $$$a_{ij}$$$ denotes the weight of the edge between nodes $$$i$$$ and $$$j+n$$$. Each edge has a distinct weight.Alice and Bob are playing a game on this graph. First Alice chooses to play as either "increasing" or "decreasing" for herself, and Bob gets the other choice. Then she places a token on any node of the graph. Bob then moves the token along any edge incident to that node. They now take turns playing the following game, with Alice going first.The current player must move the token from the current vertex to some adjacent unvisited vertex. Let $$$w$$$ be the last weight of the last edge that was traversed. The edge that is traversed must be strictly greater than $$$w$$$ if the player is playing as "increasing", otherwise, it must be strictly less. The first player unable to make a move loses.You are given $$$n$$$ and the edge weights of the graph. You can choose to play as either Alice or Bob, and you will play against the judge. You must win all the games for your answer to be judged correct.

NoteThe first example has two test cases. In the first test case, the graph looks like the following.  In the sample output, the player decides to play as Alice and chooses "decreasing" and starting at node $$$3$$$. The judge responds by moving to node $$$6$$$. After, the player moves to node $$$2$$$. At this point, the judge has no more moves (since the weight must "increase"), so it gives up and prints $$$-1$$$.In the next case, we have two nodes connected by an edge of weight $$$1$$$. The player decides to play as Bob. No matter what the judge chooses, the player can move the token to the other node and the judge has no moves so will lose.

import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;

import static java.lang.Math.*;

public class Main implements Runnable {
    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private SpaceCharFilter filter;
        private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars==-1)
                throw new InputMismatchException();

            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                }
                catch (IOException e) {
                    throw new InputMismatchException();
                }

                if(numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
        public int nextInt() {
            int c = read();

            while(isSpaceChar(c))
                c = read();

            int sgn = 1;

            if (c == '-') {
                sgn = -1;
                c = read();
            }

            int res = 0;
            do {
                if(c<'0'||c>'9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));

            return res * sgn;
        }

        public long nextLong() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;

            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));
                return res * sgn;
        }

        public double nextDouble() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            double res = 0;
            while (!isSpaceChar(c) && c != '.') {
                if (c == 'e' || c == 'E')
                    return res * Math.pow(10, nextInt());
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '.') {
                c = read();
                double m = 1;
                while (!isSpaceChar(c)) {
                    if (c == 'e' || c == 'E')
                        return res * Math.pow(10, nextInt());
                    if (c < '0' || c > '9')
                        throw new InputMismatchException();
                    m /= 10;
                    res += (c - '0') * m;
                    c = read();
                }
            }
            return res * sgn;
        }

        public String readString() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            }
            while (!isSpaceChar(c));

            return res.toString();
        }

        public boolean isSpaceChar(int c) {
            if (filter != null)
                return filter.isSpaceChar(c);
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public String next() {
            return readString();
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);
        }
    }
    public static void main(String args[]) throws Exception {
        new Thread(null, new Main(),"Main",1<<27).start();
    }
    public void run() {
        InputReader sc = new InputReader(System.in);
        PrintWriter w = new PrintWriter(System.out);

        int t = sc.nextInt();

        for(int x = 0; x < t; x++) {
            int n = sc.nextInt();

            int weight[][] = new int[n][n];
            for(int i = 0; i < n; ++i) {
                for(int j = 0; j < n; ++j)
                    weight[i][j] = sc.nextInt();
            }

            w.println("B");
            w.flush();

            char type = sc.next().charAt(0);
            int node = sc.nextInt() - 1;

            int situation = 0;
            if(node >= n)
                situation ^= 1;

            if(type == 'D') {
                situation ^= 1;
            }

            int preferences[][] = new int[2 * n][n];

            if(situation == 0) {
                for(int i = 0; i < n; ++i) {
                    Pair p[] = new Pair[n];
                    for(int j = 0; j < n; ++j) {
                        p[j] = new Pair(n + j, weight[i][j]);
                    }
                    Arrays.sort(p, new Comparator<Pair>() {
                        public int compare(Pair p1, Pair p2) {
                            return p1.val - p2.val;
                        }
                    });
                    for(int j = 0; j < n; ++j)
                        preferences[i][j] = p[j].ind;
                }

                for(int i = 0; i < n; ++i) {
                    Pair p[] = new Pair[n];
                    for(int j = 0; j < n; ++j) {
                        p[j] = new Pair(j, weight[j][i]);
                    }
                    Arrays.sort(p, new Comparator<Pair>() {
                        public int compare(Pair p1, Pair p2) {
                            return p2.val - p1.val;
                        }
                    });

                    for(int j = 0; j < n; ++j)
                        preferences[n + i][j] = p[j].ind;
                }
            }
            else {
                for(int i = 0; i < n; ++i) {
                    Pair p[] = new Pair[n];
                    for(int j = 0; j < n; ++j) {
                        p[j] = new Pair(n + j, weight[i][j]);
                    }
                    Arrays.sort(p, new Comparator<Pair>() {
                        public int compare(Pair p1, Pair p2) {
                            return p2.val - p1.val;
                        }
                    });
                    for(int j = 0; j < n; ++j)
                        preferences[i][j] = p[j].ind;
                }

                for(int i = 0; i < n; ++i) {
                    Pair p[] = new Pair[n];
                    for(int j = 0; j < n; ++j) {
                        p[j] = new Pair(j, weight[j][i]);
                    }
                    Arrays.sort(p, new Comparator<Pair>() {
                        public int compare(Pair p1, Pair p2) {
                            return p1.val - p2.val;
                        }
                    });

                    for(int j = 0; j < n; ++j)
                        preferences[n + i][j] = p[j].ind;
                }
            }

            int match[] = StableMarriage.findStableMarriage(preferences);
            while(node != -2) {
                w.println((match[node] + 1));
                w.flush();
                node = sc.nextInt() - 1;
            }
        }

        w.close();
    }
}
class Pair {
    int ind, val;
    Pair(int a, int b) {
        ind = a;
        val = b;
    }
}
class StableMarriage {
    static int[] findStableMarriage(int[][] preferences) {
        // Also known as Gale-Shapley algorithm

        // 0 - (n - 1) ids are man, rest are woman
        // preferences is a 2n (first n man and last n woman) x n matrix which gives person id's sorted rank wise
        // match is an array of size 2*n - matched person for each id

        int n = preferences.length / 2;

        int[] match = new int[2 * n];
        Arrays.fill(match, -1);

        // A rank matrix of n x n, determines rank of each man for each woman
        int rank[][] = new int[n][n];
        for(int j = n; j < 2 * n; ++j) {
            for(int i = 0; i < n; ++i) {
                rank[j - n][preferences[j][i]] = i;
            }
        }

        Queue<Integer> notMatched = new LinkedList<>();
        for(int i = 0; i < n; ++i)
            notMatched.add(i);

        int nextRankToBeProposed[] = new int[n];

        while(!notMatched.isEmpty()) {
            int ind = notMatched.poll(); // current man who will propose

            while(true) {
                int j = preferences[ind][nextRankToBeProposed[ind]++]; // woman to be proposed

                if(match[j] == -1) { // if woman not matched yet
                    match[ind] = j;
                    match[j] = ind;
                    break;
                }
                else if(rank[j - n][match[j]] > rank[j - n][ind]) { // if this man more preferable
                    match[match[j]] = -1;
                    notMatched.add(match[j]); // previous man needs to propose again
                    match[ind] = j;
                    match[j] = ind;
                    break;
                }
            }
        }

        // if(!stable(preferences, match))
        //     System.out.println("Wrong code");

        return match;
    }
    static boolean stable(int[][] preferences, int[] match) {
        int n = match.length / 2;

        for(int i = 0; i < n; ++i) {
            for(int j = 0; j < n; ++j) {
                if(match[i] == j + n)
                    continue;

                int flag = 0;
                for(int k = 0; k < n; ++k) {
                    if(preferences[i][k] == match[i])
                        break;

                    if(preferences[i][k] == j + n) {
                        flag++;
                        break;
                    }
                }
                for(int k = 0; k < n; ++k) {
                    if(preferences[j + n][k] == match[j + n])
                        break;

                    if(preferences[j + n][k] == i) {
                        flag++;
                        break;
                    }
                }
                if(flag == 2)
                    return false;
            }
        }

        return true;
    }
}

import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;

import static java.lang.Math.*;

public class Main implements Runnable {
    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private SpaceCharFilter filter;
        private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars==-1)
                throw new InputMismatchException();

            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                }
                catch (IOException e) {
                    throw new InputMismatchException();
                }

                if(numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
        public int nextInt() {
            int c = read();

            while(isSpaceChar(c))
                c = read();

            int sgn = 1;

            if (c == '-') {
                sgn = -1;
                c = read();
            }

            int res = 0;
            do {
                if(c<'0'||c>'9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));

            return res * sgn;
        }

        public long nextLong() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;

            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));
                return res * sgn;
        }

        public double nextDouble() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            double res = 0;
            while (!isSpaceChar(c) && c != '.') {
                if (c == 'e' || c == 'E')
                    return res * Math.pow(10, nextInt());
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '.') {
                c = read();
                double m = 1;
                while (!isSpaceChar(c)) {
                    if (c == 'e' || c == 'E')
                        return res * Math.pow(10, nextInt());
                    if (c < '0' || c > '9')
                        throw new InputMismatchException();
                    m /= 10;
                    res += (c - '0') * m;
                    c = read();
                }
            }
            return res * sgn;
        }

        public String readString() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            }
            while (!isSpaceChar(c));

            return res.toString();
        }

        public boolean isSpaceChar(int c) {
            if (filter != null)
                return filter.isSpaceChar(c);
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public String next() {
            return readString();
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);
        }
    }
    public static void main(String args[]) throws Exception {
        new Thread(null, new Main(),"Main",1<<27).start();
    }
    public void run() {
        InputReader sc = new InputReader(System.in);
        PrintWriter w = new PrintWriter(System.out);

        int t = sc.nextInt();

        for(int x = 0; x < t; ++x) {
            int n = sc.nextInt();

            int weight[][] = new int[n][n];
            for(int i = 0; i < n; ++i) {
                for(int j = 0; j < n; ++j)
                    weight[i][j] = sc.nextInt();
            }

            w.println("B");
            w.flush();

            char type = sc.next().charAt(0);
            int node = sc.nextInt() - 1;

            int situation = 0;
            if(node >= n)
                situation ^= 1;

            if(type == 'D') {
                situation ^= 1;
            }

            int preferences[][] = new int[2 * n][n];

            if(situation == 0) {
                for(int i = 0; i < n; ++i) {
                    Pair p[] = new Pair[n];
                    for(int j = 0; j < n; ++j) {
                        p[j] = new Pair(n + j, weight[i][j]);
                    }
                    Arrays.sort(p, new Comparator<Pair>() {
                        public int compare(Pair p1, Pair p2) {
                            return p1.val - p2.val;
                        }
                    });
                    for(int j = 0; j < n; ++j)
                        preferences[i][j] = p[j].ind;
                }

                for(int i = 0; i < n; ++i) {
                    Pair p[] = new Pair[n];
                    for(int j = 0; j < n; ++j) {
                        p[j] = new Pair(j, weight[j][i]);
                    }
                    Arrays.sort(p, new Comparator<Pair>() {
                        public int compare(Pair p1, Pair p2) {
                            return p2.val - p1.val;
                        }
                    });

                    for(int j = 0; j < n; ++j)
                        preferences[n + i][j] = p[j].ind;
                }
            }
            else {
                for(int i = 0; i < n; ++i) {
                    Pair p[] = new Pair[n];
                    for(int j = 0; j < n; ++j) {
                        p[j] = new Pair(n + j, weight[i][j]);
                    }
                    Arrays.sort(p, new Comparator<Pair>() {
                        public int compare(Pair p1, Pair p2) {
                            return p2.val - p1.val;
                        }
                    });
                    for(int j = 0; j < n; ++j)
                        preferences[i][j] = p[j].ind;
                }

                for(int i = 0; i < n; ++i) {
                    Pair p[] = new Pair[n];
                    for(int j = 0; j < n; ++j) {
                        p[j] = new Pair(j, weight[j][i]);
                    }
                    Arrays.sort(p, new Comparator<Pair>() {
                        public int compare(Pair p1, Pair p2) {
                            return p1.val - p2.val;
                        }
                    });

                    for(int j = 0; j < n; ++j)
                        preferences[n + i][j] = p[j].ind;
                }
            }

            int match[] = StableMarriage.findStableMarriage(preferences);
            while(node != -2) {
                w.println((match[node] + 1));
                w.flush();
                node = sc.nextInt() - 1;
            }
        }

        w.close();
    }
}
class Pair {
    int ind, val;
    Pair(int a, int b) {
        ind = a;
        val = b;
    }
}
class StableMarriage {
    static int[] findStableMarriage(int[][] preferences) {
        // Also known as Gale-Shapley algorithm

        // 0 - (n - 1) ids are man, rest are woman
        // preferences is a 2n (first n man and last n woman) x n matrix which gives person id's sorted rank wise
        // match is an array of size 2*n - matched person for each id

        int n = preferences.length / 2;

        int[] match = new int[2 * n];
        Arrays.fill(match, -1);

        // A rank matrix of n x n, determines rank of each man for each woman
        int rank[][] = new int[n][n];
        for(int j = n; j < 2 * n; ++j) {
            for(int i = 0; i < n; ++i) {
                rank[j - n][preferences[j][i]] = i;
            }
        }

        Queue<Integer> notMatched = new LinkedList<>();
        for(int i = 0; i < n; ++i)
            notMatched.add(i);

        int nextRankToBeProposed[] = new int[n];

        while(!notMatched.isEmpty()) {
            int ind = notMatched.poll(); // current man who will propose

            while(true) {
                int j = preferences[ind][nextRankToBeProposed[ind]++]; // woman to be proposed

                if(match[j] == -1) { // if woman not matched yet
                    match[ind] = j;
                    match[j] = ind;
                    break;
                }
                else if(rank[j - n][match[j]] > rank[j - n][ind]) { // if this man more preferable
                    match[match[j]] = -1;
                    notMatched.add(match[j]); // previous man needs to propose again
                    match[ind] = j;
                    match[j] = ind;
                    break;
                }
            }
        }

        // if(!stable(preferences, match))
        //     System.out.println("Wrong code");

        return match;
    }
    static boolean stable(int[][] preferences, int[] match) {
        int n = match.length / 2;

        for(int i = 0; i < n; ++i) {
            for(int j = 0; j < n; ++j) {
                if(match[i] == j + n)
                    continue;

                int flag = 0;
                for(int k = 0; k < n; ++k) {
                    if(preferences[i][k] == match[i])
                        break;

                    if(preferences[i][k] == j + n) {
                        flag++;
                        break;
                    }
                }
                for(int k = 0; k < n; ++k) {
                    if(preferences[j + n][k] == match[j + n])
                        break;

                    if(preferences[j + n][k] == i) {
                        flag++;
                        break;
                    }
                }
                if(flag == 2)
                    return false;
            }
        }

        return true;
    }
}

import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;

import static java.lang.Math.*;

public class Main implements Runnable
{
    static class InputReader
    {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private SpaceCharFilter filter;
        private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        public InputReader(InputStream stream)
        {
            this.stream = stream;
        }

        public int read()
        {
            if (numChars == -1)
                throw new InputMismatchException();

            if (curChar >= numChars)
            {
                curChar = 0;
                try
                {
                    numChars = stream.read(buf);
                }
                catch (IOException e)
                {
                    throw new InputMismatchException();
                }

                if(numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }

        public int nextInt()
        {
            int c = read();

            while(isSpaceChar(c))
                c = read();

            int sgn = 1;

            if (c == '-')
            {
                sgn = -1;
                c = read();
            }

            int res = 0;
            do
            {
                if(c<'0'||c>'9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }

            while (!isSpaceChar(c));

            return res * sgn;
        }

        public long nextLong()
        {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;

            if (c == '-')
            {
                sgn = -1;
                c = read();
            }
            long res = 0;

            do
            {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));
                return res * sgn;
        }

        public double nextDouble()
        {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-')
            {
                sgn = -1;
                c = read();
            }

            double res = 0;
            while (!isSpaceChar(c) && c != '.')
            {
                if (c == 'e' || c == 'E')
                    return res * Math.pow(10, nextInt());
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }

            if (c == '.')
            {
                c = read();
                double m = 1;
                while (!isSpaceChar(c))
                {
                    if (c == 'e' || c == 'E')
                        return res * Math.pow(10, nextInt());
                    if (c < '0' || c > '9')
                        throw new InputMismatchException();
                    m /= 10;
                    res += (c - '0') * m;
                    c = read();
                }
            }

            return res * sgn;
        }

        public String readString()
        {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do
            {
                res.appendCodePoint(c);
                c = read();
            }

            while (!isSpaceChar(c));

            return res.toString();
        }

        public boolean isSpaceChar(int c)
        {
            if (filter != null)
                return filter.isSpaceChar(c);
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public String next()
        {
            return readString();
        }

        public interface SpaceCharFilter
        {
            public boolean isSpaceChar(int ch);
        }
    }

    public static void main(String args[]) throws Exception
    {
        new Thread(null, new Main(),"Main",1<<27).start();
    }

    public void run()
    {
        InputReader sc = new InputReader(System.in);
        PrintWriter w = new PrintWriter(System.out);

        int t = sc.nextInt();

        for(int x = 0; x < t; x++)
        {
            int n = sc.nextInt();

            int weight[][] = new int[n][n];
            for(int i = 0; i < n; i++)
            {
                for(int j = 0; j < n; j++)
                    weight[i][j] = sc.nextInt();
            }

            w.println("B");
            w.flush();

            char type = sc.next().charAt(0);
            int node = sc.nextInt() - 1;

            int situation = 0;
            if(node >= n)
                situation ^= 1;

            if(type == 'D')
            {
                situation ^= 1;
            }

            int preferences[][] = new int[2 * n][n];

            if(situation == 0)
            {
                for(int i = 0; i < n; ++i)
                {
                    Pair p[] = new Pair[n];
                    for(int j = 0; j < n; ++j)
                    {
                        p[j] = new Pair(n + j, weight[i][j]);
                    }

                    Arrays.sort(p, new Comparator<Pair>()
                    {
                        public int compare(Pair p1, Pair p2)
                        {
                            return p1.val - p2.val;
                        }
                    });

                    for(int j = 0; j < n; ++j)
                        preferences[i][j] = p[j].ind;
                }

                for(int i = 0; i < n; ++i)
                {
                    Pair p[] = new Pair[n];
                    for(int j = 0; j < n; ++j)
                    {
                        p[j] = new Pair(j, weight[j][i]);
                    }

                    Arrays.sort(p, new Comparator<Pair>()
                    {
                        public int compare(Pair p1, Pair p2)
                        {
                            return p2.val - p1.val;
                        }
                    });

                    for(int j = 0; j < n; ++j)
                        preferences[n + i][j] = p[j].ind;
                }
            }
            else
            {
                for(int i = 0; i < n; ++i)
                {
                    Pair p[] = new Pair[n];
                    for(int j = 0; j < n; ++j)
                    {
                        p[j] = new Pair(n + j, weight[i][j]);
                    }

                    Arrays.sort(p, new Comparator<Pair>()
                    {
                        public int compare(Pair p1, Pair p2)
                        {
                            return p2.val - p1.val;
                        }
                    });

                    for(int j = 0; j < n; ++j)
                        preferences[i][j] = p[j].ind;
                }

                for(int i = 0; i < n; ++i)
                {
                    Pair p[] = new Pair[n];
                    for(int j = 0; j < n; ++j)
                    {
                        p[j] = new Pair(j, weight[j][i]);
                    }

                    Arrays.sort(p, new Comparator<Pair>()
                    {
                        public int compare(Pair p1, Pair p2)
                        {
                            return p1.val - p2.val;
                        }
                    });

                    for(int j = 0; j < n; ++j)
                        preferences[n + i][j] = p[j].ind;
                }
            }

            int match[] = StableMarriage.findStableMarriage(preferences);
            while(node != -2)
            {
                w.println((match[node] + 1));
                w.flush();
                node = sc.nextInt() - 1;
            }
        }

        w.close();
    }
}

class Pair
{
    int ind, val;
    Pair(int a, int b)
    {
        ind = a;
        val = b;
    }
}

class StableMarriage
{
    static int[] findStableMarriage(int[][] preferences)
    {
        int n = preferences.length / 2;

        int[] match = new int[2 * n];
        Arrays.fill(match, -1);

        int rank[][] = new int[n][n];
        for(int j = n; j < 2 * n; ++j)
        {
            for(int i = 0; i < n; ++i)
            {
                rank[j - n][preferences[j][i]] = i;
            }
        }

        Queue<Integer> notMatched = new LinkedList<>();
        for(int i = 0; i < n; ++i)
            notMatched.add(i);

        int nextRankToBeProposed[] = new int[n];

        while(!notMatched.isEmpty())
        {
            int ind = notMatched.poll();

            while(true)
            {
                int j = preferences[ind][nextRankToBeProposed[ind]++];

                if(match[j] == -1)
                {
                    match[ind] = j;
                    match[j] = ind;
                    break;
                }
                else if(rank[j - n][match[j]] > rank[j - n][ind])
                {
                    match[match[j]] = -1;
                    notMatched.add(match[j]);
                    match[ind] = j;
                    match[j] = ind;
                    break;
                }
            }
        }

        return match;
    }

    static boolean stable(int[][] preferences, int[] match)
    {
        int n = match.length / 2;

        for(int i = 0; i < n; ++i)
        {
            for(int j = 0; j < n; ++j)
            {
                if(match[i] == j + n)
                    continue;

                int flag = 0;
                for(int k = 0; k < n; ++k)
                {
                    if(preferences[i][k] == match[i])
                        break;

                    if(preferences[i][k] == j + n)
                    {
                        flag++;
                        break;
                    }
                }

                for(int k = 0; k < n; ++k)
                {
                    if(preferences[j + n][k] == match[j + n])
                        break;

                    if(preferences[j + n][k] == i)
                    {
                        flag++;
                        break;
                    }
                }

                if(flag == 2)
                    return false;
            }
        }

        return true;
    }
}

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;

public class CF1161F {
    public static void main(String[] args) throws Exception {
        boolean local = false;
        boolean async = false;

        Charset charset = Charset.forName("ascii");

        FastIO io = local ? new FastIO(new FileInputStream("D:\\DATABASE\\TESTCASE\\Code.in"), System.out, charset) : new FastIO(System.in, System.out, charset);
        Task task = new Task(io, new Debug(local));

        if (async) {
            Thread t = new Thread(null, task, "dalt", 1 << 27);
            t.setPriority(Thread.MAX_PRIORITY);
            t.start();
            t.join();
        } else {
            task.run();
        }

        if (local) {
            io.cache.append("\n\n--memory -- \n" + ((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) >> 20) + "M");
        }

        io.flush();
    }

    public static class Task implements Runnable {
        final FastIO io;
        final Debug debug;
        int inf = (int) 1e8;
        int mod = (int) 1e9 + 7;

        public int mod(int val) {
            val %= mod;
            if (val < 0) {
                val += mod;
            }
            return val;
        }

        public int mod(long val) {
            val %= mod;
            if (val < 0) {
                val += mod;
            }
            return (int) val;
        }

        int bitAt(int x, int i) {
            return (x >> i) & 1;
        }

        int bitAt(long x, int i) {
            return (int) ((x >> i) & 1);
        }

        public Task(FastIO io, Debug debug) {
            this.io = io;
            this.debug = debug;
        }

        @Override
        public void run() {
            int t = io.readInt();
            while (t-- > 0) {
                solve();
            }
        }

        public void solve() {
            int n = io.readInt();
            int[][] boyFavors = new int[n][n];
            int[][] girlFavors = new int[n][n];
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    boyFavors[i][j] = io.readInt();
                    girlFavors[j][i] = boyFavors[i][j];
                }
            }

            io.cache.append("B\n");
            io.flush();

            char order = io.readChar();
            if (order == 'D') {
                for (int i = 0; i < n; i++) {
                    for (int j = 0; j < n; j++) {
                        boyFavors[i][j] = -boyFavors[i][j];
                        girlFavors[j][i] = -girlFavors[j][i];
                    }
                }
            }

            int start = io.readInt();
            int fixA = -1;
            int fixB = n + 1;
            if (start > n) {
                int[][] tmp = boyFavors;
                boyFavors = girlFavors;
                girlFavors = tmp;
                fixA = -n - 1;
                fixB = 1;
            }

            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    boyFavors[i][j] = -boyFavors[i][j];
                }
            }

            StableMarriage marriage = new StableMarriage(boyFavors, girlFavors);
            while (start >= 0) {
                int huband = start + fixA;
                int wife = marriage.wifeOf(huband) + fixB;
                io.cache.append(wife).append('\n');
                io.flush();
                start = io.readInt();
            }
        }
    }

    public static class StableMarriage {
        private Girl[] girls;
        private Boy[] boys;

        private static class Girl {
            Boy fere;
            int id;
        }

        private static class Boy {
            Girl fere;
            Deque<Girl> remainChoices = new ArrayDeque<>();
            int id;
        }

        public StableMarriage(final int[][] boyFavors, final int[][] girlFavors) {
            int n = boyFavors.length;
            boys = new Boy[n];
            girls = new Girl[n];
            for (int i = 0; i < n; i++) {
                girls[i] = new Girl();
                girls[i].id = i;
            }
            for (int i = 0; i < n; i++) {
                final Boy boy = new Boy();
                boy.id = i;
                Arrays.sort(girls, (a, b) -> -Integer.compare(boyFavors[boy.id][a.id], boyFavors[boy.id][b.id]));
                boy.remainChoices.addAll(Arrays.asList(girls));
                boys[i] = boy;
            }
            Arrays.sort(girls, (a, b) -> Integer.compare(a.id, b.id));

            Deque<Boy> unmarried = new ArrayDeque<>(Arrays.asList(boys));
            while (!unmarried.isEmpty()) {
                Boy head = unmarried.removeFirst();
                Girl girl = head.remainChoices.removeFirst();
                if (girl.fere == null) {
                    combine(head, girl);
                } else if (girlFavors[girl.id][girl.fere.id] < girlFavors[girl.id][head.id]) {
                    girl.fere.fere = null;
                    unmarried.addLast(girl.fere);
                    combine(head, girl);
                } else {
                    unmarried.addFirst(head);
                }
            }
        }

        public int husbandOf(int id) {
            return girls[id].fere.id;
        }

        public int wifeOf(int id) {
            return boys[id].fere.id;
        }

        public void combine(Boy boy, Girl girl) {
            boy.fere = girl;
            girl.fere = boy;
        }

    }

    public static class FastIO {
        public final StringBuilder cache = new StringBuilder();
        private final InputStream is;
        private final OutputStream os;
        private final Charset charset;
        private StringBuilder defaultStringBuf = new StringBuilder(1 << 8);
        private byte[] buf = new byte[1 << 13];
        private int bufLen;
        private int bufOffset;
        private int next;

        public FastIO(InputStream is, OutputStream os, Charset charset) {
            this.is = is;
            this.os = os;
            this.charset = charset;
        }

        public FastIO(InputStream is, OutputStream os) {
            this(is, os, Charset.forName("ascii"));
        }

        private int read() {
            while (bufLen == bufOffset) {
                bufOffset = 0;
                try {
                    bufLen = is.read(buf);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                if (bufLen == -1) {
                    return -1;
                }
            }
            return buf[bufOffset++];
        }

        public void skipBlank() {
            while (next >= 0 && next <= 32) {
                next = read();
            }
        }

        public int readInt() {
            int sign = 1;

            skipBlank();
            if (next == '+' || next == '-') {
                sign = next == '+' ? 1 : -1;
                next = read();
            }

            int val = 0;
            if (sign == 1) {
                while (next >= '0' && next <= '9') {
                    val = val * 10 + next - '0';
                    next = read();
                }
            } else {
                while (next >= '0' && next <= '9') {
                    val = val * 10 - next + '0';
                    next = read();
                }
            }

            return val;
        }

        public long readLong() {
            int sign = 1;

            skipBlank();
            if (next == '+' || next == '-') {
                sign = next == '+' ? 1 : -1;
                next = read();
            }

            long val = 0;
            if (sign == 1) {
                while (next >= '0' && next <= '9') {
                    val = val * 10 + next - '0';
                    next = read();
                }
            } else {
                while (next >= '0' && next <= '9') {
                    val = val * 10 - next + '0';
                    next = read();
                }
            }

            return val;
        }

        public double readDouble() {
            boolean sign = true;
            skipBlank();
            if (next == '+' || next == '-') {
                sign = next == '+';
                next = read();
            }

            long val = 0;
            while (next >= '0' && next <= '9') {
                val = val * 10 + next - '0';
                next = read();
            }
            if (next != '.') {
                return sign ? val : -val;
            }
            next = read();
            long radix = 1;
            long point = 0;
            while (next >= '0' && next <= '9') {
                point = point * 10 + next - '0';
                radix = radix * 10;
                next = read();
            }
            double result = val + (double) point / radix;
            return sign ? result : -result;
        }

        public String readString(StringBuilder builder) {
            skipBlank();

            while (next > 32) {
                builder.append((char) next);
                next = read();
            }

            return builder.toString();
        }

        public String readString() {
            defaultStringBuf.setLength(0);
            return readString(defaultStringBuf);
        }

        public int readLine(char[] data, int offset) {
            int originalOffset = offset;
            while (next != -1 && next != '\n') {
                data[offset++] = (char) next;
                next = read();
            }
            return offset - originalOffset;
        }

        public int readString(char[] data, int offset) {
            skipBlank();

            int originalOffset = offset;
            while (next > 32) {
                data[offset++] = (char) next;
                next = read();
            }

            return offset - originalOffset;
        }

        public int readString(byte[] data, int offset) {
            skipBlank();

            int originalOffset = offset;
            while (next > 32) {
                data[offset++] = (byte) next;
                next = read();
            }

            return offset - originalOffset;
        }

        public char readChar() {
            skipBlank();
            char c = (char) next;
            next = read();
            return c;
        }

        public void flush() {
            try {
                os.write(cache.toString().getBytes(charset));
                os.flush();
                cache.setLength(0);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        public boolean hasMore() {
            skipBlank();
            return next != -1;
        }
    }

    public static class Debug {
        private boolean allowDebug;

        public Debug(boolean allowDebug) {
            this.allowDebug = allowDebug;
        }

        public void assertTrue(boolean flag) {
            if (!allowDebug) {
                return;
            }
            if (!flag) {
                fail();
            }
        }

        public void fail() {
            throw new RuntimeException();
        }

        public void assertFalse(boolean flag) {
            if (!allowDebug) {
                return;
            }
            if (flag) {
                fail();
            }
        }

        private void outputName(String name) {
            System.out.print(name + " = ");
        }

        public void debug(String name, int x) {
            if (!allowDebug) {
                return;
            }

            outputName(name);
            System.out.println("" + x);
        }

        public void debug(String name, long x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println("" + x);
        }

        public void debug(String name, double x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println("" + x);
        }

        public void debug(String name, int[] x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println(Arrays.toString(x));
        }

        public void debug(String name, long[] x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println(Arrays.toString(x));
        }

        public void debug(String name, double[] x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println(Arrays.toString(x));
        }

        public void debug(String name, Object x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println("" + x);
        }

        public void debug(String name, Object... x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println(Arrays.deepToString(x));
        }
    }
}

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;

public class CF1161F {
    public static void main(String[] args) throws Exception {
        boolean local = false;
        boolean async = false;

        Charset charset = Charset.forName("ascii");

        FastIO io = local ? new FastIO(new FileInputStream("D:\\DATABASE\\TESTCASE\\Code.in"), System.out, charset) : new FastIO(System.in, System.out, charset);
        Task task = new Task(io, new Debug(local));

        if (async) {
            Thread t = new Thread(null, task, "dalt", 1 << 27);
            t.setPriority(Thread.MAX_PRIORITY);
            t.start();
            t.join();
        } else {
            task.run();
        }

        if (local) {
            io.cache.append("\n\n--memory -- \n" + ((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) >> 20) + "M");
        }

        io.flush();
    }

    public static class Task implements Runnable {
        final FastIO io;
        final Debug debug;
        int inf = (int) 1e8;
        int mod = (int) 1e9 + 7;

        public int mod(int val) {
            val %= mod;
            if (val < 0) {
                val += mod;
            }
            return val;
        }

        public int mod(long val) {
            val %= mod;
            if (val < 0) {
                val += mod;
            }
            return (int) val;
        }

        int bitAt(int x, int i) {
            return (x >> i) & 1;
        }

        int bitAt(long x, int i) {
            return (int) ((x >> i) & 1);
        }

        public Task(FastIO io, Debug debug) {
            this.io = io;
            this.debug = debug;
        }

        @Override
        public void run() {
            int t = io.readInt();
            while (t-- > 0) {
                solve();
            }
        }

        public void solve() {
            int n = io.readInt();
            int[][] boyFavors = new int[n][n];
            int[][] girlFavors = new int[n][n];
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    boyFavors[i][j] = -io.readInt();
                    girlFavors[j][i] = -boyFavors[i][j];
                }
            }

            io.cache.append("B\n");
            io.flush();

            char order = io.readChar();
            if (order == 'D') {
                for (int i = 0; i < n; i++) {
                    for (int j = 0; j < n; j++) {
                        boyFavors[i][j] = -boyFavors[i][j];
                        girlFavors[j][i] = -girlFavors[j][i];
                    }
                }
            }

            int start = io.readInt();
            int fixA = -1;
            int fixB = n + 1;
            if (start > n) {
                int[][] tmp = boyFavors;
                boyFavors = girlFavors;
                girlFavors = tmp;
                fixA = -n - 1;
                fixB = 1;
            }

            StableMarriage marriage = new StableMarriage(boyFavors, girlFavors);
            while (start >= 0) {
                int huband = start + fixA;
                int wife = marriage.wifeOf(huband) + fixB;
                io.cache.append(wife).append('\n');
                io.flush();
                start = io.readInt();
            }
        }
    }

    public static class StableMarriage {
        private Girl[] girls;
        private Boy[] boys;

        private static class Girl {
            Boy fere;
            int id;
        }

        private static class Boy {
            Girl fere;
            Deque<Girl> remainChoices = new ArrayDeque<>();
            int id;
        }

        public StableMarriage(final int[][] boyFavors, final int[][] girlFavors) {
            int n = boyFavors.length;
            boys = new Boy[n];
            girls = new Girl[n];
            for (int i = 0; i < n; i++) {
                girls[i] = new Girl();
                girls[i].id = i;
            }
            for (int i = 0; i < n; i++) {
                final Boy boy = new Boy();
                boy.id = i;
                Arrays.sort(girls, (a, b) -> -Integer.compare(boyFavors[boy.id][a.id], boyFavors[boy.id][b.id]));
                boy.remainChoices.addAll(Arrays.asList(girls));
                boys[i] = boy;
            }
            Arrays.sort(girls, (a, b) -> Integer.compare(a.id, b.id));

            Deque<Boy> unmarried = new ArrayDeque<>(Arrays.asList(boys));
            while (!unmarried.isEmpty()) {
                Boy head = unmarried.removeFirst();
                Girl girl = head.remainChoices.removeFirst();
                if (girl.fere == null) {
                    combine(head, girl);
                } else if (girlFavors[girl.id][girl.fere.id] < girlFavors[girl.id][head.id]) {
                    girl.fere.fere = null;
                    unmarried.addLast(girl.fere);
                    combine(head, girl);
                } else {
                    unmarried.addFirst(head);
                }
            }
        }

        public int husbandOf(int id) {
            return girls[id].fere.id;
        }

        public int wifeOf(int id) {
            return boys[id].fere.id;
        }

        public void combine(Boy boy, Girl girl) {
            boy.fere = girl;
            girl.fere = boy;
        }

    }

    public static class FastIO {
        public final StringBuilder cache = new StringBuilder();
        private final InputStream is;
        private final OutputStream os;
        private final Charset charset;
        private StringBuilder defaultStringBuf = new StringBuilder(1 << 8);
        private byte[] buf = new byte[1 << 13];
        private int bufLen;
        private int bufOffset;
        private int next;

        public FastIO(InputStream is, OutputStream os, Charset charset) {
            this.is = is;
            this.os = os;
            this.charset = charset;
        }

        public FastIO(InputStream is, OutputStream os) {
            this(is, os, Charset.forName("ascii"));
        }

        private int read() {
            while (bufLen == bufOffset) {
                bufOffset = 0;
                try {
                    bufLen = is.read(buf);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                if (bufLen == -1) {
                    return -1;
                }
            }
            return buf[bufOffset++];
        }

        public void skipBlank() {
            while (next >= 0 && next <= 32) {
                next = read();
            }
        }

        public int readInt() {
            int sign = 1;

            skipBlank();
            if (next == '+' || next == '-') {
                sign = next == '+' ? 1 : -1;
                next = read();
            }

            int val = 0;
            if (sign == 1) {
                while (next >= '0' && next <= '9') {
                    val = val * 10 + next - '0';
                    next = read();
                }
            } else {
                while (next >= '0' && next <= '9') {
                    val = val * 10 - next + '0';
                    next = read();
                }
            }

            return val;
        }

        public long readLong() {
            int sign = 1;

            skipBlank();
            if (next == '+' || next == '-') {
                sign = next == '+' ? 1 : -1;
                next = read();
            }

            long val = 0;
            if (sign == 1) {
                while (next >= '0' && next <= '9') {
                    val = val * 10 + next - '0';
                    next = read();
                }
            } else {
                while (next >= '0' && next <= '9') {
                    val = val * 10 - next + '0';
                    next = read();
                }
            }

            return val;
        }

        public double readDouble() {
            boolean sign = true;
            skipBlank();
            if (next == '+' || next == '-') {
                sign = next == '+';
                next = read();
            }

            long val = 0;
            while (next >= '0' && next <= '9') {
                val = val * 10 + next - '0';
                next = read();
            }
            if (next != '.') {
                return sign ? val : -val;
            }
            next = read();
            long radix = 1;
            long point = 0;
            while (next >= '0' && next <= '9') {
                point = point * 10 + next - '0';
                radix = radix * 10;
                next = read();
            }
            double result = val + (double) point / radix;
            return sign ? result : -result;
        }

        public String readString(StringBuilder builder) {
            skipBlank();

            while (next > 32) {
                builder.append((char) next);
                next = read();
            }

            return builder.toString();
        }

        public String readString() {
            defaultStringBuf.setLength(0);
            return readString(defaultStringBuf);
        }

        public int readLine(char[] data, int offset) {
            int originalOffset = offset;
            while (next != -1 && next != '\n') {
                data[offset++] = (char) next;
                next = read();
            }
            return offset - originalOffset;
        }

        public int readString(char[] data, int offset) {
            skipBlank();

            int originalOffset = offset;
            while (next > 32) {
                data[offset++] = (char) next;
                next = read();
            }

            return offset - originalOffset;
        }

        public int readString(byte[] data, int offset) {
            skipBlank();

            int originalOffset = offset;
            while (next > 32) {
                data[offset++] = (byte) next;
                next = read();
            }

            return offset - originalOffset;
        }

        public char readChar() {
            skipBlank();
            char c = (char) next;
            next = read();
            return c;
        }

        public void flush() {
            try {
                os.write(cache.toString().getBytes(charset));
                os.flush();
                cache.setLength(0);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        public boolean hasMore() {
            skipBlank();
            return next != -1;
        }
    }

    public static class Debug {
        private boolean allowDebug;

        public Debug(boolean allowDebug) {
            this.allowDebug = allowDebug;
        }

        public void assertTrue(boolean flag) {
            if (!allowDebug) {
                return;
            }
            if (!flag) {
                fail();
            }
        }

        public void fail() {
            throw new RuntimeException();
        }

        public void assertFalse(boolean flag) {
            if (!allowDebug) {
                return;
            }
            if (flag) {
                fail();
            }
        }

        private void outputName(String name) {
            System.out.print(name + " = ");
        }

        public void debug(String name, int x) {
            if (!allowDebug) {
                return;
            }

            outputName(name);
            System.out.println("" + x);
        }

        public void debug(String name, long x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println("" + x);
        }

        public void debug(String name, double x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println("" + x);
        }

        public void debug(String name, int[] x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println(Arrays.toString(x));
        }

        public void debug(String name, long[] x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println(Arrays.toString(x));
        }

        public void debug(String name, double[] x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println(Arrays.toString(x));
        }

        public void debug(String name, Object x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println("" + x);
        }

        public void debug(String name, Object... x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println(Arrays.deepToString(x));
        }
    }
}

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;

public class CF1161F {
    public static void main(String[] args) throws Exception {
        boolean local = false;
        boolean async = false;

        Charset charset = Charset.forName("ascii");

        FastIO io = local ? new FastIO(new FileInputStream("D:\\DATABASE\\TESTCASE\\Code.in"), System.out, charset) : new FastIO(System.in, System.out, charset);
        Task task = new Task(io, new Debug(local));

        if (async) {
            Thread t = new Thread(null, task, "dalt", 1 << 27);
            t.setPriority(Thread.MAX_PRIORITY);
            t.start();
            t.join();
        } else {
            task.run();
        }

        if (local) {
            io.cache.append("\n\n--memory -- \n" + ((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) >> 20) + "M");
        }

        io.flush();
    }

    public static class Task implements Runnable {
        final FastIO io;
        final Debug debug;
        int inf = (int) 1e8;
        int mod = (int) 1e9 + 7;

        public int mod(int val) {
            val %= mod;
            if (val < 0) {
                val += mod;
            }
            return val;
        }

        public int mod(long val) {
            val %= mod;
            if (val < 0) {
                val += mod;
            }
            return (int) val;
        }

        int bitAt(int x, int i) {
            return (x >> i) & 1;
        }

        int bitAt(long x, int i) {
            return (int) ((x >> i) & 1);
        }

        public Task(FastIO io, Debug debug) {
            this.io = io;
            this.debug = debug;
        }

        @Override
        public void run() {
            int t = io.readInt();
            while (t-- > 0) {
                solve();
            }
        }

        public void solve() {
            int n = io.readInt();
            int[][] boyFavors = new int[n][n];
            int[][] girlFavors = new int[n][n];
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    boyFavors[i][j] = io.readInt();
                    girlFavors[j][i] = -boyFavors[i][j];
                }
            }

            io.cache.append("B\n");
            io.flush();

            char order = io.readChar();
            if (order == 'D') {
                for (int i = 0; i < n; i++) {
                    for (int j = 0; j < n; j++) {
                        boyFavors[i][j] = -boyFavors[i][j];
                        girlFavors[j][i] = -girlFavors[j][i];
                    }
                }
            }

            int start = io.readInt();
            int fixA = -1;
            int fixB = n + 1;
            if (start > n) {
                int[][] tmp = boyFavors;
                boyFavors = girlFavors;
                girlFavors = tmp;
                fixA = -n - 1;
                fixB = 1;
            }

            StableMarriage marriage = new StableMarriage(boyFavors, girlFavors);
            while (start >= 0) {
                int huband = start + fixA;
                int wife = marriage.wifeOf(huband) + fixB;
                io.cache.append(wife).append('\n');
                io.flush();
                start = io.readInt();
            }
        }
    }

    public static class StableMarriage {
        private Girl[] girls;
        private Boy[] boys;

        private static class Girl {
            Boy fere;
            int id;
        }

        private static class Boy {
            Girl fere;
            Deque<Girl> remainChoices = new ArrayDeque<>();
            int id;
        }

        public StableMarriage(final int[][] boyFavors, final int[][] girlFavors) {
            int n = boyFavors.length;
            boys = new Boy[n];
            girls = new Girl[n];
            for (int i = 0; i < n; i++) {
                girls[i] = new Girl();
                girls[i].id = i;
            }
            for (int i = 0; i < n; i++) {
                final Boy boy = new Boy();
                boy.id = i;
                Arrays.sort(girls, (a, b) -> -Integer.compare(boyFavors[boy.id][a.id], boyFavors[boy.id][b.id]));
                boy.remainChoices.addAll(Arrays.asList(girls));
                boys[i] = boy;
            }
            Arrays.sort(girls, (a, b) -> Integer.compare(a.id, b.id));

            Deque<Boy> unmarried = new ArrayDeque<>(Arrays.asList(boys));
            while (!unmarried.isEmpty()) {
                Boy head = unmarried.removeFirst();
                Girl girl = head.remainChoices.removeFirst();
                if (girl.fere == null) {
                    combine(head, girl);
                } else if (girlFavors[girl.id][girl.fere.id] < girlFavors[girl.id][head.id]) {
                    girl.fere.fere = null;
                    unmarried.addLast(girl.fere);
                    combine(head, girl);
                } else {
                    unmarried.addFirst(head);
                }
            }
        }

        public int husbandOf(int id) {
            return girls[id].fere.id;
        }

        public int wifeOf(int id) {
            return boys[id].fere.id;
        }

        public void combine(Boy boy, Girl girl) {
            boy.fere = girl;
            girl.fere = boy;
        }

    }

    public static class FastIO {
        public final StringBuilder cache = new StringBuilder();
        private final InputStream is;
        private final OutputStream os;
        private final Charset charset;
        private StringBuilder defaultStringBuf = new StringBuilder(1 << 8);
        private byte[] buf = new byte[1 << 13];
        private int bufLen;
        private int bufOffset;
        private int next;

        public FastIO(InputStream is, OutputStream os, Charset charset) {
            this.is = is;
            this.os = os;
            this.charset = charset;
        }

        public FastIO(InputStream is, OutputStream os) {
            this(is, os, Charset.forName("ascii"));
        }

        private int read() {
            while (bufLen == bufOffset) {
                bufOffset = 0;
                try {
                    bufLen = is.read(buf);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                if (bufLen == -1) {
                    return -1;
                }
            }
            return buf[bufOffset++];
        }

        public void skipBlank() {
            while (next >= 0 && next <= 32) {
                next = read();
            }
        }

        public int readInt() {
            int sign = 1;

            skipBlank();
            if (next == '+' || next == '-') {
                sign = next == '+' ? 1 : -1;
                next = read();
            }

            int val = 0;
            if (sign == 1) {
                while (next >= '0' && next <= '9') {
                    val = val * 10 + next - '0';
                    next = read();
                }
            } else {
                while (next >= '0' && next <= '9') {
                    val = val * 10 - next + '0';
                    next = read();
                }
            }

            return val;
        }

        public long readLong() {
            int sign = 1;

            skipBlank();
            if (next == '+' || next == '-') {
                sign = next == '+' ? 1 : -1;
                next = read();
            }

            long val = 0;
            if (sign == 1) {
                while (next >= '0' && next <= '9') {
                    val = val * 10 + next - '0';
                    next = read();
                }
            } else {
                while (next >= '0' && next <= '9') {
                    val = val * 10 - next + '0';
                    next = read();
                }
            }

            return val;
        }

        public double readDouble() {
            boolean sign = true;
            skipBlank();
            if (next == '+' || next == '-') {
                sign = next == '+';
                next = read();
            }

            long val = 0;
            while (next >= '0' && next <= '9') {
                val = val * 10 + next - '0';
                next = read();
            }
            if (next != '.') {
                return sign ? val : -val;
            }
            next = read();
            long radix = 1;
            long point = 0;
            while (next >= '0' && next <= '9') {
                point = point * 10 + next - '0';
                radix = radix * 10;
                next = read();
            }
            double result = val + (double) point / radix;
            return sign ? result : -result;
        }

        public String readString(StringBuilder builder) {
            skipBlank();

            while (next > 32) {
                builder.append((char) next);
                next = read();
            }

            return builder.toString();
        }

        public String readString() {
            defaultStringBuf.setLength(0);
            return readString(defaultStringBuf);
        }

        public int readLine(char[] data, int offset) {
            int originalOffset = offset;
            while (next != -1 && next != '\n') {
                data[offset++] = (char) next;
                next = read();
            }
            return offset - originalOffset;
        }

        public int readString(char[] data, int offset) {
            skipBlank();

            int originalOffset = offset;
            while (next > 32) {
                data[offset++] = (char) next;
                next = read();
            }

            return offset - originalOffset;
        }

        public int readString(byte[] data, int offset) {
            skipBlank();

            int originalOffset = offset;
            while (next > 32) {
                data[offset++] = (byte) next;
                next = read();
            }

            return offset - originalOffset;
        }

        public char readChar() {
            skipBlank();
            char c = (char) next;
            next = read();
            return c;
        }

        public void flush() {
            try {
                os.write(cache.toString().getBytes(charset));
                os.flush();
                cache.setLength(0);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        public boolean hasMore() {
            skipBlank();
            return next != -1;
        }
    }

    public static class Debug {
        private boolean allowDebug;

        public Debug(boolean allowDebug) {
            this.allowDebug = allowDebug;
        }

        public void assertTrue(boolean flag) {
            if (!allowDebug) {
                return;
            }
            if (!flag) {
                fail();
            }
        }

        public void fail() {
            throw new RuntimeException();
        }

        public void assertFalse(boolean flag) {
            if (!allowDebug) {
                return;
            }
            if (flag) {
                fail();
            }
        }

        private void outputName(String name) {
            System.out.print(name + " = ");
        }

        public void debug(String name, int x) {
            if (!allowDebug) {
                return;
            }

            outputName(name);
            System.out.println("" + x);
        }

        public void debug(String name, long x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println("" + x);
        }

        public void debug(String name, double x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println("" + x);
        }

        public void debug(String name, int[] x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println(Arrays.toString(x));
        }

        public void debug(String name, long[] x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println(Arrays.toString(x));
        }

        public void debug(String name, double[] x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println(Arrays.toString(x));
        }

        public void debug(String name, Object x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println("" + x);
        }

        public void debug(String name, Object... x) {
            if (!allowDebug) {
                return;
            }
            outputName(name);
            System.out.println(Arrays.deepToString(x));
        }
    }
}