package main

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

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

func CF1709B(in io.Reader, out io.Writer) {
	r := bufio.NewReader(in)
	w := bufio.NewWriter(out)
	defer w.Flush()

	var n, m int64
	fmt.Fscan(r, &n, &m)
	arr := make([]int64, n)
	prefix, sufFix := make([]int64, n), make([]int64, n)
	for i := int64(0); i < n; i++ {
		fmt.Fscan(r, &arr[i])
		if i == 0 {
			continue
		}
		if arr[i] < arr[i-1] {
			prefix[i] = prefix[i-1] - arr[i] + arr[i-1]
			sufFix[i] = sufFix[i-1]
		} else {
			prefix[i] = prefix[i-1]
			sufFix[i] = sufFix[i-1] - arr[i-1] + arr[i]
		}
	}

	var start, end int64
	for i := int64(0); i < m; i++ {
		fmt.Fscan(r, &start, &end)
		if start < end {
			fmt.Fprintln(w, prefix[end-1]-prefix[start-1])
		} else {
			fmt.Fprintln(w, sufFix[start-1]-sufFix[end-1])
		}
	}
}

package main

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

func main() {
	var n, m int
	in := bufio.NewReader(os.Stdin)

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

	prefix := make([]int64, n+1)
	suffix := make([]int64, n+1)
	prefix[0] = 0
	suffix[n] = 0
	prev := int64(0)
	for i := 0; i < n; i++ {
		if a[i] >= prev {
			prefix[i+1] = prefix[i]
		} else {
			prefix[i+1] = prefix[i] + (prev - a[i])
		}
		prev = a[i]
	}
	prev = 0
	for i := n - 1; i > -1; i-- {
		if a[i] >= prev {
			suffix[i] = suffix[i+1]
		} else {
			suffix[i] = suffix[i+1] + (prev - a[i])
		}
		prev = a[i]
	}
	// fmt.Println(suffix)
	var s, t int
	for i := 0; i < m; i++ {
		fmt.Fscan(in, &s, &t)
		if s > t {
			fmt.Println(suffix[t-1] - suffix[s-1])
		} else {
			fmt.Println(prefix[t] - prefix[s])
		}
	}
}

package main

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

var (
	reader     = bufio.NewReader(os.Stdin)
	n, m, s, t int
)

func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

func main() {
	fmt.Fscan(reader, &n, &m)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &a[i])
	}

	var left = make([]int64, n)
	var right = make([]int64, n)

	for i := 1; i < n; i++ {
		left[i] = int64(left[i-1]) + int64(max(a[i-1]-a[i], 0))
	}

	for i := n - 2; i >= 0; i-- {
		right[i] = int64(right[i+1]) + int64(max(a[i+1]-a[i], 0))
	}

	for i := 0; i < m; i++ {
		fmt.Fscan(reader, &s, &t)
		if s < t {
			fmt.Println(left[t-1] - left[s-1])
		} else {
			fmt.Println(right[t-1] - right[s-1])
		}
	}
}

package main

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

type Quest struct {
	s int
	t int
}

var (
	reader = bufio.NewReader(os.Stdin)
	n, m   int
	// a      []int
	// q      []Quest
)

func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

func solve(n, m int, a []int, q []Quest) string {
	var left = make([]int64, n)
	var right = make([]int64, n)

	for i := 1; i < n; i++ {
		left[i] = int64(left[i-1]) + int64(max(a[i-1]-a[i], 0))
	}

	for i := n - 2; i >= 0; i-- {
		right[i] = int64(right[i+1]) + int64(max(a[i+1]-a[i], 0))
	}
	var result = make([]string, m)
	for i := 0; i < m; i++ {
		if q[i].s < q[i].t {
			result[i] = fmt.Sprint(left[q[i].t-1] - left[q[i].s-1])
		} else {
			result[i] = fmt.Sprint(right[q[i].t-1] - right[q[i].s-1])
		}
	}
	return strings.Join(result, "\n")
}

func main() {
	fmt.Fscan(reader, &n, &m)
	a := make([]int, n)
	q := make([]Quest, m)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &a[i])
	}
	for i := 0; i < m; i++ {
		fmt.Fscan(reader, &q[i].s, &q[i].t)
	}
	fmt.Println(solve(n, m, a, q))
}

package main

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

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

	var x = getIntln(reader)
	var n = x[0]
	var A = getIntln(reader)

	var previousBlock, currentBlock, fallDamage int64 = -1, -1, 0
	var ascendHealth, descendHealth = make([]int64, n), make([]int64, n)
	for i := int64(0); i < n; i++ {
		currentBlock = A[i]
		if currentBlock < previousBlock {
			fallDamage += previousBlock - currentBlock
		}
		ascendHealth[i] = fallDamage
		previousBlock = currentBlock
	}
	fallDamage = 0
	for i := n - 1; i >= 0; i-- {
		currentBlock = A[i]
		if currentBlock < previousBlock {
			fallDamage += previousBlock - currentBlock
		}
		descendHealth[i] = fallDamage
		previousBlock = currentBlock
	}

	for m := x[1]; m > 0; m-- {
		var st = getIntln(reader)
		var s, t = st[0], st[1]
		if s > t {
			writer.WriteString(strconv.FormatInt(descendHealth[t-1]-descendHealth[s-1], 10) + "\n")
		} else if s < t {
			writer.WriteString(strconv.FormatInt(ascendHealth[t-1]-ascendHealth[s-1], 10) + "\n")
		} else {
			writer.WriteString("0\n")
		}
	}
	writer.Flush()
}

func getString(in *bufio.Reader) string {
	res, _ := in.ReadString('\n')
	res = strings.ReplaceAll(res, "\n", "")
	res = strings.ReplaceAll(res, "\r", "")
	res = strings.TrimSpace(res)
	return res
}

func getIntln(in *bufio.Reader) []int64 {
	b := strings.Split(getString(in), " ")
	res := make([]int64, len(b))
	for k, v := range b {
		res[k], _ = strconv.ParseInt(v, 10, 64)
	}
	return res
}

package main

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

var rr = bufio.NewReader(os.Stdin)
var ww = bufio.NewWriter(os.Stdout)

func main() {
	defer ww.Flush()
	var n, m int
	fmt.Fscan(rr, &n, &m)
	nums := make([]int64, n)
	for i:=0;i<len(nums);i++ {
		fmt.Fscan(rr, &nums[i])
	}
	prefix := make([]int64, n+1)
	prefix[1] = 0
	for i:=1;i<len(nums);i++ {
		cur := max(0, nums[i-1] - nums[i])
		prefix[i+1] = prefix[i] + cur
	}
	lastfix := make([]int64, n+1)
	for i:=n-2;i>=0;i-- {
		cur := max(0, nums[i+1] - nums[i])
		lastfix[i] = lastfix[i+1] + cur
	}
	for i:=0;i<m;i++ {
		var a, b int64
		fmt.Fscan(rr, &a, &b)
		if a < b {
			fmt.Fprintln(ww, prefix[b] - prefix[a])
		} else {
			fmt.Fprintln(ww, lastfix[b-1] - lastfix[a-1])
		}
	}
}

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

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var n, m int
	fmt.Fscan(in, &n, &m)
	nums := make([]int64, n+2)
	lsum := make([]int64, n+2)
	rsum := make([]int64, n+2)
	for i := 1; i <= n; i++ {
		fmt.Fscan(in, &nums[i])
	}
	for i := 2; i <= n; i++ {
		if nums[i-1] > nums[i] {
			lsum[i] = lsum[i-1] + nums[i-1] - nums[i]
		} else {
			lsum[i] = lsum[i-1]
		}
	}
	for i := n - 1; i > 0; i-- {
		if nums[i+1] > nums[i] {
			rsum[i] = rsum[i+1] + nums[i+1] - nums[i]
		} else {
			rsum[i] = rsum[i+1]
		}
	}
	var t, s int
	for i := 0; i < m; i++ {
		fmt.Fscan(in, &s, &t)
		if s < t {
			fmt.Fprintln(out, lsum[t]-lsum[s])
		} else {
			fmt.Fprintln(out, rsum[t]-rsum[s])
		}
	}
}

// 00
package main

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

func main() {
	var n, m, p, q int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &n, &m)
	h := make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &h[i])
	}
	da := make([]int64, n)
	dd := make([]int64, n)
	for i := 0; i < n-1; i++ {
		if h[i] > h[i+1] {
			da[i+1] = h[i] - h[i+1]
		}
		da[i+1] += da[i]
	}
	for i := n - 1; i > 0; i-- {
		if h[i] > h[i-1] {
			dd[i-1] = h[i] - h[i-1]
		}
		dd[i-1] += dd[i]
	}
	for i := 0; i < m; i++ {
		damage := int64(0)
		fmt.Fscan(reader, &p, &q)
		if p > q {
			damage = dd[q-1] - dd[p-1]
		} else {
			damage = da[q-1] - da[p-1]
		}

		fmt.Println(damage)
	}
}

package main

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

const cf = true

func solve() {
	var n, m int
	fmt.Fscan(in, &n, &m)
	arr := make([]int64, n)
	for i := range arr {
		fmt.Fscan(in, &arr[i])
	}
	pref := make([]int64, n)
	prefR := make([]int64, n)
	for i := 1; i < n; i++ {
		pref[i] = pref[i-1]
		if arr[i] < arr[i-1] {
			pref[i] += arr[i-1] - arr[i]
		}
	}
	for i := n - 2; i >= 0; i-- {
		prefR[i] = prefR[i+1]
		if arr[i] < arr[i+1] {
			prefR[i] += arr[i+1] - arr[i]
		}
	}
	var s, t int
	for i := 0; i < m; i++ {
		fmt.Fscan(in, &s, &t)
		s--
		t--
		if t > s {
			fmt.Fprintln(out, pref[t]-pref[s])
		} else {
			fmt.Fprintln(out, prefR[t]-prefR[s])
		}
	}
}

func main() {
	defer out.Flush()
	t := 1
	//fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		solve()
	}
}

func ConvertInt(val string, base, toBase int) (string, error) {
	i, err := strconv.ParseInt(val, base, 64)
	if err != nil {
		return "", err
	}
	return strconv.FormatInt(i, toBase), nil
}

type set[T Ordered] struct {
	itms map[T]struct{}
}

func (s *set[T]) init() {
	if s.itms == nil {
		s.itms = make(map[T]struct{})
	}
}

func (s *set[T]) add(item T) *set[T] {
	s.init()
	s.itms[item] = exists
	return s
}

func (s *set[T]) remove(item T) *set[T] {
	s.init()
	delete(s.itms, item)
	return s
}

func (s *set[T]) has(item T) bool {
	s.init()
	_, c := s.itms[item]
	return c
}

func (s *set[T]) sortedItems() []T {
	s.init()
	items := make([]T, 0, s.size())
	for item := range s.itms {
		items = append(items, item)
	}
	sort.Slice(items, func(i, j int) bool {
		return items[i] < items[j]
	})
	return items
}

func (s *set[T]) items() []T {
	s.init()
	items := make([]T, 0, s.size())
	for item := range s.itms {
		items = append(items, item)
	}
	return items
}

func (s *set[T]) size() int {
	s.init()
	return len(s.itms)
}

var exists = struct{}{}

const MaxUint = ^uint(0)
const MinUint = 0
const MaxInt = int(MaxUint >> 1)
const MinInt = -MaxInt - 1

func reverseStr(s string) string {
	r := []rune(s)
	for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
		r[i], r[j] = r[j], r[i]
	}
	return string(r)
}

type Float interface {
	~float32 | ~float64
}

type Integer interface {
	Signed | Unsigned
}

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

type Ordered interface {
	Integer | Float | ~string
}

func abs[T Signed](x T) T {
	if x < 0 {
		return -x
	}
	return x
}

func min[T Ordered](x, y T) T {
	if x < y {
		return x
	}
	return y
}

func max[T Ordered](x, y T) T {
	if x > y {
		return x
	}
	return y
}

var (
	in  = getReader()
	out = getWriter()
)

func getReader() *bufio.Reader {
	if cf {
		return bufio.NewReader(os.Stdin)
	} else {
		in, _ := os.Open("input.txt")
		return bufio.NewReader(in)
	}
}

func getWriter() *bufio.Writer {
	if cf {
		return bufio.NewWriter(os.Stdout)
	} else {
		out, _ := os.Create("output.txt")
		return bufio.NewWriter(out)
	}
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)

	var buf bytes.Buffer

	n, q := readTwoNums(reader)

	A := readNNums(reader, n)
	quests := make([][]int, q)

	for i := 0; i < q; i++ {
		quests[i] = readNNums(reader, 2)
	}

	res := solve(A, quests)

	for i := 0; i < q; i++ {
		buf.WriteString(fmt.Sprintf("%d\n", res[i]))
	}

	fmt.Print(buf.String())
}

func readString(reader *bufio.Reader) string {
	s, _ := reader.ReadString('\n')
	for i := 0; i < len(s); i++ {
		if s[i] == '\n' || s[i] == '\r' {
			return s[:i]
		}
	}
	return s
}

func normalize(s string) string {

	for i := len(s); i > 0; i-- {
		if s[i-1] >= 'a' && s[i-1] <= 'z' {
			return s[:i]
		}
	}
	return ""
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func solve(A []int, quests [][]int) []int64 {
	n := len(A)
	dp := make([]int64, n+1)
	for i := 1; i < n; i++ {
		dp[i+1] = dp[i] + int64(max(0, A[i-1]-A[i]))
	}

	fp := make([]int64, n+1)
	for i := n - 2; i >= 0; i-- {
		fp[i] = fp[i+1] + int64(max(0, A[i+1]-A[i]))
	}

	res := make([]int64, len(quests))

	for i, quest := range quests {
		s, t := quest[0], quest[1]
		if s == t {
			res[i] = 0
			continue
		}
		if s < t {
			res[i] = dp[t] - dp[s]
		} else {
			res[i] = fp[t-1] - fp[s-1]
		}
	}

	return res
}

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

package main

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

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

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

	aa := make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &aa[i])
	}
	bb := make([]int64, n)
	for i := 1; i < n; i++ {
		bb[i] = bb[i-1]
		if aa[i] < aa[i-1] {
			bb[i] += aa[i-1] - aa[i]
		}
	}
	cc := make([]int64, n)
	for i := n - 2; i >= 0; i-- {
		cc[i] = cc[i+1]
		if aa[i] < aa[i+1] {
			cc[i] += aa[i+1] - aa[i]
		}
	}

	for i := 0; i < m; i++ {
		var s, t int
		fmt.Fscan(in, &s, &t)
		if s < t {
			fmt.Fprintln(out, bb[t-1]-bb[s-1])
		} else {
			fmt.Fprintln(out, cc[t-1]-cc[s-1])
		}
	}
}

