Petya once wrote a sad love song and shared it to Vasya. The song is a string consisting of lowercase English letters. Vasya made up $$$q$$$ questions about this song. Each question is about a subsegment of the song starting from the $$$l$$$-th letter to the $$$r$$$-th letter. Vasya considers a substring made up from characters on this segment and repeats each letter in the subsegment $$$k$$$ times, where $$$k$$$ is the index of the corresponding letter in the alphabet. For example, if the question is about the substring "abbcb", then Vasya repeats letter 'a' once, each of the letters 'b' twice, letter 'c" three times, so that the resulting string is "abbbbcccbb", its length is $$$10$$$. Vasya is interested about the length of the resulting string.Help Petya find the length of each string obtained by Vasya.

The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1\leq n\leq 100\,000$$$, $$$1\leq q \leq 100\,000$$$) — the length of the song and the number of questions.  The second line contains one string $$$s$$$ — the song, consisting of $$$n$$$ lowercase letters of English letters. Vasya's questions are contained in the next $$$q$$$ lines. Each line contains two integers $$$l$$$ and $$$r$$$ ($$$1 \leq l \leq r \leq n$$$) — the bounds of the question.

Print $$$q$$$ lines: for each question print the length of the string obtained by Vasya.

NoteIn the first example Vasya is interested in three questions. In the first question Vasya considers the substring "aba", that transforms to "abba", so the answer is equal to $$$4$$$. In the second question Vasya considers "baca", that transforms to "bbaccca", so the answer is $$$7$$$. In the third question Vasya considers the string "abacaba",that transforms to "abbacccabba" of length $$$11$$$.

const lib = require('readline');
const rl = lib.createInterface({
    input: process.stdin,
    output: process.stdout
});
const getLine = (function () {
    const getLineGen = (async function* () {
        for await (const line of rl) {
            yield line;
        }
    })();
    return async () => {
        let next;
        do {
            next = (await getLineGen.next());
        } while (!next.done && next.value === '');
        return next.value;
    };
})();

let solve = async () => {
    let n, q;
    [n, q] = (await getLine()).split(' ').map(num => parseInt(num));
    const s = await getLine();
    const pre = new Array(n);
    const zeroCharCode = 'a'.charCodeAt(0) - 1;
    pre[0] = s.charCodeAt(0) - zeroCharCode;
    for(let i = 1; i < n; i++) {
        pre[i] = pre[i-1] + s.charCodeAt(i) - zeroCharCode;
    }
    for(let i = 0; i<q; i++){
        let l, r;
        [l,r] = (await getLine()).split(' ').map(num=>parseInt(num));
        let res = pre[r-1];
        if (l>1)
            res-=pre[l-2];
        console.log(res);
    }

}
solve();

const lib = require('readline');
const fs = require('fs');
const rl = lib.createInterface({
    input: process.stdin,   // fs.createReadStream('input.txt'),
    output: process.stdout
});
//const inp = fs.createReadStream('input.txt');
const getLine = (function () {
    const getLineGen = (async function* () {
        for await (const line of rl) {
            yield line;
        }
    })();
    return async () => ((await getLineGen.next()).value);
})();

let solve = async () => {
    let n, q;
    [n, q] = (await getLine()).split(' ').map(num => parseInt(num));
    const s = await getLine();
    const pre = new Array(n);
    if (s.length !== n)
        throw new Error('test');
    const zeroCharCode = 'a'.charCodeAt(0) - 1;
    pre[0] = s.charCodeAt(0) - zeroCharCode;
    const cur =s.charCodeAt(0);
    if (Number.isNaN(cur)) {
        console.log(i + ' ' + s.charAt(0));
        throw new Error('test');
    }
    for(let i = 1; i < n; i++) {
        const cur =s.charCodeAt(i);
        if (Number.isNaN(cur)) {
            console.log(i + ' ' + s.charAt(i));
            throw new Error('test');
        }

        pre[i] = pre[i-1] + s.charCodeAt(i) - zeroCharCode;
        if (pre[i] === undefined) {
            throw new Error(i + ' ');
        }
    }
    for(let i = 0; i<q; i++) {
        let l, r;
        const line = (await getLine()).replace(/\s+/g, ' ').trim();
        [l, r] = line.split(' ').map(num => parseInt(num));
        if (!Number.isInteger(l) || !Number.isInteger(r)) {
            i--;
            continue;
            //throw new Error('not interger');
        }
        l = Math.floor(l + 0.000001);
        r = Math.floor(r + 0.000001);
        if (l<1 || l>n || r<1 || r>n)
            throw new Error('test');
        let res = pre[r-1];
        if (l>1)
            res -= pre[l-2];
        /* if (pre[r-1] === undefined || (l !== 1 && pre[l-2] === undefined)){
            throw new Error(i + '');
        } */

        /* if (res === undefined)
            throw new Error('ololo'); */
        console.log(res);
    }

}

/*let txtFile = fs.createWriteStream('output2.txt', {flags:'a'});
txtFile.write('100000 100000\n');
const acode = 'a'.charCodeAt(0);
for(let i = 0; i<100000; i++){
    txtFile.write(String.fromCharCode(i%20 + acode));
}
txtFile.write('\n');
for(let i = 0; i<100000; i++)
    txtFile.write('1 ' + (i + 1) + '\n');
*/
solve();

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}
// Make a Snippet for the code above this and then write your logic in main();

function main() {

 	let testCaseNum = readline();
	testCaseNum = testCaseNum.split(' ').filter(item => +item);
	testCaseNum = testCaseNum[1];

 	let str = readline();

	let prefix = new Array(str.length).fill(0);
	prefix[-1] = 0;
	for (let i = 0; i < str.length; i++) {
		prefix[i] += prefix[i - 1];
		prefix[i] += (str[i].charCodeAt() - 96);
	}

 	while (testCaseNum) {
 		let tmp = readline();
 		tmp = tmp.split(' ').filter(item => +item);

		console.log(prefix[tmp[1] - 1] - prefix[tmp[0] - 2]);
 		testCaseNum --;
 	}
}

'use strict';

function solve(n, m, a, b) {
  const sum = new Array(n).fill(0);
  for (let i = 0; i < n; i++) {
    var _sum;
    sum[i] = ((_sum = sum[i - 1]) !== null && _sum !== void 0 ? _sum : 0) + a.charCodeAt(i) - 96;
  }
  const res = [];
  for (let [l, r] of b) {
    var _sum2;
    l--, r--;
    res.push(sum[r] - ((_sum2 = sum[l - 1]) !== null && _sum2 !== void 0 ? _sum2 : 0));
  }
  return res.join('\n');
}

