#include "bits/stdc++.h"
#define DIM 20007
#define mod 100000007
#define eps 0.000000001
#define pi 3.14
#define INF 100000000000001
#define fi first
#define se second

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<long long, long long> pll;


ll t, n, a[DIM], b, c, res, k, l, r, m;


string s;

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> s;
    n = s.length();
    c = 1;
    a[1] = 1;
    for(int i = 1; i < n; i ++){
        if(s[i] < s[i - 1]) c ++;
        a[i + 1] = c;
    }
    cin >> m;
    for(int i = 1; i <= m; i ++ ){
        cin >> l >> r;
        cout << a[r] - a[l] + 1 << endl;
    }
}