package main

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

func main() {
	inp := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)

	var n, m int
	fmt.Fscanln(inp, &n, &m)

	a := make([]int, n)
	for i := 0; i < n; i++ {
		if i == n-1 {
			fmt.Fscanln(inp, &a[i])
		} else {
			fmt.Fscan(inp, &a[i])
		}
	}

	b := make([]int64, n)
	for i := 1; i < n; i++ {
		if a[i-1] > a[i] {
			b[i] = int64(a[i-1] - a[i])
		}
	}
	for i := 1; i < n; i++ {
		b[i] += b[i-1]
	}

	c := make([]int64, n)
	for i := 1; i < n; i++ {
		if a[n-i] > a[n-i-1] {
			c[i] = int64(a[n-i] - a[n-i-1])
		}
	}
	for i := 1; i < n; i++ {
		c[i] += c[i-1]
	}

	for i := 0; i < m; i++ {
		var s, t int
		fmt.Fscanln(inp, &s, &t)
		s--
		t--
		if s < t {
			fmt.Fprintln(out, b[t]-b[s])
		} else {
			s = n - s - 1
			t = n - t - 1
			fmt.Fprintln(out, c[t]-c[s])
		}
	}

	out.Flush()
}

package main

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

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

func b1709(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n int
	var m int
	var x int
	var s int
	var e int

	Fscan(in, &n)
	Fscan(in, &m)

	a := []int{}
	for i := 0; i < n; i++ {
		Fscan(in, &x)
		a = append(a, x)
	}
	dl := make([]int64, n)
	dr := make([]int64, n)
	sl, sr := int64(0), int64(0)
	for i, _ := range a {
		if i == 0 {
			continue
		}
		if a[i] < a[i-1] {
			sl += int64(a[i-1] - a[i])
		}
		dl[i] = sl
	}
	for i := n - 2; i >= 0; i-- {
		if a[i] < a[i+1] {
			sr += int64(a[i+1] - a[i])
		}
		dr[i] = sr
	}

	for l := 0; l < m; l++ {
		Fscan(in, &s, &e)
		s--
		e--
		ans := dl[e] -dl[s]
		if e < s {
			ans = dr[e] -dr[s]
		}
		Fprintln(out, ans)
	}
}

package main

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

var _, _, _ = main, rw, do

func main() {
	r, w := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer w.Flush()
	rw(r, w)
}

func rw(in io.Reader, out io.Writer) {
	var t int = 1
	// fmt.Fscan(in, &t)
	for ti := 0; ti < t; ti++ {
		var n, m int
		fmt.Fscan(in, &n, &m)
		b := make([]int, n)
		for i := range b {
			fmt.Fscan(in, &b[i])
		}
		c := make([][2]int, m)
		for i := range c {
			fmt.Fscan(in, &c[i][0], &c[i][1])
		}
		w1 := do(n, m, b, c)
		for _, v := range w1 {
			fmt.Fprintln(out, v)
		}
	}
}

func do(n, m int, b []int, c [][2]int) []int64 {
	s1 := make([]int64, n)
	s2 := make([]int64, n)
	for i := 1; i < n; i++ {
		v := b[i-1] - b[i]
		if v < 0 {
			v = 0
		}
		s1[i] += s1[i-1] + int64(v)
	}
	for i := n - 2; i >= 0; i-- {
		v := b[i+1] - b[i]
		if v < 0 {
			v = 0
		}
		s2[i] += s2[i+1] + int64(v)
	}
	ans := make([]int64, m)
	for i, v := range c {
		x := v[0] - 1
		y := v[1] - 1
		s := s1
		if x > y {
			s = s2
		}
		ans[i] = s[y] - s[x]
	}
	return ans
}

package main

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

func A1() {
	_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, m int64

		Fscan(in, &n, &m)
		a := make([]int64, n+1)
		up := make([]int64, n+1)
		down := make([]int64, n+1)
		for i := 1; i <= int(n); i++ {
			Fscan(in, &a[i])
		}
		for i := 2; i <= int(n); i++ {
			up[i] = up[i-1]
			down[i] = down[i-1]
			if a[i] > a[i-1] {
				up[i] += (a[i] - a[i-1])
			} else if a[i] < a[i-1] {
				down[i] += (a[i-1] - a[i])
			} else {

			}
		}
		for m > 0 {
			m--
			var x, y int
			Fscan(in, &x, &y)
			if x < y {
				Fprintln(out, down[y]-down[x])
			} else {
				Fprintln(out, up[x]-up[y])
			}
		}
	}
	// Coding...ove!
	for i := 0; i < cas; i++ {
		sol()
	}
}
func main() { A1() }

package main
import (
	"fmt"
	"bufio"
	"os"
)

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

	fmt.Fscan(reader, &size)
	fmt.Fscan(reader, &test)

	height := make([]int64, size)
	forward := make([]int64, size)
	backward := make([]int64, size)
	quest := make([][2]int64, test)

	for i := range height {
		fmt.Fscan(reader, &height[i])
	}

	var sum int64
	for i := int64(1); i != size; i++ {
		if height[i-1] > height[i] {
			sum += height[i-1] - height[i]
		}
		forward[i] = sum
	}

	sum = 0
	for i := int64(size-1); i != 0; i-- {
		if height[i] > height[i-1] {
			sum += height[i] - height[i-1]
		}
		backward[i-1] = sum
	}

	for i := range quest {
		fmt.Fscan(reader, &quest[i][0], &quest[i][1])
	}

	for i := range quest {
		sum = 0
		if quest[i][0] < quest[i][1] {
			sum += forward[quest[i][1]-1] - forward[quest[i][0]-1]
		} else {
			sum += backward[quest[i][1]-1] - backward[quest[i][0]-1]
		}
		fmt.Fprintln(writer, sum)
	}
}

package main

import (
	"bufio"
	"os"
	"strconv"
)

func solve() {
	n, m := get(), get()
	a := geta(n)
	pl := make([]int64, n)
	for i := 1; i < n; i++ {
		pl[i] = pl[i-1] + int64(max(a[i-1]-a[i], 0))
	}
	pr := make([]int64, n)
	for i := n - 2; i >= 0; i-- {
		pr[i] = pr[i+1] + int64(max(a[i+1]-a[i], 0))
	}

	var s, t int
	for ; m > 0; m-- {
		s, t = get(), get()
		s--
		t--
		if s > t {
			put64ln(pr[t] - pr[s])
		} else {
			put64ln(pl[t] - pl[s])
		}
	}
}

const (
	once       = true
	maxBufSize = 0 // 10_000_000
)

var (
	in  *bufio.Scanner
	out *bufio.Writer
)

func main() {
	in = bufio.NewScanner(bufio.NewReader(os.Stdin))
	in.Split(bufio.ScanWords)
	if maxBufSize > 0 {
		in.Buffer(make([]byte, maxBufSize), maxBufSize)
	}

	out = bufio.NewWriter(os.Stdout)
	defer out.Flush()

	if once {
		solve()
		return
	}

	for t := get(); t > 0; t-- {
		solve()
	}
}

func geta(n int) []int {
	a := make([]int, n)
	for i := range a {
		a[i] = get()
	}
	return a
}

func get() int {
	x, _ := strconv.Atoi(gets())
	return x
}

func geti64() int64 {
	x, _ := strconv.ParseInt(gets(), 10, 64)
	return x
}

func gets() string {
	scan()
	return in.Text()
}

func scan() {
	in.Scan()
}

func putaln(a []int) {
	for _, v := range a {
		put(v)
		putc(' ')
	}
	putc('\n')
}

func put64(x int64) {
	out.WriteString(strconv.FormatInt(x, 10))
}

func put(x int) {
	out.WriteString(strconv.Itoa(x))
}

func put64ln(x int64) {
	put64(x)
	putc('\n')
}

func putln(x int) {
	put(x)
	putc('\n')
}

func putc(c rune) {
	out.WriteRune(c)
}

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

package main

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

func ReadLine(reader *bufio.Reader) string {
	str, _, err := reader.ReadLine()
	if err == io.EOF {
		return ""
	}
	return strings.TrimRight(string(str), "\r\n")
}

func ReadLineArray(reader *bufio.Reader) []string {
	return strings.Split(strings.TrimSpace(ReadLine(reader)), " ")
}

func ToNumberArray(array []string) []int {
	result := make([]int, len(array))
	for i := 0; i < len(array); i++ {
		number, _ := strconv.Atoi(array[i])
		result[i] = number
	}
	return result
}

func main() {
	reader := bufio.NewReaderSize(os.Stdin, 16*1024*1024)
	firstLine := ToNumberArray(ReadLineArray(reader))
	size, times := firstLine[0], firstLine[1]

	array := ToNumberArray(ReadLineArray(reader))

	frontToEnd := make([]int64, size)
	endToFront := make([]int64, size)

	for i := 1; i < size; i++ {
		difference := array[i-1] - array[i]
		frontToEnd[i] = frontToEnd[i-1]
		if difference > 0 {
			frontToEnd[i] += int64(difference)
		}
	}

	for i := size - 1; i > 0; i-- {
		difference := array[i] - array[i-1]
		endToFront[i-1] = endToFront[i]
		if difference > 0 {
			endToFront[i-1] += int64(difference)
		}
	}

	for t := 0; t < times; t++ {
		line := ToNumberArray(ReadLineArray(reader))
		front, end := line[0], line[1]
		if front > end {
			fmt.Println(endToFront[end-1] - endToFront[front-1])
		} else {
			fmt.Println(frontToEnd[end-1] - frontToEnd[front-1])
		}
	}
}

// You can edit this code!
// Click here and start typing.
package main

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

var temp1 [100004]int64
var temp2 [100004]int64
var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }

func main() {
	defer writer.Flush()
	var n, m, s, t int
	var a [100004]int64
	scanf("%d %d\n", &n, &m)
	for i := 1; i <= n; i++ {
		scanf("%d", &a[i])
	}
	temp1[1] = 0
	temp2[n] = 0
	for i := 2; i <= n; i++ {
		if a[i] <= a[i-1] {
			temp1[i] = temp1[i-1] + a[i-1] - a[i]
		} else {
			temp1[i] = temp1[i-1]
		}
		if a[n-i+1] <= a[n-i+2] {
			temp2[n-i+1] = temp2[n-i+2] + a[n-i+2] - a[n-i+1]
		} else {
			temp2[n-i+1] = temp2[n-i+2]
		}
	}
	for i := 1; i <= m; i++ {
		scanf("\n%d %d", &s, &t)
		if s > t {
			printf("%v\n", temp2[t]-temp2[s])
		} else {
			printf("%v\n", temp1[t]-temp1[s])
		}
	}

}

package main

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

func main() {
	scanner := bufio.NewReader(os.Stdin)
	printer := bufio.NewWriter(os.Stdout)
	defer printer.Flush()
	var n, m int
	fmt.Fscan(scanner, &n, &m)
	slice := make([]int64, n)
	for iter := 0; iter < n; iter++ {
		fmt.Fscan(scanner, &slice[iter])
	}
	ffirsttox := make([]int64, n)
	bxtolast := make([]int64, n)
	var ans int64 = 0
	for iter := 0; iter < n; iter++ {
		ffirsttox[iter] = ans
		if (iter != n-1) && (slice[iter+1] < slice[iter]) {
			ans += slice[iter] - slice[iter+1]
		}
	}
	ans = 0
	for iter := n - 1; iter >= 0; iter-- {
		bxtolast[iter] = ans
		if (iter != 0) && (slice[iter-1] < slice[iter]) {
			ans += slice[iter] - slice[iter-1]
		}
	}
	for i := 0; i < m; i++ {
		var a, b int
		fmt.Fscan(scanner, &a, &b)
		a--
		b--
		if a < b {
			fmt.Fprintln(printer, ffirsttox[b]-ffirsttox[a])
		} else {
			fmt.Fprintln(printer, bxtolast[b]-bxtolast[a])
		}
	}
}

package main

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

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

func CF1709B(in io.Reader, out io.Writer) {
	r := bufio.NewReader(in)
	w := bufio.NewWriter(out)
	defer w.Flush()

	var n, m int
	fmt.Fscan(r, &n, &m)
	arr := make([]int, n)
	prefix, sufFix := make([]int, n), make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(r, &arr[i])
		if i == 0 {
			continue
		}
		if arr[i] < arr[i-1] {
			prefix[i] = prefix[i-1] - arr[i] + arr[i-1]
			sufFix[i] = sufFix[i-1]
		} else {
			prefix[i] = prefix[i-1]
			sufFix[i] = sufFix[i-1] - arr[i-1] + arr[i]
		}
	}

	var start, end int
	for i := 0; i < m; i++ {
		fmt.Fscan(r, &start, &end)
		if start < end {
			fmt.Fprintln(w, prefix[end-1]-prefix[start-1])
		} else {
			fmt.Fprintln(w, sufFix[start-1]-sufFix[end-1])
		}
	}
}