async function main(read) {
  try {
    let t = 1;
    let res = new Array(t);
    for (let i = 0; i < t; i++) {
      const [n, m] = (await read()).split(' ').map(Number);
      const a = await read();
      const b = [];
      let tmp = m;
      while (tmp--) {
        b.push((await read()).split(' ').map(Number));
      }
      res[i] = solve(n, m, a, b);
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  process.stdout.write(output);
});

function gcd(x, y) {
   if (y === 0) {
      return x;
   }
   return gcd(y, x % y);
}

function div(a, b) {
   return Math.floor(a / b);
}

function solve() {
   var nq = readArray(Number);
   var n = nq[0];
   var q = nq[1];
   var s = read();
   var charCodeA = 'a'.charCodeAt(0);
   var sum = [0];
   for (var i = 0; i < n; i++) {
      var num = s.charCodeAt(i) - charCodeA + 1;
      sum.push(num + sum[i]);
   }
   for(var i = 0; i < q; i++) {
      var lr = readArray(Number);
      var l = lr[0];
      var r = lr[1];
      write(sum[r] - sum[l - 1]);
   }
}

function init() {
   var T;
   T = 1;
   //T = Number(read());
   for (var t = 0; t < T; t++) {
      solve();
   }
}

function sortF(a, b) {
   if (a < b) {
      return -1;
   }
   if (a > b) {
      return 1;
   }
   return 0;
}

function readArray(f) {
   return read().split(' ').map(f);
}

var isNode = typeof console !== 'undefined';
var MEMORY = [];
var MEMORY_INDEX = 0;
if (isNode) {
   var fs = require('fs');
   var path = require('path');

   var inTextName;
   var outTextName;

   var needWriteFile = false;

   var writeResult = '';
   if (process.argv.length === 5) {
      inTextName = process.argv[3];
      outTextName = process.argv[4];
   }

   if (inTextName) {
      fs.readFile(path.join(__dirname, inTextName), 'utf8', (err, data) => {
         MEMORY = data.split('\r\n');
         init();
         if (needWriteFile) {
            fs.writeFile(path.join(__dirname, outTextName), writeResult, (err) => {
               err ? console.error(err) : console.log('good')
            });
         }
      });
   } else {
      MEMORY = fs.readFileSync(0, 'utf-8').split('\r\n');
      init();
   }
} else {
   init();
}

function write(value) {
   if (needWriteFile) {
      writeResult += value + '\n';
      return;
   }
   isNode ? console.log('' + value) : print('' + value);
}
function read() {
   return isNode ? MEMORY[MEMORY_INDEX++] : readline();
}
function readAll() {
   return isNode && MEMORY;
}

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
// import * as readline from 'readline'
// const rl = readline.createInterface({ input: process.stdin, output: process.stdout })
// const ask = (query: string) => new Promise<string>((resolve) => rl.question(query, resolve))
// // Don't forget `rl.close()`.
const INT = Math.floor;
Array.prototype.last = function () {
    return this.length === 0 ? undefined : this[this.length - 1];
};
Array.prototype.isEmpty = function () {
    return this.length === 0;
};
const less = (a, b) => (a == b ? 0 : a < b ? -1 : 1);
const greater = (a, b) => (a == b ? 0 : a < b ? 1 : -1);
const bigIntMax = (...args) => args.reduce((m, e) => (e > m ? e : m));
const bigIntMin = (...args) => args.reduce((m, e) => (e < m ? e : m));
const bigIntAbs = (arg) => (arg < 0 ? -arg : arg);
function read_stdin() {
    return fs.readFileSync(process.env.NODE_ENV === 'debug' ? stdin : process.stdin.fd, 'utf8');
}
class Input {
    constructor(str) {
        this.index = 0;
        this.inputs = (str ? str : read_stdin()).split(/\s+/);
    }
    number() {
        return Number(this.inputs[this.index++]);
    }
    numbers(n) {
        return this.inputs.slice(this.index, (this.index += n)).map(Number);
    }
    bigint() {
        return BigInt(this.inputs[this.index++]);
    }
    bigints(n) {
        return this.inputs.slice(this.index, (this.index += n)).map(BigInt);
    }
    word() {
        return this.inputs[this.index++];
    }
    words(n) {
        return this.inputs.slice(this.index, (this.index += n));
    }
}
function array(len, init) {
    return Array(len).fill(init);
}
function array2(h, w, init) {
    return array(h, 0).map(() => array(w, init));
}
function main() {
    const input = new Input();
    const n = input.number();
    const q = input.number();
    const a = 'a'.charCodeAt(0);
    const s = input.word();
    const snums = Array.from(s).map((c) => c.charCodeAt(0) - a + 1);
    const cumu = array(n + 1, 0);
    for (let i = 0; i < n; i++) {
        cumu[i + 1] = cumu[i] + snums[i];
    }
    const ans = Array(q);
    for (let i = 0; i < q; i++) {
        const l = input.number();
        const r = input.number();
        ans[i] = cumu[r] - cumu[l - 1];
    }
    console.log(ans.join('\n'));
}
main();
//# sourceMappingURL=aoj.js.map

var readline = require('readline');

var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
var lines = [];
rl.on('line', function(input) {
    lines.push(input);
});
rl.on('close', function() {
    var l = 0;
    var [n, q] = lines[l++].split(' ').map(Number);
    var str = lines[l++];
    var qs = [];
    for (var i = 0; i < q; i++) {
        var range = lines[l++].split(' ').map(Number)
        qs.push(range)
    }
    console.log(solve(str, qs))
});

function solve(str, qs) {
    const count = []
    let cur
    for (let i = 0; i < str.length; i++) {
        const ch = str[i]
        if (i) {
            cur = { ...cur }
            cur[ch] = (cur[ch] || 0) + 1
        } else {
            cur = {}
            cur[ch] = 1
        }
        count[i] = cur
    }

    return qs.map(([l, r]) => {
        l--
        r--
        // count[r] - count[l - 1]
        let sum = 0
        for (let ch in count[r]) {
            const k = ch.charCodeAt(0) - 'a'.charCodeAt(0) + 1
            if (l) {
                sum += (count[r][ch] - (count[l - 1][ch] || 0)) * k
            } else {
                sum += count[r][ch] * k
            }
        }
        return sum
    }).join('\n')
}

nq = readline().split(' ');
n = +nq[0];
q = +nq[1];

str = readline();

a = [0];

len = str.length;
for (i = 0; i < len; ++i) {
  x = str.charCodeAt(i) - 96

  a.push(x + a[i])
}

for (i = 0; i < q; ++i) {
  lr = readline().split(' ')
  l = +lr[0];
  r = +lr[1];

  print(a[r] - a[l - 1]);
}

const lib = require('readline');
const rl = lib.createInterface({
    input: process.stdin,
    output: process.stdout
});
const getLine = (function () {
    const getLineGen = (async function* () {
        for await (const line of rl) {
            yield line;
        }
    })();
    return async () => ((await getLineGen.next()).value);
})();

let solve = async () => {
    let n, q;
    [n, q] = (await getLine()).split(' ').map(num => parseInt(num));
    const s = await getLine();
    const pre = new Array(n);
    const zeroCharCode = 'a'.charCodeAt(0) - 1;
    pre[0] = s.charCodeAt(0) - zeroCharCode;
    for(let i = 1; i < n; i++) {
        pre[i] = pre[i-1] + s.charCodeAt(i) - zeroCharCode;
    }
    for(let i = 0; i<q; i++){
        let l, r;
        [l,r] = (await getLine()).split(' ').map(num=>parseInt(num));
        let res = pre[r-1];
        if (l>1)
            res-=pre[l-2];
        console.log(res);
    }

}
solve();

const lib = require('readline');
const rl = lib.createInterface({
    input: process.stdin,
    output: process.stdout
});
const getLine = (function () {
    const getLineGen = (async function* () {
        for await (const line of rl) {
            yield line;
        }
    })();
    return async () => ((await getLineGen.next()).value);
})();

let solve = async () => {
    let n, q;
    [n, q] = (await getLine()).split(' ').map(num => parseInt(num));
    const s = await getLine();
    const pre = new Array(n);
    const zeroCharCode = 'a'.charCodeAt(0) - 1;
    pre[0] = s.charCodeAt(0) - zeroCharCode;
    for(let i = 1; i < n; i++) {
        pre[i] = pre[i-1] + s.charCodeAt(i) - zeroCharCode;
    }
    for(let i = 0; i<q; i++){
        let l, r;
        [l,r] = (await getLine()).split(' ').map(num=>parseInt(num));
        let res = pre[r];
        if (l>1)
            res-=pre[l-1];
        console.log(res);
    }

}
solve();

const lib = require('readline');
const fs = require('fs');
const rl = lib.createInterface({
    input: process.stdin,   // fs.createReadStream('input.txt'),
    output: process.stdout
});
//const inp = fs.createReadStream('input.txt');
const getLine = (function () {
    const getLineGen = (async function* () {
        for await (const line of rl) {
            yield line;
        }
    })();
    return async () => ((await getLineGen.next()).value);
})();

let solve = async () => {
    let n, q;
    [n, q] = (await getLine()).split(' ').map(num => parseInt(num));
    const s = await getLine();
    const pre = new Array(n);
    const zeroCharCode = 'a'.charCodeAt(0) - 1;
    pre[0] = s.charCodeAt(0) - zeroCharCode;
    for(let i = 1; i < n; i++) {
        const cur =s.charCodeAt(i);
        pre[i] = pre[i-1] + s.charCodeAt(i) - zeroCharCode;
    }
    for(let i = 0; i<q; i++){
        let l, r;
        [l,r] = (await getLine()).split(' ').map(parseInt);
        let res = pre[r];
        if (l>1)
            res-=pre[l-1];
        console.log(res);
    }

}
solve();

const lib = require('readline');
const fs = require('fs');
const rl = lib.createInterface({
    input: process.stdin,   // fs.createReadStream('input.txt'),
    output: process.stdout
});
//const inp = fs.createReadStream('input.txt');
const getLine = (function () {
    const getLineGen = (async function* () {
        for await (const line of rl) {
            yield line;
        }
    })();
    return async () => ((await getLineGen.next()).value);
})();

let solve = async () => {
    let n, q;
    [n, q] = (await getLine()).split(' ').map(num => parseInt(num));
    const s = await getLine();
    const pre = new Array(n);
    if (s.length !== n)
        throw new Error('test');
    const zeroCharCode = 'a'.charCodeAt(0) - 1;
    pre[0] = s.charCodeAt(0) - zeroCharCode;
    const cur =s.charCodeAt(0);
    if (Number.isNaN(cur)) {
        console.log(i + ' ' + s.charAt(0));
        throw new Error('test');
    }
    for(let i = 1; i < n; i++) {
        const cur =s.charCodeAt(i);
        if (Number.isNaN(cur)) {
            console.log(i + ' ' + s.charAt(i));
            throw new Error('test');
        }

        pre[i] = pre[i-1] + s.charCodeAt(i) - zeroCharCode;
        if (pre[i] === undefined) {
            throw new Error(i + ' ');
        }
    }
    for(let i = 0; i<q; i++) {
        let l, r;
        const line = (await getLine()).replace(/\s+/g, ' ').trim();
        [l, r] = line.split(' ').map(num => parseInt(num));
        if (!Number.isInteger(l) || !Number.isInteger(r)) {
            throw new Error('not interger');
        }
        l = Math.floor(l + 0.000001);
        r = Math.floor(r + 0.000001);
        if (l<1 || l>n || r<1 || r>n)
            throw new Error('test');
        let res = pre[r-1];
        if (l>1)
            res -= pre[l-2];
        /* if (pre[r-1] === undefined || (l !== 1 && pre[l-2] === undefined)){
            throw new Error(i + '');
        } */

        /* if (res === undefined)
            throw new Error('ololo'); */
        console.log(res);
    }

}

/*let txtFile = fs.createWriteStream('output2.txt', {flags:'a'});
txtFile.write('100000 100000\n');
const acode = 'a'.charCodeAt(0);
for(let i = 0; i<100000; i++){
    txtFile.write(String.fromCharCode(i%20 + acode));
}
txtFile.write('\n');
for(let i = 0; i<100000; i++)
    txtFile.write('1 ' + (i + 1) + '\n');
*/
solve();

const lib = require('readline');
const fs = require('fs');
const rl = lib.createInterface({
    input: process.stdin,   // fs.createReadStream('input.txt'),
    output: process.stdout
});
//const inp = fs.createReadStream('input.txt');
const getLine = (function () {
    const getLineGen = (async function* () {
        for await (const line of rl) {
            yield line;
        }
    })();
    return async () => ((await getLineGen.next()).value);
})();

let solve = async () => {
    let n, q;
    [n, q] = (await getLine()).split(' ').map(num => parseInt(num));
    const s = await getLine();
    const pre = new Array(n);
    if (s.length !== n)
        throw new Error('test');
    const zeroCharCode = 'a'.charCodeAt(0) - 1;
    pre[0] = s.charCodeAt(0) - zeroCharCode;
    const cur =s.charCodeAt(0);
    if (Number.isNaN(cur)) {
        console.log(i + ' ' + s.charAt(0));
        throw new Error('test');
    }
    for(let i = 1; i < n; i++) {
        const cur =s.charCodeAt(i);
        if (Number.isNaN(cur)) {
            console.log(i + ' ' + s.charAt(i));
            throw new Error('test');
        }

        pre[i] = pre[i-1] + s.charCodeAt(i) - zeroCharCode;
        if (pre[i] === undefined) {
            throw new Error(i + ' ');
        }
    }
    for(let i = 0; i<q; i++) {
        let l, r;
        [l, r] = (await getLine()).trim().split(' ').map(num => parseInt(num));
        if (!Number.isInteger(l) || !Number.isInteger(r)) {
            throw new Error('not interger');
        }
        l = Math.floor(l + 0.000001);
        r = Math.floor(r + 0.000001);
        if (l<1 || l>n || r<1 || r>n)
            throw new Error('test');
        let res = pre[r-1];
        if (l>1)
            res -= pre[l-2];
        /* if (pre[r-1] === undefined || (l !== 1 && pre[l-2] === undefined)){
            throw new Error(i + '');
        } */

        /* if (res === undefined)
            throw new Error('ololo'); */
        console.log(res);
    }

}

/*let txtFile = fs.createWriteStream('output2.txt', {flags:'a'});
txtFile.write('100000 100000\n');
const acode = 'a'.charCodeAt(0);
for(let i = 0; i<100000; i++){
    txtFile.write(String.fromCharCode(i%20 + acode));
}
txtFile.write('\n');
for(let i = 0; i<100000; i++)
    txtFile.write('1 ' + (i + 1) + '\n');
*/
solve();

const lib = require('readline');
const fs = require('fs');
const rl = lib.createInterface({
    input: process.stdin,   // fs.createReadStream('input.txt'),
    output: process.stdout
});
//const inp = fs.createReadStream('input.txt');
const getLine = (function () {
    const getLineGen = (async function* () {
        for await (const line of rl) {
            yield line;
        }
    })();
    return async () => ((await getLineGen.next()).value);
})();

let solve = async () => {
    let n, q;
    [n, q] = (await getLine()).split(' ').map(num => parseInt(num));
    const s = await getLine();
    const pre = new Array(n);
    if (s.length !== n)
        throw new Error('test');
    const zeroCharCode = 'a'.charCodeAt(0) - 1;
    pre[0] = s.charCodeAt(0) - zeroCharCode;
    const cur =s.charCodeAt(0);
    if (Number.isNaN(cur)) {
        console.log(i + ' ' + s.charAt(0));
        throw new Error('test');
    }
    for(let i = 1; i < n; i++) {
        const cur =s.charCodeAt(i);
        if (Number.isNaN(cur)) {
            console.log(i + ' ' + s.charAt(i));
            throw new Error('test');
        }

        pre[i] = pre[i-1] + s.charCodeAt(i) - zeroCharCode;
        if (pre[i] === undefined) {
            throw new Error(i + ' ');
        }
    }
    for(let i = 0; i<q; i++) {
        let l, r;
        [l, r] = (await getLine()).split(' ').map(num => parseInt(num));
        if (!Number.isInteger(l) || !Number.isInteger(r)) {
            throw new Error('not interger');
        }
        l = Math.floor(l + 0.000001);
        r = Math.floor(r + 0.000001);
        if (l<1 || l>n || r<1 || r>n)
            throw new Error('test');
        let res = pre[r-1];
        if (l>1)
            res -= pre[l-2];
        /* if (pre[r-1] === undefined || (l !== 1 && pre[l-2] === undefined)){
            throw new Error(i + '');
        } */

        /* if (res === undefined)
            throw new Error('ololo'); */
        console.log(res);
    }

}

/*let txtFile = fs.createWriteStream('output2.txt', {flags:'a'});
txtFile.write('100000 100000\n');
const acode = 'a'.charCodeAt(0);
for(let i = 0; i<100000; i++){
    txtFile.write(String.fromCharCode(i%20 + acode));
}
txtFile.write('\n');
for(let i = 0; i<100000; i++)
    txtFile.write('1 ' + (i + 1) + '\n');
*/
solve();

const lib = require('readline');
const fs = require('fs');
const rl = lib.createInterface({
    input: process.stdin,   // fs.createReadStream('input.txt'),
    output: process.stdout
});
//const inp = fs.createReadStream('input.txt');
const getLine = (function () {
    const getLineGen = (async function* () {
        for await (const line of rl) {
            yield line;
        }
    })();
    return async () => ((await getLineGen.next()).value);
})();

let solve = async () => {
    let n, q;
    [n, q] = (await getLine()).split(' ').map(num => parseInt(num));
    const s = await getLine();
    const pre = new Array(n);
    if (s.length !== n)
        throw new Error('test');
    const zeroCharCode = 'a'.charCodeAt(0) - 1;
    pre[0] = s.charCodeAt(0) - zeroCharCode;
    const cur =s.charCodeAt(0);
    if (Number.isNaN(cur)) {
        console.log(i + ' ' + s.charAt(0));
        throw new Error('test');
    }
    for(let i = 1; i < n; i++) {
        const cur =s.charCodeAt(i);
        if (Number.isNaN(cur)) {
            console.log(i + ' ' + s.charAt(i));
            throw new Error('test');
        }

        pre[i] = pre[i-1] + s.charCodeAt(i) - zeroCharCode;
        if (pre[i] === undefined) {
            throw new Error(i + ' ');
        }
    }
    for(let i = 0; i<q; i++) {
        let l, r;
        [l, r] = (await getLine()).split(' ').map(num => parseInt(num));
        l = Math.floor(l + 0.000001);
        r = Math.floor(r + 0.000001);
        if (l<1 || l>n || r<1 || r>n)
            throw new Error('test');
        let res = pre[r-1];
        if (l>1)
            res -= pre[l-2];
        /* if (pre[r-1] === undefined || (l !== 1 && pre[l-2] === undefined)){
            throw new Error(i + '');
        } */

        /* if (res === undefined)
            throw new Error('ololo'); */
        console.log(res);
    }

}

/*let txtFile = fs.createWriteStream('output2.txt', {flags:'a'});
txtFile.write('100000 100000\n');
const acode = 'a'.charCodeAt(0);
for(let i = 0; i<100000; i++){
    txtFile.write(String.fromCharCode(i%20 + acode));
}
txtFile.write('\n');
for(let i = 0; i<100000; i++)
    txtFile.write('1 ' + (i + 1) + '\n');
*/
solve();

const lib = require('readline');
const fs = require('fs');
const rl = lib.createInterface({
    input: process.stdin,   // fs.createReadStream('input.txt'),
    output: process.stdout
});
//const inp = fs.createReadStream('input.txt');
const getLine = (function () {
    const getLineGen = (async function* () {
        for await (const line of rl) {
            yield line;
        }
    })();
    return async () => ((await getLineGen.next()).value);
})();

let solve = async () => {
    let n, q;
    [n, q] = (await getLine()).split(' ').map(num => parseInt(num));
    const s = await getLine();
    const pre = new Array(n);
    if (s.length !== n)
        throw new Error('test');
    const zeroCharCode = 'a'.charCodeAt(0) - 1;
    pre[0] = s.charCodeAt(0) - zeroCharCode;
    const cur =s.charCodeAt(0);
    if (Number.isNaN(cur)) {
        console.log(i + ' ' + s.charAt(0));
        throw new Error('test');
    }
    for(let i = 1; i < n; i++) {
        const cur =s.charCodeAt(i);
        if (Number.isNaN(cur)) {
            console.log(i + ' ' + s.charAt(i));
            throw new Error('test');
        }

        pre[i] = pre[i-1] + s.charCodeAt(i) - zeroCharCode;
        if (Number.isNaN(pre[i])) {
            throw new Error(i + ' ');
        }
    }
    for(let i = 0; i<q; i++) {
        let l, r;
        [l, r] = (await getLine()).split(' ').map(num => parseInt(num));
        l = Math.floor(l + 0.000001);
        r = Math.floor(r + 0.000001);
        if (l<1 || l>n || r<1 || r>n)
            throw new Error('test');
        let res = pre[r-1];
        if (l>1)
            res -= pre[l-2];
        /* if (pre[r-1] === undefined || (l !== 1 && pre[l-2] === undefined)){
            throw new Error(i + '');
        } */

        /* if (res === undefined)
            throw new Error('ololo'); */
        console.log(res);
    }

}

/*let txtFile = fs.createWriteStream('output2.txt', {flags:'a'});
txtFile.write('100000 100000\n');
const acode = 'a'.charCodeAt(0);
for(let i = 0; i<100000; i++){
    txtFile.write(String.fromCharCode(i%20 + acode));
}
txtFile.write('\n');
for(let i = 0; i<100000; i++)
    txtFile.write('1 ' + (i + 1) + '\n');
*/
solve();

const lib = require('readline');
const fs = require('fs');
const rl = lib.createInterface({
    input: process.stdin,   // fs.createReadStream('input.txt'),
    output: process.stdout
});
//const inp = fs.createReadStream('input.txt');
const getLine = (function () {
    const getLineGen = (async function* () {
        for await (const line of rl) {
            yield line;
        }
    })();
    return async () => ((await getLineGen.next()).value);
})();

let solve = async () => {
    let n, q;
    [n, q] = (await getLine()).split(' ').map(num => parseInt(num));
    const s = await getLine();
    const pre = new Array(n);
    if (s.length !== n)
        throw new Error('test');
    const zeroCharCode = 'a'.charCodeAt(0) - 1;
    pre[0] = s.charCodeAt(0) - zeroCharCode;
    const cur =s.charCodeAt(0);
    if (Number.isNaN(cur)) {
        console.log(i + ' ' + s.charAt(0));
        throw new Error('test');
    }
    for(let i = 1; i < n; i++) {
        const cur =s.charCodeAt(i);
        if (Number.isNaN(cur)) {
            console.log(i + ' ' + s.charAt(i));
            throw new Error('test');
        }

        pre[i] = pre[i-1] + s.charCodeAt(i) - zeroCharCode;
        if (Number.isNaN(pre[i])) {
            throw new Error(i + ' ');
        }
    }
    for(let i = 0; i<q; i++) {
        let l, r;
        [l, r] = (await getLine()).split(' ').map(num => parseInt(num));
        l = Math.floor(l + 0.000001);
        r = Math.floor(r + 0.000001);
        /* if (l<1 || l>n || r<1 || r>n)
            throw new Error('test');*/
        let res = pre[r-1];
        if (l>1)
            res -= pre[l-2];
        /* if (pre[r-1] === undefined || (l !== 1 && pre[l-2] === undefined)){
            throw new Error(i + '');
        } */

        /* if (res === undefined)
            throw new Error('ololo'); */
        console.log(res);
    }

}

/*let txtFile = fs.createWriteStream('output2.txt', {flags:'a'});
txtFile.write('100000 100000\n');
const acode = 'a'.charCodeAt(0);
for(let i = 0; i<100000; i++){
    txtFile.write(String.fromCharCode(i%20 + acode));
}
txtFile.write('\n');
for(let i = 0; i<100000; i++)
    txtFile.write('1 ' + (i + 1) + '\n');
*/
solve();

const lib = require('readline');
const fs = require('fs');
const rl = lib.createInterface({
    input: process.stdin,   // fs.createReadStream('input.txt'),
    output: process.stdout
});
//const inp = fs.createReadStream('input.txt');
const getLine = (function () {
    const getLineGen = (async function* () {
        for await (const line of rl) {
            yield line;
        }
    })();
    return async () => ((await getLineGen.next()).value);
})();

let solve = async () => {
    let n, q;
    [n, q] = (await getLine()).split(' ').map(num => parseInt(num));
    const s = await getLine();
    const pre = new Array(n);
    if (s.length !== n)
        throw new Error('test');
    const zeroCharCode = 'a'.charCodeAt(0) - 1;
    pre[0] = s.charCodeAt(0) - zeroCharCode;
    const cur =s.charCodeAt(0);
    if (Number.isNaN(cur)) {
        console.log(i + ' ' + s.charAt(0));
        throw new Error('test');
    }
    for(let i = 1; i < n; i++) {
        const cur =s.charCodeAt(i);
        if (Number.isNaN(cur)) {
            console.log(i + ' ' + s.charAt(i));
            throw new Error('test');
        }

        pre[i] = pre[i-1] + s.charCodeAt(i) - zeroCharCode;
        if (Number.isNaN(pre[i])) {
            throw new Error(i + ' ');
        }
    }
    for(let i = 0; i<q; i++) {
        let l, r;
        [l, r] = (await getLine()).split(' ').map(num => parseInt(num));
        /* if (l<1 || l>n || r<1 || r>n)
            throw new Error('test');*/
        let res = pre[r-1];
        if (l>1)
            res -= pre[l-2];
        if (pre[r-1] === undefined || (l !== 1 && pre[l-2] === undefined)){
            throw new Error(i + '');
        }

        /* if (res === undefined)
            throw new Error('ololo'); */
        console.log(res);
    }

}

/*let txtFile = fs.createWriteStream('output2.txt', {flags:'a'});
txtFile.write('100000 100000\n');
const acode = 'a'.charCodeAt(0);
for(let i = 0; i<100000; i++){
    txtFile.write(String.fromCharCode(i%20 + acode));
}
txtFile.write('\n');
for(let i = 0; i<100000; i++)
    txtFile.write('1 ' + (i + 1) + '\n');
*/
solve();

const lib = require('readline');
const fs = require('fs');
const rl = lib.createInterface({
    input: process.stdin,   // fs.createReadStream('input.txt'),
    output: process.stdout
});
//const inp = fs.createReadStream('input.txt');
const getLine = (function () {
    const getLineGen = (async function* () {
        for await (const line of rl) {
            yield line;
        }
    })();
    return async () => ((await getLineGen.next()).value);
})();

let solve = async () => {
    let n, q;
    [n, q] = (await getLine()).split(' ').map(num => parseInt(num));
    const s = await getLine();
    const pre = new Array(n);
    if (s.length !== n)
        throw new Error('test');
    const zeroCharCode = 'a'.charCodeAt(0) - 1;
    pre[0] = s.charCodeAt(0) - zeroCharCode;
    const cur =s.charCodeAt(0);
    if (Number.isNaN(cur)) {
        console.log(i + ' ' + s.charAt(0));
        throw new Error('test');
    }
    for(let i = 1; i < n; i++) {
        const cur =s.charCodeAt(i);
        if (Number.isNaN(cur)) {
            console.log(i + ' ' + s.charAt(i));
            throw new Error('test');
        }

        pre[i] = pre[i-1] + s.charCodeAt(i) - zeroCharCode;
        if (Number.isNaN(pre[i])) {
            throw new Error(i + ' ');
        }
    }
    for(let i = 0; i<q; i++) {
        let l, r;
        [l, r] = (await getLine()).split(' ').map(num => parseInt(num));
        /* if (l<1 || l>n || r<1 || r>n)
            throw new Error('test');*/
        let res = pre[r-1];
        if (l>1)
            res -= pre[l-2];
        /*if (Number.isNaN(pre[r-1]) || (l !== 1 && Number.isNaN(pre[l-2]))){
            throw new Error(i + '');
        }*/
        if (res === undefined)
            throw new Error('ololo');
        console.log(res);
    }

}

/*let txtFile = fs.createWriteStream('output2.txt', {flags:'a'});
txtFile.write('100000 100000\n');
const acode = 'a'.charCodeAt(0);
for(let i = 0; i<100000; i++){
    txtFile.write(String.fromCharCode(i%20 + acode));
}
txtFile.write('\n');
for(let i = 0; i<100000; i++)
    txtFile.write('1 ' + (i + 1) + '\n');
*/
solve();

const lib = require('readline');
const fs = require('fs');
const rl = lib.createInterface({
    input: process.stdin,   // fs.createReadStream('input.txt'),
    output: process.stdout
});
//const inp = fs.createReadStream('input.txt');
const getLine = (function () {
    const getLineGen = (async function* () {
        for await (const line of rl) {
            yield line;
        }
    })();
    return async () => ((await getLineGen.next()).value);
})();

let solve = async () => {
    let n, q;
    [n, q] = (await getLine()).split(' ').map(num => parseInt(num));
    const s = await getLine();
    const pre = new Array(n);
    if (s.length !== n)
        throw new Error('test');
    const zeroCharCode = 'a'.charCodeAt(0) - 1;
    pre[0] = s.charCodeAt(0) - zeroCharCode;
    const cur =s.charCodeAt(0);
    if (Number.isNaN(cur)) {
        console.log(i + ' ' + s.charAt(0));
        throw new Error('test');
    }
    for(let i = 1; i < n; i++) {
        const cur =s.charCodeAt(i);
        if (Number.isNaN(cur)) {
            console.log(i + ' ' + s.charAt(i));
            throw new Error('test');
        }

        pre[i] = pre[i-1] + s.charCodeAt(i) - zeroCharCode;
        if (Number.isNaN(pre[i])) {
            throw new Error(i + ' ');
        }
    }
    for(let i = 0; i<q; i++) {
        let l, r;
        [l, r] = (await getLine()).split(' ').map(num => parseInt(num));
        /* if (l<1 || l>n || r<1 || r>n)
            throw new Error('test');*/
        let res = pre[r-1];
        if (l>1)
            res -= pre[l-2];
        /*if (Number.isNaN(pre[r-1]) || (l !== 1 && Number.isNaN(pre[l-2]))){
            throw new Error(i + '');
        }*/
        console.log(res);
    }

}

/*let txtFile = fs.createWriteStream('output2.txt', {flags:'a'});
txtFile.write('100000 100000\n');
const acode = 'a'.charCodeAt(0);
for(let i = 0; i<100000; i++){
    txtFile.write(String.fromCharCode(i%20 + acode));
}
txtFile.write('\n');
for(let i = 0; i<100000; i++)
    txtFile.write('1 ' + (i + 1) + '\n');
*/
solve();

const lib = require('readline');
const fs = require('fs');
const rl = lib.createInterface({
    input: process.stdin,   // fs.createReadStream('input.txt'),
    output: process.stdout
});
//const inp = fs.createReadStream('input.txt');
const getLine = (function () {
    const getLineGen = (async function* () {
        for await (const line of rl) {
            yield line;
        }
    })();
    return async () => ((await getLineGen.next()).value);
})();

let solve = async () => {
    let n, q;
    [n, q] = (await getLine()).split(' ').map(num => parseInt(num));
    const s = await getLine();
    const pre = new Array(n);
    if (s.length !== n)
        throw new Error('test');
    const zeroCharCode = 'a'.charCodeAt(0) - 1;
    pre[0] = s.charCodeAt(0) - zeroCharCode;
    const cur =s.charCodeAt(0);
    if (Number.isNaN(cur)) {
        console.log(i + ' ' + s.charAt(0));
        throw new Error('test');
    }
    for(let i = 1; i < n; i++) {
        const cur =s.charCodeAt(i);
        if (Number.isNaN(cur)) {
            console.log(i + ' ' + s.charAt(i));
            throw new Error('test');
        }

        pre[i] = pre[i-1] + s.charCodeAt(i) - zeroCharCode;
        if (Number.isNaN(pre[i])) {
            throw new Error(i + ' ');
        }
    }
    for(let i = 0; i<q; i++) {
        let l, r;
        [l, r] = (await getLine()).split(' ').map(num => parseInt(num));
        if (l<1 || l>n || r<1 || r>n)
            throw new Error('test');
        let res = pre[r-1];
        if (l>1)
            res -= pre[l-2];
        /*if (Number.isNaN(pre[r-1]) || (l !== 1 && Number.isNaN(pre[l-2]))){
            throw new Error(i + '');
        }*/
        console.log(res);
    }

}

/*let txtFile = fs.createWriteStream('output2.txt', {flags:'a'});
txtFile.write('100000 100000\n');
const acode = 'a'.charCodeAt(0);
for(let i = 0; i<100000; i++){
    txtFile.write(String.fromCharCode(i%20 + acode));
}
txtFile.write('\n');
for(let i = 0; i<100000; i++)
    txtFile.write('1 ' + (i + 1) + '\n');
*/
solve();

const lib = require('readline');
const fs = require('fs');
const rl = lib.createInterface({
    input: process.stdin,   // fs.createReadStream('input.txt'),
    output: process.stdout
});
//const inp = fs.createReadStream('input.txt');
const getLine = (function () {
    const getLineGen = (async function* () {
        for await (const line of rl) {
            yield line;
        }
    })();
    return async () => ((await getLineGen.next()).value);
})();

let solve = async () => {
    let n, q;
    [n, q] = (await getLine()).split(' ').map(num => parseInt(num));
    const s = await getLine();
    const pre = new Array(n);
    if (s.length !== n)
        throw new Error('test');
    const zeroCharCode = 'a'.charCodeAt(0) - 1;
    pre[0] = s.charCodeAt(0) - zeroCharCode;
    const cur =s.charCodeAt(0);
    if (Number.isNaN(cur)) {
        console.log(i + ' ' + s.charAt(0));
        throw new Error('test');
    }
    for(let i = 1; i < n; i++) {
        const cur =s.charCodeAt(i);
        if (Number.isNaN(cur)) {
            console.log(i + ' ' + s.charAt(i));
            throw new Error('test');
        }

        pre[i] = pre[i-1] + s.charCodeAt(i) - zeroCharCode;
        if (Number.isNaN(pre[i])) {
            throw new Error(i + ' ');
        }
    }
    for(let i = 0; i<q; i++) {
        let l, r;
        [l, r] = (await getLine()).split(' ').map(num => parseInt(num));
        if (l<1 || l>n || r<1 || r>n)
            throw new Error('test');
        let res = pre[r-1];
        if (l>1)
            res -= pre[l-2];
        if (Number.isNaN(pre[r-1]) || (l !== 1 && Number.isNaN(pre[l-2]))){
            throw new Error(i + '');
        }
        console.log(res);
    }

}

/*let txtFile = fs.createWriteStream('output2.txt', {flags:'a'});
txtFile.write('100000 100000\n');
const acode = 'a'.charCodeAt(0);
for(let i = 0; i<100000; i++){
    txtFile.write(String.fromCharCode(i%20 + acode));
}
txtFile.write('\n');
for(let i = 0; i<100000; i++)
    txtFile.write('1 ' + (i + 1) + '\n');
*/
solve();

const lib = require('readline');
const fs = require('fs');
const rl = lib.createInterface({
    input: process.stdin,   // fs.createReadStream('input.txt'),
    output: process.stdout
});
//const inp = fs.createReadStream('input.txt');
const getLine = (function () {
    const getLineGen = (async function* () {
        for await (const line of rl) {
            yield line;
        }
    })();
    return async () => ((await getLineGen.next()).value);
})();

let solve = async () => {
    let n, q;
    [n, q] = (await getLine()).split(' ').map(num => parseInt(num));
    const s = await getLine();
    const pre = new Array(n);
    if (s.length !== n)
        throw new Error('test');
    const zeroCharCode = 'a'.charCodeAt(0) - 1;
    pre[0] = s.charCodeAt(0) - zeroCharCode;
    const cur =s.charCodeAt(0);
    if (Number.isNaN(cur)) {
        console.log(i + ' ' + s.charAt(0));
        throw new Error('test');
    }
    for(let i = 1; i < n; i++) {
        const cur =s.charCodeAt(i);
        if (Number.isNaN(cur)) {
            console.log(i + ' ' + s.charAt(i));
            throw new Error('test');
        }

        pre[i] = pre[i-1] + s.charCodeAt(i) - zeroCharCode;
        if (Number.isNaN(Number(pre[i]))) {
            throw new Error(i + ' ');
        }
    }
    for(let i = 0; i<q; i++) {
        let l, r;
        [l, r] = (await getLine()).split(' ').map(num => parseInt(num));
        if (l<1 || l>n || r<1 || r>n)
            throw new Error('test');
        let res = pre[r-1];
        if (l>1)
            res -= pre[l-2];
        if (Number.isNaN(pre[r-1]) || (l !== 1 && Number.isNaN(pre[l-2]))){
            throw new Error(i + '');
        }
        console.log(res);
    }

}

/*let txtFile = fs.createWriteStream('output2.txt', {flags:'a'});
txtFile.write('100000 100000\n');
const acode = 'a'.charCodeAt(0);
for(let i = 0; i<100000; i++){
    txtFile.write(String.fromCharCode(i%20 + acode));
}
txtFile.write('\n');
for(let i = 0; i<100000; i++)
    txtFile.write('1 ' + (i + 1) + '\n');
*/
solve();

const lib = require('readline');
const rl = lib.createInterface({
    input: process.stdin,
    output: process.stdout
});

const getLine = (function () {
    const getLineGen = (async function* () {
        for await (const line of rl) {
            yield line;
        }
    })();
    return async () => ((await getLineGen.next()).value);
})();

let solve = async () => {
    let n, q;
    [n, q] = (await getLine()).split(' ').map(num => parseInt(num));
    const s = await getLine();
    const pre = new Array(n);
    if (s.length !== n)
        throw new Error('test');
    const zeroCharCode = 'a'.charCodeAt(0) - 1;
    pre[0] = s.charCodeAt(0) - zeroCharCode;
    const cur =s.charCodeAt(0);
    if (Number.isNaN(cur)) {
        console.log(i + ' ' + s.charAt(0));
        throw new Error('test');
    }
    for(let i = 1; i < n; i++) {
        const cur =s.charCodeAt(i);
        if (Number.isNaN(cur)) {
            console.log(i + ' ' + s.charAt(i));
            throw new Error('test');
        }

        pre[i] = pre[i-1] + s.charCodeAt(i) - zeroCharCode;
        if (Number.isNaN(Number(pre[i]))) {
            throw new Error(i + ' ');
        }
    }
    for(let i = 0; i<q; i++) {
        let l, r;
        [l, r] = (await getLine()).split(' ').map(num => parseInt(num));
        if (l<1 || l>n || r<1 || r>n)
            throw new Error('test');
        let res = pre[r-1];
        if (l>1)
            res -= pre[l-2];
        /*if (Number.isNaN(Number(pre[r-1])) || (l !== 1 && Number.isNaN(Number(pre[l-2])))){
            throw new Error(i + '');
        }*/
        console.log(res);
    }
}

solve();

const lib = require('readline');
const rl = lib.createInterface({
    input: process.stdin,
    output: process.stdout
});

const getLine = (function () {
    const getLineGen = (async function* () {
        for await (const line of rl) {
            yield line;
        }
    })();
    return async () => ((await getLineGen.next()).value);
})();

let solve = async () => {
    let n, q;
    [n, q] = (await getLine()).split(' ').map(num => parseInt(num));
    const s = await getLine();
    const pre = new Array(n);
    if (s.length !== n)
        throw new Error('test');
    const zeroCharCode = 'a'.charCodeAt(0) - 1;
    pre[0] = s.charCodeAt(0) - zeroCharCode;
    const cur =s.charCodeAt(0);
    if (Number.isNaN(cur)) {
        console.log(i + ' ' + s.charAt(0));
        throw new Error('test');
    }
    for(let i = 1; i < n; i++) {
        const cur =s.charCodeAt(i);
        if (Number.isNaN(cur)) {
            console.log(i + ' ' + s.charAt(i));
            throw new Error('test');
        }

        pre[i] = pre[i-1] + s.charCodeAt(i) - zeroCharCode;
        if (Number.isNaN(Number(pre[i]))) {
            throw new Error(i + ' ');
        }
    }
    for(let i = 0; i<q; i++) {
        let l, r;
        [l, r] = (await getLine()).split(' ').map(num => parseInt(num));
        if (l<1 || l>n || r<1 || r>n)
            throw new Error('test');
        let res = pre[r-1];
        if (l>1)
            res += pre[l-2];
        /*if (Number.isNaN(Number(pre[r-1])) || (l !== 1 && Number.isNaN(Number(pre[l-2])))){
            throw new Error(i + '');
        }*/
        console.log(res);
    }
}

solve();

const lib = require('readline');
const rl = lib.createInterface({
    input: process.stdin,
    output: process.stdout
});

const getLine = (function () {
    const getLineGen = (async function* () {
        for await (const line of rl) {
            yield line;
        }
    })();
    return async () => ((await getLineGen.next()).value);
})();

let solve = async () => {
    let n, q;
    [n, q] = (await getLine()).split(' ').map(num => parseInt(num));
    const s = await getLine();
    const pre = new Array(n);
    if (s.length !== n)
        throw new Error('test');
    const zeroCharCode = 'a'.charCodeAt(0) - 1;
    pre[0] = s.charCodeAt(0) - zeroCharCode;
    const cur =s.charCodeAt(0);
    if (Number.isNaN(cur)) {
        console.log(i + ' ' + s.charAt(0));
        throw new Error('test');
    }
    for(let i = 1; i < n; i++) {
        const cur =s.charCodeAt(i);
        if (Number.isNaN(cur)) {
            console.log(i + ' ' + s.charAt(i));
            throw new Error('test');
        }

        pre[i] = pre[i-1] + s.charCodeAt(i) - zeroCharCode;
        if (Number.isNaN(Number(pre[i]))) {
            throw new Error(i + ' ');
        }
    }
    for(let i = 0; i<q; i++) {
        let l, r;
        [l, r] = (await getLine()).split(' ').map(num => parseInt(num));
        if (l<1 || l>n || r<1 || r>n)
            throw new Error('test');
        let res = pre[r-1];
        if (l>2)
            res += pre[l-2];
        /*if (Number.isNaN(Number(pre[r-1])) || (l !== 1 && Number.isNaN(Number(pre[l-2])))){
            throw new Error(i + '');
        }*/
        console.log(res);
    }
}

solve();

const lib = require('readline');
const rl = lib.createInterface({
    input: process.stdin,
    output: process.stdout
});

const getLine = (function () {
    const getLineGen = (async function* () {
        for await (const line of rl) {
            yield line;
        }
    })();
    return async () => ((await getLineGen.next()).value);
})();

let solve = async () => {
    let n, q;
    [n, q] = (await getLine()).split(' ').map(num => parseInt(num));
    const s = await getLine();
    const pre = new Array(n);
    if (s.length !== n)
        throw new Error('test');
    const zeroCharCode = 'a'.charCodeAt(0) - 1;
    pre[0] = s.charCodeAt(0) - zeroCharCode;
    const cur =s.charCodeAt(0);
    if (Number.isNaN(cur)) {
        console.log(i + ' ' + s.charAt(0));
        throw new Error('test');
    }
    for(let i = 1; i < n; i++) {
        const cur =s.charCodeAt(i);
        if (Number.isNaN(cur)) {
            console.log(i + ' ' + s.charAt(i));
            throw new Error('test');
        }

        pre[i] = pre[i-1] + s.charCodeAt(i) - zeroCharCode;
        if (Number.isNaN(Number(pre[i]))) {
            throw new Error(i + ' ');
        }
    }
    for(let i = 0; i<q; i++) {
        let l, r;
        [l, r] = (await getLine()).split(' ').map(num => parseInt(num));
        if (l<1 || l>n || r<1 || r>n)
            throw new Error('test');
        let res;
        if (l == 1)
            res = pre[r - 1];
        else
            res = pre[r-1] - pre[l-2];
        /*if (Number.isNaN(Number(pre[r-1])) || (l !== 1 && Number.isNaN(Number(pre[l-2])))){
            throw new Error(i + '');
        }*/
        console.log(res);
    }
}

solve();

const lib = require('readline');
const rl = lib.createInterface({
    input: process.stdin,
    output: process.stdout
});

const getLine = (function () {
    const getLineGen = (async function* () {
        for await (const line of rl) {
            yield line;
        }
    })();
    return async () => ((await getLineGen.next()).value);
})();

let solve = async () => {
    let n, q;
    [n, q] = (await getLine()).split(' ').map(num => parseInt(num));
    const s = await getLine();
    const pre = new Array(n);
    if (s.length !== n)
        throw new Error('test');
    const zeroCharCode = BigInt('a'.charCodeAt(0)) - BigInt(1);
    pre[0] = BigInt(s.charCodeAt(0)) - zeroCharCode;
    const cur =s.charCodeAt(0);
    if (Number.isNaN(cur)) {
        console.log(i + ' ' + s.charAt(0));
        throw new Error('test');
    }
    for(let i = 1; i < n; i++) {
        const cur =s.charCodeAt(i);
        if (Number.isNaN(cur)) {
            console.log(i + ' ' + s.charAt(i));
            throw new Error('test');
        }

        pre[i] = pre[i-1] + BigInt(s.charCodeAt(i)) - zeroCharCode;
        if (Number.isNaN(Number(pre[i]))) {
            throw new Error(i + ' ');
        }
    }
    for(let i = 0; i<q; i++) {
        let l, r;
        [l, r] = (await getLine()).split(' ').map(num => parseInt(num));
        if (l<1 || l>n || r<1 || r>n)
            throw new Error('test');
        let res;
        if (l == 1)
            res = pre[r - 1];
        else
            res = pre[r-1] - pre[l-2];
        /*if (Number.isNaN(Number(pre[r-1])) || (l !== 1 && Number.isNaN(Number(pre[l-2])))){
            throw new Error(i + '');
        }*/
        console.log(res.toString());
    }
}

solve();

const lib = require('readline');
const rl = lib.createInterface({
    input: process.stdin,
    output: process.stdout
});

const getLine = (function () {
    const getLineGen = (async function* () {
        for await (const line of rl) {
            yield line;
        }
    })();
    return async () => ((await getLineGen.next()).value);
})();

let solve = async () => {
    let n, q;
    [n, q] = (await getLine()).split(' ').map(num => parseInt(num));
    const s = await getLine();
    const pre = new Array(n);
    if (s.length !== n)
        throw new Error('test');
    const zeroCharCode = BigInt('a'.charCodeAt(0)) - BigInt(1);
    pre[0] = BigInt(s.charCodeAt(0)) - zeroCharCode;
    const cur =s.charCodeAt(0);
    if (Number.isNaN(cur)) {
        console.log(i + ' ' + s.charAt(0));
        throw new Error('test');
    }
    for(let i = 1; i < n; i++) {
        const cur =s.charCodeAt(i);
        if (Number.isNaN(cur)) {
            console.log(i + ' ' + s.charAt(i));
            throw new Error('test');
        }

        pre[i] = pre[i-1] + BigInt(s.charCodeAt(i)) - zeroCharCode;
    }
    for(let i = 0; i<q; i++) {
        let l, r;
        [l, r] = (await getLine()).split(' ').map(num => parseInt(num));
        if (l<1 || l>n || r<1 || r>n)
            throw new Error('test');
        let res;
        if (l == 1)
            res = pre[r - 1];
        else
            res = pre[r-1] - pre[l-2];
        if (Number.isNaN(Number(pre[r-1])) || (l !== 1 && Number.isNaN(Number(pre[l-2])))){
            throw new Error(i + '');
        }
        console.log(res.toString());
    }
}

solve();

const lib = require('readline');
const rl = lib.createInterface({
    input: process.stdin,
    output: process.stdout
});

const getLine = (function () {
    const getLineGen = (async function* () {
        for await (const line of rl) {
            yield line;
        }
    })();
    return async () => ((await getLineGen.next()).value);
})();

let solve = async () => {
    let n, q;
    [n, q] = (await getLine()).split(' ').map(num => parseInt(num));
    const s = await getLine();
    const pre = new Array(n);
    if (s.length !== n)
        throw new Error('test');
    const zeroCharCode = BigInt('a'.charCodeAt(0)) - BigInt(1);
    pre[0] = BigInt(s.charCodeAt(0)) - zeroCharCode;
    const cur =s.charCodeAt(0);
    if (Number.isNaN(cur)) {
        console.log(i + ' ' + s.charAt(0));
        throw new Error('test');
    }
    for(let i = 1; i < n; i++) {
        const cur =s.charCodeAt(i);
        if (Number.isNaN(cur)) {
            console.log(i + ' ' + s.charAt(i));
            throw new Error('test');
        }

        pre[i] = pre[i-1] + BigInt(s.charCodeAt(i)) - zeroCharCode;
    }
    for(let i = 0; i<q; i++) {
        let l, r;
        [l, r] = (await getLine()).split(' ').map(num => parseInt(num));
        if (l<1 || l>n || r<1 || r>n)
            throw new Error('test');
        let res;
        if (l == 1)
            res = pre[r - 1];
        else
            res = pre[r-1] - pre[l-2];
        if (Number.isNaN(Number(res))){
            throw new Error(i + '');
        }
        console.log(res.toString());
    }
}

solve();

const lib = require('readline');
const rl = lib.createInterface({
    input: process.stdin,
    output: process.stdout
});

const getLine = (function () {
    const getLineGen = (async function* () {
        for await (const line of rl) {
            yield line;
        }
    })();
    return async () => ((await getLineGen.next()).value);
})();

let solve = async () => {
    let n, q;
    [n, q] = (await getLine()).split(' ').map(num => parseInt(num));
    const s = await getLine();
    const pre = new Array(n);
    if (s.length !== n)
        throw new Error('test');
    const zeroCharCode = BigInt('a'.charCodeAt(0)) - BigInt(1);
    pre[0] = BigInt(s.charCodeAt(0)) - zeroCharCode;
    const cur =s.charCodeAt(0);
    if (Number.isNaN(cur)) {
        console.log(i + ' ' + s.charAt(0));
        throw new Error('test');
    }
    for(let i = 1; i < n; i++) {
        const cur =s.charCodeAt(i);
        if (Number.isNaN(cur)) {
            console.log(i + ' ' + s.charAt(i));
            throw new Error('test');
        }

        pre[i] = pre[i-1] + BigInt(s.charCodeAt(i)) - zeroCharCode;
    }
    for(let i = 0; i<q; i++) {
        let l, r;
        [l, r] = (await getLine()).split(' ').map(num => parseInt(num));
        if (l<1 || l>n || r<1 || r>n)
            throw new Error('test');
        let res;
        if (l == 1)
            res = pre[r - 1];
        else
            res = pre[r-1] - pre[l-2];
        if (Number.isNaN(res.asIntN())){
            throw new Error(i + '');
        }
        console.log(res.toString());
    }
}

solve();

const lib = require('readline');
const rl = lib.createInterface({
    input: process.stdin,
    output: process.stdout
});

const getLine = (function () {
    const getLineGen = (async function* () {
        for await (const line of rl) {
            yield line;
        }
    })();
    return async () => ((await getLineGen.next()).value);
})();

let solve = async () => {
    let n, q;
    [n, q] = (await getLine()).split(' ').map(num => parseInt(num));
    const s = await getLine();
    const pre = new Array(n);
    if (s.length !== n)
        throw new Error('test');
    const zeroCharCode = BigInt('a'.charCodeAt(0)) - BigInt(1);
    pre[0] = BigInt(s.charCodeAt(0)) - zeroCharCode;
    const cur =s.charCodeAt(0);
    if (Number.isNaN(cur)) {
        console.log(i + ' ' + s.charAt(0));
        throw new Error('test');
    }
    for(let i = 1; i < n; i++) {
        const cur =s.charCodeAt(i);
        if (Number.isNaN(cur)) {
            console.log(i + ' ' + s.charAt(i));
            throw new Error('test');
        }

        pre[i] = pre[i-1] + BigInt(s.charCodeAt(i)) - zeroCharCode;
    }
    for(let i = 0; i<q; i++) {
        let l, r;
        [l, r] = (await getLine()).split(' ').map(num => parseInt(num));
        if (l<1 || l>n || r<1 || r>n)
            throw new Error('test');
        let res;
        if (l == 1)
            res = pre[r - 1];
        else
            res = pre[r-1] - pre[l-2];
        if (Number.isNan(res.asIntN())){
            throw new Error(i + '');
        }
        console.log(res.toString());
    }
}

solve();

const lib = require('readline');
const rl = lib.createInterface({
    input: process.stdin,
    output: process.stdout
});

const getLine = (function () {
    const getLineGen = (async function* () {
        for await (const line of rl) {
            yield line;
        }
    })();
    return async () => ((await getLineGen.next()).value);
})();

let solve = async () => {
    let n, q;
    [n, q] = (await getLine()).split(' ').map(num => parseInt(num));
    const s = await getLine();
    const pre = new Array(n);
    if (s.length !== n)
        throw new Error('test');
    const zeroCharCode = BigInt('a'.charCodeAt(0)) - BigInt(1);
    pre[0] = BigInt(s.charCodeAt(0)) - zeroCharCode;
    const cur =s.charCodeAt(0);
    if (Number.isNaN(cur)) {
        console.log(i + ' ' + s.charAt(0));
        throw new Error('test');
    }
    for(let i = 1; i < n; i++) {
        const cur =s.charCodeAt(i);
        if (Number.isNaN(cur)) {
            console.log(i + ' ' + s.charAt(i));
            throw new Error('test');
        }

        pre[i] = pre[i-1] + BigInt(s.charCodeAt(i)) - zeroCharCode;
    }
    for(let i = 0; i<q; i++) {
        let l, r;
        [l, r] = (await getLine()).split(' ').map(num => parseInt(num));
        if (l<1 || l>n || r<1 || r>n)
            throw new Error('test');
        let res;
        if (l == 1)
            res = pre[r - 1];
        else
            res = pre[r-1] - pre[l-2];
        console.log(res.toString());
    }
}

solve();

const lib = require('readline');
const rl = lib.createInterface({
    input: process.stdin,
    output: process.stdout
});

const getLine = (function () {
    const getLineGen = (async function* () {
        for await (const line of rl) {
            yield line;
        }
    })();
    return async () => ((await getLineGen.next()).value);
})();

let solve = async () => {
    let n, q;
    [n, q] = (await getLine()).split(' ').map(num => parseInt(num));
    const s = await getLine();
    const pre = new Array(n);
    if (s.length !== n)
        throw new Error('test');
    const zeroCharCode = BigInt('a'.charCodeAt(0)) - BigInt(1);
    pre[0] = BigInt(s.charCodeAt(0)) - zeroCharCode;
    for(let i = 1; i < n; i++) {
        const cur =s.charCodeAt(i);
        if (Number.isNaN(cur)) {
            console.log(i + ' ' + s.charAt(i));
            throw new Error('test');
        }

        pre[i] = pre[i-1] + BigInt(s.charCodeAt(i)) - zeroCharCode;
    }
    for(let i = 0; i<q; i++) {
        let l, r;
        [l, r] = (await getLine()).split(' ').map(num => parseInt(num));
        if (l<1 || l>n || r<1 || r>n)
            throw new Error('test');
        let res;
        if (l == 1)
            res = pre[r - 1];
        else
            res = pre[r-1] - pre[l-2];
        console.log(res.toString());
    }
}

solve();

const lib = require('readline');
const rl = lib.createInterface({
    input: process.stdin,
    output: process.stdout
});

const getLine = (function () {
    const getLineGen = (async function* () {
        for await (const line of rl) {
            yield line;
        }
    })();
    return async () => ((await getLineGen.next()).value);
})();

let solve = async () => {
    let n, q;
    [n, q] = (await getLine()).split(' ').map(num => parseInt(num));
    const s = await getLine();
    const pre = new Array(n);
    if (s.length !== n)
        throw new Error('test');
    const zeroCharCode = BigInt('a'.charCodeAt(0)) - BigInt(1);
    pre[0] = BigInt(s.charCodeAt(0)) - zeroCharCode;
    for(let i = 1; i < n; i++) {
        pre[i] = pre[i-1] + BigInt(s.charCodeAt(i)) - zeroCharCode;
    }
    for(let i = 0; i<q; i++) {
        let l, r;
        [l, r] = (await getLine()).split(' ').map(num => parseInt(num));
        if (l<1 || l>n || r<1 || r>n)
            throw new Error('test');
        let res;
        if (l == 1)
            res = pre[r - 1];
        else
            res = pre[r-1] - pre[l-2];
        console.log(res.toString());
    }
}

solve();

const lib = require('readline');
const rl = lib.createInterface({
    input: process.stdin,
    output: process.stdout
});

const getLine = (function () {
    const getLineGen = (async function* () {
        for await (const line of rl) {
            yield line;
        }
    })();
    return async () => ((await getLineGen.next()).value);
})();

let solve = async () => {
    let n, q;
    [n, q] = (await getLine()).split(' ').map(num => parseInt(num));
    const s = await getLine();
    const pre = new Array(n);
    const zeroCharCode = BigInt('a'.charCodeAt(0)) - BigInt(1);
    pre[0] = BigInt(s.charCodeAt(0)) - zeroCharCode;
    for(let i = 1; i < n; i++) {
        pre[i] = pre[i-1] + BigInt(s.charCodeAt(i)) - zeroCharCode;
    }
    for(let i = 0; i<q; i++) {
        let l, r;
        [l, r] = (await getLine()).split(' ').map(num => parseInt(num));
        if (l<1 || l>n || r<1 || r>n)
            throw new Error('test');
        let res;
        if (l == 1)
            res = pre[r - 1];
        else
            res = pre[r-1] - pre[l-2];
        console.log(res.toString());
    }
}

solve();

const lib = require('readline');
const rl = lib.createInterface({
    input: process.stdin,
    output: process.stdout
});

const getLine = (function () {
    const getLineGen = (async function* () {
        for await (const line of rl) {
            yield line;
        }
    })();
    return async () => ((await getLineGen.next()).value);
})();

let solve = async () => {
    let n, q;
    [n, q] = (await getLine()).split(' ').map(num => parseInt(num));
    const s = await getLine();
    const pre = new Array(n);
    const zeroCharCode = BigInt('a'.charCodeAt(0)) - BigInt(1);
    pre[0] = BigInt(s.charCodeAt(0)) - zeroCharCode;
    for(let i = 1; i < n; i++) {
        pre[i] = pre[i-1] + BigInt(s.charCodeAt(i)) - zeroCharCode;
    }
    for(let i = 0; i<q; i++) {
        let l, r;
        [l, r] = (await getLine()).split(' ').map(num => parseInt(num));
        let res;
        if (l == 1)
            res = pre[r - 1];
        else
            res = pre[r-1] - pre[l-2];
        console.log(res.toString());
    }
}

solve();

const lib = require('readline');
const rl = lib.createInterface({
    input: process.stdin,
    output: process.stdout
});

const getLine = (function () {
    const getLineGen = (async function* () {
        for await (const line of rl) {
            yield line;
        }
    })();
    return async () => ((await getLineGen.next()).value);
})();

let solve = async () => {
    let n, q;
    [n, q] = (await getLine()).split(' ').map(num => parseInt(num));
    const s = await getLine();
    const pre = new Array(s.length);
    const zeroCharCode = BigInt('a'.charCodeAt(0)) - BigInt(1);
    pre[0] = BigInt(s.charCodeAt(0)) - zeroCharCode;
    for(let i = 1; i < n; i++) {
        pre[i] = pre[i-1] + BigInt(s.charCodeAt(i)) - zeroCharCode;
    }
    for(let i = 0; i<q; i++) {
        let l, r;
        [l, r] = (await getLine()).split(' ').map(num => parseInt(num));
        let res;
        if (l == 1)
            res = pre[r - 1];
        else
            res = pre[r-1] - pre[l-2];
        console.log(res.toString());
    }
}

solve();

const lib = require('readline');
const rl = lib.createInterface({
    input: process.stdin,
    output: process.stdout
});

const getLine = (function () {
    const getLineGen = (async function* () {
        for await (const line of rl) {
            yield line;
        }
    })();
    return async () => ((await getLineGen.next()).value);
})();

let solve = async () => {
    let n, q;
    [n, q] = (await getLine()).split(' ').map(num => parseInt(num));
    const s = await getLine();
    const pre = new Array(s.length);
    const zeroCharCode = BigInt('a'.charCodeAt(0)) - BigInt(1);
    pre[0] = BigInt(s.charCodeAt(0)) - zeroCharCode;
    for(let i = 1; i < s.length; i++) {
        pre[i] = pre[i-1] + BigInt(s.charCodeAt(i)) - zeroCharCode;
    }
    for(let i = 0; i<q; i++) {
        let l, r;
        [l, r] = (await getLine()).split(' ').map(num => parseInt(num));
        let res;
        if (l == 1)
            res = pre[r - 1];
        else
            res = pre[r-1] - pre[l-2];
        console.log(res.toString());
    }
}

solve();

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}
// Make a Snippet for the code above this and then write your logic in main();

