/*
URL:
https://codeforces.com/problemset/problem/1165/D
*/

package main

import (
	"bufio"
	"errors"
	"fmt"
	"io"
	"math"
	"os"
	"sort"
	"strconv"
)

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*******************************************************************/

const (
	// General purpose
	MOD          = 1000000000 + 7
	ALPHABET_NUM = 26
	INF_INT64    = math.MaxInt64
	INF_BIT60    = 1 << 60
	INF_INT32    = math.MaxInt32
	INF_BIT30    = 1 << 30
	NIL          = -1

	// for dijkstra, prim, and so on
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

func init() {
	// bufio.ScanWords <---> bufio.ScanLines
	ReadString = newReadString(os.Stdin, bufio.ScanWords)
	stdout = bufio.NewWriter(os.Stdout)
}

var (
	t int

	n int
	D []int64
)

func main() {
	t = ReadInt()
	for i := 0; i < t; i++ {
		n = ReadInt()
		D = ReadInt64Slice(n)

		solve()
	}
}

func solve() {
	sort.Slice(D, func(i, j int) bool { return D[i] < D[j] })

	ans := D[0] * D[len(D)-1]
	for i := 1; i <= n/2; i++ {
		if ans != D[i]*D[n-1-i] {
			fmt.Println(-1)
			return
		}
	}

	C := divisor(ans)
	sort.Slice(C, func(i, j int) bool { return C[i] < C[j] })

	if len(C) != len(D) {
		fmt.Println(-1)
		return
	}
	for i := 0; i < len(C); i++ {
		if C[i] != D[i] {
			fmt.Println(-1)
			return
		}
	}

	fmt.Println(ans)
}

// 約数の列挙: O(sqrt(n))
func divisor(n int64) []int64 {
	res := []int64{}

	for i := int64(2); i*i <= n; i++ {
		if n%i == 0 {
			res = append(res, i)
			if i != (n / i) {
				res = append(res, (n / i))
			}
		}
	}

	return res
}

/*******************************************************************/

/*********** I/O ***********/

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func newReadString(ior io.Reader, sf bufio.SplitFunc) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	r.Split(sf)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintIntsLine returns integers string delimited by a space.
func PrintInts64Line(A ...int64) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.FormatInt(A[i], 10) // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintfDebug is wrapper of fmt.Fprintf(os.Stderr, format, a...)
func PrintfDebug(format string, a ...interface{}) {
	fmt.Fprintf(os.Stderr, format, a...)
}

// PrintfBufStdout is function for output strings to buffered os.Stdout.
// You may have to call stdout.Flush() finally.
func PrintfBufStdout(format string, a ...interface{}) {
	fmt.Fprintf(stdout, format, a...)
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
	"sort"
)

// github.com/EndlessCheng/codeforces-go
func CF1165D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	const mx int = 1e6
	lpf := [mx + 1]int{1: 1}
	for i := 2; i <= mx; i++ {
		if lpf[i] == 0 {
			for j := i; j <= mx; j += i {
				if lpf[j] == 0 {
					lpf[j] = i
				}
			}
		}
	}
	divisors := func(n int64, a []int) bool {
		ds := []int{}
		for d := int64(2); d*d <= n; d++ {
			if n%d == 0 {
				if d > 1e6 {
					return false
				}
				ds = append(ds, int(d))
				if d*d < n {
					if n/d > 1e6 {
						return false
					}
					ds = append(ds, int(n/d))
				}
				if len(ds) > len(a) {
					return false
				}
			}
		}
		if len(ds) < len(a) {
			return false
		}
		sort.Ints(ds)
		sort.Ints(a)
		for i, v := range ds {
			if v != a[i] {
				return false
			}
		}
		return true
	}

	var t, n int
o:
	for Fscan(in, &t); t > 0; t-- {
		ps := map[int]int8{}
		minP := int(1e9)
		Fscan(in, &n)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
			for v := a[i]; v > 1; {
				p := lpf[v]
				e := int8(1)
				for v /= p; lpf[v] == p; v /= p {
					e++
				}
				if e > ps[p] {
					ps[p] = e
				}
				if p < minP {
					minP = p
				}
			}
		}
		ans := int64(1)
		for p, e := range ps {
			for ; e > 0; e-- {
				ans *= int64(p)
				if ans > 1e12 {
					Fprintln(out, -1)
					continue o
				}
			}
		}
		for _, v := range a {
			if int64(v) == ans {
				ans *= int64(minP)
				if ans > 1e12 {
					Fprintln(out, -1)
					continue o
				}
				break
			}
		}
		if !divisors(ans, a) {
			ans = -1
		}
		Fprintln(out, ans)
	}
}

func main() { CF1165D(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
	"sort"
)

// github.com/EndlessCheng/codeforces-go
func CF1165D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	divisors := func(n int64, a []int) bool {
		res := []int{}
		for d := int64(2); d*d <= n; d++ {
			if n%d == 0 {
				res = append(res, int(d))
				if d*d < n {
					res = append(res, int(n/d))
				}
			}
		}
		sort.Ints(res)
		if len(res) != len(a) {
			return false
		}
		for i, v := range res {
			if v != a[i] {
				return false
			}
		}
		return true
	}

	var t, n int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		sort.Ints(a)
		ans := int64(a[0]) * int64(a[n-1])
		if !divisors(ans, a) {
			ans = -1
		}
		Fprintln(out, ans)
	}
}

func main() { CF1165D(os.Stdin, os.Stdout) }

// Author: sighduck
// URL: https://codeforces.com/problemset/problem/1165/D

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

func getFactors(x int64) []int64 {
	factors := []int64{}
	for i := int64(2); i*i <= x; i++ {
		if x%i == 0 {
			factors = append(factors, i)
			if i*i != x {
				factors = append(factors, x/i)
			}
		}
	}
	return factors
}

func eq(a []int64, b []int64) bool {
	for i := 0; i < len(a); i++ {
		if a[i] != b[i] {
			return false
		}
	}
	return true
}

func sortInt64(arr []int64) {
	sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })
}

func Solve(n int, d []int64) int64 {
	sortInt64(d)
	x := d[0] * d[n-1]
	factors := getFactors(x)
	sortInt64(factors)
	if eq(d, factors) {
		return x
	}
	return -1
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var t int
	fmt.Fscanf(reader, "%d\n", &t)
	for i := 0; i < t; i++ {
		var n int
		fmt.Fscanf(reader, "%d\n", &n)
		d := make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscanf(reader, "%d", &d[i])
		}
		fmt.Fscanf(reader, "\n")
		fmt.Fprintf(writer, "%d\n", Solve(n, d))
	}
}

/*
URL:
https://codeforces.com/problemset/problem/1165/D
*/

package main

import (
	"bufio"
	"errors"
	"fmt"
	"io"
	"math"
	"os"
	"sort"
	"strconv"
)

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*******************************************************************/

const (
	// General purpose
	MOD          = 1000000000 + 7
	ALPHABET_NUM = 26
	INF_INT64    = math.MaxInt64
	INF_BIT60    = 1 << 60
	INF_INT32    = math.MaxInt32
	INF_BIT30    = 1 << 30
	NIL          = -1

	// for dijkstra, prim, and so on
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

func init() {
	// bufio.ScanWords <---> bufio.ScanLines
	ReadString = newReadString(os.Stdin, bufio.ScanWords)
	stdout = bufio.NewWriter(os.Stdout)
}

var (
	t int

	n int
	D []int64
)

func main() {
	t = ReadInt()
	for i := 0; i < t; i++ {
		n = ReadInt()
		D = ReadInt64Slice(n)

		solve()
	}
}

func solve() {
	sort.Slice(D, func(i, j int) bool { return D[i] < D[j] })

	ans := D[0] * D[len(D)-1]
	for i := 1; i <= n/2; i++ {
		if ans != D[i]*D[n-1-i] {
			fmt.Println(-1)
			return
		}
	}
	fmt.Println(ans)
}

/*******************************************************************/

/*********** I/O ***********/

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func newReadString(ior io.Reader, sf bufio.SplitFunc) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	r.Split(sf)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintIntsLine returns integers string delimited by a space.
func PrintInts64Line(A ...int64) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.FormatInt(A[i], 10) // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintfDebug is wrapper of fmt.Fprintf(os.Stderr, format, a...)
func PrintfDebug(format string, a ...interface{}) {
	fmt.Fprintf(os.Stderr, format, a...)
}

// PrintfBufStdout is function for output strings to buffered os.Stdout.
// You may have to call stdout.Flush() finally.
func PrintfBufStdout(format string, a ...interface{}) {
	fmt.Fprintf(stdout, format, a...)
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
	"sort"
)

// github.com/EndlessCheng/codeforces-go
func CF1165D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	const mx int = 1e6
	lpf := [mx + 1]int{1: 1}
	for i := 2; i <= mx; i++ {
		if lpf[i] == 0 {
			for j := i; j <= mx; j += i {
				if lpf[j] == 0 {
					lpf[j] = i
				}
			}
		}
	}
	divisors := func(n int64) (res []int) {
		for d := int64(2); d*d <= n; d++ {
			if n%d == 0 {
				if d > 1e6 {
					return nil
				}
				res = append(res, int(d))
				if d*d < n {
					if n/d > 1e6 {
						return nil
					}
					res = append(res, int(n/d))
				}
			}
		}
		return
	}

	var t, n int
o:
	for Fscan(in, &t); t > 0; t-- {
		ps := map[int]int8{}
		minP := int(1e9)
		Fscan(in, &n)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
			for v := a[i]; v > 1; {
				p := lpf[v]
				e := int8(1)
				for v /= p; lpf[v] == p; v /= p {
					e++
				}
				if e > ps[p] {
					ps[p] = e
				}
				if p < minP {
					minP = p
				}
			}
		}
		ans := int64(1)
		for p, e := range ps {
			for ; e > 0; e-- {
				ans *= int64(p)
				if ans > 1e12 {
					Fprintln(out, -1)
					continue o
				}
			}
		}
		for _, v := range a {
			if int64(v) == ans {
				ans *= int64(minP)
				if ans > 1e12 {
					Fprintln(out, -1)
					continue o
				}
				break
			}
		}
		if d := divisors(ans); d == nil || len(d) != len(a) {
			Fprintln(out, -1)
		} else {
			sort.Ints(d)
			sort.Ints(a)
			for i, v := range d {
				if v != a[i] {
					Fprintln(out, -1)
					continue o
				}
			}
		}
		Fprintln(out, ans)
	}
}

