#define _CRT_SECURE_NO_WARNINGS
//#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdint>
#include <cstdlib>
#include <algorithm>
#include <cmath>
//#include <numeric>
//#include <string> 
#include <vector>
//#include <array>
//#include <unordered_map>
//#include <map>
//#include <queue>
//#include <set>
//#include <stack>
//#include <string>

using u = unsigned;

constexpr u N = (u)2e4;

char buf[N+1];
u psum[N+1];

int main() {
    //freopen("input.txt","r",stdin);
    scanf("%s", buf);
    u l = (u)strlen(buf), q;
    scanf("%u", &q);
    psum[0] = 1;
    psum[1] = 1;
    //printf("1 1 ");
    for(u i = 1; i < l; i++) {
        psum[i+1] = psum[i];
        if(buf[i] < buf[i-1]) psum[i+1]++;
        //printf("%u ", psum[i+1]);
    }
    //putchar('\n');
    while(q--) {
        u l, r;
        scanf("%u%u", &l, &r);
        printf("%u\n", psum[r]-psum[l]+1);
    }
}