// ToLiK
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>

using namespace std;
typedef long long ll;
#define speed                    \
    ios::sync_with_stdio(false); \
    cin.tie(0);                  \
    cout.tie(0);
#define endl "\n";

string s;
vector<int> v;

void master()
{
    int l, r;
    cin >> l >> r;
    cout << v[r - 1] - v[l - 1] + 1 << endl;
}

int main()
{
    speed;
    int t;
    cin >> s >> t;

    char c = '}';
    int cnt_w = 0;
    for (int i = 0; i < s.length(); i++)
    {
        if (s[i] - '0' < c - '0')
        {
            cnt_w++;
        }
        c = s[i];
        v.push_back(cnt_w);
    }

    // for (auto i : v)
    //     cout << i << endl;

    while (t--)
    {
        master();
    }

    return 0;
}