#include <bits/stdc++.h>
using namespace std;

int main()
{
    long long n,i,f,k,l,r;
    vector<int> v;
    string s;
    cin>>s;

    v.clear();
    v.push_back(0);
    for(i=0;i<s.length()-1;i++)
        {
        if((int)s[i]>(int)s[i+1]) v.push_back(i);
        }
    cin>>n;
    v.push_back(s.length());
    for (i=0;i<n;i++)
    {
            cin>>l>>r;
            for(int j=1;j<v.size(),j++;)
            {
                if (v[j-1]<l<=v[j]) l=j;
                if (v[j]>r>=v[j-1]) r=j;
            }
        cout<<r-l<<endl;
    }
  return 0;  
} 