func main() { CF1165D(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1165D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	const mx int = 1e6
	lpf := [mx + 1]int{1: 1}
	for i := 2; i <= mx; i++ {
		if lpf[i] == 0 {
			for j := i; j <= mx; j += i {
				if lpf[j] == 0 {
					lpf[j] = i
				}
			}
		}
	}

	var t, n int
	for Fscan(in, &t); t > 0; t-- {
		ps := map[int]int8{}
		Fscan(in, &n)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
			for v := a[i]; v > 1; {
				p := lpf[v]
				e := int8(1)
				for v /= p; lpf[v] == p; v /= p {
					e++
				}
				if e > ps[p] {
					ps[p] = e
				}
			}
		}
		ans := int64(1)
		for p, e := range ps {
			for ; e > 0; e-- {
				ans *= int64(p)
			}
		}
		for _, v := range a {
			if int64(v) == ans {
				ans *= 2
				break
			}
		}
		Fprintln(out, ans)
	}
}

func main() { CF1165D(os.Stdin, os.Stdout) }


package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
	"strings"
)

type CF1697B struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1697B) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1697B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1697B) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1697B) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1697B) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
gopherbots resolvi en 218ms
Run solve the problem CF1697B
Date: 27/06/22
User: pepradere
URL:  https://codeforces.com/problemset/problem/1697/B
Problem: CF1697B
**/
func (in *CF1697B) Run() {
	n, q := in.NextInt(), in.NextInt()
	sum := make([]int64, n+1)
	for i := 1; i <= n; i++ {
		sum[i] = int64(in.NextInt())
	}

	sort.Slice(sum, func(i, j int) bool {
		return sum[i] < sum[j]
	})

	for i := 1; i <= n; i++ {
		sum[i] += sum[i-1]
	}

	//var ans strings.Builder
	for i := 0; i < q; i++ {
		x, y := in.NextInt(), in.NextInt()
		fmt.Println(sum[n-x+y]-sum[n-x])
		//ans.WriteString(fmt.Sprintf("%d\n", sum[n-x+y]-sum[n-x]))
	}

	//fmt.Print(ans.String())
}

func NewCF1697B(r *bufio.Reader) *CF1697B {
	return &CF1697B{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1697B(bufio.NewReader(os.Stdin)).Run()
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var n, q int
	fmt.Fscan(in, &n, &q)
	p := make([]int64, n)
	for i := range p {
		fmt.Fscan(in, &p[i])
	}
	sort.Slice(p, func(i, j int) bool { return p[i] > p[j] })
	s := make([]int64, n+1)
	for i := 1; i <= n; i++ {
		s[i] = s[i-1] + p[i-1]
	}
	for t := 0; t < q; t++ {
		var x, y int
		fmt.Fscan(in, &x, &y)
		fmt.Fprintln(out, s[x]-s[x-y])
	}
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"sort"
)

/*

5 3
1 2 3 5  5
1 3 6 11 16

*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n, q int
	scanf("%d %d\n", &n, &q)

	p := make([]int64, n)

	for i := range p {
		scanf("%d", &p[i])
	}
	scanf("\n")

	sort.Slice(p, func(i, j int) bool {
		return p[i] < p[j]
	})

	psa := make([]int64, n)

	for i := range p {
		psa[i] = p[i]

		if i != 0 {
			psa[i] += psa[i-1]
		}
	}

	for i := 0; i < q; i++ {
		var x, y int
		scanf("%d %d\n", &x, &y)

		l := n - x
		r := n - x + y - 1

		s := psa[r] - psa[l] + p[l]
		fmt.Fprintln(writer, s)
	}
}

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	//scanf("%d\n", &t)

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

var reader = bufio.NewReader(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int) int {
	return nums[minIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func minIdx(nums ...int) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	return nums[maxIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func maxIdx(nums ...int) int {
	maxNumIdx := 0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = i
		}
	}

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func sum(nums ...int64) (sum int64) {
	for _, num := range nums {
		sum += int64(num)
	}
	return
}

//goland:noinspection GoUnusedFunction
func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int64) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := int64(5); i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

//goland:noinspection GoUnusedFunction
func factorize(num int64) []int64 {
	factors := make([]int64, 0)

	for ftr := int64(2); ftr*ftr <= num; ftr++ {
		for num%ftr == 0 {
			num /= ftr
			factors = append(factors, ftr)
		}
	}

	if num > 1 {
		factors = append(factors, num)
	}

	return factors
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var n, q int
	fmt.Fscan(in, &n, &q)

	a := make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &a[i])
	}
	sort.Slice(a, func(i, j int) bool {
		return a[i] > a[j]
	})

	m := make([]int64, n+1)
	for i := 1; i <= n; i++ {
		m[i] = m[i-1] + a[i-1]
	}
	for i := 0; i < q; i++ {
		var x, y int64
		fmt.Fscan(in, &x, &y)

		fmt.Fprintln(out, m[x]-m[x-y])
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

var (
	reader     = bufio.NewReader(os.Stdin)
	n, q, x, y int
	p, sum     []int64
)

func solve(x, y int) int64 {
	return sum[x] - sum[x-y]
}

func main() {
	fmt.Fscan(reader, &n, &q)
	p, sum = make([]int64, n), make([]int64, n+1)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &p[i])
	}
	sort.Slice(p, func(i, j int) bool {
		return p[i] > p[j]
	})
	for i := 1; i < n+1; i++ {
		sum[i] = sum[i-1] + p[i-1]
	}
	for i := 0; i < q; i++ {
		fmt.Fscan(reader, &x, &y)
		fmt.Println(solve(x, y))
	}
}

package main

import (
	"bufio"
	. "fmt"
	"os"
	"sort"
)

func A3() {
	_in, _out := os.Stdin, os.Stdout
	in, out := bufio.NewReader(_in), bufio.NewWriter(_out)
	defer out.Flush()
	var cas int = 1
	//Fscan(in, &cas)
	var sol func()
	// Coding...beg!
	sol = func() {
		var n, x, y, q int
		Fscan(in, &n, &q)
		arr, sum := make([]int, n), make([]int64, n+1)
		for i := 0; i < n; i++ {
			Fscan(in, &arr[i])
		}
		sort.Ints(arr)
		for i := 1; i <= n; i++ {
			sum[i] = sum[i-1] + int64(arr[i-1])
		}
		for i := 0; i < q; i++ {
			Fscan(in, &x, &y)
			Fprintln(out, sum[(n-x+y)]-sum[n-x])
		}
	}
	// Coding...ove!
	for i := 0; i < cas; i++ {
		sol()
	}
}

func main() { A3() }

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

var r = bufio.NewReader(os.Stdin)
var w = bufio.NewWriter(os.Stdout)

func main() {
	defer w.Flush()
	var n, q int
	fmt.Fscan(r, &n, &q)
	nums := make([]int, n)
	for i:=0;i<n;i++ {
		fmt.Fscan(r, &nums[i])
	}
	sort.Slice(nums, func(i, j int) bool {
		return nums[i] > nums[j]
	})
	pre := make([]int64, n+1)
	for i:=0;i<n;i++ {
		pre[i+1] = pre[i] + int64(nums[i])
	}
	for qq:=1;qq<=q;qq++ {
		var x, y int
		fmt.Fscan(r, &x, &y)
		fmt.Fprintln(w, pre[x] - pre[x-y])
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

func main() {
	var n, q int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &n, &q)
	a := make([]int, n)
	total := make([]int64, n+1)

	for i := 0; i < n; i++ {
		fmt.Fscan(in, &a[i])
	}
	fmt.Fscan(in)

	sort.Slice(a, func(i, j int) bool {
		return a[i] > a[j]
	})

	for i := 0; i < n; i++ {
		total[i+1] = total[i] + int64(a[i])
	}

	for i := 0; i < q; i++ {
		var x, y int
		fmt.Fscan(in, &x, &y)
		fmt.Println(total[x] - total[x-y])
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
	"strings"
)

type CF1697B struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1697B) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1697B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1697B) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1697B) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1697B) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
gopherbots resolvi en 218ms
Run solve the problem CF1697B
Date: 27/06/22
User: pepradere
URL:  https://codeforces.com/problemset/problem/1697/B
Problem: CF1697B
**/
func (in *CF1697B) Run() {
	n, q := in.NextInt(), in.NextInt()
	sum := make([]int64, n+1)
	for i := 1; i <= n; i++ {
		sum[i] = int64(in.NextInt())
	}

	sort.Slice(sum, func(i, j int) bool {
		return sum[i] < sum[j]
	})

	for i := 1; i <= n; i++ {
		sum[i] += sum[i-1]
	}

	var ans strings.Builder
	for i := 0; i < q; i++ {
		x, y := in.NextInt(), in.NextInt()
		ans.WriteString(fmt.Sprintf("%d\n", sum[n-x+y]-sum[n-x]))
	}

	fmt.Print(ans.String())
}

func NewCF1697B(r *bufio.Reader) *CF1697B {
	return &CF1697B{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1697B(bufio.NewReader(os.Stdin)).Run()
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
	"strings"
)

type CF1697B struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1697B) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1697B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1697B) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1697B) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1697B) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
gopherbots resolvi en 218ms
Run solve the problem CF1697B
Date: 27/06/22
User: pepradere
URL:  https://codeforces.com/problemset/problem/1697/B
Problem: CF1697B
**/
func (in *CF1697B) Run() {
	n, q := in.NextInt(), in.NextInt()
	sum := make([]int64, n+1)
	for i := 1; i <= n; i++ {
		sum[i] = in.NextInt64()
	}

	sort.Slice(sum, func(i, j int) bool {
		return sum[i] < sum[j]
	})

	for i := 1; i <= n; i++ {
		sum[i] += sum[i-1]
	}

	var ans strings.Builder
	for i := 0; i < q; i++ {
		x, y := in.NextInt(), in.NextInt()
		ans.WriteString(fmt.Sprintf("%d\n", sum[n-x+y]-sum[n-x]))
	}

	fmt.Print(ans.String())
}

