#include <bits/stdc++.h>
using namespace std;

string sr1,sr2,tr1,tr2;
int n,T,tcnt;

void tta() //A
{
	tcnt = 0;
	for(int i = 0; i < n; ++i)
	{
		if(sr2[i] == sr1[i])
			++tcnt;
	} 
	printf("%d\n",tcnt);
	return;
} 

void ttc(int p1, int p2)
{
	if(tr1[p1] == '0' || tr2[p2] == '0')
	{
		printf("%d\n", n - 2);
		return;
	}
	else{
		int pp1 = -1, pp2 = -1;
		if(p1 > p2){ 
			for(int j = p2 + 1, k = p1 - 1, cl1 = 0, cl2 = 0; j <= p1, k >= p2; ++j, --k)
			{
				if(!cl1 && tr1[k] == '0')
				{
						++cl1;
						pp1 = k;
				}
				if(!cl2 && tr2[j] == '0')
				{
						++cl2;
						pp2 = j;
				}
				
			}
			if(pp2 > pp1)
			{
					printf("%d\n",n);
					return;
			} 
			else
			{
					printf("%d\n",n-2);
					return;
			} 
		} 
		if(p1 < p2){ 
			for(int j = p1 + 1, k = p2 - 1, cl1 = 0, cl2 = 0; j <= p2, k >= p1; ++j, --k)
			{
				if(cl1 && cl1 == cl2)break;
				if(!cl1 && tr1[j] == '0')
				{
						++cl1;
						pp1 = j;
				}
				if(!cl2 && tr2[k] == '0')
				{
						++cl2;
						pp2 = k;
				}
				
			}
			if(pp1 > pp2)
			{
					printf("%d\n",n);
					return;
			} 
			else
			{
					printf("%d\n",n-2);
					return;
			} 
		} 
	}
}

int main()
{
	freopen("edit.in","r",stdin);
	freopen("edit.out","w",stdout);
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		sr1 = "",sr2 = "", tr1 = "", tr2 = "";
		cin>>sr1;
		cin>>sr2;
		cin>>tr1;
		cin>>tr2;
		if(sr1 == sr2)//B
		{
			printf("%d\n",n);
			continue;
		}
		if(sr1.find('0') == -1 || sr1.find('1') == -1) //->A
		{
			tta();
			continue;
		}
		int pos10 = sr1.find('0');
		int pos20 = sr2.find('0');
		string sr3 = sr1.substr(pos10 + 1, n);
		string sr4 = sr2.substr(pos20 + 1, n);
		if(sr3.find('0') == -1 && sr4.find('0') == -1) //->C
		{
			if(pos10 == pos20)
				printf("%d\n",n);
			else
				ttc(pos10,pos20);
			continue;
		}
		else
			printf("0\n");
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
} 
