#include <bits/stdc++.h>
using namespace std;
const long long INF = 1000000009;
const long long MOD = 1000000007;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef vector<ll> vect;
typedef vector<pair<ll,ll>> vll;
ll t,n,m,ind1,ind2,row,x,q;
ll a[100007],p[20007];
set<ll> se;
map<ll,ll> mapl;
string s;
int main()
{ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin >> s;
    p[0] = 1;
    for(int i=1;i<s.size();i++){
        if(s[i] < s[i-1]) p[i] = p[i-1] + 1;
        else p[i] = p[i-1];
    }
    cin >> q;
    ll l,r;
    for(int i=1;i<=q;i++){
        cin >> l >> r;
        l--;r--;
        cout << p[r]-p[l]+1 << endl;
    }
}