function main() {

 	let testCaseNum = readline();
	testCaseNum = testCaseNum.split(' ').filter(item => +item);
	testCaseNum = testCaseNum[1];

 	let str = readline();

 	while (testCaseNum) {
 		let tmp = readline();
 		tmp = tmp.split(' ').filter(item => +item);

 		let ret = '';
		for (let i = tmp[0] - 1; i <= tmp[1] - 1; i++) {
			if (str[i] == 'a') {
				ret += 'a';
			} else if (str[i] == 'b') {
				ret += 'bb';
			} else if (str[i] == 'c'){
				ret += 'ccc';
			}
		}
		console.log(ret.length);
 		testCaseNum --;
 	}
}


A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, sequence [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] are not.A fixed point of a function is a point that is mapped to itself by the function. A permutation can be regarded as a bijective function. We'll get a definition of a fixed point in a permutation. An integer i is a fixed point of permutation a0, a1, ..., an - 1 if and only if ai = i. For example, permutation [0, 2, 1] has 1 fixed point and permutation [0, 1, 2] has 3 fixed points.You are given permutation a. You are allowed to swap two elements of the permutation at most once. Your task is to maximize the number of fixed points in the resulting permutation. Note that you are allowed to make at most one swap operation.

The first line contains a single integer n (1 ≤ n ≤ 105). The second line contains n integers a0, a1, ..., an - 1 — the given permutation.