package main
import (
	"fmt"
	"bufio"
	"os"
)

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

	fmt.Fscan(reader, &size)
	fmt.Fscan(reader, &test)

	height := make([]int, size)
	forward := make([]int, size)
	backward := make([]int, size)
	quest := make([][2]int, test)

	for i := range height {
		fmt.Fscan(reader, &height[i])
	}

	sum := 0
	for i := 1; i != size; i++ {
		if height[i-1] > height[i] {
			sum += height[i-1] - height[i]
		}
		forward[i] = sum
	}

	sum = 0
	for i := size-1; i != 0; i-- {
		if height[i] > height[i-1] {
			sum += height[i] - height[i-1]
		}
		backward[i-1] = sum
	}

	for i := range quest {
		fmt.Fscan(reader, &quest[i][0], &quest[i][1])
	}

	for i := range quest {
		sum = 0
		if quest[i][0] < quest[i][1] {
			sum += forward[quest[i][1]-1] - forward[quest[i][0]-1]
		} else {
			sum += backward[quest[i][1]-1] - backward[quest[i][0]-1]
		}
		fmt.Fprintln(writer, sum)
	}
}

package main

import (
	"bufio"
	"os"
	"strconv"
)

func solve() {
	n, m := get(), get()
	a := geta(n)
	pl := make([]int, n)
	for i := 1; i < n; i++ {
		pl[i] = pl[i-1] + max(a[i-1]-a[i], 0)
	}
	pr := make([]int, n)
	for i := n - 2; i >= 0; i-- {
		pr[i] = pr[i+1] + max(a[i+1]-a[i], 0)
	}

	var s, t int
	for ; m > 0; m-- {
		s, t = get(), get()
		s--
		t--
		if s > t {
			putln(pr[t] - pr[s])
		} else {
			putln(pl[t] - pl[s])
		}
	}
}

const (
	once       = true
	maxBufSize = 0 // 10_000_000
)

var (
	in  *bufio.Scanner
	out *bufio.Writer
)

func main() {
	in = bufio.NewScanner(bufio.NewReader(os.Stdin))
	in.Split(bufio.ScanWords)
	if maxBufSize > 0 {
		in.Buffer(make([]byte, maxBufSize), maxBufSize)
	}

	out = bufio.NewWriter(os.Stdout)
	defer out.Flush()

	if once {
		solve()
		return
	}

	for t := get(); t > 0; t-- {
		solve()
	}
}

func geta(n int) []int {
	a := make([]int, n)
	for i := range a {
		a[i] = get()
	}
	return a
}

func get() int {
	x, _ := strconv.Atoi(gets())
	return x
}

func gets() string {
	scan()
	return in.Text()
}

func scan() {
	in.Scan()
}

func putaln(a []int) {
	for _, v := range a {
		put(v)
		putc(' ')
	}
	putc('\n')
}

func put(x int) {
	out.WriteString(strconv.Itoa(x))
}

func putln(x int) {
	put(x)
	putc('\n')
}

func putc(c rune) {
	out.WriteRune(c)
}

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

package main

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

func ReadLine(reader *bufio.Reader) string {
	str, _, err := reader.ReadLine()
	if err == io.EOF {
		return ""
	}
	return strings.TrimRight(string(str), "\r\n")
}

func ReadLineArray(reader *bufio.Reader) []string {
	return strings.Split(strings.TrimSpace(ReadLine(reader)), " ")
}

func ToNumberArray(array []string) []int {
	result := make([]int, len(array))
	for i := 0; i < len(array); i++ {
		number, _ := strconv.Atoi(array[i])
		result[i] = number
	}
	return result
}

func main() {
	reader := bufio.NewReaderSize(os.Stdin, 16*1024*1024)
	firstLine := ToNumberArray(ReadLineArray(reader))
	size, times := firstLine[0], firstLine[1]

	array := ToNumberArray(ReadLineArray(reader))

	frontToEnd := make([]int, size)
	endToFront := make([]int, size)

	for i := 1; i < size; i++ {
		difference := array[i-1] - array[i]
		frontToEnd[i] = frontToEnd[i-1]
		if difference > 0 {
			frontToEnd[i] += difference
		}
	}

	for i := size - 1; i > 0; i-- {
		difference := array[i] - array[i-1]
		endToFront[i-1] = endToFront[i]
		if difference > 0 {
			endToFront[i-1] += difference
		}
	}

	for t := 0; t < times; t++ {
		line := ToNumberArray(ReadLineArray(reader))
		front, end := line[0], line[1]
		if front > end {
			fmt.Println(endToFront[end-1] - endToFront[front-1])
		} else {
			fmt.Println(frontToEnd[end-1] - frontToEnd[front-1])
		}
	}
}

// You can edit this code!
// Click here and start typing.
package main

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

var temp1 [100004]int64
var temp2 [100004]int64
var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }

func main() {
	defer writer.Flush()
	var n, m, s, t int
	var a [100004]int64
	scanf("%d %d\n", &n, &m)
	for i := 1; i <= n; i++ {
		scanf("%ld", &a[i])
	}
	temp1[1] = 0
	temp2[n] = 0
	for i := 2; i <= n; i++ {
		if a[i] <= a[i-1] {
			temp1[i] = temp1[i-1] + a[i-1] - a[i]
		} else {
			temp1[i] = temp1[i-1]
		}
		if a[n-i+1] <= a[n-i+2] {
			temp2[n-i+1] = temp2[n-i+2] + a[n-i+2] - a[n-i+1]
		} else {
			temp2[n-i+1] = temp2[n-i+2]
		}
	}
	for i := 1; i <= m; i++ {
		scanf("\n%d %d", &s, &t)
		if s > t {
			printf("%ld\n", temp2[t]-temp2[s])
		} else {
			printf("%ld\n", temp1[t]-temp1[s])
		}
	}

}

// You can edit this code!
// Click here and start typing.
package main

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

var temp1 [100004]int
var temp2 [100004]int
var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }

func main() {
	defer writer.Flush()
	var n, m, s, t int
	var a [100004]int
	scanf("%d %d\n", &n, &m)
	for i := 1; i <= n; i++ {
		scanf("%d", &a[i])
	}
	temp1[1] = 0
	temp2[n] = 0
	for i := 2; i <= n; i++ {
		if a[i] <= a[i-1] {
			temp1[i] = temp1[i-1] + a[i-1] - a[i]
		} else {
			temp1[i] = temp1[i-1]
		}
		if a[n-i+1] <= a[n-i+2] {
			temp2[n-i+1] = temp2[n-i+2] + a[n-i+2] - a[n-i+1]
		} else {
			temp2[n-i+1] = temp2[n-i+2]
		}
	}
	for i := 1; i <= m; i++ {
		scanf("\n%d %d", &s, &t)
		if s > t {
			printf("%d\n", temp2[t]-temp2[s])
		} else {
			printf("%d\n", temp1[t]-temp1[s])
		}
	}

}

package main

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

func main() {
	scanner := bufio.NewReader(os.Stdin)
	printer := bufio.NewWriter(os.Stdout)
	defer printer.Flush()
	var n, m int
	fmt.Fscan(scanner, &n, &m)
	slice := make([]int, n)
	for iter := 0; iter < n; iter++ {
		fmt.Fscan(scanner, &slice[iter])
	}
	ffirsttox := make([]int, n)
	bfirsttox := make([]int, n)
	fxtolast := make([]int, n)
	bxtolast := make([]int, n)
	ans := 0
	for iter := 0; iter < n; iter++ {
		ffirsttox[iter] = ans
		if (iter != n-1) && (slice[iter+1] < slice[iter]) {
			ans += slice[iter] - slice[iter+1]
		}
	}
	ans = 0
	for iter := n - 1; iter > 0; iter-- {
		fxtolast[iter] = ans
		if (iter != 0) && (slice[iter-1] > slice[iter]) {
			ans += slice[iter-1] - slice[iter]
		}
	}
	ans = 0
	for iter := n - 1; iter >= 0; iter-- {
		bxtolast[iter] = ans
		if (iter != 0) && (slice[iter-1] < slice[iter]) {
			ans += slice[iter] - slice[iter-1]
		}
	}
	ans = 0
	for iter := 0; iter < n; iter++ {
		bfirsttox[iter] = ans
		if (iter != n-1) && (slice[iter+1] > slice[iter]) {
			ans += slice[iter+1] - slice[iter]
		}
	}
	for i := 0; i < m; i++ {
		var a, b int
		fmt.Fscan(scanner, &a, &b)
		a--
		b--
		if a < b {
			fmt.Fprintln(printer, ffirsttox[b]-ffirsttox[a])
		} else {
			fmt.Fprintln(printer, bxtolast[b]-bxtolast[a])
		}
	}
}

package main

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

func main() {
	scanner := bufio.NewReader(os.Stdin)
	printer := bufio.NewWriter(os.Stdout)
	defer printer.Flush()
	var n, m int
	fmt.Fscan(scanner, &n, &m)
	slice := make([]int, n)
	for iter := 0; iter < n; iter++ {
		fmt.Fscan(scanner, &slice[iter])
	}
	ffirsttox := make([]int, n)
	bfirsttox := make([]int, n)
	fxtolast := make([]int, n)
	bxtolast := make([]int, n)
	ans := 0
	for iter := 0; iter < n; iter++ {
		ffirsttox[iter] = ans
		if (iter != n-1) && (slice[iter+1] < slice[iter]) {
			ans += slice[iter] - slice[iter+1]
		}
	}
	ans = 0
	for iter := n - 1; iter > 0; iter-- {
		fxtolast[iter] = ans
		if (iter != 0) && (slice[iter-1] > slice[iter]) {
			ans += slice[iter-1] - slice[iter]
		}
	}
	ans = 0
	for iter := n - 1; iter >= 0; iter-- {
		bxtolast[iter] = ans
		if (iter != 0) && (slice[iter-1] < slice[iter]) {
			ans += slice[iter] - slice[iter-1]
		}
	}
	ans = 0
	for iter := 0; iter < n; iter++ {
		bfirsttox[iter] = ans
		if (iter != n-1) && (slice[iter+1] > slice[iter]) {
			ans += slice[iter+1] - slice[iter]
		}
	}
	for i := 0; i < n; i++ {
		fmt.Fprintln(printer, "!!!", ffirsttox[i], bxtolast[i])
	}
	for i := 0; i < m; i++ {
		var a, b int
		fmt.Fscan(scanner, &a, &b)
		a--
		b--
		if a < b {
			fmt.Fprintln(printer, ffirsttox[b]-ffirsttox[a])
		} else {
			fmt.Fprintln(printer, bxtolast[b]-bxtolast[a])
		}
	}
}

package main

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

func main() {
	var n, m int
	in := bufio.NewReader(os.Stdin)

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

	prefix := make([]int, n+1)
	suffix := make([]int, n+1)
	prefix[0] = 0
	suffix[n] = 0
	prev := 0
	for i := 0; i < n; i++ {
		if a[i] >= prev {
			prefix[i+1] = prefix[i]
		} else {
			prefix[i+1] = prefix[i] + (prev - a[i])
		}
		prev = a[i]
	}
	prev = 0
	for i := n - 1; i > -1; i-- {
		if a[i] >= prev {
			suffix[i] = suffix[i+1]
		} else {
			suffix[i] = suffix[i+1] + (prev - a[i])
		}
		prev = a[i]
	}
	// fmt.Println(suffix)
	var s, t int
	for i := 0; i < m; i++ {
		fmt.Fscan(in, &s, &t)
		if s > t {
			fmt.Println(suffix[t-1] - suffix[s-1])
		} else {
			fmt.Println(prefix[t] - prefix[s])
		}
	}
}

package main

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

type Quest struct {
	s int
	t int
}

var (
	reader = bufio.NewReader(os.Stdin)
	n, m   int
	// a      []int
	// q      []Quest
)

func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

func solve(n, m int, a []int, q []Quest) string {
	var left = make([]int64, n)
	var right = make([]int64, n)

	for i := 1; i < n; i++ {
		left[i] = int64(left[i-1]) + int64(max(a[i-1]-a[i], 0))
	}

	for i := n - 2; i >= 0; i-- {
		right[i] = int64(right[i+1]) + int64(max(a[i+1]-a[i], 0))
	}
	fmt.Println(left, right)
	var result = make([]string, m)
	for i := 0; i < m; i++ {
		if q[i].s < q[i].t {
			result[i] = fmt.Sprint(left[q[i].t-1] - left[q[i].s-1])
		} else {
			result[i] = fmt.Sprint(right[q[i].t-1] - right[q[i].s-1])
		}
	}
	return strings.Join(result, "\n")
}

func main() {
	fmt.Fscan(reader, &n, &m)
	a := make([]int, n)
	q := make([]Quest, m)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &a[i])
	}
	for i := 0; i < m; i++ {
		fmt.Fscan(reader, &q[i].s, &q[i].t)
	}
	fmt.Println(solve(n, m, a, q))
}

package main

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

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

	var x = getIntln(reader)
	var n = x[0]
	var A = getIntln(reader)

	var previousBlock, currentBlock, fallDamage = -1, -1, 0
	var ascendHealth, descendHealth = make([]int, n), make([]int, n)
	for i := 0; i < n; i++ {
		currentBlock = A[i]
		if currentBlock < previousBlock {
			fallDamage += previousBlock - currentBlock
		}
		ascendHealth[i] = fallDamage
		previousBlock = currentBlock
	}
	fallDamage = 0
	for i := n - 1; i >= 0; i-- {
		currentBlock = A[i]
		if currentBlock < previousBlock {
			fallDamage += previousBlock - currentBlock
		}
		descendHealth[i] = fallDamage
		previousBlock = currentBlock
	}

	for m := x[1]; m > 0; m-- {
		var st = getIntln(reader)
		var s, t = st[0], st[1]
		if s > t {
			writer.WriteString(strconv.Itoa(descendHealth[t-1]-descendHealth[s-1]) + "\n")
		} else if s < t {
			writer.WriteString(strconv.Itoa(ascendHealth[t-1]-ascendHealth[s-1]) + "\n")
		} else {
			writer.WriteString("0\n")
		}
	}
	writer.Flush()
}

func getString(in *bufio.Reader) string {
	res, _ := in.ReadString('\n')
	res = strings.ReplaceAll(res, "\n", "")
	res = strings.ReplaceAll(res, "\r", "")
	res = strings.TrimSpace(res)
	return res
}

