#include <bits/stdc++.h>

using namespace std;
#define SF scanf
#define PF printf
#define MAXN 105
#define DIV 100009
#define INF 100000000009
#define ll long long
#define ull unsigned long long
#define ld long double
//priority_queue<pair<ll,ll>, vector<pair<ll,ll>>, greater<pair<ll,ll>> > Q;
ll n,k,mx,L,a[DIV],q,l,r;
vector<ll>v;
string s;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
   cin>>s;
   a[0]=1;
   L=s.length();
    for(ll i=1;i<L;i++){
     if(s[i]<s[i-1])a[i]=a[i-1]+1;
     else a[i]=a[i-1];
    }
   // for(ll i=0;i<L;i++)cout<<a[i];
    cin>>q;
    for(ll i=1;i<=q;i++){
        cin>>l>>r;
        cout<<a[r-1]-a[l-1]+1<<endl;
    }








}

































/*      freopen("input.txt","r",stdin);
        ("output.txt","w",stdout);

        ios::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
*/