Print a single integer — the maximum possible number of fixed points in the permutation after at most one swap operation.

var n = +readline();

var a = readline().split( ' ' ).map( Number );

var ans = 0;
var f = 1;

for( var i = 0; i < n; i++ )
	if( a[i] === i ) ans++;
	else if( a[ a[ i ] ] === i ) f = 2;

print( Math.min( n, ans + f ) );

;(function () {

	var n = +readline();
	var t = readline().split(' ').map(Number);
	var a = t.map(function (e, i) { return [i, e]; }).filter(function (e) { return e[0] != e[1]; });;
	var l = a.length;

	if (a.length < 2) {
		print(n - a.length);
		return;
	}

	for (var i = 0; i < l - 1; i++)
		if (a[i][0] == t[a[i][1]] && a[i][1] == t[a[i][0]]) {
			print(n - a.length + 2);
			return;
		}

	print(n - a.length + 1);

}).call(this);

(function (){
    var i, n = parseInt(readline());
    var a = readline().split(' ');
    var fixed = 0, swap = 0;
    for ( i = 0 ; i < n ; ++i){
        if ( i == a[i] ){
            ++fixed;
        }else if ( i == a[a[i]] ){
            ++swap;
        }
    }
    if ( swap > 0 ){
        fixed += 2;
    }else if ( fixed != n ){
        fixed += 1;
    }
    print(fixed);
})();

