#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
string st1, st2;
bool ts1[N], ts2[N];
int n;
vector<string> stat1, stat2;
vector<vector<string> > sk1, sk2;

void dfs1(int num, string str) {
	if (num == sk1.size()) {
		stat1.push_back(str);
		return ;
	}
	for (string pl : sk1[num]) {
		dfs1(num + 1, str + pl);
	}
}

void dfs2(int num, string str) {
	if (num == sk2.size()) {
		stat2.push_back(str);
		return ;
	}
	for (string pl : sk2[num]) {
		dfs2(num + 1, str + pl);
	}
}

int main() {
	freopen("edit.in", "r", stdin);
	freopen("edit.out", "w", stdout);
	ios::sync_with_stdio(false);
	cin.tie(false), cout.tie(false);
	int T;
	cin >> T;
	while (T--) {
		cin >> n;
		cin >> st1 >> st2;
//		cout << st1 << ' ' << st2 << endl;
		stat1.clear(), stat2.clear(), sk1.clear(), sk2.clear();
		st1 = " " + st1, st2 = " " + st2;
		bool flagA = true, flagB = true, flagC = true;
		int cnt10 = 0, cnt20 = 0;
		char A = st1[1];
		for (int i = 1; i <= n; i++) {
			char x;
			cin >> x;
			ts1[i] = x - '0'; 
			if (ts1[i] == false)	cnt10 ++;
			if (st1[i] != A) {
				flagA = false;
			}
		}
		for (int i = 1; i <= n; i++) {
			char x;
			cin >> x;
			ts2[i] = x - '0';
			if (ts2[i] == false)	cnt20 ++;
			if (ts2[i] != ts1[i]) {
				flagB = false;
			}
		}
		if (cnt10 != cnt20 || cnt10 != 1 || cnt20 != 1)	flagC = false;
		if (flagA) {
			int ans = 0;
			for (int i = 1; i <= n; i++) {
				if (st2[i] == A) {
					ans++;
				}
			}
			cout << ans << endl;
		}
		else if (flagB) {
			int ans = 0;
			for (int i = 1; i <= n; i++) {
				if (ts1[i] == false) {
					if (st1[i] == st2[i]) {
						ans++;
					}
				}
				else {
					pair<int, int> st1Cnt = {0, 0}, st2Cnt = {0, 0};
					int j = i;
					while (ts1[j] == true && j <= n) {
						if (st1[j] == '1')	st1Cnt.first++;
						else	st1Cnt.second++;
						if (st2[j] == '1')	st2Cnt.first++;
						else	st2Cnt.second++;
						j++;
					}
					i = j - 1;
					ans = ans + min(st1Cnt.first, st2Cnt.first) + min(st1Cnt.second, st2Cnt.second);
				}
			}
			cout << ans << endl;
		}
		else if (flagC) {
			int ans = 0;
			int pos1 = 0, pos2 = 0;
			for (int i = 1; i <= n; i++) {
				if (ts1[i] == '0')	pos1 = i;
				if (ts2[i] == '0')	pos2 = i;
			}
			pair<int, int> st1Cnt = {0, 0}, st2Cnt = {0, 0};
			for (int i = 1; i < pos1; i++) {
				if (st1[i] == '1')	st1Cnt.first++;
				else	st1Cnt.second++;
			}
			for (int i = 1; i < pos2; i++) {
				if (st2[i] == '1')	st2Cnt.first++;
				else	st2Cnt.second++;
			}
			ans += min(st1Cnt.first, st2Cnt.first) + min(st2Cnt.second, st2Cnt.second);
			if (pos1 < pos2) {
				st2Cnt.first -= min(st1Cnt.first, st2Cnt.first), st2Cnt.second -= min(st2Cnt.second, st2Cnt.second);
				if (st1[pos1] == st2[pos1])	ans++;
				else {
					if (st1[pos1] == '1' && st2Cnt.first > 0)	st2Cnt.first--, ans++;
					if (st1[pos1] == '0' && st2Cnt.second > 0)	st2Cnt.second--, ans++;
				}
				st1Cnt = {0, 0};
				pair<int, int> st2CntT = {0, 0};
				for (int i = pos1 + 1; i <= n; i++) {
					if (st1[i] == '1')	st1Cnt.first++;
					else	st1Cnt.second++;
				}
				for (int i = pos2 + 1; i <= n; i++) {
					if (st2[i] == '1')	st2CntT.first++;
					else	st2CntT.second++;
				}
				ans += min(st1Cnt.first, st2CntT.first) + min(st1Cnt.second, st2CntT.second);
				st1Cnt.first -= min(st1Cnt.first, st2CntT.first), st1Cnt.second -= min(st1Cnt.second, st2CntT.second);
				if (st1[pos2] == st2[pos2])	ans++;
				else {
					if (st2[pos2] == '1' && st1Cnt.first > 0)	st1Cnt.first--, ans++;
					if (st2[pos2] == '0' && st1Cnt.second > 0)	st1Cnt.second--, ans++;
				}
				ans += min(st1Cnt.first, st2Cnt.first) + min(st1Cnt.second, st2Cnt.second);
			}
			else {
				st1Cnt.first -= min(st1Cnt.first, st2Cnt.first), st1Cnt.second -= min(st1Cnt.second, st2Cnt.second);
				if (st2[pos2] == st1[pos2])	ans++;
				else {
					if (st2[pos2] == '1' && st1Cnt.first > 0)	st1Cnt.first--, ans++;
					if (st2[pos2] == '0' && st1Cnt.second > 0)	st1Cnt.second--, ans++;
				}
				st2Cnt = {0, 0};
				pair<int, int> st1CntT = {0, 0};
				for (int i = pos2 + 1; i <= n; i++) {
					if (st2[i] == '1')	st2Cnt.first++;
					else	st2Cnt.second++;
				}
				for (int i = pos1 + 1; i <= n; i++) {
					if (st1[i] == '1')	st1CntT.first++;
					else	st1CntT.second++;
				}
				ans += min(st2Cnt.first, st1CntT.first) + min(st2Cnt.second, st1CntT.second);
				st2Cnt.first -= min(st2Cnt.first, st1CntT.first), st2Cnt.second -= min(st2Cnt.second, st1CntT.second);
				if (st1[pos1] == st2[pos1])	ans++;
				else {
					if (st1[pos1] == '1' && st2Cnt.first > 0)	st2Cnt.first--, ans++;
					if (st1[pos1] == '0' && st2Cnt.second > 0)	st2Cnt.second--, ans++;
				}
				ans += min(st1Cnt.first, st2Cnt.first) + min(st1Cnt.second, st2Cnt.second);
			}
			cout << ans << endl;
		}
		else {
			for (int i = 1; i <= n; i++) {
				if (ts1[i] == false) {
					string tmp = "";
					tmp += st1[i];
					sk1.push_back({tmp});
				}
				else {
					vector<string> tmp1;
					string tmp = "";
					int j = i;
					while (ts1[j] && j <= n) {
						tmp += st1[j];
						j++;
					}
					vector<char> mp(tmp.size());
					vector<int> narr(tmp.size()); 
					for (int k = 0; k < tmp.size(); k++) {
						mp[k] = tmp[k];
						narr[k] = k + 1;
					}
					do {
						string str1;
						for (int k = 0; k < tmp.size(); k++) {
							str1 += mp[narr[k] - 1];
						}
						tmp1.push_back(str1);
					} while (next_permutation(narr.begin(), narr.end()));
					sk1.push_back(tmp1);
					i = j - 1;
				}
			}
			for (int i = 1; i <= n; i++) {
				if (ts2[i] == false) {
					string tmp = "";
					tmp += st2[i];
					sk2.push_back({tmp});
				}
				else {
					vector<string> tmp1;
					string tmp = "";
					int j = i;
					while (ts2[j] && j <= n) {
						tmp += st2[j];
						j++;
					}
					vector<char> mp(tmp.size());
					vector<int> narr(tmp.size()); 
					for (int k = 0; k < tmp.size(); k++) {
						mp[k] = tmp[k];
						narr[k] = k + 1;
					}
					do {
						string str1;
						for (int k = 0; k < tmp.size(); k++) {
							str1 += mp[narr[k] - 1];
						}
						tmp1.push_back(str1);
					} while (next_permutation(narr.begin(), narr.end()));
					sk2.push_back(tmp1);
					i = j - 1;
				}
			}
			string tmp = "";
			dfs1(0, tmp);
			dfs2(0, tmp);
			int ans = 0;
			for (string str1 : stat1) {
				for (string str2 : stat2) {
//					cout << str1 << ' ' << str2 << endl;
					int res = 0;
					for (int i = 0; i < n; i++) {
						if (str1[i] == str2[i])	res++;
					}
					ans = max(ans, res);
				}
			}
			cout << ans << endl;
		}
	}
	
	return 0;
}
