#include <fstream>
using namespace std;
ifstream fin("edit.in");
ofstream fout("edit.out");
namespace tc5t8 {
bool verify(int n, string s1, string s2, string t1, string t2) {
char c = s1[0];
for (auto x : s1) if (x != c) return false;
return true;
}
void solve(int n, string s1, string s2, string t1, string t2) {
int cnt = 0;
for(int i = 0; i < n; ++i) {
if (s1[i] == s2[i]) ++cnt;
}
fout << cnt << endl;
}
}
namespace tc9t12 {
bool verify(int n, string s1, string s2, string t1, string t2) {
return t1 == t2;
}
void solve(int n, string s1, string s2, string t1, string t2) {
int grLeft = 1;
int ans = 0;
s1 = " " + s1;
s2 = " " + s2;
t1 = " " + t1;
s1 += '1';
s2 += '0';
t1 += '0';
for (int i = 1; i <= n + 1; ++i) {
if (t1[i] == '0') {
if (grLeft != i) {
int grRight = i - 1;
int cntS1 = 0;
for (int j = grLeft; j <= grRight; ++j) {
if (s1[j] == '1') ++cntS1;
}
int cntS2 = 0;
for (int j = grLeft; j <= grRight; ++j) {
if (s2[j] == '1') ++cntS2;
}
ans += grRight - grLeft + 1 - abs(cntS2 - cntS1);
}
if (s1[i] == s2[i]) ++ans;
grLeft = i + 1;
}
}
fout << ans << endl;
}
}
namespace tc13t16 {
bool verify(int n, string s1, string s2, string t1, string t2) {
int cnt0 = 0;
for (auto x : t1) if (x == '0') ++cnt0;
if (cnt0 != 1) return false;
for (auto x : t2) if (x == '0') ++cnt0;
return cnt0 == 2;
}
void solve(int n, string s1, string s2, string t1, string t2) {
int zero1 = 0;
int zero2 = 0;
s1 = " " + s1;
s2 = " " + s2;
t1 = " " + t1;
t2 = " " + t2;
for (int i = 1; i <= n; ++i)
if (t1[i] == '0') {
zero1 = i; break;
}
for (int i = 1; i <= n; ++i)
if (t2[i] == '0') {
zero2 = i; break;
}
if (zero1 > zero2) {
swap(s1, s2);
swap(t1, t2);
swap(zero1, zero2);
}
if (zero1 == zero2) {
tc9t12::solve(n, s1, s2, t1, t2);
return;
}
int leftUp = 0;
for (int i = 1; i <= zero1; ++i) {
if (s1[i] == '1') ++leftUp;
if (s2[i] == '1') --leftUp;
}
int midUp = 0;
for (int i = zero1 + 1; i < zero2; ++i) {
if (s1[i] == '1') ++midUp;
if (s2[i] == '1') --midUp;
}
int rightUp = 0;
for (int i = zero2; i <= n; ++i) {
if (s1[i] == '1') ++rightUp;
if (s2[i] == '1') --rightUp;
}
if (midUp > 0) {
midUp = -midUp;
leftUp = -leftUp;
rightUp = -rightUp;
}
if (leftUp > 0) {
int minv = min(leftUp, -midUp);
leftUp -= minv;
midUp += minv;
}
if (rightUp > 0) {
int minv = min(rightUp, -midUp);
rightUp -= minv;
midUp += minv;
}
int sum = abs(leftUp) + abs(rightUp) + abs(midUp);
fout << n - sum << endl;
}
}
namespace tc17t20 {
void solve(int n, string s1, string s2, string t1, string t2) {
s1 = "@" + s1 + "01"; s2 = "@" + s2 + "01";
t1 = "@" + t1; t2 = "@" + t2;
int ofBlock1[n + 2]; ofBlock1[0] = ofBlock1[n + 1] = 0;
for (int i = 1; i <= n; ++i) {
if (t1[i] == '0') {
ofBlock1[i] = ofBlock1[i - 1] + 1;
} else {
if (t1[i - 1] == '1') ofBlock1[i] = ofBlock1[i - 1];
else ofBlock1[i] = ofBlock1[i - 1] + 1;
}
}
int ofBlock2[n + 2]; ofBlock2[0] = ofBlock2[n + 1] = 0;
for (int i = 1; i <= n; ++i) {
if (t2[i] == '0') {
ofBlock2[i] = ofBlock2[i - 1] + 1;
} else {
if (t2[i - 1] == '1') ofBlock2[i] = ofBlock2[i - 1];
else ofBlock2[i] = ofBlock2[i - 1] + 1;
}
}
int leftOfTheBlock1[n + 2];
int rightOfTheBlock1[n + 2];
for (int i = 1; i <= n; ++i) {
if (ofBlock1[i] == ofBlock1[i - 1])
leftOfTheBlock1[i] = leftOfTheBlock1[i - 1];
else leftOfTheBlock1[i] = i;
}
for (int i = n; i >= 1; --i) {
if (ofBlock1[i] == ofBlock1[i + 1])
rightOfTheBlock1[i] = rightOfTheBlock1[i + 1];
else rightOfTheBlock1[i] = i;
}
int leftOfTheBlock2[n + 2];
int rightOfTheBlock2[n + 2];
for (int i = 1; i <= n; ++i) {
if (ofBlock2[i] == ofBlock2[i - 1])
leftOfTheBlock2[i] = leftOfTheBlock2[i - 1];
else leftOfTheBlock2[i] = i;
}
for (int i = n; i >= 1; --i) {
if (ofBlock2[i] == ofBlock2[i + 1])
rightOfTheBlock2[i] = rightOfTheBlock2[i + 1];
else rightOfTheBlock2[i] = i;
}
int point1 = 1;
int point0 = 1;
while (s1[point0] == '1') ++point0;
while (s1[point1] == '0') ++point1;
int cnt = 0;
for (int i = 1; i <= n; ++i) {
int reachableLeft = leftOfTheBlock2[i];
int reachableRight = rightOfTheBlock2[i];
reachableLeft = leftOfTheBlock1[reachableLeft];
reachableRight = rightOfTheBlock1[reachableRight];
if (s2[i] == '1') {
if (point1 >= reachableLeft &&
point1 <= reachableRight) {
++cnt;
do ++point1; while (s1[point1] == '0');
}
else {
do ++point0; while (s1[point0] == '1');
}
} else {
if (point0 >= reachableLeft &&
point0 <= reachableRight) {
++cnt;
do ++point0; while (s1[point0] == '1');
}
else {
do ++point1; while (s1[point1] == '0');
}
}
}
fout << cnt << endl;
}
}
void solve() {
int n;
fin >> n;
string s1, s2, t1, t2;
fin >> s1 >> s2 >> t1 >> t2;
if (tc5t8::verify(n, s1, s2, t1, t2)) tc5t8::solve(n, s1, s2, t1, t2);
else if (tc9t12::verify(n, s1, s2, t1, t2)) tc9t12::solve(n, s1, s2, t1, t2);
else if (tc13t16::verify(n, s1, s2, t1, t2)) tc13t16::solve(n, s1, s2, t1, t2);
else tc17t20::solve(n, s1, s2, t1, t2);
}
int main() {
int t;
fin >> t;
while (t--) solve();
return 0;
}