;(function () {

	var n = +readline();
	var a = readline().split(' ').map(function (e, i) { return [i, e]; }).filter(function (e) { return e[0] != e[1]; });;
	var l = a.length;

	if (a.length < 2) {
		print(n - a.length);
		return;
	}

	for (var i = 0; i < l - 1; i++)
		for (var j = i + 1; j < l; j++) {
			if (a[j][0] === a[i][1] && a[j][1] === a[i][0]) {
				print(n - a.length + 2);
				return;
			}
		}

	print(n - a.length + 1);

}).call(this);


Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees.There are $$$n$$$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $$$a_1, a_2, \dots, a_n$$$. Leon has learned three abilities which will help him to dry and water the soil.  Choose a position $$$i$$$ and decrease the level of moisture of the trees $$$1, 2, \dots, i$$$ by $$$1$$$.  Choose a position $$$i$$$ and decrease the level of moisture of the trees $$$i, i + 1, \dots, n$$$ by $$$1$$$.  Increase the level of moisture of all trees by $$$1$$$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $$$0$$$.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$)  — the number of test cases. The description of $$$t$$$ test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 200\,000$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the initial levels of trees moisture.  It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$200\,000$$$.

For each test case output a single integer — the minimum number of actions. It can be shown that the answer exists.

NoteIn the first test case it's enough to apply the operation of adding $$$1$$$ to the whole array $$$2$$$ times. In the second test case you can apply the operation of decreasing $$$4$$$ times on the prefix of length $$$3$$$ and get an array $$$6, 0, 3$$$. After that apply the operation of decreasing $$$6$$$ times on the prefix of length $$$1$$$ and $$$3$$$ times on the suffix of length $$$1$$$. In total, the number of actions will be $$$4 + 6 + 3 = 13$$$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $$$13$$$.

