#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;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBmIGZpcnN0CiNkZWZpbmUgcyBzZWNvbmQKI2RlZmluZSBwYiBwdXNoX2JhY2sKI2RlZmluZSBwb2IgcG9wX2JhY2sKI2RlZmluZSBiIGJhY2sKI2RlZmluZSBjb3V0KHgpIGNvdXQgPDwgeCA8PCBlbmRsCiNkZWZpbmUgbGIgbG9uZyBkb3VibGUKI2RlZmluZSBzb2EoYXJyLCBuKSBzb3J0KGFycixhcnIrbikKI2RlZmluZSBpbnB1dFZlY3Rvcih2LCBuKSBmb3IobGwgaSA9IDA7IGkgPCBuOyBpKyspIGNpbiA+PiB2W2ldCiNkZWZpbmUgc292KHYpIHNvcnQodi5iZWdpbigpLCB2LmVuZCgpKQojZGVmaW5lIHJldih2KSByZXZlcnNlKHYuYmVnaW4oKSwgdi5lbmQoKSkKI2RlZmluZSBwbCBwYWlyPGxsLCBsbD4KI2RlZmluZSBhbGwoYSkgICAgYS5iZWdpbigpLCBhLmVuZCgpCiNkZWZpbmUgdnBwIHZlY3RvcjxwYWlyPGxsLCBsbD4+CiNkZWZpbmUgZmFzdElvIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpLCBjaW4udGllKDApCi8vICNkZWZpbmUgZW5kbCAnXG4nCiNkZWZpbmUgTm8gY291dCA8PCAiTk9cbiI7IAojZGVmaW5lIFllcyBjb3V0IDw8ICJZRVNcbiI7CnR5cGVkZWYgbG9uZyBsb25nIGludCAgbGw7CgpsbCBQID0gOTk4MjQ0MzUzOwoKCmJvb2wgY2FuX2NvbnZlcnQoc3RyaW5nICZzLCBzdHJpbmcgJiB0LCBpbnQgaW5kLCBpbnQgbCwgaW50IHIpIHsKICAgIGluZCAtLTsKICAgIGwgLS07CiAgICByIC0tOwogICAgaWYoc1tpbmRdID09ICcxJykgcmV0dXJuIHRydWU7CgogICAgbGwgdF9sID0gdFtpbmQgLSAxXTsKICAgIGxsIHRfciA9IHRbaW5kICsgMV07CgogICAgaWYoaW5kIC0gMSA+PSBsICYmIGluZCArIDEgPD0gcikgewogICAgICAgIGNoYXIgdF9sID0gdFtpbmQgLSAxXTsKICAgICAgICBjaGFyIHRfciA9IHRbaW5kICsgMV07CgogICAgICAgIGlmKHRfbCA9PSAnMCcpIHsKICAgICAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgICAgIH0KCiAgICAgICAgaWYoc1tpbmRdID09ICcwJyAmJiBpbmQgKyAyIDw9IHIgJiYgc1tpbmQgKyAyXSA9PSAnMCcpIHsKICAgICAgICAgICAgcmV0dXJuIHRydWU7CiAgICAgICAgfQogICAgfQogICAgcmV0dXJuIGZhbHNlOwp9CgoKdm9pZCBzb2x2ZSgpIHsKICAgIGxsIG4sIGM7CiAgICBjaW4gPj4gbjsKICAgIHN0cmluZyBzLCB0OwogICAgY2luID4+IHMgPj4gdDsKCiAgICBzdHJpbmcgczEgPSBzLCB0MSA9IHQ7CiAgICBmb3IobGwgaSA9IDA7IGkgPCBuIC0gMjsgaSArKykgewogICAgICAgIGlmKHMxW2ldID09IHMxW2kgKyAyXSAmJiBzMVtpXSA9PSAnMCcpIHsKICAgICAgICAgICAgdDFbaSArIDFdID0gJzEnOwogICAgICAgIH0KICAgIH0KCiAgICBmb3IobGwgaSA9IDA7IGkgPCBuIC0gMjsgaSArKykgewogICAgICAgIGlmKHQxW2ldID09IHQxW2kgKyAyXSAmJiB0MVtpXSA9PSAnMScpIHsKICAgICAgICAgICAgczFbaSArIDFdID0gJzEnOwogICAgICAgIH0KICAgIH0KCiAgICAvLyBjb3V0IDw8IHMxIDw8ICIgIiA8PCB0MSA8PCBlbmRsOwogICAgdmVjdG9yPGxsPiBwcmVmKG4gKyAxKTsKICAgIGZvcihsbCBpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgIHByZWZbaSArIDFdID0gcHJlZltpXSArIChzMVtpXSA9PSAnMScpOwogICAgfQoKICAgIGxsIHE7CiAgICBjaW4gPj4gcTsKICAgIHdoaWxlKHEgLS0pIHsKICAgICAgICBsbCBsLCByOwogICAgICAgIGNpbiA+PiBsID4+IHI7CiAgICAgICAgbGwgY250ID0gMDsKICAgICAgICBpZihyID49IDIgJiYgciA+PSBsICsgMiAmJiBwcmVmW3IgLSAyXSAtcHJlZltsICsgMV0gPj0gMCkgewogICAgICAgICAgICBjbnQgPSBwcmVmW3IgLSAyXSAtcHJlZltsICsgMV07CiAgICAgICAgfQogICAgICAgIGlmKGNudCA8IDApIGNudCA9IDA7CiAgICAgICAgLy8gY291dCA8PCAiIGRlYnVnICIgPDwgY250IDw8IGVuZGw7CgogICAgICAgIGNudCArPSBzW2wgLSAxXSA9PSAnMSc7CiAgICAgICAgaWYociAtIDEgIT0gbCAtIDEpIHsKICAgICAgICAgICAgY250ICs9IHNbciAtIDFdID09ICcxJzsKICAgICAgICB9CiAgICAgICAgCgogICAgICAgIGlmKGwgKyAxIDw9IHIgLSAxKSB7CiAgICAgICAgICAgIGNudCArPSBjYW5fY29udmVydChzLCB0LCBsICsgMSwgbCwgcik7CiAgICAgICAgICAgICBpZihyIC0gMSAhPSBsICsgMSkgewogICAgICAgICAgICAgICAgY250ICs9IGNhbl9jb252ZXJ0KHMsIHQsIHIgLSAxLCBsLCByKTsKICAgICAgICAgICAgfQogICAgICAgIH0KCiAgICAgICAgY291dCA8PCBjbnQgPDwgZW5kbDsKCgogICAgfQoKCn0KCiBpbnQgbWFpbigpIHsKICAgIGxsIHQ9MSxpPTEsIG4sIHAsIGwsIFQ7CiAKICAgIGNpbiA+PiB0OwoKICAgIHdoaWxlKHQtLSkgewogICAgICAgIHNvbHZlKCk7CiAgICB9CiAgICByZXR1cm4gMDsKfQ==