#include <bits/stdc++.h>

typedef long long ll;
typedef long double ld;
typedef unsigned int uint;

using namespace std;

#define FOR(i, n) for(int i=0; i<n; ++i)
#define FOR2(i, a, b) for(ll i=a; i<b; ++i)
#define RFOR(i, n) for(int i=n-1; i>=0; --i)
#define FORA(i, a) for(auto i : a)
#define FORAR(i, a) for(auto & i : a)
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define V vector
#define P pair
#define S set
#define elif else if
#define str string
#define pb push_back
#define OK cout<<"OK\n";
#define watch(x) (#x) << " -> " << x

int main(){
	fast;
	str s;
	cin >> s;
	s = "d" + s;
	int n = s.size()-1;
	V<int> c;
	FOR(i, n)
		if(s[i] > s[i+1])
			c.pb(i+1);
	int q;
	cin >> q;
	while(q--){
		int l, r;
		cin >> l >> r;
		cout << upper_bound(c.begin(), c.end(), r) - upper_bound(c.begin(), c.end(), l) + 1 << '\n';
	}
}