'use strict';

function solve(n, a) {
  let res = 0;
  const segTree = new SegTree(a);
  for (let r = n - 2; r >= 0; r--) {
    let cur = segTree.query(r, r);
    if (cur > a[r + 1]) {
      let dif = cur - a[r + 1];
      res += dif;
      segTree.update(0, r, -dif);
      cur = a[r + 1];
    }
    a[r] = cur;
  }
  const x = segTree.query(0, 0),
        y = segTree.query(n - 1, n - 1);
  if (x >= 0) res += y;else res += -x + y - x;
  return res;
}
class SegTree {
  constructor(nums = [], n = nums.length - 1) {
    this.nums = nums;
    this.n = n;
    this.root = this._newNode();
    this._build(this.root, 0, n);
    this.update = (x, y, z) => {
      x = Math.max(x, 0);
      y = Math.min(y, n);
      return this._update(this.root, 0, n, x, y, z);
    };
    this.query = (x, y) => {
      x = Math.max(x, 0);
      y = Math.min(y, n);
      return this._query(this.root, 0, n, x, y);
    };
  }
  _newNode(val = 0, left = null, right = null) {
    return {
      val,
      left,
      right,
      add: 0
    };
  }
  _down(node, l, r) {
    const {
      left,
      right
    } = node;
    if (!left || !right) return;
    const mid = Math.floor((l + r) / 2);
    left.add += node.add;
    left.val += node.add * (mid - l + 1);
    right.add += node.add;
    right.val += node.add * (r - mid);
    node.add = 0;
  }
  _up(node) {
    const {
      left,
      right
    } = node;
    if (!left || !right) return;
    node.val = left.val + right.val;
  }
  _build(node = this.root, l, r) {
    if (l === r) {
      var _this$nums$l;
      node.val = (_this$nums$l = this.nums[l]) !== null && _this$nums$l !== void 0 ? _this$nums$l : 0;
      return;
    }
    const mid = Math.floor((l + r) / 2);
    node.left = this._newNode();
    node.right = this._newNode();
    this._build(node.left, l, mid);
    this._build(node.right, mid + 1, r);
    node.val = node.left.val + node.right.val;
  }
  _update(node, l, r, x, y, z) {
    if (!node) return;
    if (l === x && r === y) {
      node.add += z;
      node.val += z * (r - l + 1);
      return;
    }
    this._down(node, l, r);
    const mid = Math.floor((l + r) / 2);
    if (y <= mid) this._update(node.left, l, mid, x, y, z);else if (x > mid) this._update(node.right, mid + 1, r, x, y, z);else this._update(node.left, l, mid, x, mid, z), this._update(node.right, mid + 1, r, mid + 1, y, z);
    this._up(node);
  }
  _query(node, l, r, x, y) {
    if (y < x) return 0;
    if (!node) return 0;
    if (l === x && r === y) return node.val;
    this._down(node, l, r);
    let res = 0,
        mid = Math.floor((l + r) / 2);
    if (y <= mid) res = this._query(node.left, l, mid, x, y);else if (x > mid) res = this._query(node.right, mid + 1, r, x, y);else res = this._query(node.left, l, mid, x, mid) + this._query(node.right, mid + 1, r, mid + 1, y);
    this._up(node);
    return res;
  }
}

