#include <bits/stdc++.h>
using namespace std;
const long long N = 20100;
long long a[N];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

	string s;
    cin >> s;
    long long cnt = 1;
    a[0] = 1;
    for (long long i = 1 ; i < s.size();i++)
    {
        if (s[i]<s[i-1])
        {
            cnt++;
        }
        a[i] = cnt;
    }
    long long q,l,r;
    cin >> q;
    for (long long i = 0 ; i < q ; i++)
    {
    cin >> l >> r;
    cout << a[r-1] - a[l-1] + 1 << '\n';
    }
}
