'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() {
    const x = readline();

    // 1 1 7
    // 6 9, 15, 18
    Array(Number(x)).fill(1).map((t, iii) => {
        var n = parseInt(readline())
        // var [n, k] = readline().split(' ').map((x, iii) => {
        //     return parseInt(x)
        // })
        // var s = new Array(n + 1).fill(0)

        //
        // var count = {}
        // var a = readline().split(' ').map((x, iii) => {
        //     return parseInt(x)
        // })
        var res = []
        for (let i = 0; i < n; i++) {
            for (let j = i + 1; j < n; j++) {
                var dif=(j-i)*2
                var add=dif === n ? 0: dif < n ? 1 : -1
                res.push(add)
            }
        }
        return console.log(res.join(' '))
        var map2 = {}
        Object.keys(count).map(key => {
            if (!map2[count[key]]) map2[count[key]] = 0
            map2[count[key]]++

        })
        var keys = Object.keys(map2).map(x => parseInt(x)).sort((a, b) => map2[a] - map2[b])

        var sum = new Array(keys.length + 1).fill(0)

        var ans = n
        for (let i = 0; i < keys.length; i++) {
            sum = 0
            for (let j = 0; j < keys.length; j++) {
                if (keys[j] < keys[i]) sum += map2[keys[j]] * keys[j]
                if (keys[j] > keys[i]) sum += (keys[j] - keys[i]) * map2[keys[j]]
            }
            ans = Math.min(ans, sum)
        }
        // console.log(keys)
        // console.log(map2)
        // console.log(sum)
        console.log(ans)

    })
}

var dataitr;
var stdin_input = "";
process.stdin.resume();
process.stdin.setEncoding("ascii");
process.stdin.on("data", function (input) { stdin_input += input; });
process.stdin.on("end", function () { dataitr = stdin_input.split('\n').values(); main(); });

const rl = () => dataitr.next().value.trim();
const rn = () => Number(rl());
const ra = () => rl().split(' ');
const rna = () => ra().map(x => Number(x));

function main () {
	let T = rn();
	while (T--) {
		const n = rn();

		const ans = [];
		for (let i = 0; i < n; i++) {
			for (let j = i + 1; j < n; j++)
				if (j-i <= Math.floor((n-1)/2)) ans.push(1);
				else if (n%2 == 0 && j-i == n/2) ans.push(0);
				else ans.push(-1);
		}

		console.log(ans.join(' '));
	}
}

'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() {
    var x = Number(readline())
    Array(Number(x)).fill(1).map((t, i) => {
        var n = parseInt(readline())
        if( n===2) return console.log(0)
        if (n % 2 === 1) {
            var res = []
            for (var i = 0; i < (n * (n - 1)) / 2; i++) {
                if (i % 2 === 0) res.push(1)
                else res.push(-1)
            }
            return console.log(res.join(' '))
        }
        var res = [1]
        for (var i = n-1; i >= 2; i--) {
            res.push(-1)
            for (var j = 0; j < i-1; j++) {
                res.push(0)
            }
        }
        console.log(res.join(' '))

        // for (var i = 3; i*i <= n*4; i=i+2) {
        //     var b = Math.floor((i*i-1)/2)
// console.log(i*i, b,b+1)
//         if(b*2+1===i*i && b*2+1 <= n && b>0) {
//             console.log(i,b,b+1 )
//             count++
//         }
//         }
        // var mid = Math.fl/oor(n/2)
        // var res = Math.floor((k-1) / mid)

    })

}

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

    return gcd(b, a % b);
}

'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() {
    var x = Number(readline())
    Array(Number(x)).fill(1).map((t, i) => {
        var n = parseInt(readline())
        if (n % 2 === 1) {
            var res = []
            for (var i = 0; i < (n * (n - 1)) / 2; i++) {
                if (i % 2 === 0) res.push(1)
                else res.push(-1)
            }
            console.log(res.join(' '))
        }
        var res = [1]
        for (var i = n-1; i >= 2; i--) {
            res.push(-1)
            for (var j = 0; j < i-1; j++) {
                res.push(0)
            }
        }
        console.log(res.join(' '))

        // for (var i = 3; i*i <= n*4; i=i+2) {
        //     var b = Math.floor((i*i-1)/2)
// console.log(i*i, b,b+1)
//         if(b*2+1===i*i && b*2+1 <= n && b>0) {
//             console.log(i,b,b+1 )
//             count++
//         }
//         }
        // var mid = Math.fl/oor(n/2)
        // var res = Math.floor((k-1) / mid)

    })

}

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

    return gcd(b, a % b);
}


