// DONT USE GREEDY IF YOU CAN DP
// TASK 4 BRUTEFORCES AND TASK 1 SUBTASKS CAN GET SO MANY POINTS??? THEN WHY IM WRITING THIS HUGE IMPLEMENTATION???
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define umap unordered_map
#define emp emplace
#define empb emplace_back
#define mkp make_pair

const ll MAXN = 1e5 + 5;

ll n = 0;
string s[2];
bool t[2][MAXN];
ll sep[MAXN];

void srh(ll& ret, set<pair<string, string> >& vis, string s1, string s2) {
	if (vis.find(mkp(s1, s2)) != vis.end()) {
		return;
	}
	vis.emp(mkp(s1, s2));
	ll cnt = 0;
	for (ll i = 1; i <= n; ++i) {
		cnt += s1[i] == s2[i];
	}
	ret = max(ret, cnt);
	for (ll i = 1; i < n; ++i) {
		if (!t[0][i] || !t[0][i + 1]) {
			continue;
		}
		string s3 = s1;
		swap(s3[i], s3[i + 1]);
		srh(ret, vis, s3, s2);
	}
	for (ll i = 1; i < n; ++i) {
		if (!t[1][i] || !t[1][i + 1]) {
			continue;
		}
		string s3 = s2;
		swap(s3[i], s3[i + 1]);
		srh(ret, vis, s1, s3);
	}
}
void bruteforce() {
	ll ans = 0;
	static set<pair<string, string> > vis;
	srh(ans, vis, s[0], s[1]);
	cout << ans << endl;
}

void A() {
	char tg = s[0][1];
	ll ans = 0;
	for (char i : s[1]) {
		ans += i == tg;
	}
	cout << ans << endl;
}

void B() {
	ll last_ns = 1;
	for (ll i = 1; i <= n; ++i) {
		if (sep[i]) {
//			cerr << last_ns << ' ' << i << endl;
			sort(s[0].begin() + last_ns, s[0].begin() + i);
			sort(s[1].begin() + last_ns, s[1].begin() + i);
			last_ns = i + 1;
		}
	}
	sort(s[0].begin() + last_ns, s[0].end());
	sort(s[1].begin() + last_ns, s[1].end());
//	cerr << s[0] << endl << s[1] << endl;
	ll cnt = 0;
	for (ll i = 1; i <= n; ++i) {
		cnt += s[0][i] == s[1][i];
	}
	cout << cnt << endl;
}

void C() {
	bruteforce();
}

void init() {
	s[0] = s[1] = string();
	for (ll i = 1; i <= n; ++i) {
		t[0][i] = t[1][i] = sep[i] = 0;
	}
}
int mian() {
	cin >> n >> s[0] >> s[1];
	s[0] = '<' + s[0];
	s[1] = '>' + s[1];
	bool as = 1;
	for (ll i = 2; i <= n; ++i) {
		if (s[0][i] != s[0][i - 1]) {
			as = 0;
		}
	}
	bool ts = 1;
	ll c0[2] = {};
	for (ll i = 1; i <= n; ++i) {
		char c;
		cin >> c;
		t[0][i] = c - 48;
		if (!t[0][i]) {
			sep[i] |= 1;
			++c0[0];
		}
	}
	for (ll i = 1; i <= n; ++i) {
		char c;
		cin >> c;
		t[1][i] = c - 48;
		if (!t[1][i]) {
			sep[i] |= 2;
			++c0[1];
		}
		if (t[1][i] != t[0][i]) {
			ts = 0;
		}
	}
	if (as) {
		A();
	} else if (ts) {
		B();
	} else if (c0[0] == 1 && c0[1] == 1) {
		C();
	} else {
		bruteforce();
	}
	return 0;
}

int main() {
	freopen("edit.in", "r", stdin);
	freopen("edit.out", "w", stdout);
	ll T;
	cin >> T;
	while (T--) {
		init();
		mian();
	}
	return 0;
}











































// T1: how to write code?
// T2: how to optimize the dp?
// T3: how?
// T4: how?
// 184. Nobody is worse than me.
// 1=? To far for me to reach.
// End of OI (maybe)
/*
    +++++
    +   +
    +   +
+++++   +++++
+     OI    +
+2022.09.08 +
+2024.11.30 +
+++++   +++++
    +   +
 i  +   +  i
 |  +++++  |
*/
