#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<long long,long long> pll;
const ll DIM=20007;
const ll INF=2000000007;

long long n,m,q,l,r,g,v1,sl,v2,t,k,h,num,ans,mini,maxi,lastnum,mina,maxa,x,p,cost;
long long a[DIM],T[4*DIM],b[DIM];
string s;
vector<ll>v;

int main()
{
    cin >> s;
    n=s.length();
    for (int i=0;i<n;i++) a[i+1]=int(s[i]-'a')+1;

    g=1;
    for (int i=1;i<=n+1;i++)
    {
        if (a[i]<a[i-1])
        {
            v.push_back(i-1);
            b[i-1]=g;
            g++;
        }
    }
    cin >> q;
    for (int i=1;i<=q;i++)
    {
        cin >> l >> r;
        auto it1=lower_bound(v.begin(),v.end(),l);
        auto it2=lower_bound(v.begin(),v.end(),r);
        cout << b[*it2]-b[*it1]+1 << endl;
        //cout << *it1 << ' ' << *it2 << endl;
    }

    return 0;
}
