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

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

    int q, l, r, res;
    string s;

	cin >> s >> q;

    vector <int> v (s.length());
    v[0] = 0;

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

    /*
    for (int i = 0; i < v.size(); i++) {
        cout << v[i] << " ";
    }
    cout << "\n";
    */

    for (int i = 0; i < q; i++) {
        cin >> l >> r;
        cout << 1 + v[r - 1] - v[l - 1] << "\n";
    }

    cout << endl;
}
