#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define pll pair<ll, ll>
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef long double ld;
typedef __gnu_pbds::tree<ll, __gnu_pbds::null_type, less<ll>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> yree;
const ll inf = 0x3fff3fff3fff3fff;
const ll N = 5e5+20;
ll a[N], p[N];
signed main(){
    cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);
    string s;
    cin >> s;
    ll n=s.length();
    a[0]=1;
    for(ll i = 1;i<n;i++){
        if(s[i] < s[i-1]) a[i]=1;
    }
    partial_sum(a, a+n, p);
    ll q;
    cin >> q;
    while(q--){
        ll l, r;
        cin >> l >> r;
        --l, --r;
        ll z = p[r]-(l?p[l-1]:0)+(a[l]!=1?1:0);
        cout<<z<<'\n';
    }
}
