#include <bits/stdc++.h>
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
 
#define rep(i, a, b) for (int i = (a); i < (b); i++)
 
#define mod 1000000007
#define endl '\n'
#define INF 99999999

int n;
string s;
vector<tuple<int, int, int>> tree(400009);

tuple<int, int, int> calc(tuple<int, int, int> t1, tuple<int, int, int> t2)
{
    if (get<1>(t2) >= get<2>(t1))
        return {get<0>(t1) + get<0>(t2) - 1, get<1>(t1), get<2>(t2)};
    return {get<0>(t1) + get<0>(t2), get<1>(t1), get<2>(t2)};
}

int fill(int l, int r, int t)
{
    if (l == r) {
        tree[t] = {1, s[l], s[l]};
        return 0;
    }
    int m = (l + r) / 2;
    fill(l, m, t * 2 + 1); fill(m + 1, r, t * 2 + 2);
    auto t1 = tree[t * 2 + 1], t2 = tree[t * 2 + 2];
    tree[t] = calc(t1, t2);
    return 0;
}

tuple<int, int, int> search(int sl, int sr, int l, int r, int t)
{
    if (sl == l && sr == r)
        return tree[t];
    int m = (l + r) / 2;
    if (sl > m)
        return search(sl, sr, m + 1, r, t * 2 + 2);
    if (sr <= m)
        return search(sl, sr, l, m, t * 2 + 1);
    return calc(search(sl, m, l, m, t * 2 + 1), search(m + 1, sr, m + 1, r, t * 2 + 2));
}


int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> s;
    fill(0, s.size() - 1, 0);
    cin >> n;
    rep(i, 0, n) {
        int l, r;
        cin >> l >> r;
        auto t = search(l - 1, r - 1, 0, s.size() - 1, 0);
        cout << get<0>(t) << endl;
    }
    return 0;
}