import java.util.Scanner

fun main() {
    val scanner = Scanner(System.`in`)
    val sentence = scanner.nextLine()
    val n = scanner.nextInt()
    for (i in 0 until n) {
        val start = scanner.nextInt()
        val end = scanner.nextInt()
        val numberOfWords = countWords(sentence, start - 1, end - 1)
        println(numberOfWords)
    }
}

fun countWords(sentence: String, startIndex: Int, endIndex: Int): Int {
    var count = 1
    val chars = sentence.toCharArray()
    for (i in startIndex..endIndex) {
        if (i != startIndex && chars[i] < chars[i - 1]) {
            count++
        }
    }
    return count
}