#include <fstream>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <stack>
#include <iostream>
#include <vector>
#include <string>

#define pb push_back

using namespace std;
using ll = long long;
using ull = unsigned long long;
using dl = double;

const ll N = 20100;
const ll inf = 100000000000;

ifstream in("input.txt");
ofstream out("output.txt");

string s;
ll q;
ll al = 0;
string a[N];
ll t[N];

void split () {
    string cs;
    cs = s[0];
    ll o = 0;
    for (ll i = 1; i < s.length(); ++i) {
        if (s[i] < s[i - 1]) {
            o += cs.length();
            t[al] = o;
            a[al++] = cs;
            cs = s[i];
        } else {
            cs += s[i];
        }
    }
    t[al] = o + cs.length();
    a[al++] = cs;
}

ll f (ll v) {
    ll l = 0, r = al - 1;
    if (v == 0) return 0;
    if (v == s.length()) return al - 1;

    while (true) {
        ll m = (l + r) / 2;
        ll tl = m == 0 ? 0 : t[m - 1];
        ll tr = t[m];
        if (v > tr) {
            l = m;
        } else if (v <= tl) {
            r = m;
        } else {
            return m;
        }
    }
}

ll query (ll l, ll r) {
    return f(r) - f(l) + 1;
}

int main() {

    in >> s >> q;
    split();

//    out << f(7) << endl;

    for (ll i = 0; i < q; ++i) {
        ll l, r;
        in >> l >> r;
        out << query(l, r) << endl;
    }
    // 3, 7, 12, 13

    return 0;
}