import java.util.Scanner; import java.util.Random; public class Rooks { private static boolean solveRooks(int [] positions_x, int [] positions_y) { boolean isAttackFree = true; /* ------------------- INSERT CODE HERE ---------------------*/ boolean[] x_checked = new boolean[9]; // easiest to assume the chess board is 9x9 boolean[] y_checked = new boolean[9]; for(int i = 0; i < x_checked.length; i++) { x_checked[i] = false; y_checked[i] = false; } for(int i = 0; i < positions_x.length; i++) { if(x_checked[positions_x[i]] || y_checked[positions_y[i]]) { isAttackFree = false; break; } else { x_checked[positions_x[i]] = true; y_checked[positions_y[i]] = true; } } /* -------------------- END OF INSERTION --------------------*/ return isAttackFree; } private static void sanity(int[] positions_x, int[] positions_y) { boolean[][] used = new boolean[9][9]; for(int ii = 0; ii < 9; ii++) for(int jj = 0; jj < 9; jj++) used[ii][jj] = false; for(int i = 0; i < positions_x.length; i++) { if(used[positions_x[i]][positions_y[i]]) { System.out.println("ERROR"); System.exit(1); } else { used[positions_x[i]][positions_y[i]] = true; } } } private static void GenerateData() { Random rand = new Random(20); System.out.println(100); boolean[][] used = new boolean[9][9]; for(int i = 0; i < 100; i++) { int numRooks = 1 + rand.nextInt(14); for(int ii = 0; ii < 9; ii++) for(int jj = 0; jj < 9; jj++) used[ii][jj] = false; System.out.print(numRooks); while(numRooks > 0) { int x = 1 + rand.nextInt(8); int y = 1 + rand.nextInt(8); if(! used[x][y]) { System.out.print(" " + x + " " + y); used[x][y] = true; numRooks--; } } System.out.println(); } } public static void main(String[] args) { // GenerateData(); Scanner sc = new Scanner(System.in); int numCases = sc.nextInt(); for(int i = 0; i < numCases; i++) { int numRooks = sc.nextInt(); int[] positions_x = new int[numRooks]; int[] positions_y = new int[numRooks]; for(int j = 0; j < numRooks; j++) { positions_x[j] = sc.nextInt(); positions_y[j] = sc.nextInt(); } if(solveRooks(positions_x, positions_y)) { System.out.println("SAFE"); } else { System.out.println("NOT SAFE"); } } } }