func getIntln(in *bufio.Reader) []int {
	b := strings.Split(getString(in), " ")
	res := make([]int, len(b))
	for k, v := range b {
		res[k], _ = strconv.Atoi(v)
	}
	return res
}

package main

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

var rr = bufio.NewReader(os.Stdin)
var ww = bufio.NewWriter(os.Stdout)

func main() {
	defer ww.Flush()
	var n, m int
	fmt.Fscan(rr, &n, &m)
	nums := make([]int, n)
	for i:=0;i<len(nums);i++ {
		fmt.Fscan(rr, &nums[i])
	}
	prefix := make([]int, n+1)
	prefix[1] = 0
	for i:=1;i<len(nums);i++ {
		cur := max(0, nums[i-1] - nums[i])
		prefix[i+1] = prefix[i] + cur
	}
	lastfix := make([]int, n+1)
	for i:=n-2;i>=0;i-- {
		cur := max(0, nums[i+1] - nums[i])
		lastfix[i] = lastfix[i+1] + cur
	}
	for i:=0;i<m;i++ {
		var a, b int
		fmt.Fscan(rr, &a, &b)
		if a < b {
			fmt.Fprintln(ww, prefix[b] - prefix[a])
		} else {
			fmt.Fprintln(ww, lastfix[b-1] - lastfix[a-1])
		}
	}
}

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

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var n, m int
	fmt.Fscan(in, &n, &m)
	nums := make([]int, n+2)
	lsum := make([]int, n+2)
	rsum := make([]int, n+2)
	for i := 1; i <= n; i++ {
		fmt.Fscan(in, &nums[i])
	}
	for i := 2; i <= n; i++ {
		if nums[i-1] > nums[i] {
			lsum[i] = lsum[i-1] + nums[i-1] - nums[i]
		} else {
			lsum[i] = lsum[i-1]
		}
	}
	for i := n - 1; i > 0; i-- {
		if nums[i+1] > nums[i] {
			rsum[i] = rsum[i+1] + nums[i+1] - nums[i]
		} else {
			rsum[i] = rsum[i+1]
		}
	}
	var t, s int
	for i := 0; i < m; i++ {
		fmt.Fscan(in, &s, &t)
		if s < t {
			fmt.Fprintln(out, lsum[t]-lsum[s])
		} else {
			fmt.Fprintln(out, rsum[t]-rsum[s])
		}
	}
}

package main

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

const cf = true

func solve() {
	var n, m int
	fmt.Fscan(in, &n, &m)
	arr := make([]int, n)
	for i := range arr {
		fmt.Fscan(in, &arr[i])
	}
	pref := make([]int, n)
	prefR := make([]int, n)
	for i := 1; i < n; i++ {
		pref[i] = pref[i-1]
		if arr[i] < arr[i-1] {
			pref[i] += arr[i-1] - arr[i]
		}
	}
	for i := n - 2; i >= 0; i-- {
		prefR[i] = prefR[i+1]
		if arr[i] < arr[i+1] {
			prefR[i] += arr[i+1] - arr[i]
		}
	}
	var s, t int
	for i := 0; i < m; i++ {
		fmt.Fscan(in, &s, &t)
		s--
		t--
		if t > s {
			fmt.Fprintln(out, pref[t]-pref[s])
		} else {
			fmt.Fprintln(out, prefR[t]-prefR[s])
		}
	}
}

func main() {
	defer out.Flush()
	t := 1
	//fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		solve()
	}
}

func ConvertInt(val string, base, toBase int) (string, error) {
	i, err := strconv.ParseInt(val, base, 64)
	if err != nil {
		return "", err
	}
	return strconv.FormatInt(i, toBase), nil
}

type set[T Ordered] struct {
	itms map[T]struct{}
}

func (s *set[T]) init() {
	if s.itms == nil {
		s.itms = make(map[T]struct{})
	}
}

func (s *set[T]) add(item T) *set[T] {
	s.init()
	s.itms[item] = exists
	return s
}

func (s *set[T]) remove(item T) *set[T] {
	s.init()
	delete(s.itms, item)
	return s
}

func (s *set[T]) has(item T) bool {
	s.init()
	_, c := s.itms[item]
	return c
}

func (s *set[T]) sortedItems() []T {
	s.init()
	items := make([]T, 0, s.size())
	for item := range s.itms {
		items = append(items, item)
	}
	sort.Slice(items, func(i, j int) bool {
		return items[i] < items[j]
	})
	return items
}

func (s *set[T]) items() []T {
	s.init()
	items := make([]T, 0, s.size())
	for item := range s.itms {
		items = append(items, item)
	}
	return items
}

func (s *set[T]) size() int {
	s.init()
	return len(s.itms)
}

var exists = struct{}{}

const MaxUint = ^uint(0)
const MinUint = 0
const MaxInt = int(MaxUint >> 1)
const MinInt = -MaxInt - 1

func reverseStr(s string) string {
	r := []rune(s)
	for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
		r[i], r[j] = r[j], r[i]
	}
	return string(r)
}

type Float interface {
	~float32 | ~float64
}

type Integer interface {
	Signed | Unsigned
}

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

type Ordered interface {
	Integer | Float | ~string
}

func abs[T Signed](x T) T {
	if x < 0 {
		return -x
	}
	return x
}

func min[T Ordered](x, y T) T {
	if x < y {
		return x
	}
	return y
}

func max[T Ordered](x, y T) T {
	if x > y {
		return x
	}
	return y
}

var (
	in  = getReader()
	out = getWriter()
)

func getReader() *bufio.Reader {
	if cf {
		return bufio.NewReader(os.Stdin)
	} else {
		in, _ := os.Open("input.txt")
		return bufio.NewReader(in)
	}
}

func getWriter() *bufio.Writer {
	if cf {
		return bufio.NewWriter(os.Stdout)
	} else {
		out, _ := os.Create("output.txt")
		return bufio.NewWriter(out)
	}
}


package main

import (
	"fmt"
)

func main() {
	var n int
	var s string
	fmt.Scan(&n, &s)
	l, r := 0, 0
	for _, i := range s {
		if i == '(' {
			l++
		} else {
			if l > 0 {
				l--
			} else {
				r++
			}
		}
	}
	if l == r && r <= 1 {
		fmt.Println("Yes")
	} else {
		fmt.Println("No")
	}
}

package main

import (
	"bufio"
	"os"
	"runtime"
	"strconv"
)

/* solution */

func solve() {

	n := readInt()
	s := read()

	if n%2 == 1 {
		print("NO")
		return
	}

	cur := 0
	for _, c := range s {
		switch c {
		case '(':
			cur++
		case ')':
			cur--
		}
		if cur < -1 {
			print("NO")
			return
		}
	}

	if cur == 0 {
		print("YES")
	} else {
		print("NO")
	}

}

/* stubs */

func main() {

	var f *os.File
	if runtime.GOROOT() == "C:\\Soft\\Go" {
		f, _ = os.Open("input.txt")
		defer f.Close()
	} else {
		f = os.Stdin
	}

	writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	reader = bufio.NewScanner(f)

	const maxCapacity = 1024 * 1024
	buf := make([]byte, maxCapacity)
	reader.Buffer(buf, maxCapacity)

	reader.Split(bufio.ScanLines)

	solve()

}

var reader *bufio.Scanner
var writer *bufio.Writer

func read() string {
	reader.Scan()
	return reader.Text()
}

func readInt() int {
	reader.Scan()
	ret, _ := strconv.Atoi(reader.Text())
	return ret
}

func print(s string) {
	writer.WriteString(s)
	writer.WriteString("\n")
}

func printInt(i int) {
	print(strconv.Itoa(i))
}

func printInt64(i int64) {
	print(strconv.FormatInt(i, 10))
}

func readInt64() int64 {
	reader.Scan()
	ret, _ := strconv.ParseInt(reader.Text(), 10, 64)
	return ret
}

package main

import "fmt"

func main() {
	n := 0;
	name := "";
	fmt.Scanf("%d\n", &n);
	fmt.Scanf("%s\n", &name);

	op := 0;
	acc := 0;
	ok := true;
	for i:= 0; i < n; i++ {
		if name[i] == '(' {
			acc ++;
		} else {
			if acc == 0 {
				if op == 0 {
					op = 1;
				} else {
					ok = false;
				}
			} else {
				acc --;
			}
		}
	}

	acc -= op;
	if ok && acc == 0 {
		fmt.Printf("Yes");
	} else {
		fmt.Printf("No");
	}
}

package main

import (
	"fmt"
	"strings"
)

func main() {
	var a int
	var s string
	fmt.Scan(&a, &s)
	l := strings.Count(s, "(")
	r := strings.Count(s, ")")
	if l != r {
		fmt.Println("No")
	} else {
		min := 0
		cnt := 0
		for i := 0; i < len(s); i++ {
			if s[i] == '(' {
				cnt++
			} else {
				cnt--
			}
			if cnt < min {
				min = cnt
			}
		}
		if min < -1 {
			fmt.Println("No")
		} else {
			fmt.Println("Yes")
		}
	}
}

package main

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

func main() {

	//stdin, err := os.Open("in.txt")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdin.Close()
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)

	var n int
	fmt.Fscanf(reader, "%d\n", &n)

	var s string
	fmt.Fscanf(reader, "%s", &s)

	var o, p int
	for i := 0; i < n; i++ {
		if s[i] == ')' {
			o--
		} else {
			o++
		}
		if i < 0 {
			p++
		}
		if o < -1 || p > 1 {
			fmt.Println("No")
			return
		}

	}
	if o == 0 {
		fmt.Println("Yes")
	} else {
		fmt.Println("No")
	}

}


package main

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

const maxInputSize = 512 * 1024 //bytes

type edge struct {
	index   int
	counted bool
	to      int
}

type edges []*edge

func (e edges) Len() int {
	return len(e)
}

func (e edges) Less(i, j int) bool {
	return e[i].index < e[j].index
}

func (e edges) Swap(i, j int) {
	e[i], e[j] = e[j], e[i]
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, maxInputSize), maxInputSize)

	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	scanner.Scan()
	m, _ := strconv.Atoi(scanner.Text())

	graph := make([][]*edge, n)

	d := make([]int, n)
	total := 0
	for i := 0; i < n; i++ {
		scanner.Scan()
		d[i], _ = strconv.Atoi(scanner.Text())
		if d[i] == 1 {
			total++
		}
	}
	for i := 0; i < m; i++ {
		scanner.Scan()
		u, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		v, _ := strconv.Atoi(scanner.Text())

		graph[u-1] = append(graph[u-1], &edge{
			to:    v - 1,
			index: (i + 1),
		})
		graph[v-1] = append(graph[v-1], &edge{
			to:    u - 1,
			index: (i + 1),
		})
	}
	answer := make([]*edge, 0)

	for i := 0; i < n; i++ {
		if d[i] == -1 {
			if total%2 == 1 {
				d[i] = 1
				total = 0
				continue
			}
			d[i] = 0
		}
	}
	// now there are no "-1"s ...
	// by handshaking lemma https://en.wikipedia.org/wiki/Handshaking_lemma :
	// number of vertices with odd degree is odd
	// so let's check that this holds
	if total%2 == 1 {
		fmt.Println(-1)
		return
	}

	//let's do dfs now
	answer, _ = traverse(0, graph, d, make([]bool, n), make([]*edge, 0))
	print(answer)
}

func print(es []*edge) {
	sort.Sort(edges(es))
	fmt.Println(len(es))
	for _, e := range es {
		fmt.Println(e.index)
	}
}

func traverse(cur int, graph [][]*edge, d []int, marked []bool, answer []*edge) ([]*edge, int) {
	marked[cur] = true

	counted := 0
	for _, e := range graph[cur] {
		if marked[e.to] {
			if e.counted {
				counted++
			}
			continue
		}
		var t int
		answer, t = traverse(e.to, graph, d, marked, answer)
		if t == 1 {
			counted++
			answer = append(answer, e)
			e.counted = true
		}
	}

	if d[cur]%2 != counted%2 {
		return answer, 1
	}
	return answer, 0
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF840B(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n, m, v, w int
	Fscan(in, &n, &m)
	neg, s := -1, int8(0)
	d := make([]int8, n)
	for i := range d {
		Fscan(in, &d[i])
		if d[i] < 0 {
			d[i] = 0
			neg = i
		} else {
			s ^= d[i]
		}
	}
	if neg < 0 {
		if s > 0 {
			Fprint(out, -1)
			return
		}
	} else {
		d[neg] = s
	}

	type nb struct{ to, i int }
	g := make([][]nb, n)
	for i := 1; i <= m; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], nb{w, i})
		g[w] = append(g[w], nb{v, i})
	}
	ans := []interface{}{}
	vis := make([]bool, n)
	var f func(int) int8
	f = func(v int) int8 {
		vis[v] = true
		s := d[v]
		for _, e := range g[v] {
			if w := e.to; !vis[w] && f(w) > 0 {
				ans = append(ans, e.i)
				s ^= 1
			}
		}
		return s
	}
	f(0)
	Fprintln(out, len(ans))
	Fprintln(out, ans...)
}

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

package main

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

const maxInputSize = 512 * 1024 //bytes

type edge struct {
	index int
	to    int
}

type edges []*edge

func (e edges) Len() int {
	return len(e)
}

func (e edges) Less(i, j int) bool {
	return e[i].index < e[j].index
}

func (e edges) Swap(i, j int) {
	e[i], e[j] = e[j], e[i]
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, maxInputSize), maxInputSize)

	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	scanner.Scan()
	m, _ := strconv.Atoi(scanner.Text())

	graph := make([][]*edge, n)

	d := make([]int, n)
	total := 0
	for i := 0; i < n; i++ {
		scanner.Scan()
		d[i], _ = strconv.Atoi(scanner.Text())
		if d[i] == 1 {
			total++
		}
	}
	for i := 0; i < m; i++ {
		scanner.Scan()
		u, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		v, _ := strconv.Atoi(scanner.Text())

		graph[u-1] = append(graph[u-1], &edge{
			to:    v - 1,
			index: (i + 1),
		})
		graph[v-1] = append(graph[v-1], &edge{
			to:    u - 1,
			index: (i + 1),
		})
	}
	answer := make([]*edge, 0)

	for i := 0; i < n; i++ {
		if d[i] == -1 {
			if total%2 == 1 {
				d[i] = 1
				total = 0
				continue
			}
			d[i] = 0
		}
	}
	if total%2 == 1 {
		fmt.Println(-1)
		return
	}

	answer, _ = traverse(0, graph, d, make([]bool, n), make([]*edge, 0))
	print(answer)
}