async function main(read) {
  try {
    let t = Number(await read());
    let res = new Array(t);
    for (let i = 0; i < t; i++) {
      const n = Number(await read());
      const a = (await read()).split(' ').map(Number);
      res[i] = solve(n, a);
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  process.stdout.write(output);
});

'use strict';

function solve(n, a) {
  let res = 0;
  const segTree = new SegTree(a);
  for (let r = n - 2; r >= 0; r--) {
    let cur = segTree.query(r, r);
    if (cur > a[r + 1]) {
      let dif = cur - a[r + 1];
      res += dif;
      segTree.update(0, r, -dif);
      cur = a[r + 1];
    }
    a[r] = cur;
  }
  const x = segTree.query(0, 0),
        y = segTree.query(n - 1, n - 1);
  res += -x + y - x;
  return res;
}
class SegTree {
  constructor(nums = [], n = nums.length - 1) {
    this.nums = nums;
    this.n = n;
    this.root = this._newNode();
    this._build(this.root, 0, n);
    this.update = (x, y, z) => {
      x = Math.max(x, 0);
      y = Math.min(y, n);
      return this._update(this.root, 0, n, x, y, z);
    };
    this.query = (x, y) => {
      x = Math.max(x, 0);
      y = Math.min(y, n);
      return this._query(this.root, 0, n, x, y);
    };
  }
  _newNode(val = 0, left = null, right = null) {
    return {
      val,
      left,
      right,
      add: 0
    };
  }
  _down(node, l, r) {
    const {
      left,
      right
    } = node;
    if (!left || !right) return;
    const mid = Math.floor((l + r) / 2);
    left.add += node.add;
    left.val += node.add * (mid - l + 1);
    right.add += node.add;
    right.val += node.add * (r - mid);
    node.add = 0;
  }
  _up(node) {
    const {
      left,
      right
    } = node;
    if (!left || !right) return;
    node.val = left.val + right.val;
  }
  _build(node = this.root, l, r) {
    if (l === r) {
      var _this$nums$l;
      node.val = (_this$nums$l = this.nums[l]) !== null && _this$nums$l !== void 0 ? _this$nums$l : 0;
      return;
    }
    const mid = Math.floor((l + r) / 2);
    node.left = this._newNode();
    node.right = this._newNode();
    this._build(node.left, l, mid);
    this._build(node.right, mid + 1, r);
    node.val = node.left.val + node.right.val;
  }
  _update(node, l, r, x, y, z) {
    if (!node) return;
    if (l === x && r === y) {
      node.add += z;
      node.val += z * (r - l + 1);
      return;
    }
    this._down(node, l, r);
    const mid = Math.floor((l + r) / 2);
    if (y <= mid) this._update(node.left, l, mid, x, y, z);else if (x > mid) this._update(node.right, mid + 1, r, x, y, z);else this._update(node.left, l, mid, x, mid, z), this._update(node.right, mid + 1, r, mid + 1, y, z);
    this._up(node);
  }
  _query(node, l, r, x, y) {
    if (y < x) return 0;
    if (!node) return 0;
    if (l === x && r === y) return node.val;
    this._down(node, l, r);
    let res = 0,
        mid = Math.floor((l + r) / 2);
    if (y <= mid) res = this._query(node.left, l, mid, x, y);else if (x > mid) res = this._query(node.right, mid + 1, r, x, y);else res = this._query(node.left, l, mid, x, mid) + this._query(node.right, mid + 1, r, mid + 1, y);
    this._up(node);
    return res;
  }
}

async function main(read) {
  try {
    let t = Number(await read());
    let res = new Array(t);
    for (let i = 0; i < t; i++) {
      const n = Number(await read());
      const a = (await read()).split(' ').map(Number);
      res[i] = solve(n, a);
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  process.stdout.write(output);
});

'use strict';

function solve(n, a) {
  const min = Math.min(...a);
  let res = 0,
      lz = -1,
      rz = -1;
  if (min < 0) {
    res += Math.abs(min);
    a = a.map(num => num + Math.abs(min));
  } else if (min > 0) {
    res += min;
    a = a.map(num => num - min);
  }
  for (let [i, num] of a.entries()) {
    if (num === 0) {
      if (lz === -1) lz = i;
      rz = i;
    }
  }
  const segTree = new SegTree(a);
  for (let i = 0; i < n; i++) {
    let [min, mi] = segTree.min(0, n - 1);
    if (min === 0) break;
    if (mi < lz) {
      res += min;
      segTree.update(0, lz - 1, -min);
    } else if (mi > rz) {
      res += min;
      segTree.update(rz + 1, n - 1, -min);
    } else {
      res += min * 3;
      segTree.update(0, n - 1, min);
      segTree.update(0, mi, -min);
      segTree.update(mi, n - 1, -min);
    }
    lz = Math.min(lz, mi);
    rz = Math.max(rz, mi);
  }
  return res;
}
class SegTree {
  constructor(nums = [], n = nums.length - 1) {
    this.nums = nums;
    this.n = n;
    this.root = this._newNode();
    this._build(this.root, 0, n);
    this.update = (x, y, z) => {
      x = Math.max(x, 0);
      y = Math.min(y, n);
      this._update(this.root, 0, n, x, y, z);
    };
    this.min = (x, y) => {
      x = Math.max(x, 0);
      y = Math.min(y, n);
      return this._queryMin(this.root, 0, n, x, y);
    };
  }
  _newNode(val = 0, left = null, right = null) {
    return {
      val,
      left,
      right,
      add: 0,
      mi: -1
    };
  }
  _build(node = this.root, l, r) {
    if (l === r) {
      var _this$nums$l;
      node.val = (_this$nums$l = this.nums[l]) !== null && _this$nums$l !== void 0 ? _this$nums$l : 0;
      node.mi = l;
      return;
    }
    const mid = Math.floor((l + r) / 2);
    node.left = this._newNode();
    node.right = this._newNode();
    this._build(node.left, l, mid);
    this._build(node.right, mid + 1, r);
    this._up(node);
  }
  _down(node) {
    const {
      left,
      right
    } = node;
    if (!left || !right) return;
    if (node.add) {
      left.add += node.add;
      left.val += node.add;
      right.add += node.add;
      right.val += node.add;
      node.add = 0;
    }
    if (left.add && left.val === 0) {
      this._down(left);
      this._up(left);
    }
    if (right.add && right.val === 0) {
      this._down(right);
      this._up(right);
    }
  }
  _up(node) {
    const {
      left,
      right
    } = node;
    if (!left || !right) return;
    if (left.val === 0) {
      node.val = right.val;
      node.mi = right.mi;
    } else if (right.val === 0) {
      node.val = left.val;
      node.mi = left.mi;
    } else {
      if (left.val <= right.val) {
        node.val = left.val;
        node.mi = left.mi;
      } else {
        node.val = right.val;
        node.mi = right.mi;
      }
    }
  }
  _update(node, l, r, x, y, z) {
    if (!node) return;
    if (l === x && r === y) {
      node.add += z;
      node.val += z;
      this._down(node);
      this._up(node);
      return;
    }
    const mid = Math.floor((l + r) / 2);
    if (!node.left) {
      node.left = this._newNode();
      node.right = this._newNode();
    }
    this._down(node);
    if (y <= mid) this._update(node.left, l, mid, x, y, z);else if (x > mid) this._update(node.right, mid + 1, r, x, y, z);else this._update(node.left, l, mid, x, mid, z), this._update(node.right, mid + 1, r, mid + 1, y, z);
    this._up(node);
  }
  _queryMin(node, l, r, x, y) {
    if (y < x) return [0, -1];
    if (!node) return [0, -1];
    if (l === x && r === y) return [node.val, node.mi];
    let res = [0, -1],
        mid = Math.floor((l + r) / 2);
    if (!node.left) {
      node.left = this._newNode();
      node.right = this._newNode();
    }
    this._down(node);
    if (y <= mid) res = this._queryMin(node.left, l, mid, x, y);else if (x > mid) res = this._queryMin(node.right, mid + 1, r, x, y);else {
      const [v1, m1] = this._queryMin(node.left, l, mid, x, mid),
            [v2, m2] = this._queryMin(node.right, mid + 1, r, mid + 1, y);
      if (v1 === 0) {
        res = [v2, m2];
      } else if (v2 === 0) {
        res = [v1, m1];
      } else {
        if (v1 <= v2) {
          res = [v1, m1];
        } else {
          res = [v2, m2];
        }
      }
    }
    this._up(node);
    return res;
  }
}

async function main(read) {
  try {
    let t = Number(await read());
    let res = new Array(t);
    for (let i = 0; i < t; i++) {
      const n = Number(await read());
      const a = (await read()).split(' ').map(Number);
      res[i] = solve(n, a);
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  process.stdout.write(output);
});


Let's call a number a binary decimal if it's a positive integer and all digits in its decimal notation are either $$$0$$$ or $$$1$$$. For example, $$$1\,010\,111$$$ is a binary decimal, while $$$10\,201$$$ and $$$787\,788$$$ are not.Given a number $$$n$$$, you are asked to represent $$$n$$$ as a sum of some (not necessarily distinct) binary decimals. Compute the smallest number of binary decimals required for that.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$), denoting the number of test cases. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^9$$$), denoting the number to be represented.

For each test case, output the smallest number of binary decimals required to represent $$$n$$$ as a sum.

NoteIn the first test case, $$$121$$$ can be represented as $$$121 = 110 + 11$$$ or $$$121 = 111 + 10$$$.In the second test case, $$$5$$$ can be represented as $$$5 = 1 + 1 + 1 + 1 + 1$$$.In the third test case, $$$1\,000\,000\,000$$$ is a binary decimal itself, thus the answer is $$$1$$$.

"use strict";
process.stdin.resume();
process.stdin.setEncoding("utf-8");

function print(x) {
  console.log(x);
}
let inputString = "";
let currentLine = 0;

process.stdin.on("data", (inputStdin) => {
  inputString += inputStdin;
});
process.stdin.on("end", () => {
  inputString = inputString.split("\n");
  main();
});
function readline() {
  return inputString[currentLine++];
}

// ********** Code Start **********

function main() {

  let t = parseInt(readline());

    while(t--){

        var n;
        var count = 0;
        n = parseInt(readline());

        while(n){
          var num = n.toString();
          var num1 = "";

          for(var c in num){
            if(num[c]!='0') num1+='1';
            else num1+='0';
          }

          count++;
          n = n - parseInt(num1);
        }
        console.log(count);
    }

}

let q = '';
process.stdin.on('data', c => q += c);
process.stdin.on('end', () => {
    const {EOL} = require('os');
    const lines = q.split(EOL);
    var t = lines[0];
    for(i = 1; i <= t; i++){
        var n = lines[i];
        var x = 0;
        while(n > 0){
            var z = n % 10;
            x = Math.max(x, z);
            n /= 10;
        }
        console.log(Math.floor(x));
    }
});

let i = '';
process.stdin.on('data', c => i += c);
process.stdin.on('end', () => {
    const {EOL} = require('os');
    const lines = i.split(EOL);
    var n = lines[0];
    for(j = 1; j <= n; j++){
        var max = 0;
        var k = lines[j];
        var m = 0;
        var a = 0;
        while(k > 0){
            m = k % 10;
            a = Math.max(a, m);
            k /= 10;
        }
        console.log(Math.floor(a));
    }
});

var fs = require('fs'), currentline = 0, input = fs.readFileSync(0, 'utf8').trim().split('\n');
function readline() { return input[currentline++]; }
var T = +readline();
for (var j = 0; j < T; j++) {
    var n = readline(), ans = 0;
    for (var i = 0; i < n.length; i++)
        ans = Math.max(ans, +n[i]);
    console.log(ans);
}

//Don't have to see. start------------------------------------------
var read = require('readline').createInterface({
	input: process.stdin, output: process.stdout
});
var obj; var inLine = []; var outputList = [];var retcode = new Set();
read.on('line', function(input){
	var tmp = input.split(' ');
	for(var i = 0; i < tmp.length; i++){
		inLine.push(tmp[i]);
		if(i == tmp.length - 1){
			retcode.add(inLine.length);
		}
	}
});
read.on('close', function(){
	obj = init(inLine);
	console.error('\n↑入力 ↓出力');
	Main();
	console.log(myconv(outputList, 9));
});
function makeClone(obj){return (obj instanceof Set) ? new Set(Array.from(obj)) : JSON.parse(JSON.stringify(obj));}
function nextArray(size, code){
	var ret = new Array(size);
	for(var i = 0; i < size; i++){
		if(code == 'int'){
			ret[i] = nextInt();
		}else if(code == 'long'){
			ret[i] = BigInt(next());
		}else{
			ret[i] = next();
		}
	}
	return ret;
}
function nextIntArray(size){return nextArray(size, 'int');} function nextStrArray(size){return nextArray(size, 'str');} function nextLongArray(size){return nextArray(size, 'long');}
function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();} function nextInt(){return myconv(next(),1);} function nextLong(){return BigInt(next());}
function getCountMap(list){
	var map = {};
	for(var i = 0; i < list.length; i++){
		if(map[list[i]] == null){
			map[list[i]] = 0;
		}
		map[list[i]]++;
	}
	return map;
}
function init(input){
	return {
		list : input, index : 0, max : input.length,
		hasNext : function(){return (this.index < this.max);},
		next : function(){if(this.hasNext()){return this.list[this.index++];}else{throw 'ArrayIndexOutOfBoundsException ‚There is no more input';}},
		isReturn : function(){return retcode.has(this.index);}
	};
}
function myout(s){outputList.push(s);}
function myerr(s){console.error('debug:' + require('util').inspect(s,false,null));}
function isReturn(){return obj.isReturn();}
//param "no" is
//unknown or outlier : return i. 1: parseInt.
//2: split space. 4: split space and parseInt.
//6: split 1 character. 7: split 1 character and parseInt.
//8: join space. 9: join nextline. 0: join no character.
function myconv(i,no){try{switch(no){case 1:return parseInt(i);case 2:return i.split(' ');case 4:return i.split(' ').map(Number);case 6:return i.split('');case 7:return i.split('').map(Number);case 8:return i.join(' ');case 9:return i.join('\n');case 0:return i.join('');default:return i;}}catch(e){return i;}}

