#include <bits/stdc++.h>
using namespace std;
string ss1,ss2,tt1,tt2;
int mul[100005],mur[100005];
int mdl[100005],mdr[100005];
stack<int > pq0; //可换且未对齐的0；
stack<int> wpq0; //可换且未对齐的0；
stack<int> pq1; //可换且未对齐的1；
stack<int> wpq1; //可换且未对齐的0；
void init(){
	while(!pq0.empty()) pq0.pop();
	while(!pq1.empty()) pq1.pop();
}
int main(){
	freopen("edit.in","r",stdin);
	freopen("edit.out","w",stdout);
	int T; scanf("%d",&T);
	while(T--){
		int n; scanf("%d",&n);
		int ans=0;
		cin >> ss1 >> ss2;
		cin >> tt1 >> tt2;
		
		int pos1=0,pos2=0;
		memset(mul,0,sizeof mul); memset(mur,0,sizeof mur); memset(mdl,0,sizeof mdl); memset(mdr,0,sizeof mdr);
		for(int i=0;i<n;++i){
			if(tt1[i]=='0'){
				mul[i]=-1;
				pos1=i+1;
			}
			else mul[i]=pos1;
			
			if(tt2[i]=='0'){
				mdl[i]=-1;
				pos2=i+1;
			}
			else mdl[i]=pos2;
		}
		pos1=n-1,pos2=n-1;
		for(int i=n-1;i>=0;--i){
			if(tt1[i]=='0'){
				mur[i]=-1;
				pos1=i-1;
			}
			else mur[i]=pos1;
			
			if(tt2[i]=='0'){
				mdr[i]=-1;
				pos2=i-1;
			}
			else mdr[i]=pos2;
		}
		
		//ss1
		init();
		for(int i=0;i<n;++i){
			if(tt1[i]=='0') init();
			
			if(ss1[i]==ss2[i]) continue;
			else if(ss1[i]!=ss2[i]){
				if(tt1[i]=='1'){
					if(ss1[i]=='0'){
						if(pq1.empty())
							pq0.push(i);
						else{
							int poss=pq1.top(); pq1.pop();
							swap(ss1[i],ss1[poss]);
						}
					}
					else if(ss1[i]=='1'){
						if(pq0.empty())
							pq1.push(i);
						else{
							int poss=pq0.top(); pq0.pop();
							swap(ss1[i],ss1[poss]);
						}
					}
				}
			}
		}
		
		//ss2
		init();
		for(int i=0;i<n;++i){
			if(tt2[i]=='0') init();
			
			if(ss2[i]==ss1[i]) continue;
			else if(ss2[i]!=ss1[i]){
				if(tt2[i]=='1'){
					if(ss2[i]=='0'){
						if(pq1.empty())
							pq0.push(i);
						else{
							int poss=pq1.top(); pq1.pop();
							swap(ss2[i],ss2[poss]);
						}
					}
					else if(ss2[i]=='1'){
						if(pq0.empty())
							pq1.push(i);
						else{
							int poss=pq0.top(); pq0.pop();
							swap(ss2[i],ss2[poss]);
						}
					}
				}
			}
		}
		//处理交界外
		init();
		while(!wpq0.empty()) wpq0.pop();
		while(!wpq1.empty()) wpq1.pop();
		for(int i=0;i<n;++i){
			if(tt1[i]=='1'&&tt2[i]=='1'){
				if(mul[i]<mdl[i]&&mur[i]<mdr[i]){
					for(int j=mul[i];j<=mur[i];++j){
						if(j<mdl[i]){
							if(ss1[j]==ss2[j]) continue;
							if(ss1[j]=='1'){
								wpq1.push(j);
							}
							else{
								wpq0.push(j);
							}
						}
						else{
							if(ss1[j]==ss2[j]){
								if(ss1[j]=='1'){
									pq1.push(j);
								}
								else{
									pq0.push(j);
								}
							}
						}
					}
					for(int j=mur[i]+1;j<=mdr[i];++j){
						if(ss1[j]!=ss2[j]){
							if(ss2[j]=='1'&&!pq0.empty()&&!wpq1.empty()){
								int tmp0=pq0.top(),tmp1=wpq1.top();
								pq0.pop(),wpq1.pop();
								swap(ss2[j],ss2[tmp0]); swap(ss1[tmp1],ss1[tmp0]);
							}
							if(ss2[j]=='0'&&!pq1.empty()&&!wpq0.empty()){
								int tmp1=pq1.top(),tmp0=wpq0.top();
								pq1.pop(),wpq0.pop();
								swap(ss2[j],ss2[tmp1]); swap(ss1[tmp1],ss1[tmp0]);
							}
						}
					}
					i=min(mur[i]+1,mdr[i]+1);
				}
				
				if(mul[i]>mdl[i]&&mur[i]>mdr[i]){
					for(int j=mdl[i];j<=mdr[i];++j){
						if(j<mul[i]){
							if(ss1[j]==ss2[j]) continue;
							if(ss2[j]=='1'){
								wpq1.push(j);
							}
							else{
								wpq0.push(j);
							}
						}
						else{
							if(ss1[j]==ss2[j]){
								if(ss2[j]=='1'){
									pq1.push(j);
								}
								else{
									pq0.push(j);
								}
							}
						}
					}
					for(int j=mdr[i]+1;j<=mur[i];++j){
						if(ss1[j]!=ss2[j]){
							if(ss1[j]=='1'&&!pq0.empty()&&!wpq1.empty()){
								int tmp0=pq0.top(),tmp1=wpq1.top();
								pq0.pop(),wpq1.pop();
								swap(ss1[j],ss1[tmp0]); swap(ss2[tmp1],ss2[tmp0]);
							}
							if(ss1[j]=='0'&&!pq1.empty()&&!wpq0.empty()){
								int tmp1=pq1.top(),tmp0=wpq0.top();
								pq1.pop(),wpq0.pop();
								swap(ss1[j],ss1[tmp1]); swap(ss2[tmp1],ss2[tmp0]);
							}
						}
					}
					i=min(mur[i]+1,mdr[i]+1);
				}
				
			}
			
			init();
			while(!wpq0.empty()) wpq0.pop();
			while(!wpq1.empty()) wpq1.pop();	
		} 
		
		for(int i=0;i<n;++i)
			if(ss1[i]==ss2[i]) ++ans;
		printf("%d\n",ans);
	}
} 
