//T1(60)
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define lowbit(x) ((x)&(-(x)))
using namespace std;
int bit(int s, int i){
	return (s >> i) & 1;
}

int T, n, sz[(1 << 10) + 10];
bool f1[(1 << 10) + 10];
string s1, s2, t1, t2;

bool chk(int m, string &s, string &t){
	int cnt[2][2];
	memset(cnt, 0, sizeof(cnt));
	for (int i = 0; i < n; i++){
		cnt[0][bit(m, i)]++;
		cnt[1][s[i + 1] - '0']++;
		if (i == n - 1 || t[i + 1] == '0' || t[i + 2] == '0'){
			if (cnt[0][0] != cnt[1][0] || cnt[0][1] != cnt[1][1])
				return false;
			memset(cnt, 0, sizeof(cnt));
		}
	}
	return true;
}

void solve(){
	cin >> n >> s1 >> s2 >> t1 >> t2;
	s1 = ' ' + s1, s2 = ' ' + s2, t1 = ' ' + t1, t2 = ' ' + t2;
	
	bool equ = true;
	for (int i = 2; i <= n; i++)
		if (s1[i] != s1[i - 1])
			equ = false;
	if (equ){
		int ans = 0;
		for (int i = 1; i <= n; i++)
			ans += (s1[i] == s2[i]);
		cout << ans << "\n";
		return ;
	}
	if (t1 == t2){
		int cnt[2][2], ans = 0;
		memset(cnt, 0, sizeof(cnt));
		for (int i = 1; i <= n; i++){
			if (t1[i - 1] == '0' || t1[i] == '0')
				memset(cnt, 0, sizeof(cnt));
			cnt[0][s1[i] - '0']++, cnt[1][s2[i] - '0']++;
			if (cnt[0][0] && cnt[1][0])
				cnt[0][0]--, cnt[1][0]--, ans++;
			if (cnt[0][1] && cnt[1][1])
				cnt[0][1]--, cnt[1][1]--, ans++;
		}
		cout << ans << "\n";
		return ;
	}
	if (n <= 10){
		int ans = 0;
		for (int m1 = 0; m1 < (1 << n); m1++){
			sz[m1] = __builtin_popcount(m1);
			f1[m1] = chk(m1, s1, t1);
		}
		for (int m2 = 0; m2 < (1 << n); m2++)
			if (chk(m2, s2, t2))
				for (int m1 = 0; m1 < (1 << n); m1++)
					if (f1[m1])
						ans = max(ans, n - sz[m1 ^ m2]);
		cout << ans << "\n";
		return ;
	}
	cout << "0\n";
}

int main(){
	freopen("edit.in", "r", stdin);
	freopen("edit.out", "w", stdout);
	
	ios::sync_with_stdio(false);
	cin >> T;
	for (int it = 1; it <= T; it++)
		solve();
	return 0;
}
