// Arup Guha
// 7/14/2025
// Solution to 2025 SI@UCF CP Camp Contest #2 Problem: Beautiful Triangles

import java.util.*;

public class beautiful {

	public static int n;
	public static int[][] tri;
	
	public static void main(String[] args) {
	
		Scanner stdin = new Scanner(System.in);
		int nC = stdin.nextInt();

		// Process cases.
		for (int loop=0; loop<nC; loop++) {
		
			// Read in the input.
			n = stdin.nextInt();
			tri = new int[n][];
			for (int i=0; i<n; i++) {
				tri[i] = new int[i+1];
				for (int j=0; j<=i; j++)
					tri[i][j] = stdin.nextInt();
			}
			
			// Solve it.
			System.out.println(solve());
		}
	}
	
	public static int solve() {
	
		// There are no 3 terms, so anything goes here if it's not filled.
		if (n == 1) {
			return tri[0][0] == 0 ? 3 : 1;
		}
		
		int res = 0;
		
		// Will store all possible valid triangles and just see which ones work.
		int[][] att = new int[n][];
		for (int i=0; i<n; i++) 
			att[i] = new int[i+1];
		
		// x is the code for the first two entries.
		for (int x=0; x<9; x++) {
		
			// Fill in first two entries.
			att[0][0] = x/3+1;
			att[1][0] = x%3+1;
			
			// Third is forced.
			att[1][1] = next(att[0][0], att[1][0]);
			
			// Skip if we're already inconsistent.
			if (att[0][0] != tri[0][0] && tri[0][0] != 0) continue;
			if (att[1][0] != tri[1][0] && tri[1][0] != 0) continue;
			if (att[1][1] != tri[1][1] && tri[1][1] != 0) continue;
			
			// Fill in all forces.
			for (int i=2; i<n; i++) {
				
				// Gotta go from the middle here.
				for (int j=1; j<i; j++)
					att[i][j] = next(att[i-1][j-1], att[i-1][j]);
				
				// Now we can do the end points.
				att[i][0] = next(att[i-1][0], att[i][1]);
				att[i][i] = next(att[i-1][i-1], att[i][i-1]);
			}
			
			// Add 1 if this is consistent with the input.
			if (consistent(att)) res++;
		}
		
		// Ta da!
		return res;
	}
	
	public static boolean consistent(int[][] att) {
		
		// Look for any inconsistencies.
		for (int i=0; i<n; i++)
			for (int j=0; j<=i; j++)
				if (tri[i][j] != 0 && tri[i][j] != att[i][j])
					return false;
				
		// If we get here we're good.
		return true;
	}
	
	// a, b are chosen from {1,2,3} returns c such that a+b+c = 0 mod 3.
	public static int next(int a, int b) {
		int c = (6-a-b)%3;
		return c == 0 ? 3 : c;
	}
}