func print(es []*edge) {
	// sort.Sort(edges(es))
	fmt.Println(len(es))
	for _, e := range es {
		fmt.Println(e.index)
	}
}

func traverse(cur int, graph [][]*edge, d []int, marked []bool, answer []*edge) ([]*edge, int) {
	marked[cur] = true

	counted := 0
	for _, e := range graph[cur] {
		if marked[e.to] {
			continue
		}
		var t int
		answer, t = traverse(e.to, graph, d, marked, answer)
		if t == 1 {
			counted++
			answer = append(answer, e)
		}
	}

	if d[cur]%2 != counted%2 {
		return answer, 1
	}
	return answer, 0
}

package main

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

const maxInputSize = 512 * 1024 //bytes
var answer []*edge

type edge struct {
	index int
	to    int
}

type edges []*edge

func (e edges) Len() int {
	return len(e)
}

func (e edges) Less(i, j int) bool {
	return e[i].index < e[j].index
}

func (e edges) Swap(i, j int) {
	e[i], e[j] = e[j], e[i]
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, maxInputSize), maxInputSize)

	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	scanner.Scan()
	m, _ := strconv.Atoi(scanner.Text())

	graph := make([][]*edge, n)

	d := make([]int, n)
	total := 0
	for i := 0; i < n; i++ {
		scanner.Scan()
		d[i], _ = strconv.Atoi(scanner.Text())
		if d[i] == 1 {
			total++
		}
	}
	for i := 0; i < m; i++ {
		scanner.Scan()
		u, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		v, _ := strconv.Atoi(scanner.Text())

		graph[u-1] = append(graph[u-1], &edge{
			to:    v - 1,
			index: (i + 1),
		})
		graph[v-1] = append(graph[v-1], &edge{
			to:    u - 1,
			index: (i + 1),
		})
	}

	for i := 0; i < n; i++ {
		if d[i] == -1 {
			if total%2 == 1 {
				d[i] = 1
				total = 0
				continue
			}
			d[i] = 0
		}
	}
	if total%2 == 1 {
		fmt.Println(-1)
		return
	}

	traverse(0, graph, d, make([]bool, n))
	print(answer)
}

func print(es []*edge) {
	// sort.Sort(edges(es))
	fmt.Println(len(es))
	for _, e := range es {
		fmt.Println(e.index)
	}
}

func traverse(cur int, graph [][]*edge, d []int, marked []bool) int {
	marked[cur] = true

	counted := 0
	for _, e := range graph[cur] {
		if marked[e.to] {
			continue
		}
		t := traverse(e.to, graph, d, marked)

		if t == 1 {
			counted++
			answer = append(answer, e)
		}
	}

	if d[cur]%2 != counted%2 {
		return 1
	}
	return 0
}

package main

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

const maxInputSize = 512 * 1024 //bytes

type edge struct {
	index   int
	counted bool
	to      int
}

type edges []*edge

func (e edges) Len() int {
	return len(e)
}

func (e edges) Less(i, j int) bool {
	return e[i].index < e[j].index
}

func (e edges) Swap(i, j int) {
	e[i], e[j] = e[j], e[i]
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, maxInputSize), maxInputSize)

	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	scanner.Scan()
	m, _ := strconv.Atoi(scanner.Text())

	graph := make([][]*edge, n)

	d := make([]int, n)
	total := 0
	for i := 0; i < n; i++ {
		scanner.Scan()
		d[i], _ = strconv.Atoi(scanner.Text())
		if d[i] == 1 {
			total++
		}
	}
	for i := 0; i < m; i++ {
		scanner.Scan()
		u, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		v, _ := strconv.Atoi(scanner.Text())

		graph[u-1] = append(graph[u-1], &edge{
			to:    v - 1,
			index: (i + 1),
		})
		graph[v-1] = append(graph[v-1], &edge{
			to:    u - 1,
			index: (i + 1),
		})
	}
	answer := make([]*edge, 0)

	for i := 0; i < n; i++ {
		if d[i] == -1 {
			if total%2 == 1 {
				d[i] = 1
				total = 0
				continue
			}
			d[i] = 0
		}
	}
	// now there are no "-1"s ...
	// by handshaking lemma https://en.wikipedia.org/wiki/Handshaking_lemma :
	// number of vertices with odd degree is odd
	// so let's check that this holds
	if total%2 == 1 {
		fmt.Println(-1)
		return
	}

	//let's do dfs now
	answer, _ = traverse(0, graph, d, make([]bool, n), make([]*edge, 0))
	print(answer)
}

func print(es []*edge) {
	sort.Sort(edges(es))
	fmt.Println(len(es))
	for _, e := range es {
		fmt.Println(e.index)
	}
}

func traverse(cur int, graph [][]*edge, d []int, marked []bool, answer []*edge) ([]*edge, int) {
	marked[cur] = true

	counted := 0
	for _, e := range graph[cur] {
		if marked[e.to] {
			// if e.counted {
			// counted++
			// }
			continue
		}
		var t int
		answer, t = traverse(e.to, graph, d, marked, answer)
		if t == 1 {
			counted++
			answer = append(answer, e)
			e.counted = true
		}
	}

	if d[cur]%2 != counted%2 {
		return answer, 1
	}
	return answer, 0
}

package main

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

const maxInputSize = 512 * 1024 //bytes

type edge struct {
	index int
	to    int
}

type edges []*edge

func (e edges) Len() int {
	return len(e)
}

func (e edges) Less(i, j int) bool {
	return e[i].index < e[j].index
}

func (e edges) Swap(i, j int) {
	e[i], e[j] = e[j], e[i]
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, maxInputSize), maxInputSize)

	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	scanner.Scan()
	m, _ := strconv.Atoi(scanner.Text())

	graph := make([][]*edge, n)

	d := make([]int, n)
	deg := make([]int, n)
	for i := 0; i < n; i++ {
		scanner.Scan()
		d[i], _ = strconv.Atoi(scanner.Text())
	}
	for i := 0; i < m; i++ {
		scanner.Scan()
		u, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		v, _ := strconv.Atoi(scanner.Text())

		graph[u-1] = append(graph[u-1], &edge{
			to:    v - 1,
			index: (i + 1),
		})
		graph[v-1] = append(graph[v-1], &edge{
			to:    u - 1,
			index: (i + 1),
		})
		deg[u-1]++
		deg[v-1]++
	}
	answer := make([]*edge, 0)
	di := -1
	for i := 0; i < n; i++ {
		if d[i] == -1 {
			di = i
		}
	}
	if di != -1 {
		marked := make([]bool, n)
		for _, e := range graph[di] {
			if !marked[e.to] {
				if d[e.to] == 1 {
					answer = append(answer, e)
				}
				marked[e.to] = true
			}
		}
		print(answer)
		return
	}
	// now there are no "-1"s ...
	// by handshaking lemma https://en.wikipedia.org/wiki/Handshaking_lemma :
	// number of vertices with odd degree is odd
	// so let's check that this holds
	odd := 0
	for i := 0; i < n; i++ {
		odd += d[i] % 2
	}
	if odd%2 == 1 {
		fmt.Println(-1)
		return
	}

	//let's do dfs now
	answer, _ = traverse(0, graph, d, make([]bool, n), make([]*edge, 0))
	print(answer)
}

func print(es []*edge) {
	sort.Sort(edges(es))
	fmt.Println(len(es))
	for _, e := range es {
		fmt.Println(e.index)
	}
}

func traverse(cur int, graph [][]*edge, d []int, marked []bool, answer []*edge) ([]*edge, int) {
	marked[cur] = true

	counted := 0
	for _, e := range graph[cur] {
		if marked[e.to] {
			continue
		}
		var t int
		answer, t = traverse(e.to, graph, d, marked, answer)
		if t == 1 {
			counted++
			answer = append(answer, e)
		}
	}

	if d[cur]%2 != counted%2 {
		return answer, 1
	}
	return answer, 0
}

package main

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

const maxInputSize = 512 * 1024 //bytes

type edge struct {
	index int
	to    int
}

type edges []*edge

func (e edges) Len() int {
	return len(e)
}

func (e edges) Less(i, j int) bool {
	return e[i].index < e[j].index
}

func (e edges) Swap(i, j int) {
	e[i], e[j] = e[j], e[i]
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, maxInputSize), maxInputSize)

	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	scanner.Scan()
	m, _ := strconv.Atoi(scanner.Text())

	graph := make([][]*edge, n)

	d := make([]int, n)
	deg := make([]int, n)
	for i := 0; i < n; i++ {
		scanner.Scan()
		d[i], _ = strconv.Atoi(scanner.Text())
	}
	for i := 0; i < m; i++ {
		scanner.Scan()
		u, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		v, _ := strconv.Atoi(scanner.Text())

		graph[u-1] = append(graph[u-1], &edge{
			to:    v - 1,
			index: (i + 1),
		})
		graph[v-1] = append(graph[v-1], &edge{
			to:    u - 1,
			index: (i + 1),
		})
		deg[u-1]++
		deg[v-1]++
	}
	answer := make([]*edge, 0)
	di := -1
	for i := 0; i < n; i++ {
		if d[i] == -1 {
			di = i
		}
	}
	if di != -1 {
		marked := make([]bool, n)
		for _, e := range graph[di] {
			if !marked[e.to] {
				if d[e.to] == 1 {
					answer = append(answer, e)
				}
				marked[e.to] = true
			}
		}
		print(answer)
		return
	}
	// now there are no "-1"s ...
	// by handshaking lemma https://en.wikipedia.org/wiki/Handshaking_lemma :
	// number of vertices with odd degree is odd
	// so let's check that this holds
	odd := 0
	for i := 0; i < n; i++ {
		odd += deg[i] % 2
	}
	if odd%2 == 1 {
		fmt.Println(-1)
		return
	}

	//let's do dfs now
	answer, _ = traverse(0, graph, d, make([]bool, n), make([]*edge, 0))
	print(answer)
}

func print(es []*edge) {
	sort.Sort(edges(es))
	fmt.Println(len(es))
	for _, e := range es {
		fmt.Println(e.index)
	}
}

func traverse(cur int, graph [][]*edge, d []int, marked []bool, answer []*edge) ([]*edge, int) {
	marked[cur] = true

	counted := 0
	for _, e := range graph[cur] {
		if marked[e.to] {
			continue
		}
		var t int
		answer, t = traverse(e.to, graph, d, marked, answer)
		if t == 1 {
			counted++
			answer = append(answer, e)
		}
	}

	if d[cur]%2 != counted%2 {
		return answer, 1
	}
	return answer, 0
}

package main

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

const maxInputSize = 512 * 1024 //bytes

type edge struct {
	index   int
	counted bool
	to      int
}

type edges []*edge

func (e edges) Len() int {
	return len(e)
}

func (e edges) Less(i, j int) bool {
	return e[i].index < e[j].index
}

func (e edges) Swap(i, j int) {
	e[i], e[j] = e[j], e[i]
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, maxInputSize), maxInputSize)

	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	scanner.Scan()
	m, _ := strconv.Atoi(scanner.Text())

	graph := make([][]*edge, n)

	d := make([]int, n)
	deg := make([]int, n)
	for i := 0; i < n; i++ {
		scanner.Scan()
		d[i], _ = strconv.Atoi(scanner.Text())
	}
	for i := 0; i < m; i++ {
		scanner.Scan()
		u, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		v, _ := strconv.Atoi(scanner.Text())

		graph[u-1] = append(graph[u-1], &edge{
			to:    v - 1,
			index: (i + 1),
		})
		graph[v-1] = append(graph[v-1], &edge{
			to:    u - 1,
			index: (i + 1),
		})
		deg[u-1]++
		deg[v-1]++
	}
	answer := make([]*edge, 0)
	di := -1
	for i := 0; i < n; i++ {
		if d[i] == -1 {
			di = i
		}
	}
	if di != -1 {
		marked := make([]bool, n)
		for _, e := range graph[di] {
			if !marked[e.to] {
				if d[e.to] == 1 {
					answer = append(answer, e)
				}
				marked[e.to] = true
			}
		}
		print(answer)
		return
	}
	// now there are no "-1"s ...
	// by handshaking lemma https://en.wikipedia.org/wiki/Handshaking_lemma :
	// number of vertices with odd degree is odd
	// so let's check that this holds
	odd := 0
	for i := 0; i < n; i++ {
		odd += d[i] % 2
	}
	if odd%2 == 1 {
		fmt.Println(-1)
		return
	}

	//let's do dfs now
	answer, _ = traverse(0, graph, d, make([]bool, n), make([]*edge, 0))
	print(answer)
}

func print(es []*edge) {
	sort.Sort(edges(es))
	fmt.Println(len(es))
	for _, e := range es {
		fmt.Println(e.index)
	}
}

func traverse(cur int, graph [][]*edge, d []int, marked []bool, answer []*edge) ([]*edge, int) {
	marked[cur] = true

	counted := 0
	for _, e := range graph[cur] {
		if marked[e.to] {
			if e.counted {
				counted++
			}
			continue
		}
		var t int
		answer, t = traverse(e.to, graph, d, marked, answer)
		if t == 1 {
			counted++
			answer = append(answer, e)
			e.counted = true
		}
	}

	if d[cur]%2 != counted%2 {
		return answer, 1
	}
	return answer, 0
}

package main

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

const maxInputSize = 512 * 1024 //bytes

type edge struct {
	index   int
	counted bool
	to      int
}

type edges []*edge

func (e edges) Len() int {
	return len(e)
}

func (e edges) Less(i, j int) bool {
	return e[i].index < e[j].index
}