func NewCF1697B(r *bufio.Reader) *CF1697B {
	return &CF1697B{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1697B(bufio.NewReader(os.Stdin)).Run()
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
	"strings"
)

type CF1697B struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1697B) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1697B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1697B) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1697B) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1697B) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
gopherbots resolvi en 218ms
Run solve the problem CF1697B
Date: 27/06/22
User: pepradere
URL:  https://codeforces.com/problemset/problem/1697/B
Problem: CF1697B
**/
func (in *CF1697B) Run() {
	n, q := in.NextInt(), in.NextInt()
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = in.NextInt()
	}
	sort.Ints(arr)
	sum := make([]int64, n+1)
	for i := 0; i < n; i++ {
		if i == 0 {
			sum[i+1] = int64(arr[i])
		} else {
			sum[i+1] = sum[i] + int64(arr[i])
		}
	}

	var ans strings.Builder
	for i := 0; i < q; i++ {
		x, y := in.NextInt(), in.NextInt()
		ans.WriteString(fmt.Sprintf("%d\n", sum[n-x+y]-sum[n-x]))
	}

	fmt.Print(ans.String())
}

func NewCF1697B(r *bufio.Reader) *CF1697B {
	return &CF1697B{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1697B(bufio.NewReader(os.Stdin)).Run()
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
	"strings"
)

type CF1697B struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1697B) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1697B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1697B) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1697B) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1697B) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1697B
Date: 27/06/22
User: pepradere
URL:  https://codeforces.com/problemset/problem/1697/B
Problem: CF1697B
**/
func (in *CF1697B) Run() {
	n, q := in.NextInt(), in.NextInt()
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = in.NextInt()
	}
	sort.Ints(arr)
	sum := make([]int64, n+1)
	for i := 0; i < n; i++ {
		if i == 0 {
			sum[i+1] = int64(arr[i])
		} else {
			sum[i+1] = sum[i] + int64(arr[i])
		}
	}

	var ans strings.Builder
	for i := 0; i < q; i++ {
		x, y := in.NextInt(), in.NextInt()
		if x < n {
			ans.WriteString(fmt.Sprintf("%d\n", sum[n-x+y]-sum[n-x]))
		} else {
			ans.WriteString(fmt.Sprintf("%d\n", sum[y]))
		}

	}

	fmt.Print(ans.String())
}

func NewCF1697B(r *bufio.Reader) *CF1697B {
	return &CF1697B{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1697B(bufio.NewReader(os.Stdin)).Run()
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

var in = bufio.NewReader(os.Stdin)
var tc = 1
var DEBUG = os.Getenv("DEBUG") != ""

func solve() {}

func main() {
	// var T int
	// scan(&T)
	// for ; T > 0; T-- {
	// 	solve()
	// 	tc++
	// }
	var n, q int
	scan(&n, &q)
	a := make([]int, n)
	for i := range a {
		scan(&a[i])
	}
	sort.Ints(a)
	for i := 1; i < n; i++ {
		a[i] += a[i-1]
	}
	for i := 0; i < q; i++ {
		var x, y int
		scan(&x, &y)

		ret := a[n-x+y-1]
		if n-x-1 >= 0 {
			ret -= a[n-x-1]
		}
		printf("%d\n", ret)

		tc++
	}

}

func yn(b bool) string      { return map[bool]string{true: "YES", false: "NO"}[b] }
func scan(a ...interface{}) { fmt.Fscan(in, a...) }
func printf(format string, a ...interface{}) {
	if DEBUG {
		format = fmt.Sprintf("Test #%d: %s", tc, format)
	}
	fmt.Printf(format, a...)
}

func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

// 00
package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

func main() {
	var n, q int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &n, &q)
	p := make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &p[i])
	}
	sort.Slice(p, func(i, j int) bool { return p[i] < p[j] })

	for i := 1; i < n; i++ {
		p[i] += p[i-1]
	}
	for i := 0; i < q; i++ {
		var x, y int
		fmt.Fscan(reader, &x, &y)
		ans := p[n-x+y-1]
		if n-x-1 >= 0 {
			ans -= p[n-x-1]
		}
		fmt.Println(ans)
		// add
	}
}

// 00
package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

func main() {
	var n, q int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &n, &q)
	p := make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &p[i])
	}
	sort.Slice(p, func(i, j int) bool { return p[i] < p[j] })

	for i := 1; i < n; i++ {
		p[i] += p[i-1]
	}
	for i := 0; i < q; i++ {
		var x, y int
		fmt.Fscan(reader, &x, &y)
		ans := p[n-x+y-1]
		if n-x-1 >= 0 {
			ans -= p[n-x-1]
		}
		fmt.Println(ans)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

func main() {
	//var l int
	//fmt.Scan(&l)
	//for l > 0 {
	solve()
	//l--}
}

func solve() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var n, q, l int
	fmt.Fscan(in, &n, &q)
	arr := make([]int, n)
	for l < n {
		fmt.Fscan(in, &arr[l])
		l++
	}
	l = n - 2
	der := make([]int64, n)
	sort.Ints(arr)
	der[n-1] = int64(arr[n-1])
	for l >= 0 {
		der[l] = der[l+1] + int64(arr[l])
		l--
	}
	//fmt.Println(der)
	//fmt.Println(arr)
	var b, c int
	for i := 0; i < q; {
		fmt.Fscan(in, &b, &c)
		if b == c {
			fmt.Fprintln(out, der[n-b])
		} else {
			fmt.Fprintln(out, der[n-b]-der[n-b+c])
		}
		i++
	}

}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
	"strings"
)

const maxTokenSize = 262144 * 10

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]byte, maxTokenSize), maxTokenSize)
	scanner.Scan()

	in := strings.Split(scanner.Text(), " ")
	//maxProducts, _ := strconv.Atoi(in[0])
	c, _ := strconv.Atoi(in[1])

	scanner.Scan()
	sub := strings.Split(scanner.Text(), " ")

	var sums []int64
	for _, s := range sub {
		ss, _ := strconv.ParseInt(s, 10, 64)
		sums = append(sums, ss)
	}

	sort.Slice(sums, func(i, j int) bool {
		return sums[i] > sums[j]
	})

	var sum int64
	for i, v := range sums {
		sum += v
		sums[i] = sum
	}

	for i := 0; i < c; i++ {
		scanner.Scan()
		sub := strings.Split(scanner.Text(), " ")

		x, _ := strconv.Atoi(sub[0])
		y, _ := strconv.Atoi(sub[1])

		if x == y {
			fmt.Println(sums[x - 1])
		} else {
			fmt.Println(sums[x-1] - sums[x-y-1])
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

func main() {
	var n, q int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &n, &q)
	a := make([]int, n)
	total := make([]int, n+1)

	for i := 0; i < n; i++ {
		fmt.Fscan(in, &a[i])
	}
	fmt.Fscan(in)

	sort.Slice(a, func(i, j int) bool {
		return a[i] > a[j]
	})

	for i := 0; i < n; i++ {
		total[i+1] = total[i] + a[i]
	}

	for i := 0; i < q; i++ {
		var x, y int
		fmt.Fscan(in, &x, &y)
		fmt.Println(total[x] - total[x-y])
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

func main() {
	var n, q int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &n, &q)
	a := make([]int, n)
	total := make([]int, n+1)

	for i := 0; i < n; i++ {
		fmt.Fscan(in, &a[i])
	}
	fmt.Fscan(in)

	sort.Slice(a, func(i, j int) bool {
		return a[i] > a[j]
	})

	for i := 0; i < n; i++ {
		total[i+1] = total[i] + a[i]
	}

	for i := 0; i < q; i++ {
		var x, y int
		fmt.Fscan(in, &x, &y)
		fmt.Fprintln(os.Stdout, total[x]-total[x-y])
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

func main() {
	var n, q int
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	fmt.Fscan(in, &n, &q)
	a := make([]int, n)
	total := make([]int, n+1)

	for i := 0; i < n; i++ {
		fmt.Fscan(in, &a[i])
	}
	fmt.Fscan(in)

	sort.Slice(a, func(i, j int) bool {
		return a[i] > a[j]
	})

	for i := 0; i < n; i++ {
		total[i+1] = total[i] + a[i]
	}

	for i := 0; i < q; i++ {
		var x, y int
		fmt.Fscan(in, &x, &y)
		fmt.Fprintln(out, total[x]-total[x-y])
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

func main() {
	var n, q int
	fmt.Scanln(&n, &q)
	a := make([]int, n)
	total := make([]int, n+1)
	in := bufio.NewReader(os.Stdin)

	for i := 0; i < n; i++ {
		fmt.Scan(in, &a[i])
	}
	fmt.Scanln(in)

	sort.Slice(a, func(i, j int) bool {
		return a[i] > a[j]
	})

	for i := 0; i < n; i++ {
		total[i+1] = total[i] + a[i]
	}

	for i := 0; i < q; i++ {
		var x, y int
		fmt.Scanln(in, &x, &y)
		fmt.Println(total[x] - total[x-y])
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

func main() {
	var n, q int
	fmt.Scanln(&n, &q)
	a := make([]int, n)
	total := make([]int, n+1)
	in := bufio.NewReader(os.Stdin)

	for i := 0; i < n; i++ {
		fmt.Scan(in, &a[i])
	}
	fmt.Scanln(in)

	sort.Slice(a, func(i, j int) bool {
		return a[i] > a[j]
	})

	for i := 0; i < n; i++ {
		total[i+1] = total[i] + a[i]
	}

	for i := 0; i < q; i++ {
		var x, y int
		fmt.Scanln(in, &x, &y)
		fmt.Println(in, total[x]-total[x-y])
	}
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var n, q int
	fmt.Scanln(&n, &q)
	a := make([]int, n)
	total := make([]int, n)

	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
	}
	fmt.Scanln()

	sort.Slice(a, func(i, j int) bool {
		return a[i] > a[j]
	})

	total[0] = a[0]
	for i := 1; i < n; i++ {
		total[i] = total[i-1] + a[i]
	}

	fmt.Println("->", total)

	for i := 0; i < q; i++ {
		var x, y int
		fmt.Scanln(&x, &y)
		if x == y {
			fmt.Println(total[x-1])
		} else {
			fmt.Println(total[x-1] - total[x-y-1])
		}

	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
	"strings"
)

type CF1697B struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1697B) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1697B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1697B) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1697B) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1697B) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
gopherbots resolvi en 218ms
Run solve the problem CF1697B
Date: 27/06/22
User: pepradere
URL:  https://codeforces.com/problemset/problem/1697/B
Problem: CF1697B
**/
func (in *CF1697B) Run() {
	n, q := in.NextInt(), in.NextInt()
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = in.NextInt()
	}
	sort.Ints(arr)
	sum := make([]int, n+1)
	for i := 0; i < n; i++ {
		if i == 0 {
			sum[i+1] = arr[i]
		} else {
			sum[i+1] = sum[i] + arr[i]
		}
	}

	var ans strings.Builder
	for i := 0; i < q; i++ {
		x, y := in.NextInt(), in.NextInt()
		ans.WriteString(fmt.Sprintf("%d\n", sum[n-x+y]-sum[n-x]))
	}

	fmt.Print(ans.String())
}

func NewCF1697B(r *bufio.Reader) *CF1697B {
	return &CF1697B{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1697B(bufio.NewReader(os.Stdin)).Run()
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
	"strings"
)

type CF1697B struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1697B) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1697B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1697B) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1697B) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1697B) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1697B
Date: 27/06/22
User: pepradere
URL:  https://codeforces.com/problemset/problem/1697/B
Problem: CF1697B
**/
func (in *CF1697B) Run() {
	n, q := in.NextInt(), in.NextInt()
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = in.NextInt()
	}
	sort.Ints(arr)
	sum := make([]int64, n+1)
	for i := 0; i < n; i++ {
		if i == 0 {
			sum[i+1] = int64(arr[i])
		} else {
			sum[i+1] = sum[i] + int64(arr[i])
		}
	}

	var ans strings.Builder
	for i := 0; i < q; i++ {
		x, y := in.NextInt(), in.NextInt()
		if x < n {
			if x == y {
				ans.WriteString(fmt.Sprintf("%d\n", sum[n]-sum[n-y]))
			} else if y == 1{
				ans.WriteString(fmt.Sprintf("%d\n", sum[n-x+y]-sum[n-x]))
			} else {
				ans.WriteString(fmt.Sprintf("%d\n", sum[n]-(sum[n-x+y]-sum[n-x])))
			}
		} else {
			ans.WriteString(fmt.Sprintf("%d\n", sum[y]))
		}

	}

	fmt.Print(ans.String())
}

