#include <iostream>
#include <fstream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

typedef long long ll;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //freopen("inB.txt", "r", stdin);

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

    int q;
    cin >> q;
    while (q--) {
        int l, r;
        cin >> l >> r;
        l--; r--;

        cout << a[r-1] - a[l-1] + 1;

        cout << "\n";
    }

    return 0;
}