func (e edges) Swap(i, j int) {
	e[i], e[j] = e[j], e[i]
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, maxInputSize), maxInputSize)

	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	scanner.Scan()
	m, _ := strconv.Atoi(scanner.Text())

	graph := make([][]*edge, n)

	d := make([]int, n)
	total := 0
	for i := 0; i < n; i++ {
		scanner.Scan()
		d[i], _ = strconv.Atoi(scanner.Text())
		total += (d[i] % 2)
	}
	for i := 0; i < m; i++ {
		scanner.Scan()
		u, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		v, _ := strconv.Atoi(scanner.Text())

		graph[u-1] = append(graph[u-1], &edge{
			to:    v - 1,
			index: (i + 1),
		})
		graph[v-1] = append(graph[v-1], &edge{
			to:    u - 1,
			index: (i + 1),
		})
	}
	answer := make([]*edge, 0)
	for i := 0; i < n; i++ {
		if d[i] == -1 {
			if total%2 == 1 {
				d[i] = 1
				total = 0
				continue
			}
			d[i] = 0
		}
	}
	// now there are no "-1"s ...
	// by handshaking lemma https://en.wikipedia.org/wiki/Handshaking_lemma :
	// number of vertices with odd degree is odd
	// so let's check that this holds
	if total%2 == 1 {
		fmt.Println(-1)
		return
	}

	//let's do dfs now
	answer, _ = traverse(0, graph, d, make([]bool, n), make([]*edge, 0))
	print(answer)
}

func print(es []*edge) {
	sort.Sort(edges(es))
	fmt.Println(len(es))
	for _, e := range es {
		fmt.Println(e.index)
	}
}

func traverse(cur int, graph [][]*edge, d []int, marked []bool, answer []*edge) ([]*edge, int) {
	marked[cur] = true

	counted := 0
	for _, e := range graph[cur] {
		if marked[e.to] {
			if e.counted {
				counted++
			}
			continue
		}
		var t int
		answer, t = traverse(e.to, graph, d, marked, answer)
		if t == 1 {
			counted++
			answer = append(answer, e)
			e.counted = true
		}
	}

	if d[cur]%2 != counted%2 {
		return answer, 1
	}
	return answer, 0
}


package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ... interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ... interface{}) { fmt.Fscanf(reader, f, a...) }

const maxn int = 1e5+2

var db bool = true

var tc int
var s, t string

var cc [maxn][26]int
var ch_pos [26]int

func main() {
    defer writer.Flush()

    scanf("%d\n", &tc)

    for ; tc > 0; tc-- {

        scanf("%s\n", &s)
        scanf("%s\n", &t)

        for ch := 0; ch < 26; ch++ {
            ch_pos[ch] = 0
        }

        len_s := len(s)
        for i := len_s; i >= 1; i-- {
            for ch := 0; ch < 26; ch++ {
                cc[i][ch] = ch_pos[ch]
            }
            ch_pos[s[i-1]-'a'] = i;
        }
        for ch := 0; ch < 26; ch++ {
            cc[0][ch] = ch_pos[ch]
        }

        ans, ch_v := 0, 0
        len_t := len(t)
        for ti := 0; ti < len_t; {
            ch_v = int(t[ti]-'a')
            if(cc[0][ch_v] == 0) {
                ans = -1
                break;
            }
            ans++
            for si := 0; si <= len_s && ti < len_t; {
                ch_v = int(t[ti]-'a')
                if cc[si][ch_v] == 0 {
                    break;
                } else {
                    si = cc[si][ch_v]
                }
                ti++;
            }
        }
        printf("%d\n", ans)
    }
}

package main

import (
  "fmt"
)

// DEBUG = Only for development
const DEBUG bool = false

func foundL(l []int, maxIndex *int) bool {
  start := 0
  end := len(l)-1

  if(l[start] >= *maxIndex){
    *maxIndex = l[start] + 1
    return true
  }
  if(l[end] == *maxIndex){
    *maxIndex = l[end] + 1
    return true
  }

  if(l[end] < *maxIndex){
    return false
  }

  for end-start > 1{
    mid := (end+start)/2
    if l[mid] == *maxIndex{
      *maxIndex = l[mid] + 1
      return true
    } else if l[mid] > *maxIndex{
      end = mid
    } else {
      start = mid
    }
  }

  *maxIndex = l[end] + 1
  return true
}

func main() {
  if DEBUG {
    fmt.Print("Enter the number of testcases :: ")
  }
  var numTest uint16
  fmt.Scanln(&numTest)

  var s string
  var t string

  for numTest > 0 {
    fmt.Scanln(&s)
    fmt.Scanln(&t)

    if DEBUG {
      fmt.Println("s :: ", s)
      fmt.Println("t :: ", t)
    }

    letterArr := make([][]int, 26)

    for i := range s {
      letterArr[s[i]-'a'] = append(letterArr[s[i]-'a'], i)
    }

    if DEBUG {
      fmt.Println("letterArr :: ", letterArr)
    }

    var maxIndex int = 0
    var cost int = 1
    var notFoundLetter = false

    for i := range t {
      letterIndex := t[i] - 'a'
      //var found bool = false

      if len(letterArr[letterIndex]) == 0{
        notFoundLetter = true
        fmt.Println("-1")
        break
      }

      /*for j := range letterArr[letterIndex] {
        if letterArr[letterIndex][j] >= maxIndex {
          maxIndex = letterArr[letterIndex][j]+1
          found = true
        }
        if found {
          break
        }
      }*/

      if !foundL(letterArr[letterIndex], &maxIndex){
        cost++
        maxIndex = letterArr[letterIndex][0]+1
      }
    }

    if !notFoundLetter {
      fmt.Println(cost)
    }

    numTest--
  }
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1295C(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var t int
	for Fscan(in, &t); t > 0; t-- {
		var s, t []byte
		Fscan(in, &s, &t)
		pos := [26][]int{}
		for i := range s {
			s[i] -= 'a'
			pos[s[i]] = append(pos[s[i]], i)
		}
		for i := range pos {
			pos[i] = append(pos[i], -1)
		}
		next := make([][]int, len(s)+1)
		next[0] = make([]int, 26)
		for i := range next[0] {
			next[0][i] = pos[i][0]
		}
		cur := [26]int{}
		for i, b := range s {
			cur[b]++
			next[i+1] = make([]int, 26)
			copy(next[i+1], next[i])
			next[i+1][b] = pos[b][cur[b]]
		}
		ans := 0
		for i := 0; i < len(t); {
			si := next[0][t[i]-'a']
			if si == -1 {
				ans = -1
				break
			}
			for i++; i < len(t); i++ {
				si = next[si+1][t[i]-'a']
				if si == -1 {
					break
				}
			}
			ans++
		}
		Fprintln(out, ans)
	}
}

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

// Author: sighduck
// URL: https://codeforces.com/problemset/problem/1295/C

package main

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

const alphabet = "abcdefghijklmnopqrstuvwxyz"

func createJumpTable(n int) [][]int {
	m := make([][]int, n)
	for i := 0; i < n; i++ {
		m[i] = make([]int, 26)
		for j := 0; j < 26; j++ {
			m[i][j] = -1
		}
	}
	return m
}

func traverseMap(count int, n int, mi int, ti int, m [][]int, t string) int {
	if ti == len(t) {
		return count
	}
	if mi == n {
		return traverseMap(count+1, n, 0, ti, m, t)
	}
	if m[mi][t[ti]-'a'] == -1 && mi == 0 {
		return -1
	}
	if m[mi][t[ti]-'a'] == -1 {
		return traverseMap(count+1, n, 0, ti, m, t)
	}
	return traverseMap(count, n, m[mi][t[ti]-'a']+1, ti+1, m, t)
}

func Solve(s string, t string) int {
	n := len(s)
	m := createJumpTable(n)
	for i := n - 1; i >= 0; i-- {
		for j := 0; j < 26; j++ {
			if i+1 <= n-1 {
				m[i][j] = m[i+1][j]
			}
		}
		m[i][s[i]-'a'] = i
	}
	return traverseMap(1, n, 0, 0, m, t)
}

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 s, t string
		fmt.Fscanf(reader, "%s\n%s\n", &s, &t)
		fmt.Fprintf(writer, "%d\n", Solve(s, t))
	}
}


package main

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

type building struct {
	id   int
	v, d int64
}

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

		b := make([]building, n)
		for i := 0; i < n; i++ {
			var build building
			build.id = i
			fmt.Fscan(in, &build.v)

			b[i] = build
		}

		sort.SliceStable(b, func(i, j int) bool {
			return b[i].v > b[j].v
		})

		m := make(map[int]building, n)
		diff := 1
		var sum int64
		for i := 0; i < n; i++ {
			if i > 0 && i%2 == 0 {
				diff++
			}

			build := b[i]
			build.d = int64(diff)
			sum += build.d * build.v

			if i%2 == 1 {
				build.d *= -1
			}
			m[build.id] = build
		}

		fmt.Fprintln(out, sum*2)
		fmt.Fprintf(out, "%d ", 0)

		for i := 0; i < n; i++ {
			fmt.Fprintf(out, "%d ", m[i].d)
		}
		fmt.Fprintln(out)
	}
}

package main

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

type building struct {
	id   int
	v, d int64
}

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

		b := make([]building, n)
		for i := 0; i < n; i++ {
			var build building
			build.id = i
			fmt.Fscan(in, &build.v)

			b[i] = build
		}

		sort.SliceStable(b, func(i, j int) bool {
			return b[i].v > b[j].v
		})

		ans := make([]int, n+1)
		diff := 1
		var sum int64
		for i := 0; i < n; i++ {
			if i > 0 && i%2 == 0 {
				diff++
			}

			build := b[i]
			build.d = int64(diff)
			sum += build.d * build.v

			if i%2 == 1 {
				build.d *= -1
			}
			ans[build.id+1] = int(build.d)
		}

		fmt.Fprintln(out, sum*2)
		for i := 0; i < n+1; i++ {
			fmt.Fprintf(out, "%d ", ans[i])
		}
		fmt.Fprintln(out)
	}
}

package main

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

var sc = bufio.NewScanner(os.Stdin)

func scan() string {
	for sc.Scan() {
		break
	}
	return sc.Text()
}

func scanInt() int {
	i, err := strconv.Atoi(scan())
	if err != nil {
		panic(err)
	}
	return i
}

func scanInt8() int8 {
	i, err := strconv.ParseInt(scan(), 10, 8)
	if err != nil {
		panic(err)
	}
	return int8(i)
}

func scanInt64() int64 {
	i, err := strconv.ParseInt(scan(), 10, 64)
	if err != nil {
		panic(err)
	}
	return i
}

func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

func min(ints ...int) int {
	a := ints[0]
	n := len(ints)
	for i := 1; i < n; i++ {
		if ints[i] < a {
			a = ints[i]
		}
	}
	return a
}

func max(ints ...int) int {
	a := ints[0]
	n := len(ints)
	for i := 1; i < n; i++ {
		if ints[i] > a {
			a = ints[i]
		}
	}
	return a
}

type Noop struct{}

var noop = Noop{}

type Node struct {
	x int64
	p int
}

func proc() {
	n := scanInt()
	mas := make([]Node, n)
	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		mas[i].x = scanInt64()
		mas[i].p = i
	}
	sort.Slice(mas, func(i, j int) bool { return mas[i].x < mas[j].x })
	var ans int64
	k := -1
	for i := n - 1; i >= 0; i-- {
		ans += mas[i].x * int64(abs(k)) * 2
		arr[mas[i].p] = int64(k)
		if k < 0 {
			k = -k
		} else {
			k = -(k + 1)
		}
	}
	fmt.Println(ans)
	fmt.Printf("0 ")
	for i := 0; i < n; i++ {
		if i+1 == n {
			fmt.Println(arr[i])
		} else {
			fmt.Printf("%d ", arr[i])
		}
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	t := scanInt()
	for i := 0; i < t; i++ {
		proc()
	}
}

package main

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

var (
	in  = bufio.NewScanner(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

var (
	TC, n int
)

type info struct {
	idx, times int
}

func main() {
	defer out.Flush()
	in.Split(bufio.ScanWords)
	TC = nextInt()
	for t := 0; t < TC; t++ {
		n = nextInt()
		a := make([]info, 0)
		for i := 0; i < n; i++ {
			tmp := info{i, nextInt()}
			a = append(a, tmp)
		}
		T, x := solve(a)
		fmt.Fprintln(out, T)
		fmt.Fprintln(out, strings.Trim(fmt.Sprint(x), "[]"))
	}
}

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

	min_time := int64(0)
	x := make([]int, len(a)+1)

	x[0] = 0
	coord := 1
	for _, val := range a {
		x[val.idx+1] = coord
		min_time += (int64(abs(coord)) * int64(val.times) * 2)
		coord *= -1
		if coord > 0 {
			coord++
		}
	}

	return min_time, x
}

func abs(v int) int {
	if v < 0 {
		return -v
	} else {
		return v
	}
}

func nextStr() string {
	in.Scan()
	return in.Text()
}

func nextInt() int {
	ret, _ := strconv.Atoi(nextStr())
	return ret
}

package main

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

type Building struct {
	number     int
	coordinate int
	visit      int
}

var (
	reader = bufio.NewReader(os.Stdin)
	t, n   int
)

func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

func solve(n int, a []Building) string {
	sort.Slice(a, func(i, j int) bool {
		return a[i].visit > a[j].visit
	})
	var time int64
	for i := 0; i < n; i++ {
		a[i].coordinate = i/2 + 1
		if i%2 == 0 {
			a[i].coordinate *= -1
		}
		time += int64(a[i].visit) * int64(abs(a[i].coordinate))
	}
	sort.Slice(a, func(i, j int) bool {
		return a[i].number < a[j].number
	})
	y := make([]string, n+1)
	for i := range a {
		y[i] = fmt.Sprint(a[i].coordinate)
	}
	return fmt.Sprintln(2*time) + strings.Join(y, " ")
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n)
		a := make([]Building, n+1)
		for i := 1; i <= n; i++ {
			a[i].number = i + 1
			fmt.Fscan(reader, &a[i].visit)
		}
		fmt.Println(solve(n, a))
	}
}

