#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<ll> vl;
typedef vector<vector<ll> > vvl;

#define sz(x) (int)(x).size()
#define ins insert
#define pb push_back
#define ff first
#define ss second
#define bust ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define nl '\n'
#define int ll
const ll MAXN = 300005;

ll a[MAXN], tree[4*MAXN];

void build(int pos, int ql, int qr){
    if(ql == qr){
        tree[pos] = a[ql];
    }
    else{
        int mid = (ql + qr)>>1;
        build((pos<<1), ql, mid);
        build((pos<<1) + 1, mid + 1, qr);
        tree[pos] = tree[(pos<<1)] + tree[(pos<<1) + 1];
    }
}

ll gett(int pos, int l, int r, int ql, int qr){
    if(r < ql || l > qr) return 0;
    if(l <= ql && qr <= r){
        return tree[pos];
    }
    int mid = (ql + qr)>>1;
    return gett((pos<<1), l, r, ql, mid) + gett((pos<<1) + 1, l, r, mid + 1, qr);
}
signed main()
{
    bust
    string s;
    cin >> s;
    ll n = s.length();
    for(int i = 0; i < n-1; ++i){
        a[i] = 0;
        if((int)s[i] > s[i + 1]){
            a[i] = 1;
        }
//        cout << a[i] << " ";
    }
    build(1, 0, n - 1);
    ll q;
    cin >> q;
    for(int i = 0; i < q; i++){
        ll l, r;
        cin >> l >> r;
        --l;
        --r;
        ll ans = gett(1, l, r, 0, n-1) + 1;
        ans -= a[r];
        cout << ans << '\n';
    }
    return 0;
}

