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

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

	int n, c, e;
    string s;
    vector <int> f, b;
    cin >> s;
    n = s.size();
    f.resize(n + 2);
    b.resize(n + 2);
    f[0] = 0;
    for (int i = 1; i < n; i++) {
        f[i] = f[i - 1];
        if (s[i - 1] > s[i]) {
            f[i]++;
        }
    }
    b[n] = 0;
    for (int i = n - 1; i > 0; i--) {
        b[i] = b[i + 1];
        if (s[i] < s[i - 1]) {
            b[i]++;
        }
    }
    e = f[n - 1];
    int k, l, r;
    cin >> k;
    for (int i = 0; i < k; i++) {
        cin >> l >> r;
        cout << e - f[l - 1] - b[r] + 1 << "\n";
    }
}