func NewCF1697B(r *bufio.Reader) *CF1697B {
	return &CF1697B{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1697B(bufio.NewReader(os.Stdin)).Run()
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
	"strings"
)

type CF1697B struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1697B) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1697B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1697B) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1697B) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1697B) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1697B
Date: 27/06/22
User: pepradere
URL:  https://codeforces.com/problemset/problem/1697/B
Problem: CF1697B
**/
func (in *CF1697B) Run() {
	n, q := in.NextInt(), in.NextInt()
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = in.NextInt()
	}
	sort.Ints(arr)
	sum := make([]int64, n+1)
	for i := 0; i < n; i++ {
		if i == 0 {
			sum[i+1] = int64(arr[i])
		} else {
			sum[i+1] = sum[i] + int64(arr[i])
		}
	}

	var ans strings.Builder
	for i := 0; i < q; i++ {
		x, y := in.NextInt(), in.NextInt()
		if x < n {
			if x == y {
				ans.WriteString(fmt.Sprintf("%d\n", sum[n]-sum[n-y]))
			} else {
				ans.WriteString(fmt.Sprintf("%d\n", sum[n]-(sum[n-x+y]-sum[n-x])))
			}
		} else {
			ans.WriteString(fmt.Sprintf("%d\n", sum[y]))
		}

	}

	fmt.Print(ans.String())
}

func NewCF1697B(r *bufio.Reader) *CF1697B {
	return &CF1697B{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1697B(bufio.NewReader(os.Stdin)).Run()
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
	"strings"
)

type CF1697B struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1697B) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1697B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1697B) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1697B) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1697B) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1697B
Date: 27/06/22
User: pepradere
URL:  https://codeforces.com/problemset/problem/1697/B
Problem: CF1697B
**/
func (in *CF1697B) Run() {
	n, q := in.NextInt(), in.NextInt()
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = in.NextInt()
	}
	sort.Ints(arr)
	sum := make([]int, n+1)
	for i := 0; i < n; i++ {
		if i == 0 {
			sum[i+1] = arr[i]
		} else {
			sum[i+1] = sum[i] + arr[i]
		}
	}

	var ans strings.Builder
	for i := 0; i < q; i++ {
		x, y := in.NextInt(), in.NextInt()
		if x < n {
			if x == y {
				ans.WriteString(fmt.Sprintf("%d\n", sum[n]-sum[n-y]))
			} else {
				ans.WriteString(fmt.Sprintf("%d\n", sum[n]-(sum[n-x+y]-sum[n-x])))
			}
		} else {
			ans.WriteString(fmt.Sprintf("%d\n", sum[y]))
		}

	}

	fmt.Print(ans.String())
}

