// Arup Guha
// 3/7/2019
// Solution to 2019 Mercer Programming Contest Problem 9: Meow Meow Meow

import java.util.*;

public class meow {
	
	// Between any swapped pair, one will be either on left or top.
	final public static int[] DX = {0,1};
	final public static int[] DY = {1,0};

	// Problem Constants.
	final public static int MATCH3 = 100;
	final public static int MATCH4 = 200;
	final public static int MATCH5 = 400;
	final public static int CASCADE = 1000;
	
	final public static int N = 8;
	public static char[][] orig;

	public static void main(String[] args) {
	
		Scanner stdin = new Scanner(System.in);
		int nC = stdin.nextInt();
		
		// Process each case.
		for (int loop=0; loop<nC; loop++) {
		
			// Get grid.
			orig = new char[N][];
			for (int i=0; i<N; i++)
				orig[i] = stdin.next().toCharArray();
			
			int res = 0;
			
			// Try all swaps.
			for (int i=0; i<N; i++) {
				for (int j=0; j<N; j++) {
					for (int k=0; k<DX.length; k++) {
						if (!inbounds(i+DX[k],j+DY[k])) continue;
						
						// Resulting board from swap.
						char[][] newb = getNewBoard(i,j,i+DX[k],j+DY[k]);
						
						// See if this one's better.
						res = Math.max(res, score(newb));				
					}
				}
			}
			
			// Ta da!
			System.out.println(res);
		}
	}
	
	public static char[][] getNewBoard(int x1, int y1, int x2, int y2) {
		
		// Copy...
		char[][] res = new char[N][N];
		for (int i=0; i<N; i++)
			for (int j=0; j<N; j++)
				res[i][j] = orig[i][j];
			
		// Swap and make lower case (to track bonuses)
		char tmp = res[x1][y1];
		res[x1][y1] = res[x2][y2];
		res[x2][y2] = tmp;
		res[x1][y1] = (char)(res[x1][y1] - 'A' + 'a');
		res[x2][y2] = (char)(res[x2][y2] - 'A' + 'a');
		
		return res;
	}
	
	// Returns the score of this board.
	public static int score(char[][] b) {
		
		// Keep on playing until no pts on a round.
		int res = 0;
		int rndScore = round(b);
		while (rndScore > 0) {
			res += rndScore;
			rndScore = round(b);
		}
		
		// Ta da!
		return res;
	}
	
	public static int round(char[][] b) {
		
		// Will mark squares to drop.
		boolean[][] marked = new boolean[N][N];
		
		int res = 0;
		
		// Look for horizontal matches.
		for (int i=0; i<N; i++) {
			int j = 0;
			while (j < N) {
				
				// Find # of matching chars.
				int k = j+1;
				while (k<N && eq(b[i][j],b[i][k])) k++;
				
				// Get regular matches.
				if (k - j == 3) res += MATCH3;
				else if (k - j == 4) res += MATCH4;
				else if (k - j >= 5) res += MATCH5;
				else { j = k; continue; }
				
				// Look for cascade bonus.
				boolean casc = true;
				for (int z=j+1; z<k; z++)
					if (b[i][j] != b[i][z])
						casc = false;
					
				// Marked for deletion.
				for (int z=j; z<k; z++) marked[i][z] = true;
					
				// Award if necessary.
				if (casc) res += CASCADE;
					
				// Update for row.
				j = k;
			}
		}
		
		// Look for vertical matches.
		for (int j=0; j<N; j++) {
			int i = 0;
			while (i < N) {
				
				// Find # of matching chars.
				int k = i+1;
				while (k<N && eq(b[i][j],b[k][j])) k++;
				
				// Get regular matches.
				if (k - i == 3) res += MATCH3;
				else if (k - i == 4) res += MATCH4;
				else if (k - i >= 5) res += MATCH5;
				else { i = k; continue; }
				
				// Look for cascade bonus.
				boolean casc = true;
				for (int z=i+1; z<k; z++)
					if (b[i][j] != b[z][j])
						casc = false;
					
				// Marked for deletion.
				for (int z=i; z<k; z++) marked[z][j] = true;
					
				// Award if necessary.
				if (casc) res += CASCADE;
					
				// Update for row.
				i = k;
			}
		}

		// Drop in spaces.
		drop(b, marked);
		
		// Ta da!
		return res;
	}
	
	public static void drop(char[][] b, boolean[][] marked) {
		
		// All dropped chars at first.
		char[][] res = new char[N][N];
		for (int i=0; i<N; i++) Arrays.fill(res[i], ' ');
	
		// Process each column.
		for (int j=0; j<N; j++) {
			int resI = N-1;
			for (int i=N-1; i>=0; i--) 
				if (!marked[i][j]) 
					res[resI--][j] = b[i][j];
		}
		
		// Copy back.
		for (int i=0; i<N; i++)
			for (int j=0; j<N; j++)
				b[i][j] = res[i][j];
	}
	
	public static boolean eq(char c, char d) {
		if (!Character.isLetter(c) || !Character.isLetter(d)) return false;
		return Character.toLowerCase(c) == Character.toLowerCase(d);
	}
	
	public static boolean inbounds(int x, int y) {
		return x >= 0 && x < N && y >= 0 && y < N;
	}
}