#include <iostream>

const int N = 2e4;

using namespace std;
typedef long long ll;

int main(){
    string ln;
    int q, a[N + 10], lst = 0, n;
    getline(cin, ln);
    n = ln.size();
    int cnt = 0;
    for(int i = 1; i < n; ++i){
        while(i < n && ln[i - 1] <= ln[i]) ++i;
        for(int j = lst; j < i; ++j){
            a[j] = cnt;
        }
        ++cnt;
        lst = i;
        a[lst] = cnt;
    }
    cin >> q;
    for(int i = 0, l, r; i < q; ++i){
        cin >> l >> r; --l; --r;
        cout << a[r] - a[l] + 1 << '\n';
    }
}
