#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
string s1,s2,t1,t2;
int n,ans,get_ans=0;
namespace A_B
{
	void work()
	{
		bool checkA=1;
		for(int i=0;i<n-1;i++)
			if(s1[i]!=s1[i+1])
			{
				checkA=0;
				break;
			}
		if(checkA)
		{
			for(int i=0;i<n;i++)
				if(s1[i]==s2[i])
					ans++;
			cout<<ans<<endl;
			get_ans=1;
			return;
		}
		if(t1==t2)//checkB
		{
			int lastl=-1,lastr=-1;
			for(int i=0;i<n;i++)
			{
				if(t1[i]=='1')
				{
					lastl=lastr=i;
					for(;lastr<n&&t1[lastr]=='1';lastr++);
					lastr--;
					sort(s1.begin()+lastl,s1.begin()+lastr+1);
					sort(s2.begin()+lastl,s2.begin()+lastr+1);
					i=lastr;
				}
			}
			for(int i=0;i<n;i++)
				if(s1[i]==s2[i])
					ans++;
			cout<<ans<<endl;
			get_ans=1;
			return;
		}
	}
}
namespace C
{
	void work()
	{
		int sum1=0,sum2=0;
		for(int i=0;i<n;i++)
		{
			if(t1[i]=='0') sum1++;
			if(t2[i]=='0') sum2++;
		}
		if(sum1==sum2&&sum1==1)
		{

		}
	}
}
namespace bf
{
	struct sub
	{
		int l,r,sum0=0,sum1=0;
	};
	vector<sub>sub1,sub2;
	string tmp1,tmp2;
	void work2(int i,int j)
	{
		if(i==sub2.size())
		{
			int sum=0;
			for(int i=0;i<n;i++)
				if(tmp1[i]==tmp2[i])
					sum++;
			ans=max(ans,sum);
			return;
		}
		if(sub2[i].sum0)
		{
			sub2[i].sum0--;
			char tmp=tmp2[j];
			tmp2[j]='0';
			if(j==sub2[i].r) work2(i+1,(i+1)==sub2.size()?0:sub2[i+1].l);
			else work2(i,j+1);
			tmp2[j]=tmp;
			sub2[i].sum0++;
		}
		if(sub2[i].sum1)
		{
			sub2[i].sum1--;
			char tmp=tmp2[j];
			tmp2[j]='1';
			if(j==sub2[i].r) work2(i+1,(i+1)==sub2.size()?0:sub2[i+1].l);
			else work2(i,j+1);
			tmp2[j]=tmp;
			sub2[i].sum1++;
		}
	}
	void work1(int i,int j)
	{
		if(i==sub1.size())
		{
			work2(0,sub2[0].l);
			return;
		}
		if(sub1[i].sum0)
		{
			sub1[i].sum0--;
			char tmp=tmp1[j];
			tmp1[j]='0';
			if(j==sub1[i].r) work1(i+1,(i+1)==sub1.size()?0:sub1[i+1].l);
			else work1(i,j+1);
			tmp1[j]=tmp;
			sub1[i].sum0++;
		}
		if(sub1[i].sum1)
		{
			sub1[i].sum1--;
			char tmp=tmp1[j];
			tmp1[j]='1';
			if(j==sub1[i].r) work1(i+1,(i+1)==sub1.size()?0:sub1[i+1].l);
			else work1(i,j+1);
			tmp1[j]=tmp;
			sub1[i].sum1++;
		}
	}
	void init()
	{
		tmp1=s1;tmp2=s2;
		sub1.clear();sub2.clear();
		for(int i=0;i<n;i++)
		{
			if(t1[i]=='1')
			{
				int l=i,r=i,sum1=0,sum0=0;
				while(r<n&&t1[r]=='1')
				{
					if(s1[r]=='0') sum0++;
					else sum1++;
					r++;
				}
				r--;
				if(l==r) continue;
				sub1.push_back(sub{l,r,sum0,sum1});
				i=r;
			}
		}
		for(int i=0;i<n;i++)
		{
			if(t2[i]=='1')
			{
				int l=i,r=i,sum1=0,sum0=0;
				while(r<n&&t2[r]=='1')
				{
					if(s2[r]=='0') sum0++;
					else sum1++;
					r++;
				}
				r--;
				if(l==r) continue;
				sub2.push_back(sub{l,r,sum0,sum1});
				i=r;
			}
		}
		work1(0,sub1[0].l);
		cout<<ans<<endl;
	}
}
int main()
{
	freopen("edit.in","r",stdin);
	freopen("edit.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	int T;
	cin>>T;
	while(T--)
	{
		ans=0;
		cin>>n>>s1>>s2>>t1>>t2;
		A_B::work();
		// C::work();
		if(get_ans) continue;
		if(n<=10) bf::init();
		else
		{
			sort(s1.begin(),s1.end());
			sort(s2.begin(),s2.end());
			ans=0;
			for(int i=0;i<n;i++)
				if(s1[i]==s2[i])
					ans++;
			cout<<ans<<endl;
		}
	}
	//T1都A不了是不是可以退役了
	//我当然不能知道我有没有估错难度
	//盲评一手 绿 蓝 紫 黑
	//所以都说难度上去了是真的吗
}