#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>

const int MAXN = 100000;

std::string s1, s2;
std::string t1, t2;
int n;

bool is_case_a() {
    if (s1.size() == 1)
        return true;
    char first = s1[0];
    for (int i = 1; i < n; i++) {
        if (s1[i] != first) return false;
    }
    return true;
}

bool is_case_b() {
    return t1 == t2;
}

int solve_b() {
    int a = 0;
    int total = 0;
    while (a < n) {
        if (t1[a] == '0') {
            total += s1[a] == s2[a];
            a++; // Don't use goto
            continue;
        }

        int b = a;
        while (t1[b+1] == '1' && b < n) {
            b++;
        }

        int nones1 = 0, nzeros1 = 0;
        int nones2 = 0, nzeros2 = 0;
        for (int i = a; i <= b; i++) {
            if (s1[i] == '1') { nones1++; } else { nzeros1++; }
            if (s2[i] == '1') { nones2++; } else { nzeros2++; }
        }

        total += std::min(nones1, nones2) + std::min(nzeros1, nzeros2);

        a = b + 1;
    }
    return total;
}

int solve() {
    if (is_case_a()) {
        int count = 0;
        for (char c: s2) {
            if (c == s1[0])
                count++;
        }
        return count;
    }

    if (is_case_b()) {
        return solve_b();
    }
    return -1;
}

int main() {
#ifdef LOCAL_
    std::ifstream fin("data/edit/edit2.in");
    auto&& fout = std::cout;
#else
    std::ifstream fin("edit.in");
    std::ofstream fout("edit.out");
#endif // LOCAL_
    int t;
    fin >> t;
    for (; t > 0; t--) {
        fin >> n;
        fin >> s1 >> s2;
        fin >> t1 >> t2;
        fout << solve() << '\n';
    }
    return 0;
}

