#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
vector<int> v;
vector<int> v1;
 string s;
 cin>>s;
 v.push_back(0);
 for(int i=1;i<s.size();i++)
    {
    if(s[i-1]>s[i]) {v1.push_back(i-1); v.push_back(i);}
    }
v1.push_back(s.size()-1);

 int q;
 cin>>q;
 while(q--)
    {
    int a,b;
     cin>>a>>b;
     a--;
     b--;
    int j=0;
     while(v[j]<=a)
        j++;
    j--;
    int j1=0;
    while(v1[j1]<b) j1++;
    cout<<j1-j+1<<endl;

    }
}

