// chess.cpp : Defines the entry point for the console application.
//

#include <stdio.h>
#include <string.h>
#include <math.h>


int main()
{
	freopen("chess.in","r",stdin);
	//freopen("chess.out","w",stdout);
	int tests;
	scanf("%d\n",&tests);
	for (int curtest = 1; curtest<=tests; ++curtest)
	{
		char a[20];
		char b[20];
		scanf("%c%c%c %c%c%c\n%c%c%c %c%c%c\n%c%c%c %c%c%c\n\n",
			&a[1], &a[4], &a[7], &b[1], &b[4], &b[7], 
			&a[6], &a[0], &a[2], &b[6], &b[0], &b[2], 
			&a[3], &a[8], &a[5], &b[3], &b[8], &b[5]);
		for (int i=1;i<=8;++i) a[i+8] = a[i];
		for (int i=1;i<=8;++i) b[i+8] = b[i];
		a[9] = 0; 
		b[9] = 0;
		char a_clear[10];
		strcpy (a_clear,a);

		if (a[0] != b[0]) 
		{
			//клетка b2 отличается.
			printf("-1\n");
			continue;
		}

		int pos_a[9];
		char fig_a[9]={0};
		int pos_b[9];
		char fig_b[9]={0};

		//перепишем позиции и тип фигур
		int count = 0;
		for (int i = 1; i <= 8; ++i)
			if (a[i]!='.')
			{
				pos_a[count] = i;
				fig_a[count++] = a[i];
			}

		count = 0;
		for (int i = 1; i <= 8; ++i)
			if (b[i]!='.')
			{
				pos_b[count] = i;
				fig_b[count++] = b[i];
			}

		int cycle = strlen(fig_b);
		for (int i = 0;i<cycle;++i)
		{
			fig_b[cycle+i] = fig_b[i];
			pos_b[cycle+i] = pos_b[i] + 8;
			pos_a[cycle+i] = pos_a[i] + 8;
		}

		int bestresult = 9999; // impossible
		for (int shift = 0; shift < cycle; ++shift)
			if (strstr(fig_b + shift, fig_a) == fig_b + shift)
			{
				//получили, что фигура а[i] может быть переставлена в b[i+shift]
				int moves = 0;
				for (int i=0;i<cycle;++i)
					moves += abs(pos_a[i] - pos_b[i + shift]);
				if (bestresult > moves)
					bestresult = moves;
				moves = 0;
				for (int i=0;i<cycle;++i)
					moves += abs(pos_a[i + cycle] - pos_b[i + shift]);
				if (bestresult > moves)
					bestresult = moves;
			}
		if (bestresult == 9999)
			printf("-1\n");
		else
			printf("%d\n",bestresult);
	}	
	return 0;
}

