#include #define int long long #define INF 0x3f3f3f3f3f3f3f3f #define endl '\n' using namespace std; typedef pair pii; const int N = + 29; int n, m; string s1, s2, t1, t2; bool judge1() { char ju = s1[0]; for (auto i : s1) if (ju != i) return false; return true; } void sp1() { char ju = s1[0]; int ans = 0; for (auto i : s2) { if (i == ju) ans++; } cout << ans << endl; return; } void sp2() { int ans = 0; int cnt10 = 0, cnt11 = 0, cnt20 = 0, cnt21 = 0; for (int i = 0; i < n; i++) { if (t1[i] == '0') { ans = ans + min(cnt10, cnt20) + min(cnt11, cnt21); cnt10 = 0, cnt11 = 0, cnt20 = 0, cnt21 = 0; if (s1[i] == s2[i]) ans++; continue; } if (s1[i] == '0') cnt10++; if (s1[i] == '1') cnt11++; if (s2[i] == '0') cnt20++; if (s2[i] == '1') cnt21++; } ans += min(cnt10, cnt20) + min(cnt11, cnt21); cout << ans << endl; return; } void tbcsolve() { cin >> n >> s1 >> s2 >> t1 >> t2; if (judge1()) { sp1(); return; } if (t1 == t2) { sp2(); return; } int i = 0, j = 0, ans = 0; int cnt10 = 0, cnt11 = 0, cnt20 = 0, cnt21 = 0; char tt1, tt2; while (true) { for ( ; i < n; i++) { if (t1[i] == '0') { tt1 = s1[i]; break; } if (s1[i] == '0') cnt10++; if (s1[i] == '1') cnt11++; } for ( ; j < n; j++) { if (t2[j] == '0') { tt2 = s2[j]; break; } if (s2[j] == '0') cnt20++; if (s2[j] == '1') cnt21++; } if (i > j) { if (tt2 == '0') cnt20++; else cnt21++; if (cnt10 >= cnt20) { ans += cnt20; cnt10 -= cnt20; } else { ans += cnt10; cnt11 -= (cnt20 - cnt10); cnt10 = 0; } if (cnt11 >= cnt21) { ans += cnt21; cnt11 -= cnt21; } else { ans += cnt11; cnt10 -= (cnt21 - cnt11); cnt11 = 0; } cnt20 = cnt21 = 0; j++; } else if (i < j) { if (tt1 == '0') cnt10++; else cnt11++; if (cnt10 <= cnt20) { ans += cnt10; cnt20 -= cnt10; } else { ans += cnt20; cnt21 -= (cnt10 - cnt20); cnt20 = 0; } if (cnt11 <= cnt21) { ans += cnt11; cnt21 -= cnt11; } else { ans += cnt21; cnt20 -= (cnt11 - cnt21); cnt21 = 0; } cnt10 = cnt11 = 0; i++; } else { ans += min(cnt10, cnt20); ans += min(cnt11, cnt21); if (i < n && tt1 == tt2) ans++; cnt10 = cnt11 = cnt20 = cnt21 = 0; i++; j++; } if (i >= n && j >= n) break; } cout << ans << endl; } void tbcinit() { } signed main() { freopen("edit.in", "r", stdin); freopen("edit.out", "w", stdout); ios:: sync_with_stdio(0), cin.tie(0), cout.tie(0); int zs = 1; cin >> zs; while (zs--) { tbcinit(); tbcsolve(); } return 0; } /* 1 10 0110111100 1111111100 1110000100 0110111011 */