#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <iomanip>
#include <set>
#include <cmath>

#define LF '\n'

using namespace std;
typedef long long int lli;

vector<int> v;
vector<int> d;

int build(int a, int l, int r)
{
    if (l == r) {
        d[a] = v[l];
        return d[a];
    }
    int m = (r + l) / 2;
    d[a] = build(a * 2 + 1, l, m) + build(a * 2 + 2, m + 1, r);
    return d[a];
}

int get(int a, int tl, int tr, int l, int r)
{
    if (tl >= l && tr <= r) return d[a];
    if (tr<l || tl>r) return 0;
    int m = (tl + tr) / 2;
    return get(a * 2 + 1, tl, m, l, r) + get(a * 2 + 2, m + 1,tr, l, r);
}

void solve()
{
    string s;
    cin >> s;
    int n = s.size();
    d.resize(4 * n);

    v.push_back(0);
    for (int i = 1; i < n; i++) {
        if (s[i - 1] <= s[i]) v.push_back(0);
        else v.push_back(1);
    }

    build(0, 0, n - 1);
    int q;
    cin >> q;
    for (int i = 1; i <= q; i++) {
        int l, r;
        cin >> l >> r;
        int ans = get(0, 0, n - 1, l-1, r-1);
        if (v[l - 1] == 0) ans++;
        cout << ans << LF;
    }
}

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

#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    solve();
}

