#include <bits/stdc++.h>
#define endl '\n'

using namespace std;

typedef long long ll;
typedef pair<ll,ll> pll;
const ll DIM = 1e5+7;

ll n,a[DIM],q;
string s;


int main()
{
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    cin >> s;
    n = s.size();
    a[1] = 0;
    for(int i=2;i<=n;i++){
        a[i] = a[i-1];
        if(s[i-1]<s[i-2]){
            a[i]++;
        }
    }

    cin >> q;
    ll L,R;
    for(int i=1;i<=q;i++){
        cin >> L >> R;
        cout << a[R] - a[L] + 1 << endl;
    }

    return 0;
}

