#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

#define PI 3.1415926535897932384626433832795

const int INF = 1e9;
const ll LINF = 1e18;

const ll MOD = 1000000007;
const ll FMOD = 998244353;

const ld eps = 1e-9;

bool same(int st, int en, int st2, int en2) {
  if ((st >= st2 && en <= en2) || (st <= st2 && en >= st2) ||
      (st <= en2 && en >= en2)) {
    return true;
  }

  return false;
}

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

  string s;
  cin >> s;
  int q;
  cin >> q;
  vector<pair<int, int>> v;

  // cout << "--\n";
  pair<int, int> temp = make_pair(0, 0);
  for (int i = 0; i < s.size(); i++) {
    if (i != 0) {
      if (s[i - 1] <= s[i]) {
        temp.second += 1;
      } else {
        v.push_back(temp);
        // cout << temp.first << " " << temp.second << "\n";
        temp = make_pair(i, i);
      }
    }
  }
  v.push_back(temp);

  // cout << v.size() << "\n";

  for (int i = 0; i < q; i++) {
    int st, en, c = 0;
    cin >> st >> en;
    for (int j = 0; j < v.size(); j++) {
      if (same(v[j].first, v[j].second, st - 1, en - 1)) {
        c += 1;
      }
    }
    cout << c << "\n";
  }

  return 0;
}