"use strict";
var input = '';
var inputResolve;
var inputPromise = new Promise(resolve => inputResolve = resolve);
process.stdin.on('data', c => input += c);
process.stdin.on('end', () => {
    inputResolve();
});
function gcd(a, b) {
    if (b == 0)
        return a;
    if (a < b)
        return gcd(b, a);
    return gcd(b, a % b);
}
(async () => {
    // var time = Date.now();
    await inputPromise;
    var inputs = input.split(/\r?\n/);
    var [n, vb, vs] = inputs[0].split(' ').map(v => +v);
    var arr = inputs[1].split(' ').map(v => +v);
    var [x, y] = inputs[2].split(' ').map(v => +v);
    var min = 1e10;
    var resDis = 0;
    var resId = 0;
    for (let i = 1; i < n; i++) {
        let dis = Math.sqrt((arr[i] - x) ** 2 + y * y);
        let t = arr[i] / vb + dis / vs;
        if (t < min || (t == min && dis < resDis)) {
            min = t;
            resId = i;
            resDis = dis;
        }
    }
    console.log(resId + 1);
})();

function trim(s) {
	return s.replace(/^\s+|\s+$/gm, '');
}

function tokenize(s) {
	return trim(s).split(/\s+/);
}

function tokenizeIntegers(s) {
	var tokens = tokenize(s);
	for (var i = 0; i < tokens.length; i += 1) {
		tokens[i] = parseInt(tokens[i]);
	};
	return tokens;
}

function main() {
	var integers = tokenizeIntegers(readline());
	var stopNum = integers[0], busSpeed = integers[1], runSpeed = integers[2];
	var xs = tokenizeIntegers(readline());
	var integers = tokenizeIntegers(readline());
	var xTarget = integers[0], yTarget = integers[1];

	function getDistance(x0, y0, x1, y1) {
		return Math.sqrt(Math.pow(x1-x0, 2) + Math.pow(y1-y0, 2));
	}

	var records = [];

	for (var i = 1; i < xs.length; i += 1) {
		var distance = getDistance(xs[i], 0, xTarget, yTarget);
		var time = xs[i]/busSpeed + distance/runSpeed;
		records.push({ distance: distance, time: time, id: i+1 });
	}

	records.sort(function(a, b) {
		return (a.time == b.time ? a.distance - b.distance : a.time - b.time);
	});

	print(records[0].id);
}

main();

var lll = readline().split(' ')
var on = lll[0]
var vb = lll[1]
var vs = lll[2]

var os = readline().split(' ')
lll = readline().split(' ')
var ux = lll[0]
var uy = lll[1]

var t = 0
var pot
var lo

for (var i = 1; i < on; i++) {
  var tv = t + st(i)
  if (pot && pot < tv) {
    lo = i - 1
    break
  } else if (pot && pot == tv) {
    lo = i
    break
  }
  pot = tv
  t += bt(i)
}

if (!lo) lo = os.length - 1

print(+lo + 1)

function st (o) {
  var ox = os[o]
  return Math.sqrt((ux - ox) * (ux - ox) + uy * uy) / vs
}

function bt (o) {
  if (o == os.length) return 0
  var to = os[o]
  var so = os[o + 1]
  return (so - to) / vb
}

function trim(s) {
	return s.replace(/^\s+|\s+$/gm, '');
}

function tokenize(s) {
	return trim(s).split(/\s+/);
}

function tokenizeIntegers(s) {
	var tokens = tokenize(s);
	for (var i = 0; i < tokens.length; i += 1) {
		tokens[i] = parseInt(tokens[i]);
	};
	return tokens;
}

function main() {
	var integers = tokenizeIntegers(readline());
	var stopNum = integers[0], busSpeed = integers[1], runSpeed = integers[2];
	var xs = tokenizeIntegers(readline());
	var integers = tokenizeIntegers(readline());
	var xTarget = integers[0], yTarget = integers[1];

	function dist(x0, y0, x1, y1) {
		return Math.sqrt(Math.pow(x1-x0, 2) + Math.pow(y1-y0, 2));
	}

	var bestStop = 1;
	var bestTime = xs[1]/busSpeed + dist(xs[1], 0, xTarget, yTarget)/runSpeed;

	for (var i = 2; i < xs.length; i += 1) {
		var time = xs[i]/busSpeed + dist(xs[i], 0, xTarget, yTarget)/runSpeed;
		if (time < bestTime) {
			bestTime = time;
			bestStop = i;
		}
	}

	print(bestStop+1);
}

main();


'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);
});

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();

'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 --;
 	}
}

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();

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 --;
 	}
}


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);


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));
    }
});

'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);

}

"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 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;
}