#include <bits/stdc++.h>

#define INF 1000000005
#define INFL 1000000000000000005ll
#define EPS 1e-6
#define pb push_back
#define pause system("pause")
#define exit exit(0)
#define endl '\n'

using namespace std;
using ull = unsigned long long;
using ll = long long;

typedef pair<ll, ll> pii;
const int N = 200005, MOD = 998244353;

int n, q, cnt[N];
string s;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);

	cin >> s;
	n = s.length();
	for (int i = 1; i < n; ++i) {
		cnt[i] = cnt[i - 1] + (s[i - 1] > s[i]);
	}

	int l, r;
	cin >> q;
	while (q--) {
		cin >> l >> r;
		--l, --r;
		cout << cnt[r] - cnt[l] + 1 << endl;
	}
	//pause;
	return 0;
}
