#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

using namespace std;

#define fast ios_base::sync_with_stdio();cin.tie();cout.tie();
#define ops cout<<"ops"<<endl;
#define en cout<<endl;
#define fi first
#define se second

typedef int ll;
typedef long double ld;
typedef pair<ll,ll> pllll;
typedef string str;
typedef char cr;

const ll DIM=2e4+7;
const ll DIMM=1e5+7;
const ll DDIM=1e7+7;
const ll INF=1e16+7;
const ll X=1e9;
const ll BS=37;
const ll MODULO=1e9+7;

ll n,m,k;

str s;

ll a[DIM],res[DIMM];

struct z{ll l,r,i;} q[DIMM];

bool cmp(z p1,z p2){
    if(p1.l==p2.l){
     if(p1.r==p2.r)return p1.i<p2.i;
     return p1.r<p2.r;
    }
    return p1.l<p2.l;
}

void solve(){

    cin>>s;

    n=s.length();

    for(int i=0;i<n;i++)a[i+1]=s[i]-'a'+1;

    cin>>k;

    for(int i=1;i<=k;i++){
      cin>>q[i].l>>q[i].r;
      q[i].i=i;
    }

    sort(q+1,q+1+k,cmp);

    ll cres;
    ll p=1;

    for(int l=1;l<=n;l++){

     cres=1;

     for(int r=(l+1);r<=n;r++){

      if(a[r]<a[r-1])cres++;

      while(p<=k && q[p].l==l && q[p].r==r){
       res[q[p].i]=cres;
       p++;
      }

     }

    }

    for(int i=1;i<=k;i++)cout<<res[i]<<endl;

    return;
}

int main()
{

    fast;

    int nt;

    nt=1;

    //cin>>nt;

    while(nt--)
        solve();

    return 0;
}
