#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const long long DIM = 2e4 + 7, INF = 1e9 + 7;

int a[DIM];

int main()
{
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  //freopen("./input.txt", "r", stdin);

  string s;

  cin >> s;

  int curr = 0;
  for (int i = 1; i < (int)s.size(); i++)
  {
    if (s[i - 1] > s[i])
      curr++;
    a[i] = curr;
  }

  int q, l, r;

  cin >> q;
  for (int i = 0; i < q; i++)
  {
    cin >> l >> r, l -= 1, r -= 1;
    cout << a[r] - a[l] + 1 << '\n';
  }

  return 0;
}
