let inIndex = 0;
let sentence;
let n
let positions = []

function main() {
    for (let i = 0; i < n; i++) {
        console.log(countWords(sentence, positions[i].start - 1, positions[i].end - 1))
    }
}

function countWords(sentence, startIndex, endIndex) {
    let count = 1
    for (let i = startIndex; i <= endIndex; i++) {
        if (i !== startIndex && sentence.charCodeAt(i) < sentence.charCodeAt(i - 1)) {
            count++
        }
    }
    return count
}

process.stdin.on('data', function (data) {
    switch (inIndex) {
        case 0:
            sentence = data.toString();
            break
        case 1:
            n = parseInt(data, 10);
            break
        default:
            let [start, end] = data.toString().split(" ")
            positions.push({start: parseInt(start, 10), end: parseInt(end, 10)})
    }
    inIndex++
    if(inIndex===2+n){
        main()
    }
});