//Don't have to see. end------------------------------------------
function Main(){
	var t = nextInt();
	while(hasNext()){
		var N = nextInt();
		var max = 0;
		while(N > 0){
			max = Math.max(max, N % 10);
			N = Math.floor(N / 10);
		}
		myout(max);
	}
}

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}

function main() {
    const n = Number(readline());

    for(let i = 0; i < n; i++) {
        const decimal = readline().split('').map(Number);

        decimal.sort((a,b) => b-a);

        console.log(decimal[0]);
    }
}

let i = ''
process.stdin.on('data', c => i += c)
process.stdin.on('end', () => {
    const {EOL} = require('os')
    const lines = i.split(EOL) /*your input text, split by lines*/
    const size = lines[0];
    for(let j = 1; j <= size; j++) {
        let max = 0;
        let num = lines[j];
        while (num !== 0) {
            digit = num % 10;
            if (digit > max) {
                max = digit;
            }
            num = Math.floor(num / 10);
        }
        console.log(max);
    }
})

let input = '';
process.stdin.on('data', chunk => input += chunk);
process.stdin.on('end', () => {
  const {EOL} = require('os');
  const lines = input.split(EOL);
  const count = parseInt(lines[0]);
  for (let i = 1; i <= count; ++i) {
    const solution = solve(lines[i]);
    console.log(solution);
  }
});

function solve(question) {
  let max = 0;
  for (const digit of question) {
    if (digit.match(/^\d$/)) {
      const num = parseInt(digit);
      if (num > max) {
        max = num;
      }
    }
  }
  return max;
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}
/**
3
1
2
3

**/

function gcd(a, b) {
    if (!b) {
        return a;
    }

    return gcd(b, a % b);
};

function lcm(a, b) {
    return (a / gcd(a, b) * b);
}

function main() {
    let n = Number(readline());

    for(let r=0;r<n;r++) {
        solve();
    }

}

function solve() {
    let n = readline().split("");

    let max = 1;

    for(let i=0;i<n.length;i++) {
        max = Math.max(max, parseInt(n[i]));
    }

    console.log(max);

}