#include <bits/stdc++.h>

using namespace std;

#define endl "\n"
#define pb push_back
#define mp make_pair
#define in insert
#define INF 1e9
#define eps 0.0000000001
#define fi first
#define se second
#define y1 yy
#define speedup ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)

typedef long long ll;

//---------------------------------------|

const int DIM=1e5+7;

ll n,m,x,y,l,r;
ll a[DIM];
string s;

//---------------------------------------|

int main()
{
    speedup;

    cin >> s;
    n=s.size();

    for (int i=1;i<n;i++)
    {
        if (s[i]<s[i-1]) a[i]=a[i-1]+1;
        else a[i]=a[i-1];
    }

    cin >> m;
    while(m--)
    {
        cin >> l >> r;
        l--;
        r--;
        cout << a[r]-a[l]+1 << endl;
    }

    return 0;
}
