#include <bits/stdc++.h>
#define ll long long

using namespace std;

int main()
{
    string s;
    ll n, a, b;
    cin >> s;
    
    vector <ll> num;
    num.push_back(1);
    
    for (ll i = 1; i < s.length(); ++i)
        if (int(s[i]) < int(s[i - 1])) num.push_back(i + 1);
    
    cin >> n;
    for (; n > 0; n--) {
        ll k = 0, z;
        cin >> a >> b;
        
        for (ll i = 0; i < num.size(); ++i)
            if (num[i] > a) {
                k++;
                z = i;
                break;
            } else if (num[i] == a) {
                    z = i;
                    break;    
            }
        
        for (ll i = z; i < num.size(); ++i)
            if (num[i] >= a) {
                if (num[i] <= b) k++;
                    else break;
            }
        cout << k << endl;        
    }

    return 0;
}