package main

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

var n int

var a []int

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)

	for i := 0; i < t; i++ {
		fmt.Fscan(r, &n)

		a = make([]int, n)

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

		solve()

		fmt.Println()
	}
}

type pair struct {
	k, v int
}

type ByK []*pair

func (a ByK) Len() int           { return len(a) }
func (a ByK) Less(i, j int) bool { return a[i].k < a[j].k }
func (a ByK) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

type item struct {
	la, ra int
	lb, rb int
}

const MAX int = 1e6

func solve() {

	var p []*pair

	for i := 0; i < n; i++ {
		p = append(p, &pair{a[i], i})
	}
	sort.Sort(ByK(p))

	ans := make([]int, n + 1)
	ans[0] = 0

	var dist int64

	r := 0
	right := 1
	left := 1

	for i := n - 1; i >= 0; i-- {
		b := p[i].v
		times := p[i].k

		if r % 2 == 0 {
			ans[b + 1] = right
			dist += int64(times) * int64(right)

			right++

		} else {
			ans[b + 1] = -left
			dist+= int64(times)* int64(left)

			left++
		}

		r++
	}

	fmt.Println(2 * dist)

	for i := 0; i <= n; i++ {
		fmt.Print(ans[i], " ")
	}
}

func add(idx int, delta int, bit *[]int, n int) {
	for ; idx <= n; idx += idx & -idx {
		(*bit)[idx] += delta
	}
}

func get(idx int, bit *[]int) int {
	ret := 0
	for ; idx > 0; idx -= idx & -idx {
		ret += (*bit)[idx];
	}
	return ret
}

//A -  1 3 5 3 1

//B -  4 3 6 1

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

package main

import (
	"bufio"
	//	"container/heap" for heap.Init()
	"fmt"
	"os"
	"runtime"
	"sort"
)

func check(b bool) {
	if !b {
		_, _, line, _ := runtime.Caller(1)
		fmt.Printf("check failed line %d\n", line)
		os.Exit(-1)
	}
}

func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

type IntHeap []int

func (h IntHeap) Len() int            { return len(h) }
func (h IntHeap) Less(i, j int) bool  { return h[i] < h[j] }
func (h IntHeap) Swap(i, j int)       { h[i], h[j] = h[j], h[i] }
func (h *IntHeap) Push(x interface{}) { *h = append(*h, x.(int)) }
func (h *IntHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

const (
	INIT = -1
)

var (
	n   int
	arr []int64
)

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

	var tc int
	fmt.Fscan(r, &tc)

	for ; tc > 0; tc-- {
		fmt.Fscan(r, &n)
		arr = make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(r, &arr[i])
		}

		iord := make([]int, n)
		for i := 0; i < n; i++ {
			iord[i] = i
		}
		sort.Slice(iord, func(i, j int) bool {
			return arr[iord[i]] < arr[iord[j]]
		})

		// for i := 0; i < n; i++ {
		// 	fmt.Printf("%d ", arr[iord[i]])
		// }
		// fmt.Println()

		var sum int64
		var cnt int64
		pos := make([]int64, n+1)
		for i := n - 1; i >= 0; i-- {
			diff := cnt/2 + 1

			sum += 2 * arr[iord[i]] * diff

			if (cnt % 2) == 1 {
				diff = -diff
			}
			pos[iord[i]+1] = diff
			cnt++
		}

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

	}
}

package main

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

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

func scan(a ...interface{})             { fmt.Fscan(reader, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }

func redirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

func B() {
	var n int64
	scan(&n)
	a := make([][2]int64, n)
	for i := range a {
		a[i][0] = int64(i)
		scan(&a[i][1])
	}
	sort.Slice(a, func(i, j int) bool { return a[i][1] > a[j][1] })

	var x, time int64 = 1, 0
	ans := make([]int64, n)
	for i := range a {
		y := x
		if y < 0 {
			y = -y
		}
		time += a[i][1] * 2 * y
		ans[a[i][0]] = x
		if x < 0 {
			x = -x + 1
		} else {
			x = -x
		}
	}

	printf("%v\n0 ", time)
	for i := int64(0); i < n; i++ {
		printf("%v ", ans[i])
	}
	printf("\n")
}

func main() {
	defer writer.Flush()
	//redirectInputToFile(reader, "input.txt")

	T := 1
	scan(&T)
	for t := 0; t < T; t++ {
		B()
	}
}

// 00
package main

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

func write(f *bufio.Writer, a ...interface{}) {
	f.Write([]byte(fmt.Sprint(a...)))
}

type Number interface {
	int64 | float64 | int
}

func min[K Number](a K, b K) K {
	if a < b {
		return a
	}
	return b
}
func max[K Number](a K, b K) K {
	if a > b {
		return a
	}
	return b
}
func abs[K Number](a K) K {
	if a < 0 {
		return -a
	}
	return a
}

type Pair struct {
	i, v int
}

func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	f := bufio.NewWriter(os.Stdout)
	defer f.Flush()
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n int
		fmt.Fscan(reader, &n)
		a := make([]Pair, n)
		as := make([]int, n)
		for i := 0; i < n; i++ {
			a[i] = Pair{i: i}
			fmt.Fscan(reader, &a[i].v)
			as[i] = a[i].v
		}
		sort.Slice(a, func(i, j int) bool { return a[i].v > a[j].v })
		x := make([]int, n+1)
		curr := 1
		for i := 0; i < n; i++ {
			if i%2 == 0 {
				x[a[i].i+1] = curr
			} else {
				x[a[i].i+1] = -curr
			}
			if i%2 == 1 {
				curr++
			}
		}
		ans := int64(0)
		for i := 1; i <= n; i++ {
			ans += abs(int64(as[i-1] * 2 * x[i]))
		}
		write(f, ans, "\n")
		for i := 0; i <= n; i++ {
			write(f, x[i])
			if i == n {
				write(f, "\n")
			} else {
				write(f, " ")
			}
		}
	}
}

package main

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

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

type Value struct {
	value int
	index int
}

func solveCase() {
	var n int
	fmt.Fscan(in, &n)
	var a []Value = make([]Value, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &a[i].value)
		a[i].index = i
	}
	sort.Slice(a[:], func(i, j int) bool {
		return a[i].value > a[j].value
	})
	var ans []int = make([]int, n+1)
	var totalDist int64 = 0
	for i := 0; i < n; i++ {
		var coordinate = (i / 2) + 1
		totalDist += int64(a[i].value) * int64(coordinate)
		if i%2 == 1 {
			coordinate = -coordinate
		}
		ans[a[i].index+1] = coordinate
	}
	fmt.Fprintln(out, totalDist*2)
	for i := 0; i <= n; i++ {
		fmt.Fprintf(out, "%d ", ans[i])
	}
	fmt.Fprintln(out)
}

func main() {
	var ntc int
	fmt.Fscan(in, &ntc)
	for t := 0; t < ntc; t++ {
		solveCase()
	}
	out.Flush()
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"io/ioutil"
	"os"
	"sort"
	"strings"
)

// github.com/EndlessCheng/codeforces-go
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	solve := func(Case int) {
		var n int
		Fscan(in, &n)
		type pair struct{ v, i int }
		a := make([]pair, n)
		for i := range a {
			Fscan(in, &a[i].v)
			a[i].i = i
		}
		sort.Slice(a, func(i, j int) bool { return a[i].v > a[j].v })
		ans := make([]int, n)
		l,r := -1,1
		t := int64(0)
		for i, p := range a {
			if i&1==0 {
				t+=int64(r)*2*int64(p.v)
				ans[p.i]=r
				r++
			} else {
				t+=int64(-l)*2*int64(p.v)
				ans[p.i] = l
				l--
			}
		}
		Fprintln(out, t)
		Fprint(out, 0," ")
		for _, v := range ans {
			Fprint(out, v, " ")
		}
		Fprintln(out)
	}

	T := 1
	Fscan(in, &T)
	for Case := 1; Case <= T; Case++ {
		solve(Case)
	}

	_leftData, _ := ioutil.ReadAll(in)
	if _s := strings.TrimSpace(string(_leftData)); _s != "" {
		panic("有未读入的数据：\n" + _s)
	}

}

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

package main

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

type building struct {
	id, v, d int
}

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

		b := make([]building, n)
		for i := 0; i < n; i++ {
			var build building
			build.id = i
			fmt.Fscan(in, &build.v)

			b[i] = build
		}

		sort.SliceStable(b, func(i, j int) bool {
			return b[i].v > b[j].v
		})

		m := make(map[int]building, n)
		diff := 1
		var sum int
		for i := 0; i < n; i++ {
			if i > 0 && i%2 == 0 {
				diff++
			}

			build := b[i]
			build.d = diff
			sum += build.d * build.v

			if i%2 == 1 {
				build.d *= -1
			}
			m[build.id] = build
		}

		fmt.Fprintln(out, sum*2)
		fmt.Fprintf(out, "%d ", 0)

		for i := 0; i < n; i++ {
			fmt.Fprintf(out, "%d ", m[i].d)
		}
		fmt.Fprintln(out)
	}
}

package main

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

type building struct {
	id, v, d int
}

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

		b := make([]building, n)
		for i := 0; i < n; i++ {
			var build building
			build.id = i
			fmt.Fscan(in, &build.v)

			b[i] = build
		}

		sort.SliceStable(b, func(i, j int) bool {
			return b[i].v < b[j].v
		})

		var sum int
		m := make(map[int]building, n)
		for i := 0; i < n; i++ {
			b[i].d = ((n - 1 - i) / 2) + 1
			sum += b[i].v * b[i].d
			m[b[i].id] = b[i]
		}

		fmt.Fprintln(out, sum*2)
		fmt.Fprintf(out, "%d ", 0)

		for i := 0; i < n; i++ {
			d := m[i].d
			if i%2 > 0 {
				d *= -1
			}

			fmt.Fprintf(out, "%d ", d)
		}
		fmt.Fprintln(out)
	}
}

package main

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

type building struct {
	id, v int
}

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

		b := make([]building, n)
		for i := 0; i < n; i++ {
			var build building
			build.id = i
			fmt.Fscan(in, &build.v)

			b[i] = build
		}

		sort.SliceStable(b, func(i, j int) bool {
			return b[i].v < b[j].v
		})

		var sum int
		for i := 0; i < n; i++ {
			d := ((n - 1 - i) / 2) + 1
			sum += b[i].v * d
		}

		fmt.Fprintln(out, sum*2)
		fmt.Fprintf(out, "%d ", 0)

		for i := 0; i < n; i++ {
			d := ((n - 1 - i) / 2) + 1
			if i%2 > 0 {
				d *= -1
			}

			fmt.Fprintf(out, "%d ", d)
		}
		fmt.Fprintln(out)
	}
}

package main

import (
	"fmt"
	"sort"
)

// var sc = bufio.NewScanner(os.Stdin)

// func scan() string {
// 	for sc.Scan() {
// 		break
// 	}
// 	return sc.Text()
// }

// func scanInt() int {
// 	i, err := strconv.Atoi(scan())
// 	if err != nil {
// 		panic(err)
// 	}
// 	return i
// }

// func scanInt8() int8 {
// 	i, err := strconv.ParseInt(scan(), 10, 8)
// 	if err != nil {
// 		panic(err)
// 	}
// 	return int8(i)
// }

// func scanInt64() int64 {
// 	i, err := strconv.ParseInt(scan(), 10, 64)
// 	if err != nil {
// 		panic(err)
// 	}
// 	return i
// }

func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

func min(ints ...int) int {
	a := ints[0]
	n := len(ints)
	for i := 1; i < n; i++ {
		if ints[i] < a {
			a = ints[i]
		}
	}
	return a
}

func max(ints ...int) int {
	a := ints[0]
	n := len(ints)
	for i := 1; i < n; i++ {
		if ints[i] > a {
			a = ints[i]
		}
	}
	return a
}

type Noop struct{}

var noop = Noop{}

func proc() {
	var n int
	fmt.Scan(&n)
	mas := make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&mas[i])
	}
	sort.Slice(mas, func(i, j int) bool { return mas[i] < mas[j] })
	var ans int64
	k := -1
	for i := n - 1; i >= 0; i-- {
		ans += mas[i] * int64(abs(k)) * 2
		if k < 0 {
			k = -k
		} else {
			k = -(k + 1)
		}
	}
	fmt.Println(ans)
	k = -1
	fmt.Printf("0 ")
	for i := n - 1; i >= 0; i-- {
		if i == 0 {
			fmt.Println(k)
		} else {
			fmt.Printf("%d ", k)
		}
		if k < 0 {
			k = -k
		} else {
			k = -(k + 1)
		}
	}
}

func main() {
	// sc.Split(bufio.ScanWords)
	var t int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		proc()
	}
}

package main

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

var sc = bufio.NewScanner(os.Stdin)

func scan() string {
	for sc.Scan() {
		break
	}
	return sc.Text()
}

func scanInt() int {
	i, err := strconv.Atoi(scan())
	if err != nil {
		panic(err)
	}
	return i
}

func scanInt8() int8 {
	i, err := strconv.ParseInt(scan(), 10, 8)
	if err != nil {
		panic(err)
	}
	return int8(i)
}

func scanInt64() int64 {
	i, err := strconv.ParseInt(scan(), 10, 64)
	if err != nil {
		panic(err)
	}
	return i
}

func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

func min(ints ...int) int {
	a := ints[0]
	n := len(ints)
	for i := 1; i < n; i++ {
		if ints[i] < a {
			a = ints[i]
		}
	}
	return a
}

func max(ints ...int) int {
	a := ints[0]
	n := len(ints)
	for i := 1; i < n; i++ {
		if ints[i] > a {
			a = ints[i]
		}
	}
	return a
}

type Noop struct{}

var noop = Noop{}