func NewCF1697B(r *bufio.Reader) *CF1697B {
	return &CF1697B{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1697B(bufio.NewReader(os.Stdin)).Run()
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
	"strings"
)

func main() {

	scanner := bufio.NewScanner(os.Stdin)
	if scanner.Scan() {
		line := scanner.Text()
		item_queries := strings.Split(line, " ")

		iq := make([]int, len(item_queries))
		for i := range iq {
			iq[i], _ = strconv.Atoi(item_queries[i])
		}

		if scanner.Scan() {
			line := scanner.Text()
			numbers := strings.Split(line, " ")
			numb := make([]int, len(numbers))
			for i := range numb {
				numb[i], _ = strconv.Atoi(numbers[i])
			}

			sort.Ints(numb)
			iteration := iq[1]

			for i := 0; i < iteration; i++ {
				if scanner.Scan() {
					line := scanner.Text()
					queries := strings.Split(line, " ")
					q := make([]int, len(queries))
					for z := range q {
						q[z], _ = strconv.Atoi(queries[z])
					}

					max := numb[len(numb)-q[0]:]

					min := max[:q[1]]

					var m int
					for _, minus := range min {
						m = m + minus
					}
					fmt.Println(m)
				}
			}
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
	"strings"
)

func main() {

	scanner := bufio.NewScanner(os.Stdin)
	if scanner.Scan() {
		line := scanner.Text()
		item_queries := strings.Split(line, " ")

		iq := make([]int, len(item_queries))
		for i := range iq {
			iq[i], _ = strconv.Atoi(item_queries[i])
		}

		if scanner.Scan() {
			line := scanner.Text()
			numbers := strings.Split(line, " ")
			numb := make([]int, len(numbers))
			for i := range numb {
				numb[i], _ = strconv.Atoi(numbers[i])
			}

			sort.Sort(sort.Reverse(sort.IntSlice(numb)))
			iteration := iq[1]
			m := numb[0]
			var l []int
			l = append(l, m)
			for i := 0; i < len(numb)-1; i++ {
				m = l[i] + numb[i+1]
				l = append(l, m)
			}
			for i := 0; i < iteration; i++ {
				if scanner.Scan() {
					line := scanner.Text()
					queries := strings.Split(line, " ")
					q := make([]int, len(queries))
					for z := range q {
						q[z], _ = strconv.Atoi(queries[z])
					}

					t := (q[0] - q[1]) - 1
					if t >= 0 {
						fmt.Println(l[q[0]-1] - l[t])
					} else {
						fmt.Println(l[q[0]-1])
					}
				}
			}
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
	"strings"
)

func main() {

	scanner := bufio.NewScanner(os.Stdin)
	buf := make([]byte, 0, 64*1024)
	scanner.Buffer(buf, 1024*1024)
	if scanner.Scan() {
		line := scanner.Text()
		item_queries := strings.Split(line, " ")

		iq := make([]int, len(item_queries))
		for i := range iq {
			iq[i], _ = strconv.Atoi(item_queries[i])
		}

		if scanner.Scan() {
			line := scanner.Text()
			numbers := strings.Split(line, " ")
			numb := make([]int, len(numbers))
			for i := range numb {
				numb[i], _ = strconv.Atoi(numbers[i])
			}

			sort.Sort(sort.Reverse(sort.IntSlice(numb)))
			iteration := iq[1]
			m := numb[0]
			var l []int
			l = append(l, m)
			for i := 0; i < len(numb)-1; i++ {
				m = l[i] + numb[i+1]
				l = append(l, m)
			}
			for i := 0; i < iteration; i++ {
				if scanner.Scan() {
					line := scanner.Text()
					queries := strings.Split(line, " ")
					q := make([]int, len(queries))
					for i := range q {
						q[i], _ = strconv.Atoi(queries[i])
					}

					t := (q[0] - q[1]) - 1
					if t >= 0 {
						fmt.Println(l[q[0]-1] - l[t])
					} else {
						fmt.Println(l[q[0]-1])
					}
				}
			}
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
	"strings"
)

func main() {

	scanner := bufio.NewScanner(os.Stdin)
	if scanner.Scan() {
		line := scanner.Text()
		item_queries := strings.Split(line, " ")

		iq := make([]int, len(item_queries))
		for i := range iq {
			iq[i], _ = strconv.Atoi(item_queries[i])
		}

		if scanner.Scan() {
			line := scanner.Text()
			numbers := strings.Split(line, " ")
			numb := make([]int, len(numbers))
			for i := range numb {
				numb[i], _ = strconv.Atoi(numbers[i])
			}

			sort.Sort(sort.Reverse(sort.IntSlice(numb)))
			iteration := iq[1]
			m := numb[0]
			var l []int
			l = append(l, m)
			for i := 0; i < len(numb)-1; i++ {
				m = l[i] + numb[i+1]
				l = append(l, m)
			}
			for i := 0; i < iteration; i++ {
				if scanner.Scan() {
					line := scanner.Text()
					queries := strings.Split(line, " ")
					q := make([]int, len(queries))
					for i := range q {
						q[i], _ = strconv.Atoi(queries[i])
					}

					t := (q[0] - q[1]) - 1
					if t >= 0 {
						fmt.Println(l[q[0]-1] - l[t])
					} else {
						fmt.Println(l[q[0]-1])
					}
				}
			}
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
	"strings"
)

func main() {

	scanner := bufio.NewScanner(os.Stdin)
	if scanner.Scan() {
		line := scanner.Text()
		item_queries := strings.Split(line, " ")

		iq := make([]int, len(item_queries))
		for i := range iq {
			iq[i], _ = strconv.Atoi(item_queries[i])
		}

		if scanner.Scan() {
			line := scanner.Text()
			numbers := strings.Split(line, " ")
			numb := make([]int, len(numbers))
			for i := range numb {
				numb[i], _ = strconv.Atoi(numbers[i])
			}

			sort.Sort(sort.Reverse(sort.IntSlice(numb)))
			fmt.Println(numb)
			iteration := iq[1]
			m := numb[0]
			var l []int
			l = append(l, m)
			for i := 0; i < len(numb)-1; i++ {
				m = l[i] + numb[i+1]
				l = append(l, m)
			}
			for i := 0; i < iteration; i++ {
				if scanner.Scan() {
					line := scanner.Text()
					queries := strings.Split(line, " ")
					q := make([]int, len(queries))
					for i := range q {
						q[i], _ = strconv.Atoi(queries[i])
					}

					t := (q[0] - q[1]) - 1
					if t >= 0 {
						fmt.Println(l[q[0]-1] - l[t])
					} else {
						fmt.Println(l[q[0]-1])
					}
				}
			}
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
	"strings"
)

func main() {

	scanner := bufio.NewScanner(os.Stdin)
	if scanner.Scan() {
		line := scanner.Text()
		item_queries := strings.Split(line, " ")

		iq := make([]int, len(item_queries))
		for i := range iq {
			iq[i], _ = strconv.Atoi(item_queries[i])
		}

		if scanner.Scan() {
			line := scanner.Text()
			numbers := strings.Split(line, " ")
			numb := make([]int, len(numbers))
			for i := range numb {
				numb[i], _ = strconv.Atoi(numbers[i])
			}

			sort.Sort(sort.Reverse(sort.IntSlice(numb)))
			fmt.Println(numb)
			iteration := iq[1]
			m := numb[0]
			var l []int
			l = append(l, m)
			for i := 0; i < len(numb)-1; i++ {
				m = l[i] + numb[i+1]
				l = append(l, m)
			}
			for i := 0; i < iteration; i++ {
				if scanner.Scan() {
					line := scanner.Text()
					queries := strings.Split(line, " ")
					q := make([]int, len(queries))
					for i := range q {
						q[i], _ = strconv.Atoi(queries[i])
					}

					t := (q[0] - q[1]) - 1
					if t >= 0 {
						fmt.Println("réponse", l[q[0]-1]-l[t])
					} else {
						fmt.Println("réponseé", l[q[0]-1])
					}
				}
			}
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

var in = bufio.NewReader(os.Stdin)
var tc = 1
var DEBUG = os.Getenv("DEBUG") != ""

func solve() {}

func main() {
	// var T int
	// scan(&T)
	// for ; T > 0; T-- {
	// 	solve()
	// 	tc++
	// }
	var n, q int
	scan(&n, &q)
	a := make([]int, n)
	for i := range a {
		scan(&a[i])
	}
	sort.Slice(a, func(i, j int) bool { return a[i] < a[j] })
	for i := 1; i < n; i++ {
		a[i] += a[i-1]
	}
	for i := 0; i < q; i++ {
		var x, y int
		scan(&x, &y)

		ret := a[n-x+y-1]
		if n-x-1 > 0 {
			ret -= a[n-x-1]
		}
		printf("%d\n", ret)

		tc++
	}

}

func yn(b bool) string      { return map[bool]string{true: "YES", false: "NO"}[b] }
func scan(a ...interface{}) { fmt.Fscan(in, a...) }
func printf(format string, a ...interface{}) {
	if DEBUG {
		format = fmt.Sprintf("Test #%d: %s", tc, format)
	}
	fmt.Printf(format, a...)
}

func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

var in = bufio.NewReader(os.Stdin)
var tc = 1
var DEBUG = os.Getenv("DEBUG") != ""

func solve() {}

func main() {
	// var T int
	// scan(&T)
	// for ; T > 0; T-- {
	// 	solve()
	// 	tc++
	// }
	var n, q int
	scan(&n, &q)
	a := make([]int, n)
	for i := range a {
		scan(&a[i])
	}
	sort.Ints(a)
	for i := 1; i < n; i++ {
		a[i] += a[i-1]
	}
	for i := 0; i < q; i++ {
		var x, y int
		scan(&x, &y)

		ret := a[n-x+y-1]
		if n-x-1 > 0 {
			ret -= a[n-x-1]
		}
		printf("%d\n", ret)

		tc++
	}

}

func yn(b bool) string      { return map[bool]string{true: "YES", false: "NO"}[b] }
func scan(a ...interface{}) { fmt.Fscan(in, a...) }
func printf(format string, a ...interface{}) {
	if DEBUG {
		format = fmt.Sprintf("Test #%d: %s", tc, format)
	}
	fmt.Printf(format, a...)
}

func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

// 00
package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

func main() {
	var n, q int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &n, &q)
	p := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &p[i])
	}
	//sort.Slice(p, func(i, j int) bool { return p[i] < p[j] })
	sort.Ints(p)
	for i := 1; i < n; i++ {
		p[i] += p[i-1]
	}
	for i := 0; i < q; i++ {
		var x, y int
		fmt.Fscan(reader, &x, &y)
		ans := p[n-x+y-1]
		if n-x-1 >= 0 {
			ans -= p[n-x-1]
		}
		fmt.Println(ans)
	}
}

// 00
package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

func main() {
	var n, q int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &n, &q)
	p := make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &p[i])
	}
	sort.Slice(p, func(i, j int) bool { return p[i] < p[j] })

	for i := 1; i < n; i++ {
		p[i] += p[i-1]
	}
	for i := 0; i < q; i++ {
		var x, y int
		fmt.Fscan(reader, &x, &y)
		ans := p[n-x+y-1]
		if n-x-1 > 0 {
			ans -= p[n-x-1]
		}
		fmt.Println(ans)
	}
}

// 00
package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

func main() {
	var n, q int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &n, &q)
	p := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &p[i])
	}
	sort.Ints(p)
	for i := 1; i < n; i++ {
		p[i] += p[i-1]
	}
	for i := 0; i < q; i++ {
		var x, y int
		fmt.Fscan(reader, &x, &y)
		ans := p[n-x+y-1]
		if n-x-1 > 0 {
			ans -= p[n-x-1]
		}
		fmt.Println(ans)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

func main() {
	//var l int
	//fmt.Scan(&l)
	//for l > 0 {
	solve()
	//l--}
}

func solve() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var n, q, l int
	fmt.Fscan(in, &n, &q)
	arr := make([]int, n)
	for l < n {
		fmt.Fscan(in, &arr[l])
		l++
	}
	l = n - 2
	der := make([]int, n)
	sort.Ints(arr)
	der[n-1] = arr[n-1]
	for l >= 0 {
		der[l] = der[l+1] + arr[l]
		l--
	}
	//fmt.Println(der)
	//fmt.Println(arr)
	var b, c int
	for i := 0; i < q; {
		fmt.Fscan(in, &b, &c)
		if b == c {
			fmt.Fprintln(out, der[n-b])
		} else {
			fmt.Fprintln(out, der[n-b]-der[n-b+c])
		}
		i++
	}

}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var n, q int
	fmt.Fscan(in, &n, &q)
	p := make([]int, n)
	for i := range p {
		fmt.Fscan(in, &p[i])
	}
	sort.Slice(p, func(i, j int) bool { return p[i] > p[j] })
	s := make([]int, n+1)
	for i := 1; i <= n; i++ {
		s[i] = s[i-1] + p[i-1]
	}
	for t := 0; t < q; t++ {
		var x, y int
		fmt.Fscan(in, &x, &y)
		fmt.Fprintln(out, s[x]-s[x-y])
	}
}


package main

import (
	"bufio"
	"bytes"
	"fmt"
	"os"
)

func Init() {
}

func Solve(io *FastIO) {
	S := io.NextLine()
	N := len(S)

	P := MakeIntMatrix(N, N)
	for c := 0; c < N; c++ {
		P[c][c] = 1
		for lo, hi := c - 1, c + 1; lo >= 0 && hi < N; lo, hi = lo - 1, hi + 1 {
			if S[lo] == S[hi] {
				P[lo][hi] = 1
			} else {
				break
			}
		}
		for lo, hi := c, c + 1; lo >= 0 && hi < N; lo, hi = lo - 1, hi + 1 {
			if S[lo] == S[hi] {
				P[lo][hi] = 1
			} else {
				break
			}
		}
	}

	for len := 2; len <= N; len++ {
		half := len >> 1
		for i := 0; i + len <= N; i++ {
			if prev := P[i][i + half - 1]; prev > 0 && S[i : i+half] == S[i+len-half : i+len] {
				P[i][i + len - 1] = 1 + prev
			}
		}
	}

	freq := make([]int, N + 1)
	for i := 0; i < N; i++ {
		for j := i; j < N; j++ {
			freq[P[i][j]]++
		}
	}
	for i := N - 2; i >= 0; i-- {
		freq[i] += freq[i + 1]
	}

	for i := 1; i <= N; i++ {
		io.Printf("%d ", freq[i])
	}
	io.Println("")
}

func MakeIntMatrix(rows, cols int) [][]int {
	mat := make([][]int, rows)
	for i := range mat {
		mat[i] = make([]int, cols)
	}
	return mat
}

type FastIO struct {
	reader *bufio.Reader
	writer *bufio.Writer
}

func (io *FastIO) NextChar() byte {
	c, _ := io.reader.ReadByte()
	return c
}

func (io *FastIO) NextInt() int {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := 1
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := 0
	for !IsSpaceChar(c) {
		res = (res * 10) + int(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextLong() int64 {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := int64(1)
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := int64(0)
	for !IsSpaceChar(c) {
		res = (res * 10) + int64(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextIntArray(size int) []int {
	return io.NextIntArrayOffset(size, 0)
}

func (io *FastIO) NextIntArrayOffset(size, offset int) []int {
	arr := make([]int, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextInt()
	}
	return arr
}

func (io *FastIO) NextLongArray(size int) []int64 {
	return io.NextLongArrayOffset(size, 0)
}

func (io *FastIO) NextLongArrayOffset(size, offset int) []int64 {
	arr := make([]int64, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextLong()
	}
	return arr
}

func (io *FastIO) NextString() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsSpaceChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) NextLine() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsLineBreakChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) Println(args ...interface{}) {
	io.writer.WriteString(fmt.Sprintln(args...))
}

func (io *FastIO) Printf(format string, args ...interface{}) {
	io.writer.WriteString(fmt.Sprintf(format, args...))
}

func (io *FastIO) FlushOutput() {
	io.writer.Flush()
}

func IsSpaceChar(c byte) bool {
	switch c {
	case 0, '\t', '\n', '\r', ' ':
		return true
	default:
		return false
	}
}

func IsLineBreakChar(c byte) bool {
	switch c {
		case 0, '\n', '\r':
			return true
		default:
			return false
	}
}

func main() {
	io := FastIO{reader: bufio.NewReader(os.Stdin), writer: bufio.NewWriter(os.Stdout)}
	Init()
	Solve(&io)
	io.FlushOutput()
}

package main

import "fmt"

const (
	MOD  =  982451653  // some large prime
	MULTI = 13758397 // another large prime
	MULTI2 = 13756541 // another large prime
)

var (
	forwardHash [5001][5001] int
	reverseHash [5001][5001] int
	palindromeOfType [5001] int
	input string
	n int
)

func computeHash(accumulated int, val byte) int {
	var buff int64
	buff = int64(accumulated) * MULTI + int64(int(val) - int('a')) * MULTI2
	return int(buff % MOD)
}

func getTypeOfPalindrome(start, length int) int {
	end := start + length - 1

	if length == 1 {
		return 1
	}
	if forwardHash[start][length] == reverseHash[end][length] {
		return 1 + getTypeOfPalindrome(start, length / 2)
	}
	return 0
}

func main()  {
	fmt.Scan(&input)
	n = len(input)

	for i := 0; i < n; i++ {
		var fhash, rhash = computeHash(0, byte(input[i])), computeHash(0, byte(input[i]))
		forwardHash[i][1] = fhash
		reverseHash[i][1] = rhash

		for j := 1; ; j++ {
			computedHashes := 0
			if i + j < n {
				fhash = computeHash(fhash, byte(input[i+j]))
				forwardHash[i][j + 1] = fhash
				computedHashes += 1
			}
			if i - j >= 0 {
				rhash = computeHash(rhash, byte(input[i-j]))
				reverseHash[i][j + 1] = rhash
				computedHashes += 1
			}
			if computedHashes == 0 {
				break
			}
		}
	}

	for i := 0; i < n; i++ {
		for j := 1; i + j <= n; j++ {
			palindromeOfType[getTypeOfPalindrome(i, j)]++
		}
	}

	for i := n; i >= 1; i-- {
		palindromeOfType[i - 1] += palindromeOfType[i]
	}

	for i := 1; i <= n; i++ {
		fmt.Printf("%d ", palindromeOfType[i])
	}
	fmt.Println()
}

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"os"
)

func Init() {
}

func Solve(io *FastIO) {
	S := io.NextLine()
	N := len(S)

	P := MakeIntMatrix(N, N)
	for c := 0; c < N; c++ {
		P[c][c] |= (1 << 1)
		for lo, hi := c - 1, c + 1; lo >= 0 && hi < N; lo, hi = lo - 1, hi + 1 {
			if S[lo] == S[hi] {
				P[lo][hi] |= (1 << 1)
			} else {
				break
			}
		}
		for lo, hi := c, c + 1; lo >= 0 && hi < N; lo, hi = lo - 1, hi + 1 {
			if S[lo] == S[hi] {
				P[lo][hi] |= (1 << 1)
			} else {
				break
			}
		}
	}

	for len := 2; len <= N; len++ {
		half := len >> 1
		for i := 0; i + len <= N; i++ {
			if S[i : i+half] == S[i+len-half : i+len] {
				for b := (1 << 1); b < (1 << 16); b <<= 1 {
					if (P[i][i + half - 1] & b) > 0 && (P[i + len - half][i + len - 1] & b) > 0 {
						P[i][i + len - 1] |= (b << 1)
					}
				}
			}
		}
	}

	freq := make([]int, N + 1)
	for i := 0; i < N; i++ {
		for j := i; j < N; j++ {
			for k := uint(1); k < 16; k++ {
				b := (1 << k)
				if P[i][j] & b > 0 {
					freq[k]++
				}
			}
		}
	}

	for i := 1; i <= N; i++ {
		io.Printf("%d ", freq[i])
	}
	io.Println("")
}

func MakeIntMatrix(rows, cols int) [][]int {
	mat := make([][]int, rows)
	for i := range mat {
		mat[i] = make([]int, cols)
	}
	return mat
}

type FastIO struct {
	reader *bufio.Reader
	writer *bufio.Writer
}

func (io *FastIO) NextChar() byte {
	c, _ := io.reader.ReadByte()
	return c
}

func (io *FastIO) NextInt() int {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := 1
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := 0
	for !IsSpaceChar(c) {
		res = (res * 10) + int(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextLong() int64 {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := int64(1)
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := int64(0)
	for !IsSpaceChar(c) {
		res = (res * 10) + int64(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextIntArray(size int) []int {
	return io.NextIntArrayOffset(size, 0)
}

func (io *FastIO) NextIntArrayOffset(size, offset int) []int {
	arr := make([]int, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextInt()
	}
	return arr
}

func (io *FastIO) NextLongArray(size int) []int64 {
	return io.NextLongArrayOffset(size, 0)
}

func (io *FastIO) NextLongArrayOffset(size, offset int) []int64 {
	arr := make([]int64, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextLong()
	}
	return arr
}

func (io *FastIO) NextString() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsSpaceChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) NextLine() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsLineBreakChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) Println(args ...interface{}) {
	io.writer.WriteString(fmt.Sprintln(args...))
}

func (io *FastIO) Printf(format string, args ...interface{}) {
	io.writer.WriteString(fmt.Sprintf(format, args...))
}

func (io *FastIO) FlushOutput() {
	io.writer.Flush()
}

func IsSpaceChar(c byte) bool {
	switch c {
	case 0, '\t', '\n', '\r', ' ':
		return true
	default:
		return false
	}
}

func IsLineBreakChar(c byte) bool {
	switch c {
		case 0, '\n', '\r':
			return true
		default:
			return false
	}
}

func main() {
	io := FastIO{reader: bufio.NewReader(os.Stdin), writer: bufio.NewWriter(os.Stdout)}
	Init()
	Solve(&io)
	io.FlushOutput()
}

package main

import "fmt"

const MAXN = 5001

var (
	input          string
	n              int
	isPalindrome   [MAXN][MAXN]int
	palindromeType [MAXN]int
)

func init() {
	for i := 0; i < MAXN; i++ {
		for j := 0; j < MAXN; j++ {
			isPalindrome[i][j] = -1
		}
	}
}

func computeIsPalindrome(inclusiveStart, inclusiveEnd int) int {
	switch {
	case isPalindrome[inclusiveStart][inclusiveEnd] != -1:
	case inclusiveStart == inclusiveEnd:
		isPalindrome[inclusiveStart][inclusiveEnd] = 1
	case input[inclusiveStart] != input[inclusiveEnd]:
		isPalindrome[inclusiveStart][inclusiveEnd] = 0
	case inclusiveStart == inclusiveEnd-1:
		isPalindrome[inclusiveStart][inclusiveEnd] = 1
	default:
		isPalindrome[inclusiveStart][inclusiveEnd] = computeIsPalindrome(inclusiveStart+1, inclusiveEnd-1)
	}
	return isPalindrome[inclusiveStart][inclusiveEnd]
}

func getPalindromeType(start, length int) int {
	switch end := start + length - 1; {
	case end-start == 0:
		return 1
	case isPalindrome[start][end] == 1:
		return 1 + getPalindromeType(start, length/2)
	default:
		return 0
	}
}

func main() {
	fmt.Scan(&input)
	n = len(input)

	for i := 0; i < n; i++ {
		for j := i; j < n; j++ {
			computeIsPalindrome(i, j)
		}
	}

	for i := 0; i < n; i++ {
		for j := 1; i+j <= n; j++ {
			palindromeType[getPalindromeType(i, j)]++
		}
	}

	for i := n; i >= 1; i-- {
		palindromeType[i-1] += palindromeType[i]
	}

	for i := 1; i <= n; i++ {
		fmt.Printf("%d ", palindromeType[i])
	}
}

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"os"
)

func Init() {
}

func Solve(io *FastIO) {
	S := io.NextLine()
	N := len(S)

	P := MakeIntMatrix(N, N)
	for c := 0; c < N; c++ {
		P[c][c] = 1
		for lo, hi := c - 1, c + 1; lo >= 0 && hi < N; lo, hi = lo - 1, hi + 1 {
			if S[lo] == S[hi] {
				P[lo][hi] = 1
			} else {
				break
			}
		}
		for lo, hi := c, c + 1; lo >= 0 && hi < N; lo, hi = lo - 1, hi + 1 {
			if S[lo] == S[hi] {
				P[lo][hi] = 1
			} else {
				break
			}
		}
	}

	for len := 2; len <= N; len++ {
		half := len >> 1
		for i := 0; i + len <= N; i++ {
			if S[i : i+half] == S[i+len-half : i+len] {
				P[i][i + len - 1] = 1 + P[i][i + half - 1]
			}
		}
	}

	freq := make([]int, N + 1)
	for i := 0; i < N; i++ {
		for j := i; j < N; j++ {
			freq[P[i][j]]++
		}
	}
	for i := N - 2; i >= 0; i-- {
		freq[i] += freq[i + 1]
	}

	for i := 1; i <= N; i++ {
		io.Printf("%d ", freq[i])
	}
	io.Println("")
}

func MakeIntMatrix(rows, cols int) [][]int {
	mat := make([][]int, rows)
	for i := range mat {
		mat[i] = make([]int, cols)
	}
	return mat
}

type FastIO struct {
	reader *bufio.Reader
	writer *bufio.Writer
}

func (io *FastIO) NextChar() byte {
	c, _ := io.reader.ReadByte()
	return c
}

func (io *FastIO) NextInt() int {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := 1
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := 0
	for !IsSpaceChar(c) {
		res = (res * 10) + int(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextLong() int64 {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := int64(1)
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := int64(0)
	for !IsSpaceChar(c) {
		res = (res * 10) + int64(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextIntArray(size int) []int {
	return io.NextIntArrayOffset(size, 0)
}

func (io *FastIO) NextIntArrayOffset(size, offset int) []int {
	arr := make([]int, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextInt()
	}
	return arr
}

func (io *FastIO) NextLongArray(size int) []int64 {
	return io.NextLongArrayOffset(size, 0)
}

func (io *FastIO) NextLongArrayOffset(size, offset int) []int64 {
	arr := make([]int64, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextLong()
	}
	return arr
}

func (io *FastIO) NextString() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsSpaceChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) NextLine() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsLineBreakChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) Println(args ...interface{}) {
	io.writer.WriteString(fmt.Sprintln(args...))
}

func (io *FastIO) Printf(format string, args ...interface{}) {
	io.writer.WriteString(fmt.Sprintf(format, args...))
}

func (io *FastIO) FlushOutput() {
	io.writer.Flush()
}

func IsSpaceChar(c byte) bool {
	switch c {
	case 0, '\t', '\n', '\r', ' ':
		return true
	default:
		return false
	}
}

func IsLineBreakChar(c byte) bool {
	switch c {
		case 0, '\n', '\r':
			return true
		default:
			return false
	}
}

func main() {
	io := FastIO{reader: bufio.NewReader(os.Stdin), writer: bufio.NewWriter(os.Stdout)}
	Init()
	Solve(&io)
	io.FlushOutput()
}

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"os"
)

func Init() {
}

func Solve(io *FastIO) {
	S := io.NextLine()
	N := len(S)

	P := MakeIntMatrix(N, N)
	for c := 0; c < N; c++ {
		P[c][c] = 1
		for lo, hi := c - 1, c + 1; lo >= 0 && hi < N; lo, hi = lo - 1, hi + 1 {
			if S[lo] == S[hi] {
				P[lo][hi] = 1
			} else {
				break
			}
		}
		for lo, hi := c, c + 1; lo >= 0 && hi < N; lo, hi = lo - 1, hi + 1 {
			if S[lo] == S[hi] {
				P[lo][hi] = 2
			} else {
				break
			}
		}
	}

	for len := 3; len <= N; len++ {
		half := len >> 1
		for i := 0; i + len <= N; i++ {
			if S[i : i+half] == S[i+len-half : i+len] {
				P[i][i + len - 1] = 1 + P[i][i + half - 1]
			}
		}
	}

	freq := make([]int, N + 1)
	for i := 0; i < N; i++ {
		for j := i; j < N; j++ {
			freq[P[i][j]]++
		}
	}
	for i := N - 2; i >= 0; i-- {
		freq[i] += freq[i + 1]
	}

	for i := 1; i <= N; i++ {
		io.Printf("%d ", freq[i])
	}
	io.Println("")
}

func MakeIntMatrix(rows, cols int) [][]int {
	mat := make([][]int, rows)
	for i := range mat {
		mat[i] = make([]int, cols)
	}
	return mat
}

type FastIO struct {
	reader *bufio.Reader
	writer *bufio.Writer
}

func (io *FastIO) NextChar() byte {
	c, _ := io.reader.ReadByte()
	return c
}

func (io *FastIO) NextInt() int {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := 1
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := 0
	for !IsSpaceChar(c) {
		res = (res * 10) + int(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextLong() int64 {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := int64(1)
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := int64(0)
	for !IsSpaceChar(c) {
		res = (res * 10) + int64(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextIntArray(size int) []int {
	return io.NextIntArrayOffset(size, 0)
}

func (io *FastIO) NextIntArrayOffset(size, offset int) []int {
	arr := make([]int, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextInt()
	}
	return arr
}

func (io *FastIO) NextLongArray(size int) []int64 {
	return io.NextLongArrayOffset(size, 0)
}

func (io *FastIO) NextLongArrayOffset(size, offset int) []int64 {
	arr := make([]int64, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextLong()
	}
	return arr
}

func (io *FastIO) NextString() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsSpaceChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) NextLine() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsLineBreakChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) Println(args ...interface{}) {
	io.writer.WriteString(fmt.Sprintln(args...))
}

func (io *FastIO) Printf(format string, args ...interface{}) {
	io.writer.WriteString(fmt.Sprintf(format, args...))
}

func (io *FastIO) FlushOutput() {
	io.writer.Flush()
}

func IsSpaceChar(c byte) bool {
	switch c {
	case 0, '\t', '\n', '\r', ' ':
		return true
	default:
		return false
	}
}

func IsLineBreakChar(c byte) bool {
	switch c {
		case 0, '\n', '\r':
			return true
		default:
			return false
	}
}

func main() {
	io := FastIO{reader: bufio.NewReader(os.Stdin), writer: bufio.NewWriter(os.Stdout)}
	Init()
	Solve(&io)
	io.FlushOutput()
}

package main

import "fmt"

const MOD  =  179424691  // some large prime

var (
	forwardHash [5001][5001] int
	reverseHash [5001][5001] int
	palindromeOfType [5001] int
	input string
	n int
)

func computeHash(accumulated int, val byte) int {
	return (accumulated + int(val) + accumulated * 7 + (int(val) - int('a')) * 3) % MOD
}

func getTypeOfPalindrome(start, length int) int {
	end := start + length - 1

	if length == 1 {
		return 1
	}
	if forwardHash[start][length] == reverseHash[end][length] {
		return 1 + getTypeOfPalindrome(start, length / 2)
	}
	return 0
}

func main()  {
	fmt.Scan(&input)
	n = len(input)

	for i := 0; i < n; i++ {
		var fhash, rhash = computeHash(0, byte(input[i])), computeHash(0, byte(input[i]))
		forwardHash[i][1] = fhash
		reverseHash[i][1] = rhash

		for j := 1; ; j++ {
			computedHashes := 0
			if i + j < n {
				fhash = computeHash(fhash, byte(input[i+j]))
				forwardHash[i][j + 1] = fhash
				computedHashes += 1
			}
			if i - j >= 0 {
				rhash = computeHash(rhash, byte(input[i-j]))
				reverseHash[i][j + 1] = rhash
				computedHashes += 1
			}
			if computedHashes == 0 {
				break
			}
		}
	}

	for i := 0; i < n; i++ {
		for j := 1; i + j <= n; j++ {
			palindromeOfType[getTypeOfPalindrome(i, j)]++
		}
	}

	for i := n; i >= 1; i-- {
		palindromeOfType[i - 1] += palindromeOfType[i]
	}

	for i := 1; i <= n; i++ {
		fmt.Printf("%d ", palindromeOfType[i])
	}
	fmt.Println()
}

package main

import "fmt"

const MOD  =  160481219  // some large prime

var (
	forwardHash [5001][5001] int
	reverseHash [5001][5001] int
	palindromeOfType [5001] int
	input string
	n int
)

func computeHash(accumulated int, val byte) int {
	return (accumulated * 7 + (int(val) - int('a')) * 3) % MOD
}

func getTypeOfPalindrome(start, length int) int {
	end := start + length - 1

	if length == 1 {
		return 1
	}
	if forwardHash[start][length] == reverseHash[end][length] {
		return 1 + getTypeOfPalindrome(start, length / 2)
	}
	return 0
}

func main()  {
	fmt.Scan(&input)
	n = len(input)

	for i := 0; i < n; i++ {
		var fhash, rhash = computeHash(0, byte(input[i])), computeHash(0, byte(input[i]))
		forwardHash[i][1] = fhash
		reverseHash[i][1] = rhash

		for j := 1; ; j++ {
			computedHashes := 0
			if i + j < n {
				fhash = computeHash(fhash, byte(input[i+j]))
				forwardHash[i][j + 1] = fhash
				computedHashes += 1
			}
			if i - j >= 0 {
				rhash = computeHash(rhash, byte(input[i-j]))
				reverseHash[i][j + 1] = rhash
				computedHashes += 1
			}
			if computedHashes == 0 {
				break
			}
		}
	}

	for i := 0; i < n; i++ {
		for j := 1; i + j <= n; j++ {
			palindromeOfType[getTypeOfPalindrome(i, j)]++
		}
	}

	for i := n; i >= 1; i-- {
		palindromeOfType[i - 1] += palindromeOfType[i]
	}

	for i := 1; i <= n; i++ {
		fmt.Printf("%d ", palindromeOfType[i])
	}
	fmt.Println()
}

package main

import "fmt"

const MOD  =  179424691  // some large prime

var (
	forwardHash [5001][5001] int
	reverseHash [5001][5001] int
	palindromeOfType [5001] int
	input string
	n int
)

func computeHash(accumulated int, val byte) int {
	return (accumulated * 7 + (int(val) - int('a')) * 3) % MOD
}

func getTypeOfPalindrome(start, length int) int {
	end := start + length - 1

	if length == 1 {
		return 1
	}
	if forwardHash[start][length] == reverseHash[end][length] {
		return 1 + getTypeOfPalindrome(start, length / 2)
	}
	return 0
}

func main()  {
	fmt.Scan(&input)
	n = len(input)

	for i := 0; i < n; i++ {
		var fhash, rhash = computeHash(0, byte(input[i])), computeHash(0, byte(input[i]))
		forwardHash[i][1] = fhash
		reverseHash[i][1] = rhash

		for j := 1; ; j++ {
			computedHashes := 0
			if i + j < n {
				fhash = computeHash(fhash, byte(input[i+j]))
				forwardHash[i][j + 1] = fhash
				computedHashes += 1
			}
			if i - j >= 0 {
				rhash = computeHash(rhash, byte(input[i-j]))
				reverseHash[i][j + 1] = rhash
				computedHashes += 1
			}
			if computedHashes == 0 {
				break
			}
		}
	}

	for i := 0; i < n; i++ {
		for j := 1; i + j <= n; j++ {
			palindromeOfType[getTypeOfPalindrome(i, j)]++
		}
	}

	for i := n; i >= 1; i-- {
		palindromeOfType[i - 1] += palindromeOfType[i]
	}

	for i := 1; i <= n; i++ {
		fmt.Printf("%d ", palindromeOfType[i])
	}
	fmt.Println()
}

package main

import "fmt"

const MOD  =  982451653  // some large prime

var (
	forwardHash [5001][5001] int
	reverseHash [5001][5001] int
	palindromeOfType [5001] int
	input string
	n int
)

func computeHash(accumulated int, val byte) int {
	return ((accumulated+1) * ((accumulated + int(val)) % 2) + int(val) - int('a')) % MOD
}

func getTypeOfPalindrome(start, length int) int {
	end := start + length - 1

	if length == 1 {
		return 1
	}
	if forwardHash[start][length] == reverseHash[end][length] {
		return 1 + getTypeOfPalindrome(start, length / 2)
	}
	return 0
}

func main()  {
	fmt.Scan(&input)
	n = len(input)

	for i := 0; i < n; i++ {
		var fhash, rhash = computeHash(0, byte(input[i])), computeHash(0, byte(input[i]))
		forwardHash[i][1] = fhash
		reverseHash[i][1] = rhash

		for j := 1; ; j++ {
			computedHashes := 0
			if i + j < n {
				fhash = computeHash(fhash, byte(input[i+j]))
				forwardHash[i][j + 1] = fhash
				computedHashes += 1
			}
			if i - j >= 0 {
				rhash = computeHash(rhash, byte(input[i-j]))
				reverseHash[i][j + 1] = rhash
				computedHashes += 1
			}
			if computedHashes == 0 {
				break
			}
		}
	}

	for i := 0; i < n; i++ {
		for j := 1; i + j <= n; j++ {
			palindromeOfType[getTypeOfPalindrome(i, j)]++
		}
	}

	for i := n; i >= 1; i-- {
		palindromeOfType[i - 1] += palindromeOfType[i]
	}

	for i := 1; i <= n; i++ {
		fmt.Printf("%d ", palindromeOfType[i])
	}
	fmt.Println()
}

package main

import "fmt"

const MOD  =  982451653  // some large prime

var (
	forwardHash [5001][5001] int
	reverseHash [5001][5001] int
	palindromeOfType [5001] int
	input string
	n int
)

func computeHash(accumulated int, val byte) int {
	return (accumulated % 17 + accumulated + (int(val) - int('a')) * 3) % MOD
}

func getTypeOfPalindrome(start, length int) int {
	end := start + length - 1

	if length == 1 {
		return 1
	}
	if forwardHash[start][length] == reverseHash[end][length] {
		return 1 + getTypeOfPalindrome(start, length / 2)
	}
	return 0
}

func main()  {
	fmt.Scan(&input)
	n = len(input)

	for i := 0; i < n; i++ {
		var fhash, rhash = computeHash(0, byte(input[i])), computeHash(0, byte(input[i]))
		forwardHash[i][1] = fhash
		reverseHash[i][1] = rhash

		for j := 1; ; j++ {
			computedHashes := 0
			if i + j < n {
				fhash = computeHash(fhash, byte(input[i+j]))
				forwardHash[i][j + 1] = fhash
				computedHashes += 1
			}
			if i - j >= 0 {
				rhash = computeHash(rhash, byte(input[i-j]))
				reverseHash[i][j + 1] = rhash
				computedHashes += 1
			}
			if computedHashes == 0 {
				break
			}
		}
	}

	for i := 0; i < n; i++ {
		for j := 1; i + j <= n; j++ {
			palindromeOfType[getTypeOfPalindrome(i, j)]++
		}
	}

	for i := n; i >= 1; i-- {
		palindromeOfType[i - 1] += palindromeOfType[i]
	}

	for i := 1; i <= n; i++ {
		fmt.Printf("%d ", palindromeOfType[i])
	}
	fmt.Println()
}

package main

import "fmt"

const MOD  =  982451653  // some large prime

var (
	forwardHash [5001][5001] int
	reverseHash [5001][5001] int
	palindromeOfType [5001] int
	input string
	n int
)

func computeHash(accumulated int, val byte) int {
	return (accumulated * ((accumulated + int(val)) % 2) + int(val) - int('a')) % MOD
}

func getTypeOfPalindrome(start, length int) int {
	end := start + length - 1

	if length == 1 {
		return 1
	}
	if forwardHash[start][length] == reverseHash[end][length] {
		return 1 + getTypeOfPalindrome(start, length / 2)
	}
	return 0
}

func main()  {
	fmt.Scan(&input)
	n = len(input)

	for i := 0; i < n; i++ {
		var fhash, rhash = computeHash(0, byte(input[i])), computeHash(0, byte(input[i]))
		forwardHash[i][1] = fhash
		reverseHash[i][1] = rhash

		for j := 1; ; j++ {
			computedHashes := 0
			if i + j < n {
				fhash = computeHash(fhash, byte(input[i+j]))
				forwardHash[i][j + 1] = fhash
				computedHashes += 1
			}
			if i - j >= 0 {
				rhash = computeHash(rhash, byte(input[i-j]))
				reverseHash[i][j + 1] = rhash
				computedHashes += 1
			}
			if computedHashes == 0 {
				break
			}
		}
	}

	for i := 0; i < n; i++ {
		for j := 1; i + j <= n; j++ {
			palindromeOfType[getTypeOfPalindrome(i, j)]++
		}
	}

	for i := n; i >= 1; i-- {
		palindromeOfType[i - 1] += palindromeOfType[i]
	}

	for i := 1; i <= n; i++ {
		fmt.Printf("%d ", palindromeOfType[i])
	}
	fmt.Println()
}

package main

import "fmt"

const MOD  =  13757927  // some large prime

var (
	forwardHash [5001][5001] int
	reverseHash [5001][5001] int
	palindromeOfType [5001] int
	input string
	n int
)

func computeHash(accumulated int, val byte) int {
	return (accumulated * 7 + (int(val) - int('a')) * 3) % MOD
}

func getTypeOfPalindrome(start, length int) int {
	end := start + length - 1

	if length == 1 {
		return 1
	}
	if forwardHash[start][length] == reverseHash[end][length] {
		return 1 + getTypeOfPalindrome(start, length / 2)
	}
	return 0
}

func main()  {
	fmt.Scan(&input)
	n = len(input)

	for i := 0; i < n; i++ {
		var fhash, rhash = computeHash(0, byte(input[i])), computeHash(0, byte(input[i]))
		forwardHash[i][1] = fhash
		reverseHash[i][1] = rhash

		for j := 1; ; j++ {
			computedHashes := 0
			if i + j < n {
				fhash = computeHash(fhash, byte(input[i+j]))
				forwardHash[i][j + 1] = fhash
				computedHashes += 1
			}
			if i - j >= 0 {
				rhash = computeHash(rhash, byte(input[i-j]))
				reverseHash[i][j + 1] = rhash
				computedHashes += 1
			}
			if computedHashes == 0 {
				break
			}
		}
	}

	for i := 0; i < n; i++ {
		for j := 1; i + j <= n; j++ {
			palindromeOfType[getTypeOfPalindrome(i, j)]++
		}
	}

	for i := n; i >= 1; i-- {
		palindromeOfType[i - 1] += palindromeOfType[i]
	}

	for i := 1; i <= n; i++ {
		fmt.Printf("%d ", palindromeOfType[i])
	}
	fmt.Println()
}