#include<bits/stdc++.h>

#define ll long long
#define db double
#define lb lower_bound
#define up upper_bound
#define pb push_back
#define eb emplace_back
#define il inline
using namespace std;

inline long long read(){
	ll res=0,f=1; char c = getchar();
	while('0' > c || c > '9'){ if(c=='-')f=-1; c=getchar(); }
	while('0' <= c && c <= '9'){ res = (res<<1)+(res<<3)+(c-48); c=getchar(); }
	return res*f;
} 

inline void write(long long x){
	if(x < 0) putchar('-'),x=-x;
	if(0 <= x && x <= 9) putchar(x+48);
	else write(x/10),putchar(x%10+48);
	return ;
}

const int MAXN = 1e5+10;

ll n;
char s1[MAXN],s2[MAXN];
char opt1[MAXN],opt2[MAXN];
int sum[MAXN][2];//统计前缀上'1'的个数 
int pos[MAXN][2],cnt[2]; //每个不能操作位的位置 

ll ans = 0;
int now,lst,sum0,sum1;
int t1=0,t0=0,flupd=1;

void solve(int &mx,int &mi,int mxid,int miid){
	//cout << mx << " " << mi << " " << mxid << " " << miid << " " << flupd << endl;
	
	now = pos[mx][mxid],lst = pos[mx-1][mxid];
	sum1 = sum[now-1][mxid] - sum[lst][mxid],sum0 = now-lst-1-sum1;
	 
	//cout << now << " " << lst << " " << sum0 << " " << sum1 << endl;
	while(mi <= cnt[miid] && pos[mx][mxid] >= pos[mi][miid]){
		if(flupd){
			t1 = sum[pos[mi][miid]-1][miid] - sum[pos[mi-1][miid]][miid];
			t0 = pos[mi][miid]-1-pos[mi-1][miid]-t1,flupd=0;
		}
		
		//cout << t0 << " " << t1 << endl;
		if(sum1 >= t1) ans += t1,sum1 -= t1,t1 = 0;
		else ans += sum1,t1 -= sum1,sum1 = 0,sum0 -= t1,t1 = 0;
		if(sum0 >= t0) ans += t0,sum0 -= t0,t0 = 0;
		else ans += sum0,t0 -= sum0,sum0 = 0,sum1 -= t0,t0 = 0;
		
		if(pos[mx][mxid] == pos[mi][miid]) break;
		if(mxid == 0){
			if(s2[pos[mi][miid]] == '0'){
				if(sum0) sum0--,ans++; else sum1--;
			}else if(s2[pos[mi][miid]] == '1'){
				if(sum1) sum1--,ans++; 
				else sum0--;
			} 
		}else{
			if(s1[pos[mi][miid]] == '0'){
				if(sum0) sum0--,ans++; else sum1--;
			}else if(s1[pos[mi][miid]] == '1'){
				if(sum1) sum1--,ans++; 
				else sum0--;
			} 
		}
		mi++;
		if(pos[mi][miid] <= pos[mx][mxid]) flupd=1;
	}if(pos[mi][miid] > pos[mx][mxid]) t0 = sum0,t1 = sum1;
	//cout << sum0 << "-" << sum1 << endl;
	//cout << ans << endl;
}

void init(){
	ans = 0;flupd=1;
	memset(sum,0,sizeof(sum));
	memset(pos,0,sizeof(pos));
	cnt[0] = cnt[1] = 0;t1 = 0,t0 = 0,sum1 = 0,sum0 = 0;
}

int main(){
	freopen("edit.in","r",stdin);
	freopen("edit.out","w",stdout);
	ll Tc;Tc = read();
	while(Tc--){
		n = read(); init();
		scanf("%s",s1+1); scanf("%s",s2+1); s1[n+1] = '0',s2[n+1] = '1';//确保最后的corner case不出错 
		sum[0][0] = 0,sum[0][1] = 0;
		for(int i = 1;i <= n;i++) sum[i][0] = sum[i-1][0] + (s1[i]=='1'),sum[i][1] = sum[i-1][1]+(s2[i]=='1');  
		scanf("%s",opt1+1); scanf("%s",opt2+1);
		for(int i = 1;i <= n;i++){
			if(opt1[i] == '0') pos[++cnt[0]][0] = i;
			if(opt2[i] == '0') pos[++cnt[1]][1] = i;
		}
		pos[++cnt[0]][0] = n+1,pos[++cnt[1]][1] = n+1; 
		int ida=1,idb=1;
		while(1){
			//cout << ida << " " << idb << " " << ans << " " << sum[pos[idb][1]][1] << " " << pos[idb][1] << endl << endl;
			
			if(pos[ida][0] >= pos[idb][1]){ solve(ida,idb,0,1); }
			else if(pos[ida][0] < pos[idb][1]){ solve(idb,ida,1,0); }
			if(pos[ida][0] == pos[idb][1]){
				if(ida == cnt[0] && idb == cnt[1]) break;
				ans += (s1[pos[ida][0]] == s2[pos[idb][1]]); 
				ida++,idb++;t0 = 0,t1 = 0,flupd=1;
			} 
		} printf("%lld\n",ans);
	} return 0;
}// NOIP2024 Rp++
