#include <bits/stdc++.h>
using namespace std;
long long n, maxx = -1;
long long same(string a, string b) {
	long long num = 0;
	for (int i = 0; i < n; i++) {
		if (a[i] == b[i]) {
			num++;
		}
	}
	return num;
}/*没问题*/
string t3, t4;//都不能动的数,相同的数字
//t4为0代表要么不换，要么全换
long long dfs(string s1, string s2, string t1, string t2, int l) {
	if ((long long)max(same(s1,s2), (long long)maxx) == maxx) {
		return same(s1,s2);
	} else {
		for (int i = l; i < n; i++) { //以s1为基准进行分析
			if (t1[i] == '0') { //s1不能动
				if (t2[i] == '0') { //s2 不能动
					continue;
				} else {
					for (int j = i + 1; j < n; j++) { //从s2的 i+1 处开始寻找后继替换者
						if (s2[j] != s2[i] && t2[j] == '1') {
							swap(s2[j],s2[i]);
							maxx = max(maxx, same(s1, s2));
							/*if (maxx >= same(s1, s2)) {
								maxx = max(dfs(s1, s2, t1, t2, i), maxx);
							}*/
							maxx = max(dfs(s1, s2, t1, t2, i), maxx);
							swap(s2[j],s2[i]);//还原
						}
					}
				}
			} else { //S1 能动
				if (t4[i] == '1') { //本来就不相等
					for (int j = i + 1; j < n; j++) { //从s1的 i+1 处开始寻找后继替换者
						//s1 变换
						if (s1[j] != s1[i] && t1[j] == '1') {
							swap(s1[j],s1[i]);
							maxx = max(maxx, same(s1, s2));
							/*if (maxx >= same(s1, s2)) {
								maxx = max((long long)dfs(s1, s2, t1, t2, i), (long long)maxx);
							}*/
							maxx = max((long long)dfs(s1, s2, t1, t2, i), (long long)maxx);
							swap(s1[j],s1[i]);//还原
						}
					}
					for (int j = i + 1; j < n; j++) { //从s2的 i+1 处开始寻找后继替换者
						//s2变换
						if (s2[j] != s2[i] && t2[j] == '1') {
							swap(s2[j],s2[i]);
							maxx = max(maxx, same(s1, s2));
							/*if (maxx >= same(s1, s2)) {
								maxx = max((long long)dfs(s1, s2, t1, t2, i), (long long)maxx);
							}*/
							maxx = max((long long)dfs(s1, s2, t1, t2, i), (long long)maxx);
							swap(s2[j],s2[i]);//还原
						}
					}
				} /*else { //s1 s2 相等
					if (t2[i] == '0') {
						continue;
					} else {
						for (int j = i + 1; j < n; j++) {
							if (t1[j] == '1' && t2[j] == '1') { //判断交换是否可以联通使子段增长
								swap(s1[i], s1[j]);
								swap(s2[i], s2[j]);
								maxx = max(maxx, same(s1, s2));
								if (maxx >= same(s1, s2))
								{
									maxx = max((long long)dfs(s1, s2, t1, t2, i), (long long)maxx);
								}
								swap(s1[i], s1[j]);
								swap(s2[i], s2[j]);
							}
						}
				}*/
			}
		}
	}
}
int main() {
	freopen("edit.in","r",stdin);
	freopen("edit.out","w",stdout);
	int T;
	cin >> T;
	while (T--) {
		maxx = -1;//还原
		cin >> n;
		string s1, s2, t1, t2;
		cin >> s1 >> s2 >> t1 >> t2;
		/*预处理*/
		for (int i = 0; i < n; i++) {
			if (t1[i] == '0' && t2[i] == '0') {
				t3 = t3 + "0";
			} else {
				t3 = t3 + "1";
			}
			if (s1[i] == s2[i]) {
				t4 = t4 + "0";
			} else {
				t4 = t4 + "1";
			}
		}/*预处理没问题*/
		maxx = max(maxx, same(s1, s2));
		maxx = max(dfs(s1, s2, t1, t2, 0), maxx);
		for (int i = 0; i < n; i++) { //以s1为基准进行分析
			if (t1[i] == '0') { //s1不能动
				if (t2[i] == '0') { //s2 不能动
					continue;
				} else {
					for (int j = i + 1; j < n; j++) { //从s2的 i+1 处开始寻找后继替换者
						if (s2[j] != s2[i] && t2[j] == '1') {
							swap(s2[j],s2[i]);
							maxx = max(maxx, same(s1, s2));
							//cout << same(s1, s2) << "\n";
							swap(s2[j],s2[i]);//还原
						}
					}
				}
			} else { //S1 能动
				if (t4[i] == '1') { //本来就不相等
					for (int j = i + 1; j < n; j++) { //从s1的 i+1 处开始寻找后继替换者
						//s1 变换
						if (s1[j] != s1[i] && t1[j] == '1') {
							swap(s1[j],s1[i]);
							maxx = max(maxx, same(s1, s2));
							//cout << same(s1, s2) << "\n";
							swap(s1[j],s1[i]);//还原
						}
					}
					for (int j = i + 1; j < n; j++) { //从s2的 i+1 处开始寻找后继替换者
						//s2变换
						if (s2[j] != s2[i] && t2[j] == '1') {
							swap(s2[j],s2[i]);
							maxx = max(maxx, same(s1, s2));
							//cout << same(s1, s2) << "\n";
							swap(s2[j],s2[i]);//还原
						}
					}
				} else { //s1 s2 相等
					if (t2[i] == '0') {
						continue;
					} else {
						for (int j = i + 1; j < n; j++) {
							if (t1[j] == '1' && t2[j] == '1') { //判断交换是否可以联通使子段增长
								swap(s1[i], s1[j]);
								swap(s2[i], s2[j]);
								maxx = max(maxx, same(s1, s2));
								//cout << same(s1, s2) << "\n";
								swap(s1[i], s1[j]);
								swap(s2[i], s2[j]);
							}
						}
					}
				}
			}
		}
		//cout << t3 << endl;
		//cout << t4 << endl;
		//cout << same(s1, s2) << "\n";
		cout << maxx << endl;
	}
	return 0;
}
