#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
#define pob pop_back
#define b back
#define cout(x) cout << x << endl
#define lb long double
#define soa(arr, n) sort(arr,arr+n)
#define inputVector(v, n) for(ll i = 0; i < n; i++) cin >> v[i]
#define sov(v) sort(v.begin(), v.end())
#define rev(v) reverse(v.begin(), v.end())
#define pl pair<ll, ll>
#define all(a)    a.begin(), a.end()
#define vpp vector<pair<ll, ll>>
#define fastIo ios_base::sync_with_stdio(false), cin.tie(0)
// #define endl '\n'
#define No cout << "NO\n"; 
#define Yes cout << "YES\n";
typedef long long int  ll;

ll P = 998244353;


bool can_convert(string &s, string & t, int ind, int l, int r) {
    ind --;
    l --;
    r --;
    if(s[ind] == '1') return true;

    ll t_l = t[ind - 1];
    ll t_r = t[ind + 1];

    if(ind - 1 >= l && ind + 1 <= r) {
        char t_l = t[ind - 1];
        char t_r = t[ind + 1];

        if(t_l == '0') {
            return false;
        }

        if(s[ind] == '0' && ind + 2 <= r && s[ind + 2] == '0') {
            return true;
        }
    }
    return false;
}


void solve() {
    ll n, c;
    cin >> n;
    string s, t;
    cin >> s >> t;

    string s1 = s, t1 = t;
    for(ll i = 0; i < n - 2; i ++) {
        if(s1[i] == s1[i + 2] && s1[i] == '0') {
            t1[i + 1] = '1';
        }
    }

    for(ll i = 0; i < n - 2; i ++) {
        if(t1[i] == t1[i + 2] && t1[i] == '1') {
            s1[i + 1] = '1';
        }
    }

    // cout << s1 << " " << t1 << endl;
    vector<ll> pref(n + 1);
    for(ll i = 0; i < n; i++) {
        pref[i + 1] = pref[i] + (s1[i] == '1');
    }

    ll q;
    cin >> q;
    while(q --) {
        ll l, r;
        cin >> l >> r;
        ll cnt = 0;
        if(r >= 2 && r >= l + 2 && pref[r - 2] -pref[l + 1] >= 0) {
            cnt = pref[r - 2] -pref[l + 1];
        }
        if(cnt < 0) cnt = 0;
        // cout << " debug " << cnt << endl;

        cnt += s[l - 1] == '1';
        if(r - 1 != l - 1) {
            cnt += s[r - 1] == '1';
        }
        

        if(l + 1 <= r - 1) {
            cnt += can_convert(s, t, l + 1, l, r);
             if(r - 1 != l + 1) {
                cnt += can_convert(s, t, r - 1, l, r);
            }
        }

        cout << cnt << endl;


    }


}

 int main() {
    ll t=1,i=1, n, p, l, T;
 
    cin >> t;

    while(t--) {
        solve();
    }
    return 0;
}