#include <bits/stdc++.h>
using namespace std;

const long long DIM = 20005;
#define endl '\n'

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

    string s;
	int n, l, r, a[DIM], ns;

    cin >> s;

    ns = s.length();
    for(int i=1; i<ns; i++){
        a[i+1] = a[i];
        if(s[i] < s[i-1]) a[i+1]++;
    }

	cin>>n;
    for(int i=1; i<=n; i++){
        cin >> l >> r;
        cout << a[r]-a[l]+1 << endl;
    }
}
