#include <iostream>
#include "caesar.h"
using namespace std;

const int BUFSIZE = (1 << 17);

int rpos;
char rbuf[BUFSIZE];
int wpos;
char wbuf[BUFSIZE];

static inline char readCh() {
    rpos = ((rpos + 1) & (BUFSIZE - 1));
    if(!rpos)
        fread(rbuf, 1, BUFSIZE, stdin);
    return rbuf[rpos];
}

int readInt() {
    int n = 0;
    char ch;
    while(!isdigit(ch = readCh()));
    do {
        n = n * 10 + ch - '0';
    } while(isdigit(ch = readCh()));
    return n;
}
char readChar() {
    char ch;
    while(!isalpha(ch = readCh()));
    return ch;
}

static inline void writeCh(char ch) {
    //fputc(ch, stdout);
    wbuf[wpos] = ch;
    wpos = ((wpos + 1) & (BUFSIZE - 1));
    if(!wpos)
        fwrite(wbuf, 1, BUFSIZE, stdout);
}

void writeInt(long long x) {
    if(x < 0) {
        writeCh('-');
        x = -x;
    }
    if(x == 0)
        writeCh('0');
    else {
        long long p10 = 1;
        while(p10 * 10 <= x) {
            p10 *= 10;
        }
        while(p10) {
            char cf = '0';
            while(x >= p10) {
                cf++;
                x -= p10;
            }
            p10 /= 10;
            writeCh(cf);
        }
    }
}

static inline void flushWBuf() {
    fwrite(wbuf, 1, wpos, stdout);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    rpos = BUFSIZE - 1;
    wpos = 0;
    int n, q, st, dr;
    n = readInt();   
    char a[n + 10], b[n + 10];
    for(int i = 0; i < n; i++)
        a[i] = readChar();
    for(int i = 0; i < n; i++)
        b[i] = readChar(); 
    q = readInt();

    init(n, q, a, b);

    while(q--) {
        st = readInt() - 1;
        dr = readInt() - 1;

        writeInt(query(st, dr));
        writeCh('\n');
    }
    flushWBuf();

    return 0;
}