func proc() {
	n := scanInt()
	mas := make([]int64, n)
	for i := 0; i < n; i++ {
		mas[i] = scanInt64()
	}
	sort.Slice(mas, func(i, j int) bool { return mas[i] < mas[j] })
	var ans int64
	k := -1
	for i := n - 1; i >= 0; i-- {
		ans += mas[i] * int64(abs(k)) * 2
		if k < 0 {
			k = -k
		} else {
			k = -(k + 1)
		}
	}
	fmt.Println(ans)
	k = -1
	fmt.Printf("0 ")
	for i := n - 1; i >= 0; i-- {
		if i == 0 {
			fmt.Println(k)
		} else {
			fmt.Printf("%d ", k)
		}
		if k < 0 {
			k = -k
		} else {
			k = -(k + 1)
		}
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	t := scanInt()
	for i := 0; i < t; i++ {
		proc()
	}
}

package main

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

var (
	in  = bufio.NewScanner(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

var (
	TC, n int
)

type info struct {
	idx, times int
}

func main() {
	defer out.Flush()
	in.Split(bufio.ScanWords)
	TC = nextInt()
	for t := 0; t < TC; t++ {
		n = nextInt()
		a := make([]info, 0)
		for i := 0; i < n; i++ {
			tmp := info{i, nextInt()}
			a = append(a, tmp)
		}
		T, x := solve(a)
		fmt.Fprintln(out, T)
		fmt.Fprintln(out, strings.Trim(fmt.Sprint(x), "[]"))
	}
}

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

	min_time := 0
	x := make([]int, len(a)+1)

	x[0] = 0
	coord := 1
	for _, val := range a {
		x[val.idx+1] = coord
		min_time += (abs(coord) * val.times * 2)
		coord *= -1
		if coord > 0 {
			coord++
		}
	}

	return min_time, x
}

func abs(v int) int {
	if v < 0 {
		return -v
	} else {
		return v
	}
}

func nextStr() string {
	in.Scan()
	return in.Text()
}

func nextInt() int {
	ret, _ := strconv.Atoi(nextStr())
	return ret
}

package main

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

var (
	in  = bufio.NewScanner(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

var (
	TC, n int
)

type info struct {
	idx, times int
}

func main() {
	defer out.Flush()
	in.Split(bufio.ScanWords)
	TC = nextInt()
	for t := 0; t < TC; t++ {
		n = nextInt()
		a := make([]info, 0)
		for i := 0; i < n; i++ {
			tmp := info{i, nextInt()}
			a = append(a, tmp)
		}
		T, x := solve(a)
		fmt.Fprintln(out, T)
		fmt.Fprintln(out, strings.Trim(fmt.Sprint(x), "[]"))
	}
}

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

	min_time := 0
	x := make([]int, len(a)+1)

	x[0] = 0
	coord := 1
	for _, val := range a {
		x[val.idx+1] = coord
		min_time += func(v int) int {
			if v < 0 {
				v *= -1
			}
			return v
		}(coord) * val.times * 2
		coord *= -1
		if coord > 0 {
			coord++
		}
	}

	return min_time, x
}

func nextStr() string {
	in.Scan()
	return in.Text()
}

func nextInt() int {
	ret, _ := strconv.Atoi(nextStr())
	return ret
}

package main

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

var n int

var a []int

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)

	for i := 0; i < t; i++ {
		fmt.Fscan(r, &n)

		a = make([]int, n)

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

		solve()

		fmt.Println()
	}
}

type pair struct {
	k, v int
}

type ByK []*pair

func (a ByK) Len() int           { return len(a) }
func (a ByK) Less(i, j int) bool { return a[i].k < a[j].k }
func (a ByK) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

type item struct {
	la, ra int
	lb, rb int
}

const MAX int = 1e6

func solve() {

	var p []*pair

	for i := 0; i < n; i++ {
		p = append(p, &pair{a[i], i})
	}
	sort.Sort(ByK(p))

	ans := make([]int, n + 1)
	ans[0] = 0

	var dist int64

	r := 0
	right := 1
	left := 1

	for i := n - 1; i >= 0; i-- {
		b := p[i].v
		times := p[i].k

		if r % 2 == 0 {
			ans[b] = right
			dist += int64(times) * int64(right)

			right++

		} else {
			ans[b] = -left
			dist+= int64(times)* int64(left)

			left++
		}

		r++
	}

	fmt.Println(2 * dist)

	for i := 0; i <= n; i++ {
		fmt.Print(ans[i], " ")
	}
}

func add(idx int, delta int, bit *[]int, n int) {
	for ; idx <= n; idx += idx & -idx {
		(*bit)[idx] += delta
	}
}

func get(idx int, bit *[]int) int {
	ret := 0
	for ; idx > 0; idx -= idx & -idx {
		ret += (*bit)[idx];
	}
	return ret
}

//A -  1 3 5 3 1

//B -  4 3 6 1

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

package main

import (
	"bufio"
	//	"container/heap" for heap.Init()
	"fmt"
	"os"
	"runtime"
	"sort"
)

func check(b bool) {
	if !b {
		_, _, line, _ := runtime.Caller(1)
		fmt.Printf("check failed line %d\n", line)
		os.Exit(-1)
	}
}

func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

type IntHeap []int

func (h IntHeap) Len() int            { return len(h) }
func (h IntHeap) Less(i, j int) bool  { return h[i] < h[j] }
func (h IntHeap) Swap(i, j int)       { h[i], h[j] = h[j], h[i] }
func (h *IntHeap) Push(x interface{}) { *h = append(*h, x.(int)) }
func (h *IntHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

const (
	INIT = -1
)

var (
	n   int
	arr []int
)

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

	var tc int
	fmt.Fscan(r, &tc)

	for ; tc > 0; tc-- {
		fmt.Fscan(r, &n)
		arr = make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(r, &arr[i])
		}

		iord := make([]int, n)
		for i := 0; i < n; i++ {
			iord[i] = i
		}
		sort.Slice(iord, func(i, j int) bool {
			return arr[iord[i]] < arr[iord[j]]
		})

		// for i := 0; i < n; i++ {
		// 	fmt.Printf("%d ", arr[iord[i]])
		// }
		// fmt.Println()

		sum := 0
		cnt := 0
		pos := make([]int, n+1)
		for i := n - 1; i >= 0; i-- {
			diff := cnt/2 + 1

			sum += 2 * arr[iord[i]] * diff

			if (cnt % 2) == 1 {
				diff = -diff
			}
			pos[iord[i]+1] = diff
			cnt++
		}

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

	}
}

package main

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

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

func scan(a ...interface{})             { fmt.Fscan(reader, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }

func redirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

func B() {
	n := 0
	scan(&n)
	a := make([][2]int, n)
	for i := range a {
		a[i][0] = i
		scan(&a[i][1])
	}
	sort.Slice(a, func(i, j int) bool { return a[i][1] > a[j][1] })

	x := 1
	time := 0
	ans := make([]int, n)
	for i := range a {
		y := x
		if y < 0 {
			y = -y
		}
		time += a[i][1] * 2 * y
		ans[a[i][0]] = x
		if x < 0 {
			x = -x + 1
		} else {
			x = -x
		}
	}

	printf("%v\n0 ", time)
	for i := 0; i < n; i++ {
		printf("%v ", ans[i])
	}
	printf("\n")
}

func main() {
	defer writer.Flush()
	//redirectInputToFile(reader, "input.txt")

	T := 1
	scan(&T)
	for t := 0; t < T; t++ {
		B()
	}
}


package main

import (
	"bufio"
	"fmt"
	"io"
	"math/bits"
	"os"
)

func main() {
	io := NewFastIO(os.Stdin, os.Stdout)
	defer io.Flush()
	clim := int('t'-'a') + 1

	n := io.NextInt()
	g := make([][]int, n+1)
	for i := 0; i < n-1; i++ {
		u, v := io.NextInt(), io.NextInt()
		g[u] = append(g[u], v)
		g[v] = append(g[v], u)
	}
	a := make([]int, n+1)
	for i, c := range io.Next() {
		a[i+1] = 1 << (int(c) - 'a')
	}

	good := []int{0}
	for i := 0; i < clim; i++ {
		good = append(good, 1<<i)
	}
	upto, cnts := make([]int, n+1), make([]int, 1<<clim)
	ans := make([]int64, n+1)

	centroidDecompose(g, func(cen int, off []bool) {
		var upd func(u, p, d int)
		upd = func(u, p, d int) {
			upto[u] = upto[p] ^ a[u]
			cnts[upto[u]] += d
			for _, v := range g[u] {
				if v != p && !off[v] {
					upd(v, u, d)
				}
			}
		}

		var cenCnt int64
		var get func(u, p int) int64
		get = func(u, p int) int64 {
			var cnt int64
			for _, x := range good {
				cnt += int64(cnts[upto[u]^a[cen]^x])
			}
			if bits.OnesCount(uint(upto[u])) <= 1 {
				cenCnt++
			}
			for _, v := range g[u] {
				if v != p && !off[v] {
					cnt += get(v, u)
				}
			}
			ans[u] += int64(cnt)
			return cnt
		}

		var cnt int64
		upd(cen, 0, +1)
		for _, v := range g[cen] {
			if !off[v] {
				upd(v, cen, -1)
				cnt += get(v, cen)
				upd(v, cen, +1)
			}
		}
		upd(cen, 0, -1)
		ans[cen] += (cnt-cenCnt)/2 + cenCnt + 1
	})

	for _, x := range ans[1:] {
		fmt.Fprint(io, x, " ")
	}
	fmt.Fprintln(io)
}

func centroidDecompose(g [][]int, f func(int, []bool)) {
	sz, off := make([]int, len(g)), make([]bool, len(g))

	var calcSz func(u, p int)
	calcSz = func(u, p int) {
		sz[u] = 1
		for _, v := range g[u] {
			if v != p && !off[v] {
				calcSz(v, u)
				sz[u] += sz[v]
			}
		}
	}

	var getCentroid func(u, p, cur int) int
	getCentroid = func(u, p, cur int) int {
		for _, v := range g[u] {
			if v != p && !off[v] {
				if sz[v]*2 > cur {
					return getCentroid(v, u, cur)
				}
			}
		}
		return u
	}

	var decompose func(int)
	decompose = func(u int) {
		calcSz(u, u)
		cen := getCentroid(u, u, sz[u])
		f(cen, off)
		off[cen] = true
		for _, v := range g[cen] {
			if !off[v] {
				decompose(v)
			}
		}
	}
	decompose(1)
}

type FastIO struct {
	*bufio.Reader
	*bufio.Writer
	c [1]byte
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	if n, _ := f.Read(f.c[:]); n != 0 {
		return f.c[0]
	}
	return 0
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) Next() string {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	var s []byte
	for ; c > ' '; c = f.readC() {
		s = append(s, c)
	}
	return string(s)
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"math/bits"
	"os"
)

func main() {
	io := NewFastIO(os.Stdin, os.Stdout)
	defer io.Flush()
	cLim := int('t'-'a') + 1

	n := io.NextInt()
	g := make([][]int, n+1)
	for i := 0; i < n-1; i++ {
		u, v := io.NextInt(), io.NextInt()
		g[u] = append(g[u], v)
		g[v] = append(g[v], u)
	}
	a := make([]int, n+1)
	for i, c := range io.Next() {
		a[i+1] = 1 << (int(c) - 'a')
	}

	good := []int{0}
	for i := 0; i < cLim; i++ {
		good = append(good, 1<<i)
	}
	upto, cnts := make([]int, n+1), make([]int, 1<<cLim)
	ans := make([]int64, n+1)

	centroidDecompose(g, func(cen int) {
		var upd func(u, x, d int)
		upd = func(u, x, d int) {
			upto[u] = x ^ a[u]
			cnts[upto[u]] += d
			for _, v := range g[u] {
				upd(v, upto[u], d)
			}
		}

		var cenCnt int64
		var get func(int) int64
		get = func(u int) int64 {
			var cnt int64
			for _, x := range good {
				cnt += int64(cnts[upto[u]^a[cen]^x])
			}
			if bits.OnesCount(uint(upto[u])) <= 1 {
				cenCnt++
			}
			for _, v := range g[u] {
				cnt += get(v)
			}
			ans[u] += int64(cnt)
			return cnt
		}

		var cnt int64
		upd(cen, 0, +1)
		for _, v := range g[cen] {
			upd(v, a[cen], -1)
			cnt += get(v)
			upd(v, a[cen], +1)
		}
		upd(cen, 0, -1)
		ans[cen] += (cnt-cenCnt)/2 + cenCnt + 1
	})

	for _, x := range ans[1:] {
		fmt.Fprint(io, x, " ")
	}
	fmt.Fprintln(io)
}

func centroidDecompose(g [][]int, f func(int)) {
	remove := func(u, v int) {
		j := len(g[u]) - 1
		for i, x := range g[u] {
			if x == v {
				g[u][i] = g[u][j]
				g[u] = g[u][:j]
				return
			}
		}
	}

	sz := make([]int, len(g))
	var calcSz func(int)
	calcSz = func(u int) {
		sz[u] = 1
		for _, v := range g[u] {
			remove(v, u)
			calcSz(v)
			sz[u] += sz[v]
		}
	}
	calcSz(1)

	var getCentroid func(int) int
	getCentroid = func(u int) int {
		for _, v := range g[u] {
			if sz[v]*2 > sz[u] {
				remove(u, v)
				g[v], sz[u], sz[v] = append(g[v], u), sz[u]-sz[v], sz[u]
				return getCentroid(v)
			}
		}
		return u
	}

	var decompose func(int)
	decompose = func(u int) {
		cen := getCentroid(u)
		f(cen)
		for _, v := range g[cen] {
			remove(v, cen)
			decompose(v)
		}
	}
	decompose(1)
}

type FastIO struct {
	*bufio.Writer
	r    io.Reader
	buf  [8192]byte
	i, n int
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{r: r, Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	if f.i == f.n {
		f.n, _ = f.r.Read(f.buf[:])
		if f.n == 0 {
			return 0
		}
		f.i = 0
	}
	f.i++
	return f.buf[f.i-1]
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) Next() string {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	var s []byte
	for ; c > ' '; c = f.readC() {
		s = append(s, c)
	}
	return string(s)
}