#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define ld long double
#define F first
#define S second
#define pb push_back
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define pii pair<ll, ll>

using namespace std;

int main(){
	fast;
	string s;
	cin >> s;
	ll n, l=1, r=1, ans=1;
	deque<ll> a={1};
	cin >> n;
	pair<ll, pair<ll, ll>> q[n];
	ll anz[n];
	for(ll i = 0; i < n; i++){
		cin >> q[i].F >> q[i].S.F;
		q[i].S.S = i;
	}
	sort(q, &q[n]);
	for(ll i = 0; i < n; i++){
		while(l < q[i].F){
			if(a.front() == l){
				a.pop_front();
				if(s[l] >= s[l-1])
					a.push_front(l+1);
				else
					ans--;
			}
			l++;
		}
		while(r > q[i].S.F){
			if(a.back() == r){
				a.pop_back();
				ans--;
			}
			r--;
		}
		while(r < q[i].S.F){
			r++;
			if(s[r-1]<s[r-2]){
				a.push_back(r);
				ans++;
			}
		}/*
		cout << l << ' ' << r << ":   ";
		for(auto j:a)
			cout << j << ' ';
		cout << '\n';*/
		anz[q[i].S.S] = ans;
	}
	for(ll i = 0; i < n; i++){
		cout << anz[i] << '\n';
	}
}
