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

int binsearch(int n, vector<int> p, int l, int r){
    int ind = (l+r)/2;
    if (p[ind] == n || ind == l || l >= r) return ind;
    if (p[ind] > n) return binsearch(n,p,l,ind);
    if (p[ind] < n) return binsearch(n,p,ind,r);
}

int binsearch2(int n, vector<int> p, int l, int r){
    int ind = (l+r)/2;
    if (p[ind] == n) return ind;
    if (ind == l || l >= r) return -1;
    if (p[ind] > n) return binsearch2(n,p,l,ind);
    if (p[ind] < n) return binsearch2(n,p,ind,r);
}

int calc(int l, int r, vector<int> p){
    int k = 0;
    l--;
    r--;
    //cout << binsearch(r,p,0,p.size()+1) << " - " << binsearch2(l,p,0,p.size()+1) << endl;
    k = binsearch(r,p,0,p.size()+1) - binsearch2(l,p,0,p.size()+1);
    return k;
}

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

	int n, l, r;
    string s;
    vector<int> p, ans; //протиріччя, відповідь
	cin >> s >> n;
    for (int i = 1; i < s.size();i++){
        if (s[i] < s[i-1])p.push_back(i);
    }
    for (int i = 0; i < n; i++){
        cin >> l >> r;
        ans.push_back(calc(l,r,p));
    }
    for (int i = 0; i < ans.size(); i++) {
        cout << ans[i] + 1 << endl;
    }
}
