package main

import (
	"fmt"
	"os"
)

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

func pow(n int64, x int) int64 {
	y := int64(1)
	if x == 0 {
		return y
	}

	for x > 1 {
		if x&1 == 1 {
			y *= n
		}

		n *= n
		x >>= 1
	}

	return y * n

}

func readnumbers() (nums []int) {
	fileInfo, _ := os.Stdin.Stat()
	p := make([]byte, fileInfo.Size())
	os.Stdin.Read(p)

	num, neg := 0, 1
	for i := 0; i < len(p); i++ {
		if p[i] >= '0' {
			num = 10*num + int(p[i]) - 48
		} else if p[i] == '-' {
			neg = -1
		} else if p[i] != '\r' {
			nums = append(nums, neg*num)
			num, neg = 0, 1
		}
	}

	if len(p) > 0 && p[len(p)-1] >= '0' {
		nums = append(nums, neg*num)
	}

	return
}

func main() {
	nums := readnumbers()
	idx := 0

	t := nums[idx]
	idx++
	for i := 0; i < t; i++ {
		n, k := nums[idx], nums[idx+1]
		idx += 2

		a := nums[idx : idx+n]
		idx += n

		fxk, res := int(1e9), 0
		for j := 0; j < n-k; j++ {
			x := (a[j] + a[j+k]) / 2
			fik := max(x-a[j], a[j+k]-x)
			if fik < fxk {
				fxk = fik
				res = x
			}
		}

		fmt.Println(res)
	}
}

package main

import (
	"os"
	"runtime/debug"
	"strconv"
)

var stdout []byte

func readnumbers() (nums []int) {
	fileInfo, _ := os.Stdin.Stat()
	p := make([]byte, fileInfo.Size())
	os.Stdin.Read(p)

	num, neg := 0, 1
	for i := 0; i < len(p); i++ {
		if p[i] >= '0' {
			num = 10*num + int(p[i]-'0')
		} else if p[i] == '-' {
			neg = -1
		} else if p[i] != '\r' {
			nums = append(nums, neg*num)
			num, neg = 0, 1
		}
	}

	if len(p) > 0 && p[len(p)-1] >= '0' {
		nums = append(nums, neg*num)
	}

	return
}

func print(b []byte) {
	stdout = append(stdout, b...)
	stdout = append(stdout, '\n')
}

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

func main() {
	debug.SetGCPercent(-1)
	nums := readnumbers()
	idx := 0

	t := nums[idx]
	idx++
	for i := 0; i < t; i++ {
		n, k := nums[idx], nums[idx+1]
		idx += 2

		fxk, res := int(1e9), 0
		for j := 0; j < n-k; j++ {
			x := (nums[idx+j] + nums[idx+j+k]) / 2
			fik := max(x-nums[idx+j], nums[idx+j+k]-x)
			if fik < fxk {
				fxk = fik
				res = x
			}
		}

		idx += n
		print([]byte(strconv.Itoa(res)))
	}

	os.Stdout.Write(stdout)
}

package main

import (
	"fmt"
	"os"
)

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

func main() {
	var nums []int

	fileInfo, _ := os.Stdin.Stat()
	p := make([]byte, fileInfo.Size())
	os.Stdin.Read(p)

	num, neg := 0, 1
	for i := 0; i < len(p); i++ {
		if p[i] >= '0' {
			num = 10*num + int(p[i]) - 48
		} else if p[i] == '-' {
			neg = -1
		} else if p[i] != '\r' {
			nums = append(nums, neg*num)
			num, neg = 0, 1
		}
	}

	if len(p) > 0 && p[len(p)-1] >= '0' {
		nums = append(nums, neg*num)
	}

	idx := 0
	t := nums[idx]
	idx++
	for i := 0; i < t; i++ {
		n, k := nums[idx], nums[idx+1]
		idx += 2

		fxk, res := int(1e9), 0
		for j := 0; j < n-k; j++ {
			x := (nums[idx+j] + nums[idx+j+k]) / 2
			fik := max(x-nums[idx+j], nums[idx+j+k]-x)
			if fik < fxk {
				fxk = fik
				res = x
			}
		}

		idx += n
		fmt.Println(res)
	}
}

package main

import (
	"os"
	"strconv"
)

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

func main() {
	var nums []int

	fileInfo, _ := os.Stdin.Stat()
	p := make([]byte, fileInfo.Size())
	os.Stdin.Read(p)

	num, neg := 0, 1
	for i := 0; i < len(p); i++ {
		if p[i] >= '0' {
			num = 10*num + int(p[i]-'0')
		} else if p[i] == '-' {
			neg = -1
		} else if p[i] != '\r' {
			nums = append(nums, neg*num)
			num, neg = 0, 1
		}
	}

	if len(p) > 0 && p[len(p)-1] >= '0' {
		nums = append(nums, neg*num)
	}

	idx := 0
	t := nums[idx]
	idx++

	var toPrint []byte
	for i := 0; i < t; i++ {
		n, k := nums[idx], nums[idx+1]
		idx += 2

		fxk, res := int(1e9), 0
		for j := 0; j < n-k; j++ {
			x := (nums[idx+j] + nums[idx+j+k]) / 2
			fik := max(x-nums[idx+j], nums[idx+j+k]-x)
			if fik < fxk {
				fxk = fik
				res = x
			}
		}

		idx += n
		toPrint = append(toPrint, []byte(strconv.Itoa(res))...)
		toPrint = append(toPrint, '\n')
	}

	os.Stdout.Write(toPrint)
}

package main

import (
	"fmt"
	"os"
)

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

func readnumbers() (nums []int) {
	fileInfo, _ := os.Stdin.Stat()
	p := make([]byte, fileInfo.Size())
	os.Stdin.Read(p)

	num, neg := 0, 1
	for i := 0; i < len(p); i++ {
		if p[i] >= '0' {
			num = 10*num + int(p[i]) - 48
		} else if p[i] == '-' {
			neg = -1
		} else if p[i] != '\r' {
			nums = append(nums, neg*num)
			num, neg = 0, 1
		}
	}

	if len(p) > 0 && p[len(p)-1] >= '0' {
		nums = append(nums, neg*num)
	}

	return
}

func main() {
	nums := readnumbers()
	idx := 0

	t := nums[idx]
	idx++
	for i := 0; i < t; i++ {
		n, k := nums[idx], nums[idx+1]
		idx += 2

		fxk, res := int(1e9), 0
		for j := 0; j < n-k; j++ {
			x := (nums[idx+j] + nums[idx+j+k]) / 2
			fik := max(x-nums[idx+j], nums[idx+j+k]-x)
			if fik < fxk {
				fxk = fik
				res = x
			}
		}

		idx += n
		fmt.Println(res)
	}
}

package main

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

type inputReader struct {
    *bufio.Scanner
}

func newInputReader() *inputReader {
    input := inputReader{ bufio.NewScanner(os.Stdin) }
    input.Split(bufio.ScanWords)
    return &input
}

func (input *inputReader) readInt() int {
    input.Scan()
    result, _ := strconv.Atoi(input.Text())
    return result
}

func main() {
    input := newInputReader()
    t := input.readInt()

    a := make([]int, 200000, 200000)
    for kase := 1; kase <= t; kase++ {
        n := input.readInt()
        k := input.readInt()
        for i := 0; i < n; i++ {
            a[i] = input.readInt()
        }

        pos := 0
        mindiff := math.MaxInt32
        for i := 0; i < n - k; i++ {
            diff := a[i + k] - a[i]
            if diff < mindiff {
                mindiff = diff
                pos = i
            }
        }

        fmt.Println((a[pos] + a[pos + k]) / 2)
    }
}

package main

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

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

	var T, n, k, x int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &k)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		if k == 0 {
			Fprintln(out, a[0])
			continue
		}
		min := int(1e9)
		for i, v := range a[k:] {
			if d := (v - a[i] + 1) / 2; d < min {
				min = d
				x = (v + a[i]) / 2
			}
		}
		Fprintln(out, x)
	}
}

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

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := 0
	fmt.Fscanf(in, "%d\n", &t)
	for tt := 0; tt < t; tt++ {
		n, k := 0, 0
		fmt.Fscanf(in, "%d %d\n", &n, &k)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d", &a[i])
		}
		//fmt.Println(a)
		fmt.Fscanf(in, "\n")
		min := a[n-1] - a[0]
		ans := (a[0] + a[n-1]) / 2
		for i := 0; i < n; i++ {
			till := i + k
			if till >= n {
				break
			}
			if a[till]-a[i] < min {
				min = a[till] - a[i]
				ans = (a[till] + a[i]) / 2
			}
		}
		fmt.Fprintf(out, "%d\n", ans)
	}
	return
}

package main

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

var a [200100]int

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

	var t int
	fmt.Fscan(in, &t)
	for t > 0 {
		var n, k int
		fmt.Fscan(in, &n)
		fmt.Fscan(in, &k)
		k++
		for i := 1; i <= n; i++ {
			fmt.Fscan(in, &a[i])
		}

		if k == 1 {
			fmt.Println(a[1])
			t--
			continue
		}

		mn := 2000000000
		_ = mn
		x := -1

		for i := k; i <= n; i++ {
			l := i - k + 1
			r := i
			dist := a[r] - a[l]
			md := dist % 2
			dist /= 2
			dist += md
			if dist < mn {
				mn = dist
				x = (a[l] + a[r]) / 2
			}
		}

		fmt.Println(x)
		t--
	}
}

package main

import (
	"fmt"
)

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

func pow(n int64, x int) int64 {
	y := int64(1)
	if x == 0 {
		return y
	}

	for x > 1 {
		if x&1 == 1 {
			y *= n
		}

		n *= n
		x >>= 1
	}

	return y * n

}

func main() {
	var t int
	fmt.Scanf("%d", &t)
	for i := 0; i < t; i++ {
		var n, k int
		fmt.Scanf("%d%d", &n, &k)
		a := make([]int64, n)
		for j := 0; j < n; j++ {
			fmt.Scanf("%d", &a[j])
		}

		fxk := pow(int64(10), 18)
		res := int64(0)
		for j := 0; j < n-k; j++ {
			x := (a[j] + a[j+k]) / 2
			fik := max(x-a[j], a[j+k]-x)
			if fik < fxk {
				fxk = fik
				res = x
			}
		}

		fmt.Println(res)
	}
}

package main

import (
	"fmt"
)

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

func pow(n int64, x int) int64 {
	y := int64(1)
	if x == 0 {
		return y
	}

	for x > 1 {
		if x&1 == 1 {
			y *= n
		}

		n *= n
		x >>= 1
	}

	return y * n

}

func main() {
	var t int
	fmt.Scanf("%d\n", &t)
	for i := 0; i < t; i++ {
		var n, k int
		fmt.Scanf("%d %d\n", &n, &k)
		a := make([]int64, n)
		for j := 0; j < n; j++ {
			fmt.Scanf("%d ", &a[j])
		}

		fxk := pow(int64(10), 18)
		res := int64(0)
		for j := 0; j < n-k; j++ {
			x := (a[j] + a[j+k]) / 2
			fik := max(x-a[j], a[j+k]-x)
			if fik < fxk {
				fxk = fik
				res = x
			}
		}

		fmt.Println(res)
	}
}

package main

import (
	"fmt"
)

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

func pow(n int64, x int) int64 {
	y := int64(1)
	if x == 0 {
		return y
	}

	for x > 1 {
		if x&1 == 1 {
			y *= n
		}

		n *= n
		x >>= 1
	}

	return y * n

}

func main() {
	var t int
	fmt.Scanf("%d\n", &t)
	for i := 0; i < t; i++ {
		var n, k int
		fmt.Scanf("%d %d\n", &n, &k)
		a := make([]int64, n)
		for j := 0; j < n; j++ {
			fmt.Scanf("%d", &a[j])
		}

		fxk := pow(int64(10), 18)
		res := int64(0)
		for j := 0; j < n-k; j++ {
			x := (a[j] + a[j+k]) / 2
			fik := max(x-a[j], a[j+k]-x)
			if fik < fxk {
				fxk = fik
				res = x
			}
		}

		fmt.Println(res)
	}
}


package main

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

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

	max := func(a, b int) int {
		if a > b {
			return a
		}
		return b
	}
	var st [][18]int
	stInit := func(a []int) {
		n := len(a)
		st = make([][18]int, n)
		for i := range st {
			st[i][0] = a[i]
		}
		for j := uint(1); 1<<j <= n; j++ {
			for i := 0; i+(1<<j)-1 < n; i++ {
				st[i][j] = max(st[i][j-1], st[i+(1<<(j-1))][j-1])
			}
		}
	}
	stQuery := func(l, r int) int {
		k := uint(bits.Len(uint(r-l+1)) - 1)
		return max(st[l][k], st[r-(1<<k)+1][k])
	}
	type hero struct{ power, endurance int }

	var t int
	for Fscan(in, &t); t > 0; t-- {
		var n, m int
		Fscan(in, &n)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		stInit(a)
		Fscan(in, &m)
		heroes := make([]hero, m)
		for i := range heroes {
			Fscan(in, &heroes[i].power, &heroes[i].endurance)
		}
		sort.Slice(heroes, func(i, j int) bool {
			return heroes[i].endurance > heroes[j].endurance || heroes[i].endurance == heroes[j].endurance && heroes[i].power > heroes[j].power
		})

		maxPowers := make([]int, n+1)
		maxPower := 0
		for i := 0; i < m; {
			hi := heroes[i]
			maxPower = max(maxPower, hi.power)
			j := i + 1
			for ; j < m && heroes[j].endurance == hi.endurance; j++ {
			}
			nextEndurance := 0
			if j < m {
				nextEndurance = heroes[j].endurance
			}
			for k := hi.endurance; k > nextEndurance; k-- {
				maxPowers[k] = maxPower
			}
			i = j
		}
		ans := 0
		for l := 0; l < n; ans++ {
			maxKills := sort.Search(n-l+1, func(kills int) bool {
				if kills == 0 {
					return !true
				}
				maxMonsterPower := stQuery(l, l+kills-1)
				return !(maxMonsterPower <= maxPowers[kills])
			}) - 1
			if maxKills == 0 {
				ans = -1
				break
			}
			l += maxKills
		}
		Fprintln(out, ans)
	}
}

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

package main

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

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

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

func init() {
	ReadString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	// r.Buffer(make([]byte, 1024), int(1e+11)) // for AtCoder
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

/*********** DP sub-functions ***********/

// ChMin accepts a pointer of integer and a target value.
// If target value is SMALLER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMin(updatedValue *int, target int) bool {
	if *updatedValue > target {
		*updatedValue = target
		return true
	}
	return false
}

// ChMax accepts a pointer of integer and a target value.
// If target value is LARGER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMax(updatedValue *int, target int) bool {
	if *updatedValue < target {
		*updatedValue = target
		return true
	}
	return false
}

// NthBit returns nth bit value of an argument.
// n starts from 0.
func NthBit(num, nth int) int {
	return num >> uint(nth) & 1
}

// OnBit returns the integer that has nth ON bit.
// If an argument has nth ON bit, OnBit returns the argument.
func OnBit(num, nth int) int {
	return num | (1 << uint(nth))
}

// OffBit returns the integer that has nth OFF bit.
// If an argument has nth OFF bit, OffBit returns the argument.
func OffBit(num, nth int) int {
	return num & ^(1 << uint(nth))
}

// PopCount returns the number of ON bit of an argument.
func PopCount(num int) int {
	res := 0

	for i := 0; i < 70; i++ {
		if ((num >> uint(i)) & 1) == 1 {
			res++
		}
	}

	return res
}

/*********** Arithmetic ***********/

// Max returns the max integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Max(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m < integer {
			m = integer
		}
	}
	return m
}

// Min returns the min integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Min(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m > integer {
			m = integer
		}
	}
	return m
}

// Sum returns multiple integers sum.
func Sum(integers ...int) int {
	s := 0

	for _, i := range integers {
		s += i
	}

	return s
}

// PowInt is integer version of math.Pow
// PowInt calculate a power by Binary Power (二分累乗法(O(log e))).
func PowInt(a, e int) int {
	if a < 0 || e < 0 {
		panic(errors.New("[argument error]: PowInt does not accept negative integers"))
	}

	if e == 0 {
		return 1
	}

	if e%2 == 0 {
		halfE := e / 2
		half := PowInt(a, halfE)
		return half * half
	}

	return a * PowInt(a, e-1)
}

// AbsInt is integer version of math.Abs
func AbsInt(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

// Gcd returns the Greatest Common Divisor of two natural numbers.
// Gcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Gcd uses the Euclidean Algorithm.
func Gcd(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}
	if a < b {
		a, b = b, a
	}

	// Euclidean Algorithm
	for b > 0 {
		div := a % b
		a, b = b, div
	}

	return a
}

// Lcm returns the Least Common Multiple of two natural numbers.
// Lcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Lcd uses the Euclidean Algorithm indirectly.
func Lcm(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}

	// a = a'*gcd, b = b'*gcd, a*b = a'*b'*gcd^2
	// a' and b' are relatively prime numbers
	// gcd consists of prime numbers, that are included in a and b
	gcd := Gcd(a, b)

	// not (a * b / gcd), because of reducing a probability of overflow
	return (a / gcd) * b
}

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

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

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

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

	return string(res)
}

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

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

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

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

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

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

const MOD = 1000000000 + 7
const ALPHABET_NUM = 26
const INF_INT64 = math.MaxInt64
const INF_BIT60 = 1 << 60

type Hero struct {
	key  int
	p, s int
}
type HeroList []*Hero
type byKey struct {
	HeroList
}

func (l HeroList) Len() int {
	return len(l)
}
func (l HeroList) Swap(i, j int) {
	l[i], l[j] = l[j], l[i]
}

func (l byKey) Less(i, j int) bool {
	return l.HeroList[i].key < l.HeroList[j].key
}

// how to use
// L := make(HeroList, 0, 200000+5)
// L = append(L, &Hero{key: intValue})
// sort.Stable(byKey{ L })                // Stable ASC
// sort.Stable(sort.Reverse(byKey{ L }))  // Stable DESC

var t int
var n, m int
var A, P, S []int
var st *SegTreeRMQ

func main() {
	t = ReadInt()

	for tc := 0; tc < t; tc++ {
		n = ReadInt()
		A = ReadIntSlice(n)
		m = ReadInt()
		P, S = make([]int, m), make([]int, m)
		for i := 0; i < m; i++ {
			p, s := ReadInt2()
			P[i], S[i] = p, s
		}

		solve()
	}
}

func solve() {
	L := make(HeroList, 0)
	for i := 0; i < m; i++ {
		p, s := P[i], S[i]
		L = append(L, &Hero{key: s, p: p, s: s})
	}
	sort.Stable(byKey{L})

	// enduranceでソートされたヒーロー列
	SS := make([]int, m)
	for i := 0; i < m; i++ {
		SS[i] = L[i].s
	}
	// enduranceのソート列に従ってpowerをセグメントツリーに放り込む
	st = NewSegTreeRMQ(m, 0)
	for i := 0; i < m; i++ {
		st.Update(i, L[i].p)
	}

	l := 0
	ans := 0
	for l < n {
		length := sub2(l, SS)
		if length == -1 {
			fmt.Println(-1)
			return
		} else {
			l += length
			ans++
		}
	}

	fmt.Println(ans)
}

func sub(l int, H []int) int {
	// m は中央を意味する何らかの値
	isOK := func(m int) bool {
		if H[m] >= l {
			return true
		}
		return false
	}

	ng, ok := -1, len(H)
	for int(math.Abs(float64(ok-ng))) > 1 {
		mid := (ok + ng) / 2
		if isOK(mid) {
			ok = mid
		} else {
			ng = mid
		}
	}

	return ok
}

func sub2(s int, H []int) int {
	maxMP := A[s]
	length := -1
	for l := 0; s+l < n; l++ {
		maxMP = Max(maxMP, A[s+l])
		idx := sub(l+1, H)
		if idx == len(H) {
			break
		}

		maxHP := st.Query(idx, len(H))
		if maxHP >= maxMP {
			length = l + 1
		} else {
			break
		}
	}

	return length
}

// RMQを高速で処理するセグメントツリー構造体
type SegTreeRMQ struct {
	ElemNum int // 配列の要素数
	// Dat     [2*200005 - 1]int // セグメント木のノード集合
	Dat        []int // セグメント木のノード集合
	INIT_VALUE int   // 初期化用の値
}

// 配列の要素数と初期化用の値を渡す
func NewSegTreeRMQ(elemNum, initValue int) *SegTreeRMQ {
	st := new(SegTreeRMQ)
	st.INIT_VALUE = initValue
	// st.Dat = make([]int, 4*elemNum+10)

	// 要素数を2べきの数に補正する
	st.ElemNum = 1
	for st.ElemNum < elemNum {
		st.ElemNum *= 2
	}
	st.Dat = make([]int, 2*st.ElemNum)

	// すべての節点を初期化する
	for i := 0; i < 2*st.ElemNum-1; i++ {
		st.Dat[i] = st.INIT_VALUE
	}

	return st
}

// k番目の値（0-based）をaに変更する
func (st *SegTreeRMQ) Update(k, a int) {
	// 葉の節点を変更
	k += st.ElemNum - 1
	st.Dat[k] = a

	// 登りながら各節点を変更
	for k > 0 {
		k = (k - 1) / 2
		st.Dat[k] = int(math.Max(float64(st.Dat[2*k+1]), float64(st.Dat[2*k+2])))
	}
}

// [a, b)の区間の最小値を返す
// 再帰関数のラッパー関数
func (st *SegTreeRMQ) Query(a, b int) int {
	return st.subQuery(a, b, 0, 0, st.ElemNum)
}

// [a, b)の区間の最小値を返す
// k: 検索先のセグメントツリーのノード番号(0-based)
// l, r: k番目のセグメントツリーノードの半開区間の左端と右端, [l, r)
func (st *SegTreeRMQ) subQuery(a, b, k, l, r int) int {
	if r <= a || b <= l {
		return st.INIT_VALUE
	}

	if a <= l && r <= b {
		return st.Dat[k]
	}

	vl, vr := st.subQuery(a, b, 2*k+1, l, (l+r)/2), st.subQuery(a, b, 2*k+2, (l+r)/2, r)
	return int(math.Max(float64(vl), float64(vr)))
}

// MODはとったか？
// 遷移だけじゃなくて最後の最後でちゃんと取れよ？

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

package main

import (
	"bufio"
	"container/heap"
	"os"
	"runtime"
	"sort"
	"strconv"
)

/* solution */

// Hero ...
type Hero struct {
	i int
	p int
	s int
}

// StaminaList ...
type StaminaList []Hero

func (h StaminaList) Len() int           { return len(h) }
func (h StaminaList) Less(i, j int) bool { return h[i].s < h[j].s }
func (h StaminaList) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

// PowerHeap ...
type PowerHeap struct {
	pointer map[int]int
	heap    []Hero
}

func (h PowerHeap) Len() int           { return len(h.heap) }
func (h PowerHeap) Less(i, j int) bool { return h.heap[i].p > h.heap[j].p }
func (h PowerHeap) Swap(i, j int) {
	h.heap[i], h.heap[j] = h.heap[j], h.heap[i]
	h.pointer[h.heap[i].i], h.pointer[h.heap[j].i] = i, j
}

// Push ...
func (h *PowerHeap) Push(x interface{}) {
	h.heap = append(h.heap, x.(Hero))
	h.pointer[x.(Hero).i] = len(h.heap) - 1
}

// Pop ...
func (h *PowerHeap) Pop() interface{} {
	old := h.heap
	n := len(old)
	x := old[n-1]
	h.heap = old[0 : n-1]
	return x
}

func solve() {
	t := readInt()
	for i := 0; i < t; i++ {
		printInt(solveOne())
	}
}

func solveOne() int {

	n := readInt()
	beasts := make([]int, n)
	maxBeast := 0
	for i := 0; i < n; i++ {
		p := readInt()
		beasts[i] = p
		if p > maxBeast {
			maxBeast = p
		}
	}

	m := readInt()
	stamina := make(StaminaList, m)
	power := PowerHeap{make(map[int]int), make([]Hero, 0)}
	for i := 0; i < m; i++ {
		hero := Hero{i, readInt(), readInt()}
		stamina[i] = hero
		heap.Push(&power, hero)
	}

	if maxBeast > power.heap[0].p {
		return -1
	}

	sort.Sort(stamina)

	d := make([]int, n+1)
	i := 0
	for s := 1; s <= n; s++ {
		for i < m && stamina[i].s < s && len(power.heap) > 0 {
			heap.Remove(&power, power.pointer[stamina[i].i])
			i++
		}
		if len(power.heap) == 0 {
			break
		}
		d[s] = power.heap[0].p
	}

	ans := 1
	s := 1
	giant := -1
	for i := 0; i < n; {
		if beasts[i] > giant {
			giant = beasts[i]
		}
		if d[s] >= giant {
			i++
			s++
		} else {
			s = 1
			giant = -1
			ans++
		}
	}

	return ans

}

/* 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.ScanWords)

	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 (
	"bufio"
	"fmt"
	"os"
	"sort"
)

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

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

func Dprint(format string, a ...interface{}) {
	if Debug {
		fmt.Fprintf(os.Stderr, format+"\n", a...)
	}
}

type hero struct {
	pow int
	end int
}

// findHero will find the weakest hero with pow >= pow.
func findHero(pow int, he []hero, start int, end int) int {
	if start == end {
		if he[start].pow < pow {
			// not found
			return -1
		}
		return start
	}

	mid := (start + end) / 2
	if he[mid].pow >= pow {
		return findHero(pow, he, start, mid)
	} else {
		return findHero(pow, he, mid+1, end)
	}
}

func calc(a []int, he []hero) int {
	// Sort heroes by power, low to high.
	sort.Slice(he, func(i, j int) bool {
		return he[i].pow < he[j].pow
	})

	{
		// Modify heroes list so that when i<j, he[i].end > he[j].end.
		var he2 []hero
		// Invariant: if i<j, then he[i].end > he[j].end
		for _, h := range he {
			for len(he2) > 0 {
				if he2[len(he2)-1].end > h.end {
					break
				}
				he2 = he2[0 : len(he2)-1]
			}
			he2 = append(he2, h)
		}
		he = he2
	}

	// Start killing monsters with the weakest hero (that has highest endurance).
	nKills := 0 // how many monsters have we killed in this iteration
	iters := 0  // total number of iterations
	hStart := 0 // the weakest hero that can be used in the current iteration.

	for i := 0; i < len(a); {
		ai := a[i]
		hIdx := findHero(ai, he, hStart, len(he)-1) // find the weakest hero (with biggest endurance) that can kill monster ai.
		if hIdx == -1 {
			// No hero (beginning with hStart) can kill this monster. We always have the strongest hero available.
			return -1
		}

		if he[hIdx].end < nKills+1 {
			// This hero can't kill a monster because there is not enough endurance. Next round.
			iters++
			nKills = 0
			hStart = 0
			continue
		}

		// This hero can kill our monster.
		nKills++
		hStart = hIdx
		i++
	}

	return iters + 1
}

func main() {
	defer writer.Flush()

	var t int
	Scanf("%d\n", &t)
	for i := 0; i < t; i++ {
		var n int
		Scanf("%d\n", &n)

		a := make([]int, n)
		for j := 0; j < n; j++ {
			Scanf("%d", &a[j])
		}
		Scanf("\n")

		var m int
		Scanf("%d\n", &m)

		he := make([]hero, m)
		for j := 0; j < m; j++ {
			Scanf("%d %d\n", &he[j].pow, &he[j].end)
		}

		Printf("%d\n", calc(a, he))
	}
}

package main

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

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

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

func init() {
	ReadString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	// r.Buffer(make([]byte, 1024), int(1e+11)) // for AtCoder
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

/*********** DP sub-functions ***********/

// ChMin accepts a pointer of integer and a target value.
// If target value is SMALLER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMin(updatedValue *int, target int) bool {
	if *updatedValue > target {
		*updatedValue = target
		return true
	}
	return false
}

// ChMax accepts a pointer of integer and a target value.
// If target value is LARGER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMax(updatedValue *int, target int) bool {
	if *updatedValue < target {
		*updatedValue = target
		return true
	}
	return false
}

// NthBit returns nth bit value of an argument.
// n starts from 0.
func NthBit(num, nth int) int {
	return num >> uint(nth) & 1
}

// OnBit returns the integer that has nth ON bit.
// If an argument has nth ON bit, OnBit returns the argument.
func OnBit(num, nth int) int {
	return num | (1 << uint(nth))
}

// OffBit returns the integer that has nth OFF bit.
// If an argument has nth OFF bit, OffBit returns the argument.
func OffBit(num, nth int) int {
	return num & ^(1 << uint(nth))
}

// PopCount returns the number of ON bit of an argument.
func PopCount(num int) int {
	res := 0

	for i := 0; i < 70; i++ {
		if ((num >> uint(i)) & 1) == 1 {
			res++
		}
	}

	return res
}

/*********** Arithmetic ***********/

// Max returns the max integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Max(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m < integer {
			m = integer
		}
	}
	return m
}

// Min returns the min integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Min(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m > integer {
			m = integer
		}
	}
	return m
}

// Sum returns multiple integers sum.
func Sum(integers ...int) int {
	s := 0

	for _, i := range integers {
		s += i
	}

	return s
}

// PowInt is integer version of math.Pow
// PowInt calculate a power by Binary Power (二分累乗法(O(log e))).
func PowInt(a, e int) int {
	if a < 0 || e < 0 {
		panic(errors.New("[argument error]: PowInt does not accept negative integers"))
	}

	if e == 0 {
		return 1
	}

	if e%2 == 0 {
		halfE := e / 2
		half := PowInt(a, halfE)
		return half * half
	}

	return a * PowInt(a, e-1)
}

// AbsInt is integer version of math.Abs
func AbsInt(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

// Gcd returns the Greatest Common Divisor of two natural numbers.
// Gcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Gcd uses the Euclidean Algorithm.
func Gcd(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}
	if a < b {
		a, b = b, a
	}

	// Euclidean Algorithm
	for b > 0 {
		div := a % b
		a, b = b, div
	}

	return a
}

// Lcm returns the Least Common Multiple of two natural numbers.
// Lcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Lcd uses the Euclidean Algorithm indirectly.
func Lcm(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}

	// a = a'*gcd, b = b'*gcd, a*b = a'*b'*gcd^2
	// a' and b' are relatively prime numbers
	// gcd consists of prime numbers, that are included in a and b
	gcd := Gcd(a, b)

	// not (a * b / gcd), because of reducing a probability of overflow
	return (a / gcd) * b
}

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

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

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

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

	return string(res)
}

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

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

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

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

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

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

const MOD = 1000000000 + 7
const ALPHABET_NUM = 26
const INF_INT64 = math.MaxInt64
const INF_BIT60 = 1 << 60

type Hero struct {
	key  int
	p, s int
}
type HeroList []*Hero
type byKey struct {
	HeroList
}

func (l HeroList) Len() int {
	return len(l)
}
func (l HeroList) Swap(i, j int) {
	l[i], l[j] = l[j], l[i]
}

func (l byKey) Less(i, j int) bool {
	return l.HeroList[i].key < l.HeroList[j].key
}

// how to use
// L := make(HeroList, 0, 200000+5)
// L = append(L, &Hero{key: intValue})
// sort.Stable(byKey{ L })                // Stable ASC
// sort.Stable(sort.Reverse(byKey{ L }))  // Stable DESC

var t int
var n, m int
var A, P, S []int
var L HeroList // ヒーロー構造体の配列
var M []int    // [idx, m-1] のヒーロー区間におけるpowerの最大値を記憶

func main() {
	t = ReadInt()

	for tc := 0; tc < t; tc++ {
		n = ReadInt()
		A = ReadIntSlice(n)
		m = ReadInt()
		P, S = make([]int, m), make([]int, m)
		for i := 0; i < m; i++ {
			p, s := ReadInt2()
			P[i], S[i] = p, s
		}

		solve()
	}
}

func solve() {
	L = make(HeroList, 0)
	for i := 0; i < m; i++ {
		p, s := P[i], S[i]
		L = append(L, &Hero{key: s, p: p, s: s})
	}
	sort.Stable(byKey{L}) // endurance順に昇順ソートする

	// 後半部分のpowerに関する累積Maxを計算しておく
	M = make([]int, m)
	M[m-1] = L[m-1].p
	for i := m - 2; i >= 0; i-- {
		M[i] = Max(M[i+1], L[i].p)
	}

	l := 0
	ans := 0
	for l < n {
		length := sub(l)
		if length == -1 {
			fmt.Println(-1)
			return
		} else {
			l += length
			ans++
		}
	}

	fmt.Println(ans)
}

// モンスターの配列に対して、A[s]から数えて何体倒せるかを計算する関数
// -1を返したら1体も倒せない（=失敗）
func sub(s int) int {
	maxMP := A[s]
	length := -1
	for l := 0; s+l < n; l++ {
		maxMP = Max(maxMP, A[s+l]) // A[s]から現在見ているところまでのモンスターのpowerの最大値
		idx := sub2(l + 1)
		if idx == m {
			break
		}

		maxHP := M[idx] // l+1以上のenduranceを持つヒーローの中のpowerの最大値
		if maxHP >= maxMP {
			length = l + 1
		} else {
			break
		}
	}

	return length
}

// enduranceがl以上となるギリギリのインデックスを二分探索で計算
// mを返したらそのようなヒーローは存在しない
func sub2(l int) int {
	// m は中央を意味する何らかの値
	isOK := func(mid int) bool {
		if L[mid].s >= l {
			return true
		}
		return false
	}

	ng, ok := -1, m
	for int(math.Abs(float64(ok-ng))) > 1 {
		mid := (ok + ng) / 2
		if isOK(mid) {
			ok = mid
		} else {
			ng = mid
		}
	}

	return ok
}

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


package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := int64(1)
	fmt.Fscanf(in, "%d\n", &t)
	for tt := int64(0); tt < t; tt++ {
		n, m := int64(0), int64(0)
		fmt.Fscanf(in, "%d %d\n", &n, &m)
		fmt.Fprintf(out, "%d\n", (n*m+1)/2)
	}
	return
}

func minDigit(a int64) int64 {
	ans := a % 10
	for a > 0 {
		ans = min(ans, a%10)
		a /= 10
	}
	return ans
}

func maxDigit(a int64) int64 {
	ans := a % 10
	for a > 0 {
		ans = max(ans, a%10)
		a /= 10
	}
	return ans
}

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

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

package main
import "fmt"
func main(){
    var t, n, m, ans int
    fmt.Scan(&t)
    for ; t > 0; t-- {
        fmt.Scan(&n, &m)
        if m == 1 {
            ans = (n+1)/2
        }else if n == 1 {
            ans = (m+1)/2
        }else if m % 2 == 0 {
            ans = (m / 2) * n
        }else {
            ans = ((m-1)/2)*n + ((n+1)/2)
        }
        fmt.Println(ans)
    }
}

package main

import "fmt"

func main() {
	var testCases int
	var m int
	var n int
	var result int

	fmt.Scanf("%d\n", &testCases)

	for i := 0; i < testCases; i++ {
		fmt.Scanf("%d%d\n", &m, &n)
		if (m*n)%2 == 0 {
			result = m*n/2
			fmt.Printf("%d\n", result)
		} else {
			result = n*m/2 + 1
			fmt.Printf("%d\n", result)
		}
	}
}

package main

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

func main() {
	var t, n, m int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &n, &m)
		ans := n / 2 * m
		if n%2 == 1 {
			ans += m / 2
			if m%2 == 1 {
				ans++
			}
		}
		fmt.Println(ans)
	}
}

package main

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

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

	tc := readNum(reader)

	for tc > 0 {
		tc--
		n, m := readTwoNums(reader)
		fmt.Println(solve(n, m))
	}
}

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(n, m int) int64 {
	if n%2 == 0 || m%2 == 0 {
		return int64(m) * int64(n) / 2
	}
	res := int64(n-1) * int64(m) / 2
	res += int64(m) / 2
	res++

	return res
}

package main

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

func main() {
	s := NewScanner()
	t := s.NextInt()

	for i := 0; i < t; i++ {
		n, m := s.NextInt(), s.NextInt()

		if n%2 == 0 || m%2 == 0 {
			fmt.Println(m * n / 2)
			continue
		}

		if n%2 == 1 {
			fmt.Println(m*(n/2) + (m+1)/2)
			continue
		}

		if m%2 == 1 {
			fmt.Println(n*(m/2) + (n+1)/2)
		}
	}
}

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

type scanner struct {
	*bufio.Scanner
}

func NewScanner() *scanner {
	s := bufio.NewScanner(os.Stdin)
	s.Split(bufio.ScanWords)
	return &scanner{s}
}

func (s *scanner) NextInt() int {
	s.Scan()
	i, _ := strconv.Atoi(s.Text())
	return i
}

func (s *scanner) NextInt64() int64 {
	s.Scan()
	i, _ := strconv.ParseInt(s.Text(), 10, 64)
	return i
}

func (s *scanner) NextString() string {
	s.Scan()
	return s.Text()
}

// https://codeforces.com/contest/1355/problem/A
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
var input io.Reader

func main() {
	var (
		T int
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &T)
	for i := 0; i < T; i++ {
		solver()
	}
}

func solver() {
	var n, m int
	fmt.Fscan(input, &n, &m)
	if m & 1 == 0 {
	  fmt.Println(m/2*n)
	} else {
	  fmt.Println(m/2*n + (n + 1)/2)
	}

}

package main

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

func main() {
	defer out.Flush()
	for t := ScanInt(); t > 0; t-- {
		P(solve())
	}
}

func solve() int {
	n, m := ScanInt(), ScanInt()
	sq := n * m
	return (sq / 2) + (sq % 2)
}

/*********************** I/O ***********************/
var in *bufio.Scanner
var out *bufio.Writer

func init() {
	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(1e+9))
	in.Split(bufio.ScanWords)
	out = bufio.NewWriter(os.Stdout)
}

func P(args ...interface{}) {
	print("\n", args...)
}

func PS(args ...interface{}) {
	print(" ", args...)
	print("\n", nil)
}

func print(seperator string, args ...interface{}) {
	for _, arg := range args {
		str := ""
		switch v := arg.(type) {
		case byte:
			str = string(v)
		case rune:
			str = string(v)
		case int:
			str = strconv.Itoa(v)
		case []int:
			sb := strings.Builder{}
			for i := 0; i < len(v); i++ {
				sb.WriteString(strconv.Itoa(v[i]) + " ")
			}
			str = sb.String()
		case int64:
			str = strconv.FormatInt(v, 10)
		case []int64:
			sb := strings.Builder{}
			for i := 0; i < len(v); i++ {
				sb.WriteString(strconv.FormatInt(v[i], 10) + " ")
			}
			str = sb.String()
		case string:
			str = v
		default:
			panic("invalid print arg: " + fmt.Sprintf("%v", arg))

		}
		out.WriteString(str + seperator)
	}
}

func ScanWord() string { in.Scan(); return in.Text() }
func ScanWords(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ScanWord()
	}
	return s
}
func ScanInt() int { i, _ := strconv.Atoi(ScanWord()); return i }
func ScanInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = ScanInt()
	}
	return a
}

func MnMx(args ...int) (int, int) {
	minVal, maxVal := math.MaxInt32, math.MinInt32
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
		if v > maxVal {
			maxVal = v
		}
	}
	return minVal, maxVal
}
func Mn(args ...int) int { mn, _ := MnMx(args...); return mn }
func Mx(args ...int) int { _, mx := MnMx(args...); return mx }
func LCM(a, b int) int   { return a / GCD(a, b) * b }
func GCD(a, b int) int {
	if a == 0 {
		return b
	}
	return GCD(b%a, a)
}
func Abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

package main

import "fmt"

func main() {
	var T int
	fmt.Scan(&T)
	var answers = make([]int, T)

	for i := 0; i < T; i++ {
		var rows, columns int
		fmt.Scan(&rows)
		fmt.Scan(&columns)
		answers[i] = findLightCount(rows, columns)
	}

	for _, ans := range answers {
		fmt.Println(ans)
	}
}

func findLightCount(rows, columns int) int {
	if (rows*columns)%2 == 0 {
		return (rows * columns) / 2
	}
	return ((rows*columns)/2 + 1)
}

package main

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

var reader = NewWordScanner(os.Stdin)
var writer = NewBufferedWriter(os.Stdout)

func main() {
	defer writer.Flush()
	t := reader.NextInt64()
	for ; t > 0; t-- {
		a,b:=reader.NextInt(),reader.NextInt()
		writer.Printf("%d\n", sol(a,b))
	}
}

func sol(a,b int) int {
	if(a==1){
		return (b+1)/2
	}
	if(b==1){
		return (a+1)/2
	}
	if(a==2){
		return b
	}
 	if(b==2){
 		return a
 	}
 	if(b%2==0 && a%2==0){
 		return (a/2)*b
 	}
 	if(b%2==1 && a%2==1){
 		x,y:=MnMx(a,b)
 		return (sol(y-1,x)+sol(1,x))
 	}
 	x,y:=oddeven(a,b)
 	return x*(y/2)
}

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

func oddeven(a,b int)(int,int) {
	if(a%2==1){
		return a,b
	}
	return b,a
}

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

type BufferedWriter interface {
	Printf(format string, a ...interface{})
	Flush()
}

type writerImpl struct {
	*bufio.Writer
}

func NewBufferedWriter(writer io.Writer) BufferedWriter {
	return &writerImpl{Writer: bufio.NewWriter(writer)}
}

func (impl *writerImpl) Printf(f string, a ...interface{}) {
	fmt.Fprintf(impl.Writer, f, a...)
}

func (impl *writerImpl) Flush() {
	impl.Writer.Flush()
}

type WordScanner interface {
	NextInt() int
	NextInt64() int64
	NextString() string
}

type wordScannerImpl struct {
	*bufio.Scanner
}

func NewWordScanner(reader io.Reader) WordScanner {
	s := bufio.NewScanner(reader)
	s.Split(bufio.ScanWords)
	// adjust the following size as needed
	// 2 << 17 is good enough to scan strings of len 2*10^5
	// it could go as high as 2 << 26 before cf judge breaks
	size := 2 << 20
	buf := make([]byte, size)
	s.Buffer(buf, size)
	return &wordScannerImpl{Scanner: s}
}

func (impl *wordScannerImpl) NextInt() int {
	impl.Scan()
	bb := impl.Bytes()
	i := 0
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int(b - '0')
	}
	return i
}

func (impl *wordScannerImpl) NextInt64() int64 {
	impl.Scan()
	bb := impl.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func (impl *wordScannerImpl) NextString() string {
	impl.Scan()
	return impl.Text()
}
/*********************** Utils ***********************/

func S(v interface{}) string { return fmt.Sprintf("%v", v) }
func SArr(a []int) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.Itoa(a[i]) + " ")
	}
	return sb.String()
}
func SArr64(a []int64) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.FormatInt(a[i], 10) + " ")
	}
	return sb.String()
}
func MnMx(args ...int) (int, int) {
	minVal, maxVal := int(math.MaxInt32), int(math.MinInt32)
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
		if v > maxVal {
			maxVal = v
		}
	}
	return minVal, maxVal
}
// func Mn(args ...int64) int64 { mn, _ := MnMx(args...); return mn }
// func Mx(args ...int64) int64 { _, mx := MnMx(args...); return mx }
// func LCM(a, b int64) int64   { return a / GCD(a, b) * b }
// func GCD(a, b int64) int64 {
// 	if a == 0 {
// 		return b
// 	}
// 	return GCD(b%a, a)
// }
// func sortInt64Slice(a []int64) { sort.Slice(a, func(i, j int) bool { return a[i] < a[j] }) }
// func abs(x int64) int64 {
// 	if x < 0 {
// 		return -x
// 	}
// 	return x
// }

package main

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

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

	var t, n, m int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &m)
		Fprintln(out, (n*m+1)/2)
	}
}

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

/*
URL:
https://codeforces.com/contest/1358/problem/A
*/

package main

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

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

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

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

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

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

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

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

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

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

var (
	t    int
	n, m int
)

func main() {
	t = ReadInt()
	for i := 0; i < t; i++ {
		n, m = ReadInt2()
		solve(n, m)
	}
}

func solve(n, m int) {
	num := n * m

	ans := num/2 + num%2
	fmt.Println(ans)
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

	return string(res)
}

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

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

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

	return string(res)
}

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

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

package main

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

var nTest, n, m int

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

	fmt.Fscan(in, &nTest)
	for t := 0; t < nTest; t++ {
		fmt.Fscan(in, &m, &n)

		res := (n*m + 1) / 2

		fmt.Fprintln(out, res)
	}

	out.Flush()
}

package main

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

func min(a, b int) int {
	return int(math.Min(float64(a), float64(b)))
}

func max(a, b int) int {
	return int(math.Max(float64(a), float64(b)))
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscanf(in, "%v\n", &t)
	for now := 0; now < t; now++ {
		var n, m int
		fmt.Fscanf(in, "%v %v\n", &n, &m)
		fmt.Fprintln(out, min(n * (m / 2) + (m % 2) * ((n + 1) / 2), m * (n / 2) + (n % 2) * ((m + 1) / 2)))
	}
	return
}

package main

import "fmt"

func CountLamps(n, m int) int {
	if m > n {
		n, m = m, n
	}
	return n/2 * m + (n % 2) * (m / 2 + m%2)
}

func main () {
	var t int
	fmt.Scan(&t)

	for i := 0; i < t; i++ {
		var n, m int
		fmt.Scan(&n, &m)
		fmt.Println(CountLamps(n, m))
	}
}

package main

import (
	"fmt"
)

func main(){
	var T int
	fmt.Scan(&T)
	for t := 0; t < T; t++ {
		var n, m int
		fmt.Scan(&n, &m)
		if n%2 == 0 || m%2 == 0 {
			ans := (n*m) >> 1
			fmt.Println(ans)
		} else {
			ans := (n*m + 1) >> 1
			fmt.Println(ans)
		}
	}
}

package main

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

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

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

	for i := 0; i < t; i++ {
		var n, m int
		fmt.Fscan(r, &n, &m)
		p := n * m

		fmt.Fprintln(w, p/2+p%2)
	}
}

package main

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

type BufferedWriter interface {
	Printf(format string, a ...interface{})
	Flush()
}

type writerImpl struct {
	*bufio.Writer
}

func NewBufferedWriter(writer io.Writer) BufferedWriter {
	return &writerImpl{Writer: bufio.NewWriter(writer)}
}

func (impl *writerImpl) Printf(f string, a ...interface{}) {
	fmt.Fprintf(impl.Writer, f, a...)
}

func (impl *writerImpl) Flush() {
	impl.Writer.Flush()
}

type WordScanner interface {
	Int64() int64
	UInt64() uint64
	String() string
}

type wordScannerImpl struct {
	*bufio.Scanner
}

func NewWordScanner(reader io.Reader) WordScanner {
	s := bufio.NewScanner(reader)
	s.Split(bufio.ScanWords)
	// adjust the following size as needed
	// 2 << 17 is good enough to scan strings of len 2*10^5
	// it could go as high as 2 << 26 before cf judge breaks
	size := 2 << 20
	buf := make([]byte, size)
	s.Buffer(buf, size)
	return &wordScannerImpl{Scanner: s}
}

func (impl *wordScannerImpl) Int64() int64 {
	impl.Scan()
	bb := impl.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func (impl *wordScannerImpl) UInt64() uint64 {
	impl.Scan()
	bb := impl.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func (impl *wordScannerImpl) String() string {
	impl.Scan()
	return impl.Text()
}

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

func lcm(a, b int64) int64 {
	return a * b / gcd(a, b)
}

func abs(a int64) int64 {
	if a < 0 {
		return a * -1
	}
	return a
}

func min(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r < a {
			a = r
		}
	}
	return a
}

func max(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r > a {
			a = r
		}
	}
	return a
}

const (
	INF = math.MaxInt64
	MOD = 1e9 + 7
)

var reader = NewWordScanner(os.Stdin)
var writer = NewBufferedWriter(os.Stdout)

func main() {
	defer writer.Flush()
	t := reader.Int64()
	for ; t > 0; t-- {
		n, m := reader.Int64(), reader.Int64()
		writer.Printf("%d\n", sol(n,m))
	}
}

func sol(n, m int64) int64 {
	if n * m % 2 == 0 {
		return n * m / 2
	}
	return n * m / 2 + 1
}

package main

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

var reader = NewWordScanner(os.Stdin)
var writer = NewBufferedWriter(os.Stdout)

func main() {
	defer writer.Flush()
	t := reader.NextInt64()
	for ; t > 0; t-- {
		a,b:=reader.NextInt(),reader.NextInt()
		writer.Printf("%d\n", sol(a,b))
	}
}

func sol(a,b int) int {
	if(a==1){
		return (b+1)/2
	}
	if(b==1){
		return (a+1)/2
	}
	if(a==2){
		return b
	}
 	if(b==2){
 		return a
 	}
 	if(b%2==0 && a%2==0){
 		return (a/2)*b
 	}
 	if(b%2==1 && a%2==1){
 		x:=max(a,b)
 		return x+ (x+1)/2
 	}
 	x,y:=oddeven(a,b)
 	return x*(y/2)
}

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

func oddeven(a,b int)(int,int) {
	if(a%2==1){
		return a,b
	}
	return b,a
}

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

type BufferedWriter interface {
	Printf(format string, a ...interface{})
	Flush()
}

type writerImpl struct {
	*bufio.Writer
}

func NewBufferedWriter(writer io.Writer) BufferedWriter {
	return &writerImpl{Writer: bufio.NewWriter(writer)}
}

func (impl *writerImpl) Printf(f string, a ...interface{}) {
	fmt.Fprintf(impl.Writer, f, a...)
}

func (impl *writerImpl) Flush() {
	impl.Writer.Flush()
}

type WordScanner interface {
	NextInt() int
	NextInt64() int64
	NextString() string
}

type wordScannerImpl struct {
	*bufio.Scanner
}

func NewWordScanner(reader io.Reader) WordScanner {
	s := bufio.NewScanner(reader)
	s.Split(bufio.ScanWords)
	// adjust the following size as needed
	// 2 << 17 is good enough to scan strings of len 2*10^5
	// it could go as high as 2 << 26 before cf judge breaks
	size := 2 << 20
	buf := make([]byte, size)
	s.Buffer(buf, size)
	return &wordScannerImpl{Scanner: s}
}

func (impl *wordScannerImpl) NextInt() int {
	impl.Scan()
	bb := impl.Bytes()
	i := 0
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int(b - '0')
	}
	return i
}

func (impl *wordScannerImpl) NextInt64() int64 {
	impl.Scan()
	bb := impl.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func (impl *wordScannerImpl) NextString() string {
	impl.Scan()
	return impl.Text()
}
/*********************** Utils ***********************/

func S(v interface{}) string { return fmt.Sprintf("%v", v) }
func SArr(a []int) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.Itoa(a[i]) + " ")
	}
	return sb.String()
}
func SArr64(a []int64) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.FormatInt(a[i], 10) + " ")
	}
	return sb.String()
}
func MnMx(args ...int64) (int64, int64) {
	minVal, maxVal := int64(math.MaxInt64), int64(math.MinInt64)
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
		if v > maxVal {
			maxVal = v
		}
	}
	return minVal, maxVal
}
func Mn(args ...int64) int64 { mn, _ := MnMx(args...); return mn }
func Mx(args ...int64) int64 { _, mx := MnMx(args...); return mx }
func LCM(a, b int64) int64   { return a / GCD(a, b) * b }
func GCD(a, b int64) int64 {
	if a == 0 {
		return b
	}
	return GCD(b%a, a)
}
func sortInt64Slice(a []int64) { sort.Slice(a, func(i, j int) bool { return a[i] < a[j] }) }
func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}

package main

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

var reader = NewWordScanner(os.Stdin)
var writer = NewBufferedWriter(os.Stdout)

func main() {
	defer writer.Flush()
	t := reader.NextInt64()
	for ; t > 0; t-- {
		a,b:=reader.NextInt(),reader.NextInt()
		writer.Printf("%d\n", sol(a,b))
	}
}

func sol(a,b int) int {
	if(a==1){
		return (b+1)/2
	}
	if(b==1){
		return (a+1)/2
	}
	if(a==2){
		return b
	}
 	if(b==2){
 		return a
 	}
 	if(b%2==0 && a%2==0){
 		return a+b
 	}
 	if(b%2==1 && a%2==1){
 		x:=max(a,b)
 		return x+ (x+1)/2
 	}
 	y:=even(a,b)
 	return y+y/2
}

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

func even(a,b int)int {
	if(a%2==0){
		return a
	}
	return b
}

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

type BufferedWriter interface {
	Printf(format string, a ...interface{})
	Flush()
}

type writerImpl struct {
	*bufio.Writer
}

func NewBufferedWriter(writer io.Writer) BufferedWriter {
	return &writerImpl{Writer: bufio.NewWriter(writer)}
}

func (impl *writerImpl) Printf(f string, a ...interface{}) {
	fmt.Fprintf(impl.Writer, f, a...)
}

func (impl *writerImpl) Flush() {
	impl.Writer.Flush()
}

type WordScanner interface {
	NextInt() int
	NextInt64() int64
	NextString() string
}

type wordScannerImpl struct {
	*bufio.Scanner
}

func NewWordScanner(reader io.Reader) WordScanner {
	s := bufio.NewScanner(reader)
	s.Split(bufio.ScanWords)
	// adjust the following size as needed
	// 2 << 17 is good enough to scan strings of len 2*10^5
	// it could go as high as 2 << 26 before cf judge breaks
	size := 2 << 20
	buf := make([]byte, size)
	s.Buffer(buf, size)
	return &wordScannerImpl{Scanner: s}
}

func (impl *wordScannerImpl) NextInt() int {
	impl.Scan()
	bb := impl.Bytes()
	i := 0
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int(b - '0')
	}
	return i
}

func (impl *wordScannerImpl) NextInt64() int64 {
	impl.Scan()
	bb := impl.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func (impl *wordScannerImpl) NextString() string {
	impl.Scan()
	return impl.Text()
}
/*********************** Utils ***********************/

func S(v interface{}) string { return fmt.Sprintf("%v", v) }
func SArr(a []int) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.Itoa(a[i]) + " ")
	}
	return sb.String()
}
func SArr64(a []int64) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.FormatInt(a[i], 10) + " ")
	}
	return sb.String()
}
func MnMx(args ...int64) (int64, int64) {
	minVal, maxVal := int64(math.MaxInt64), int64(math.MinInt64)
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
		if v > maxVal {
			maxVal = v
		}
	}
	return minVal, maxVal
}
func Mn(args ...int64) int64 { mn, _ := MnMx(args...); return mn }
func Mx(args ...int64) int64 { _, mx := MnMx(args...); return mx }
func LCM(a, b int64) int64   { return a / GCD(a, b) * b }
func GCD(a, b int64) int64 {
	if a == 0 {
		return b
	}
	return GCD(b%a, a)
}
func sortInt64Slice(a []int64) { sort.Slice(a, func(i, j int) bool { return a[i] < a[j] }) }
func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}

package main

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

var reader = NewWordScanner(os.Stdin)
var writer = NewBufferedWriter(os.Stdout)

func main() {
	defer writer.Flush()
	t := reader.NextInt64()
	for ; t > 0; t-- {
		a,b:=reader.NextInt(),reader.NextInt()
		writer.Printf("%d\n", sol(a,b))
	}
}

func sol(a,b int) int {
	if(a==1){
		return (b+1)/2
	}
	if(b==1){
		return (a+1)/2
	}
	if(a==2){
		return b
	}
 	if(b==2){
 		return a
 	}
 	if(b%2==0 && a%2==0){
 		return a+b
 	}
 	if(b%2==1 && a%2==1){
 		x:=max(a,b)
 		return x+ (x+1)/2
 	}
 	x,y:=oddeven(a,b)
 	return x*(y/2)
}

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

func oddeven(a,b int)(int,int) {
	if(a%2==1){
		return a,b
	}
	return b,a
}

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

type BufferedWriter interface {
	Printf(format string, a ...interface{})
	Flush()
}

type writerImpl struct {
	*bufio.Writer
}

func NewBufferedWriter(writer io.Writer) BufferedWriter {
	return &writerImpl{Writer: bufio.NewWriter(writer)}
}

func (impl *writerImpl) Printf(f string, a ...interface{}) {
	fmt.Fprintf(impl.Writer, f, a...)
}

func (impl *writerImpl) Flush() {
	impl.Writer.Flush()
}

type WordScanner interface {
	NextInt() int
	NextInt64() int64
	NextString() string
}

type wordScannerImpl struct {
	*bufio.Scanner
}

func NewWordScanner(reader io.Reader) WordScanner {
	s := bufio.NewScanner(reader)
	s.Split(bufio.ScanWords)
	// adjust the following size as needed
	// 2 << 17 is good enough to scan strings of len 2*10^5
	// it could go as high as 2 << 26 before cf judge breaks
	size := 2 << 20
	buf := make([]byte, size)
	s.Buffer(buf, size)
	return &wordScannerImpl{Scanner: s}
}

func (impl *wordScannerImpl) NextInt() int {
	impl.Scan()
	bb := impl.Bytes()
	i := 0
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int(b - '0')
	}
	return i
}

func (impl *wordScannerImpl) NextInt64() int64 {
	impl.Scan()
	bb := impl.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func (impl *wordScannerImpl) NextString() string {
	impl.Scan()
	return impl.Text()
}
/*********************** Utils ***********************/

func S(v interface{}) string { return fmt.Sprintf("%v", v) }
func SArr(a []int) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.Itoa(a[i]) + " ")
	}
	return sb.String()
}
func SArr64(a []int64) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.FormatInt(a[i], 10) + " ")
	}
	return sb.String()
}
func MnMx(args ...int64) (int64, int64) {
	minVal, maxVal := int64(math.MaxInt64), int64(math.MinInt64)
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
		if v > maxVal {
			maxVal = v
		}
	}
	return minVal, maxVal
}
func Mn(args ...int64) int64 { mn, _ := MnMx(args...); return mn }
func Mx(args ...int64) int64 { _, mx := MnMx(args...); return mx }
func LCM(a, b int64) int64   { return a / GCD(a, b) * b }
func GCD(a, b int64) int64 {
	if a == 0 {
		return b
	}
	return GCD(b%a, a)
}
func sortInt64Slice(a []int64) { sort.Slice(a, func(i, j int) bool { return a[i] < a[j] }) }
func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}


package main

import "fmt"

func main() {
	var n int
	var v, d, p []int64
	fmt.Scanf("%d\n", &n)
	for i := 0; i < n; i++ {
		var vi, di, pi int64
		fmt.Scanf("%d %d %d\n", &vi, &di, &pi)
		v = append(v, vi)
		d = append(d, di)
		p = append(p, pi)
	}
	var ans []int
	for i := 0; i < n; i++ {
		if p[i] >= 0 {
			ans = append(ans, i+1)
			var cry int64
			for j := i + 1; j < n; j++ {
				if p[j] < 0 { // already left
					continue
				}
				p[j] -= cry + v[i]
				if p[j] < 0 && cry < int64(1e7) {
					cry += d[j]
				}
				if v[i] > 0 {
					v[i]--
				}
			}
		}
	}

	fmt.Println(len(ans))
	fmt.Printf("%d", ans[0])
	for _, x := range ans[1:] {
		fmt.Printf(" %d", x)
	}
	fmt.Println()
}

package main

import (
    "fmt"
)

const MAXN int = 4010

func main() {
    var n int
    fmt.Scan(&n)
    v := make([]int64, n)
    d := make([]int64, n)
    p := make([]int64, n)
    c := make([]bool, n)
    for i := 0; i < n; i++ {
        fmt.Scan(&v[i],&d[i],&p[i])
    }

    st := make([]int,0)

    for i := 0; i < n; i++ {
        if !c[i] {
            st = append(st,i+1)
            c[i] = true
            for j, vi := i+1, v[i]; j < n && vi > 0; j++ {
                if !c[j] {
                    p[j] -= vi
                    vi--
                }
            }
        }
        for {
             idx := func() int {
                for i := 0; i < n; i++ {
                    if !c[i] && p[i] < 0 {
                        return i
                    }
                }
                return -1;
                } ()
             if idx == -1 { break }
             c[idx] = true
             for j := idx+1; j < n; j++ {
                p[j] -= d[idx]
             }
        }
    }

    fmt.Println(len(st))
    for i := 0; i < len(st); i++ {
        if i == len(st)-1 {
            fmt.Println(st[i])
        } else {
            fmt.Print(st[i]," ")
        }
    }

}

package main
import (
	"fmt"
)

func main(){
	var n int
	var a []int64
	var b []int64
	var c []int64
	var rez []int
	fmt.Scanf("%d\n",&n)
	for i:=0;i<n;i++{
		var v,d,p int64
		fmt.Scanf("%d %d %d\n",&v,&d,&p)
		a=append(a,v)
		b=append(b,d)
		c=append(c,p)
	}
	for i:=0;i<n;i++{
		if c[i]>=0{
			rez=append(rez,i+1)
			var pl int64
			for j:=i+1;j<n;j++{
				if c[j]<0{
					continue
				}
				c[j]-=pl+a[i]
				if c[j]<0{
					pl+=b[j]
				}
				if a[i]>0{
					a[i]-=1
				}
			}
		}
	}
	fmt.Println(len(rez))
	for i:=0;i<len(rez);i++{
		fmt.Print(rez[i]," ")
	}

}

package main

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

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

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	v, d, p := make([]int, n), make([]int, n), make([]int, n)
	isLeaves := make([]bool, n)
	kid := make([]int, 0)

	for i := range v {
		v[i], d[i], p[i] = nextInt(), nextInt(), nextInt()
	}

	for i := range isLeaves {
		if !isLeaves[i] {
			reduced := 0

			for j := i + 1; j < n; j++ {
				if isLeaves[j] {
					continue
				}

				p[j] -= v[i] + reduced

				if v[i] > 0 {
					v[i]--
				}

				if p[j] < 0 {
					isLeaves[j] = true

					if reduced < 1000000 {
						reduced += d[j]
					}
				}
			}

			kid = append(kid, i+1)
		}
	}

	println(len(kid))

	for i := range kid {
		print(kid[i], " ")
	}

	println()
}

func scan(a ...interface{}) {
	fmt.Fscan(reader, a...)
}

func next() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func nextInt64() int64 {
	n, _ := strconv.ParseInt(next(), 0, 64)
	return n
}

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{})               { fmt.Fprintln(writer, a...) }
func print(a ...interface{})                 { fmt.Fprint(writer, a...) }
func printf(format string, a ...interface{}) { fmt.Fprintf(writer, format, a...) }

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

type Child struct {
  InsideCry, OutsideCry, Confidence int64
  Departed bool
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  n := getI()
  children := make([]*Child, n)
  for i := range children {
    children[i] = &Child{ getI64(), getI64(), getI64(), false }
  }
  treated := []int{}

  for pos, child := range children {
    if child.Departed {
      continue
    }
    treated = append(treated, pos + 1)
    insideCry := child.InsideCry
    for i := pos + 1; i < n; i++ {
      if !children[i].Departed {
        children[i].Confidence -= insideCry
        insideCry--
        if insideCry == 0 {
          break
        }
      }
    }
    outsideCry := int64(0)
    for i := pos + 1; i < n; i++ {
      if children[i].Departed {
        continue
      }
      children[i].Confidence -= outsideCry
      if children[i].Confidence < 0 {
        children[i].Departed = true
        outsideCry += children[i].OutsideCry
      }
    }
  }

  writer.WriteString(fmt.Sprintf("%d\n%d", len(treated), treated[0]))
  for i := 1; i < len(treated); i++ {
    writer.WriteString(fmt.Sprintf(" %d", treated[i]))
  }
  writer.WriteString("\n")
}

package main

import "fmt"

func main() {
    var n int
    var v, d, p []int
    fmt.Scanf("%d\n", &n)
    for i := 0; i < n; i++ {
        var vi, di, pi int
        fmt.Scanf("%d %d %d\n", &vi, &di, &pi)
        v = append(v, vi)
        d = append(d, di)
        p = append(p, pi)
    }
    var ans []int
    for i := 0; i < n; i++ {
        if p[i] >= 0 {
            ans = append(ans, i+1)
            var cry int
            for j := i + 1; j < n; j++ {
                if p[j] < 0 { // already left
                    continue
                }
                p[j] -= cry + v[i]
                if p[j] < 0 && cry < int(1e7) {
                    cry += d[j]
                }
                if v[i] > 0 {
                    v[i]--
                }
            }
        }
    }

    fmt.Println(len(ans))
    fmt.Printf("%d", ans[0])
    for _, x := range ans[1:] {
        fmt.Printf(" %d", x)
    }
}

package main

import (
    "fmt"
)

const MAXN int = 4010

func main() {
    var n int
    fmt.Scan(&n)
    v := make([]int, n)
    d := make([]int, n)
    p := make([]int, n)
    for i := 0; i < n; i++ {
        fmt.Scan(&v[i],&d[i],&p[i])
    }

    st := make([]int,0)

    dec := 0
    for i, pi := range p {
        pi -= dec
        if pi >= 0 {
            st = append(st,i+1)
            for j, vj := i+1, v[i]; j < n && vj > 0; j, vj = j+1, vj-1 {
                p[j] -= vj
            }
        } else {
            dec += d[i]
        }
    }

    fmt.Println(len(st))
    for i := 0; i < len(st); i++ {
        if i == len(st)-1 {
            fmt.Println(st[i])
        } else {
            fmt.Print(st[i]," ")
        }
    }

}

package main

import (
    "fmt"
)

const MAXN int = 4010

func main() {
    var n int
    fmt.Scan(&n)
    v := make([]int64, n)
    d := make([]int64, n)
    p := make([]int64, n)
    for i := 0; i < n; i++ {
        fmt.Scan(&v[i],&d[i],&p[i])
    }

    st := make([]int,0)

    dec := int64(0)
    for i, pi := range p {
        pi -= dec
        if pi >= 0 {
            st = append(st,i+1)
            for j, vj := i+1, v[i]; j < n && vj > 0; j, vj = j+1, vj-1 {
                p[j] -= vj
            }
        } else {
            dec += d[i]
        }
    }

    fmt.Println(len(st))
    for i := 0; i < len(st); i++ {
        if i == len(st)-1 {
            fmt.Println(st[i])
        } else {
            fmt.Print(st[i]," ")
        }
    }

}

package main

import (
    "fmt"
)

const MAXN int = 4010

func main() {
    var n int
    fmt.Scan(&n)
    v := make([]int, n)
    d := make([]int, n)
    p := make([]int, n)
    for i := 0; i < n; i++ {
        fmt.Scan(&v[i],&d[i],&p[i])
    }

    st := make([]int,0)

    dec := 0
    for i, pi := range p {
        pi -= dec
        if pi > 0 {
            st = append(st,i+1)
            for j, vj := i+1, v[i]; j < n && vj > 0; j, vj = j+1, vj-1 {
                p[j] -= vj
            }
        } else {
            dec += d[i]
        }
    }

    fmt.Println(len(st))
    for i := 0; i < len(st); i++ {
        if i == len(st)-1 {
            fmt.Println(st[i])
        } else {
            fmt.Print(st[i]," ")
        }
    }

}

package main

import (
    "fmt"
)

const MAXN int = 4010

func main() {
    var n int
    fmt.Scan(&n)
    v := make([]int, n)
    d := make([]int, n)
    p := make([]int, n)
    for i := 0; i < n; i++ {
        fmt.Scan(&v[i],&d[i],&p[i])
    }

    st := make([]int,0)

    dec := 0
    for i, pi := range p {
        pi -= dec
        if pi > 0 {
            st = append(st,i)
            for j, vj := i+1, v[i]; j < n && vj > 0; j, vj = j+1, vj-1 {
                p[j] -= vj
            }
        } else {
            dec += d[i]
        }
    }

    fmt.Println(len(st))
    for i := 0; i < len(st); i++ {
        if i == len(st)-1 {
            fmt.Println(st[i])
        } else {
            fmt.Print(st[i]," ")
        }
    }

}

package main

import (
    "fmt"
)

const MAXN int = 4010

func main() {
    var n int
    fmt.Scan(&n)
    v := make([]int64, n)
    d := make([]int64, n)
    p := make([]int64, n)
    for i := 0; i < n; i++ {
        fmt.Scan(&v[i],&d[i],&p[i])
    }

    st := make([]int,0)

    dec := int64(0)
    for i, pi := range p {
        pi -= dec
        if pi >= 0 {
            st = append(st,i+1)
            for j, vj := i+1, v[i]; j < n && vj > 0; j, vj = j+1, vj-1 {
                if p[j] < 0 {
                    vj++
                } else {
                    p[j] -= vj
                }
            }
        } else {
            dec += d[i]
        }
    }

    fmt.Println(len(st))
    for i := 0; i < len(st); i++ {
        if i == len(st)-1 {
            fmt.Println(st[i])
        } else {
            fmt.Print(st[i]," ")
        }
    }

}

package main
import (
	"fmt"
)

func main(){
	var n int
	var a []int
	var b []int
	var c []int
	var rez []int
	fmt.Scanf("%d\n",&n)
	for i:=0;i<n;i++{
		var v,d,p int
		fmt.Scanf("%d %d %d\n",&v,&d,&p)
		a=append(a,v)
		b=append(b,d)
		c=append(c,p)
	}
	for i:=0;i<n;i++{
		if c[i]>=0{
			rez=append(rez,i+1)
			var pl int
			for j:=i+1;j<n;j++{
				if c[j]<0{
					continue
				}
				c[j]-=pl+a[i]
				if c[j]<0{
					pl+=b[j]
				}
				if a[i]>0{
					a[i]-=1
				}
			}
		}
	}
	fmt.Println(len(rez))
	for i:=0;i<len(rez);i++{
		fmt.Print(rez[i]," ")
	}

}

package main
import (
	"fmt"
)

func main(){
	var n int
	var a []int
	var b []int
	var c []int
	var pl int
	var rez []int
	fmt.Scanf("%d\n",&n)
	for i:=0;i<n;i++{
		var v,d,p int
		fmt.Scanf("%d %d %d\n",&v,&d,&p)
		a=append(a,v)
		b=append(b,d)
		c=append(c,p)
	}
	for i:=0;i<n;i++{
		if c[i]>=0{
			rez=append(rez,i+1)
			for j:=i+1;j<n;j++{
				if c[j]<0{
					continue
				}
				c[j]-=pl+a[i]
				if c[j]<0{
					pl+=b[j]
				}
				if a[i]>0{
					a[i]-=1
				}
			}
		}
	}
	fmt.Println(len(rez))
	for i:=0;i<len(rez);i++{
		fmt.Print(rez[i]," ")
	}

}

package main

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

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

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	v, d, p := make([]int, n), make([]int, n), make([]int, n)
	isLeaves := make([]bool, n)
	haveChild := true
	kid := make([]int, 0)

	for i := range v {
		v[i], d[i], p[i] = nextInt(), nextInt(), nextInt()
	}

	for haveChild {
		haveChild = false

		for i := range isLeaves {
			if !isLeaves[i] {
				haveChild = true
				isLeaves[i] = true
				reduced := 0

				for j := i + 1; j < n; j++ {
					if v[i] > 0 {
						v[i]--
					}

					if isLeaves[j] {
						continue
					}

					p[j] -= v[i] + reduced

					if p[j] < 0 {
						isLeaves[j] = true
						reduced += d[j]
					}
				}

				kid = append(kid, i+1)
				break
			}
		}
	}

	println(len(kid))

	for i := range kid {
		print(kid[i], " ")
	}

	println()
}

func scan(a ...interface{}) {
	fmt.Fscan(reader, a...)
}

func next() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func nextInt64() int64 {
	n, _ := strconv.ParseInt(next(), 0, 64)
	return n
}

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{})               { fmt.Fprintln(writer, a...) }
func print(a ...interface{})                 { fmt.Fprint(writer, a...) }
func printf(format string, a ...interface{}) { fmt.Fprintf(writer, format, a...) }

package main

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

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

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	v, d, p := make([]int, n), make([]int, n), make([]int, n)
	isLeaves := make([]bool, n)
	haveChild := true
	kid := make([]int, 0)

	for i := range v {
		v[i], d[i], p[i] = nextInt(), nextInt(), nextInt()
	}

	for haveChild {
		haveChild = false

		for i := range isLeaves {
			if !isLeaves[i] {
				haveChild = true
				isLeaves[i] = true
				reduced := 0

				for j := i + 1; j < n; j++ {
					if isLeaves[j] {
						continue
					}

					p[j] -= v[i] + reduced

					if v[i] > 0 {
						v[i]--
					}

					if p[j] < 0 {
						isLeaves[j] = true
						reduced += d[j]
					}
				}

				kid = append(kid, i+1)
				break
			}
		}
	}

	println(len(kid))

	for i := range kid {
		print(kid[i], " ")
	}

	println()
}

func scan(a ...interface{}) {
	fmt.Fscan(reader, a...)
}

func next() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func nextInt64() int64 {
	n, _ := strconv.ParseInt(next(), 0, 64)
	return n
}

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{})               { fmt.Fprintln(writer, a...) }
func print(a ...interface{})                 { fmt.Fprint(writer, a...) }
func printf(format string, a ...interface{}) { fmt.Fprintf(writer, format, a...) }

package main

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

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

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	v, d, p := make([]int, n), make([]int, n), make([]int, n)
	isLeaves := make([]bool, n)
	haveChild := true
	kid := make([]int, 0)

	for i := range v {
		v[i], d[i], p[i] = nextInt(), nextInt(), nextInt()
	}

	for haveChild {
		haveChild = false

		for i := range isLeaves {
			if !isLeaves[i] {
				haveChild = true
				isLeaves[i] = true
				reduced := 0

				for j := i + 1; j < n; j++ {
					p[j] -= v[i] + reduced

					if v[i] > 0 {
						v[i]--
					}

					if isLeaves[j] {
						continue
					}

					if p[j] < 0 {
						isLeaves[j] = true
						reduced += d[j]
					}
				}

				kid = append(kid, i+1)
				break
			}
		}
	}

	println(len(kid))

	for i := range kid {
		print(kid[i], " ")
	}

	println()
}

func scan(a ...interface{}) {
	fmt.Fscan(reader, a...)
}

func next() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func nextInt64() int64 {
	n, _ := strconv.ParseInt(next(), 0, 64)
	return n
}

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{})               { fmt.Fprintln(writer, a...) }
func print(a ...interface{})                 { fmt.Fprint(writer, a...) }
func printf(format string, a ...interface{}) { fmt.Fprintf(writer, format, a...) }

package main

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

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

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	v, d, p := make([]int, n), make([]int, n), make([]int, n)
	isLeaves := make([]bool, n)
	haveChild := true
	kid := make([]int, 0)

	for i := range v {
		v[i], d[i], p[i] = nextInt(), nextInt(), nextInt()
	}

	for haveChild {
		haveChild = false

		for i := range isLeaves {
			if !isLeaves[i] {
				haveChild = true
				isLeaves[i] = true
				reduced := 0

				for j := i + 1; j < n; j++ {
					p[j] -= v[i] + reduced

					if v[i] > 0 {
						v[i]--
					}

					if p[j] < 0 {
						isLeaves[j] = true
						reduced += d[j]
					}
				}

				kid = append(kid, i+1)
				break
			}
		}
	}

	println(len(kid))

	for i := range kid {
		print(kid[i], " ")
	}

	println()
}

func scan(a ...interface{}) {
	fmt.Fscan(reader, a...)
}

func next() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func nextInt64() int64 {
	n, _ := strconv.ParseInt(next(), 0, 64)
	return n
}

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{})               { fmt.Fprintln(writer, a...) }
func print(a ...interface{})                 { fmt.Fprint(writer, a...) }
func printf(format string, a ...interface{}) { fmt.Fprintf(writer, format, a...) }

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

type Child struct {
  InsideCry, OutsideCry, Confidence int
  Departed bool
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  n := getI()
  children := make([]*Child, n)
  for i := range children {
    children[i] = &Child{ getI(), getI(), getI(), false }
  }
  treated := []int{}

  for pos, child := range children {
    if child.Departed {
      continue
    }
    treated = append(treated, pos + 1)
    insideCry := child.InsideCry
    for i := pos + 1; i < n; i++ {
      if !children[i].Departed {
        children[i].Confidence -= insideCry
        insideCry--
        if insideCry == 0 {
          break
        }
      }
    }
    outsideCry := 0
    for i := pos + 1; i < n; i++ {
      if children[i].Departed {
        continue
      }
      children[i].Confidence -= outsideCry
      if children[i].Confidence < 0 {
        children[i].Departed = true
        outsideCry += children[i].OutsideCry
      }
    }
  }

  writer.WriteString(fmt.Sprintf("%d\n%d", len(treated), treated[0]))
  for i := 1; i < len(treated); i++ {
    writer.WriteString(fmt.Sprintf(" %d", treated[i]))
  }
  writer.WriteString("\n")
}

package main

import "fmt"

func main() {
	var n int
	var v, d, p []int
	fmt.Scanf("%d", &n)
	for i := 0; i < n; i++ {
		var vi, di, pi int
		fmt.Scanf("%d %d %d", &vi, &di, &pi)
		v = append(v, vi)
		d = append(d, di)
		p = append(p, pi)
	}
	var ans []int
	for i := 0; i < n; i++ {
		if p[i] >= 0 {
			ans = append(ans, i+1)
			var cry int
			for j := i + 1; j < n; j++ {
				if p[j] < 0 { // already left
					continue
				}
				p[j] -= cry + v[i]
				if p[j] < 0 && cry < int(1e7) {
					cry += d[j]
				}
				if v[i] > 0 {
					v[i]--
				}
			}
		}
	}

	fmt.Println(len(ans))
	fmt.Printf("%d", ans[0])
	for _, x := range ans[1:] {
		fmt.Printf(" %d", x)
	}
}


// B. Choosing Symbol Pairs
/*
There is a given string S consisting of N symbols. Your task is to find the number of ordered pairs of integers i and j such that

1. 1 ≤ i, j ≤ N

2. S[i] = S[j], that is the i-th symbol of string S is equal to the j-th.

Input
The single input line contains S, consisting of lowercase Latin letters and digits. It is guaranteed that string S in not empty and its length does not exceed 105.

Output
Print a single number which represents the number of pairs i and j with the needed property. Pairs (x, y) and (y, x) should be considered different, i.e. the ordered pairs count.

Examples
Input
great10

Output
7

Input
aaaaaaaaaa

Output
100
*/
package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	s, _ := reader.ReadString('\n')
	s = strings.TrimSpace(s)
	aMap := make(map[byte]int, len(s))
	for i := 0; i < len(s); i++ {
		aMap[s[i]]++
	}

	var total int64
	for i := 0; i < len(s); i++ {
		total += int64(aMap[s[i]])
	}
	fmt.Print(total)
}

// B. Choosing Symbol Pairs
/*
There is a given string S consisting of N symbols. Your task is to find the number of ordered pairs of integers i and j such that

1. 1 ≤ i, j ≤ N

2. S[i] = S[j], that is the i-th symbol of string S is equal to the j-th.

Input
The single input line contains S, consisting of lowercase Latin letters and digits. It is guaranteed that string S in not empty and its length does not exceed 105.

Output
Print a single number which represents the number of pairs i and j with the needed property. Pairs (x, y) and (y, x) should be considered different, i.e. the ordered pairs count.

Examples
Input
great10

Output
7

Input
aaaaaaaaaa

Output
100
*/
package main

import (
	"fmt"
)

func main() {
	var s string
	fmt.Scanf("%v\n", &s)

	aMap := make(map[byte]int, len(s))
	for i := 0; i < len(s); i++ {
		aMap[s[i]]++
	}

	var total int64
	for i := 0; i < len(s); i++ {
		total += int64(aMap[s[i]])
	}
	fmt.Print(total)
}

// B. Choosing Symbol Pairs
/*
There is a given string S consisting of N symbols. Your task is to find the number of ordered pairs of integers i and j such that

1. 1 ≤ i, j ≤ N

2. S[i] = S[j], that is the i-th symbol of string S is equal to the j-th.

Input
The single input line contains S, consisting of lowercase Latin letters and digits. It is guaranteed that string S in not empty and its length does not exceed 105.

Output
Print a single number which represents the number of pairs i and j with the needed property. Pairs (x, y) and (y, x) should be considered different, i.e. the ordered pairs count.

Examples
Input
great10

Output
7

Input
aaaaaaaaaa

Output
100
*/
package main

import (
	"fmt"
)

func main() {
	var s string
	fmt.Scanf("%v\n", &s)

	aMap := make(map[byte]int64, len(s))
	for i := 0; i < len(s); i++ {
		aMap[s[i]]++
	}

	var total int64
	for i := 0; i < len(s); i++ {
		total += aMap[s[i]]
	}
	fmt.Print(total)
}

// B. Choosing Symbol Pairs
/*
There is a given string S consisting of N symbols. Your task is to find the number of ordered pairs of integers i and j such that

1. 1 ≤ i, j ≤ N

2. S[i] = S[j], that is the i-th symbol of string S is equal to the j-th.

Input
The single input line contains S, consisting of lowercase Latin letters and digits. It is guaranteed that string S in not empty and its length does not exceed 105.

Output
Print a single number which represents the number of pairs i and j with the needed property. Pairs (x, y) and (y, x) should be considered different, i.e. the ordered pairs count.

Examples
Input
great10

Output
7

Input
aaaaaaaaaa

Output
100
*/
package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	s, _ := reader.ReadString('\n')
	s = strings.TrimSpace(s)
	l := len(s)
	aMap := make(map[byte]int, l)
	for i := 0; i < len(s); i++ {
		aMap[s[i]]++
	}

	var total int64
	for i := 0; i < l; i++ {
		total += int64(aMap[s[i]])
	}
	fmt.Print(total)
}

package main

import (
	"fmt"
)

func main() {
	var (
		i, lw int32
		wi    byte
		w     string
		frek  [26 + 26 + 10]int32
		ans   int64
	)

	fmt.Scan(&w)
	lw = int32(len(w))
	for i = 0; i < lw; i++ {
		wi = w[i]
		switch {
		case 'a' <= wi && wi <= 'z':
			frek[wi-'a']++
			//			println(int32(wi-'a'), wi)
		case 'A' <= wi && wi <= 'Z':
			frek[wi+26-'A']++
			//			println(int32(wi+26-'A'), wi)

		case '0' <= wi && wi <= '9':
			frek[wi+26+26-'0']++
			//			println(int32(wi+26+26-'A'), wi)

		}
	}
	for i = 0; i < 26+26+10; i++ {
		ans += int64(frek[i]) * int64(frek[i])
	}
	fmt.Println(ans)
}

package main

import (
	"fmt"
)

func main() {
	var (
		i    int32
		w    string
		frek [26 + 26 + 10]int32
		ans  int64
	)

	fmt.Scan(&w)
	for _, wi := range w {
		switch {
		case 'a' <= wi && wi <= 'z':
			frek[wi-'a']++
			//			println(int32(wi-'a'), wi)
		case 'A' <= wi && wi <= 'Z':
			frek[wi+26-'A']++
			//			println(int32(wi+26-'A'), wi)

		case '0' <= wi && wi <= '9':
			frek[wi+26+26-'0']++
			//			println(int32(wi+26+26-'A'), wi)

		}
	}
	for i = 0; i < 26+26+10; i++ {
		ans += int64(frek[i]) * int64(frek[i])
	}
	fmt.Println(ans)
}

//  URL: http://codeforces.com/contest/50/problem/B
//  PROBLEM RESTATEMENT
//      Given string s of length N, and 1 <= i, j <= N, count number of pairs
//      s.t s[i] == s[j]. i can equal j, and order matters (i, j) != (j, i).
//  APPROACH
//      1. Brute force approach is O(n^2), n can be an array of size 10^5, so
//         we need to find an analytical approach.
//      2. Let's say that a character appears in s `n` times.
//      3. How many ways can we pick two elements from a set of size `n`?
//             - There are n choose 2 ways of picking two elements from a set.
//             - There are 2! ways of re-ordering each pair.
//             - So, n C 2 * 2!. We can simplify n!/n-2!2! * 2! to n!/n-2!
//               which is n * (n - 1)
//      4. So we have n * (n - 1), for a given character. Because i can equal
//         j, we have to add n, so n * (n - 1) + n = n * n.
//      4. Our approach is now simple. Create a map of type [rune, int64].
//         Iterate through the map, and add value + value.

package main

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

func solve(s string) int64 {
	var count int64 = 0
	m := make(map[rune]int64, 0)
	for _, elem := range s {
		m[elem] += 1
	}
	for _, value := range m {
		count += value*value
	}
	return count
}

func main() {
	reader, writer := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var s string
	fmt.Fscanf(reader, "%s\n", &s)
	fmt.Fprintf(writer, "%d\n", solve(s))
}

// B. Choosing Symbol Pairs
/*
There is a given string S consisting of N symbols. Your task is to find the number of ordered pairs of integers i and j such that

1. 1 ≤ i, j ≤ N

2. S[i] = S[j], that is the i-th symbol of string S is equal to the j-th.

Input
The single input line contains S, consisting of lowercase Latin letters and digits. It is guaranteed that string S in not empty and its length does not exceed 105.

Output
Print a single number which represents the number of pairs i and j with the needed property. Pairs (x, y) and (y, x) should be considered different, i.e. the ordered pairs count.

Examples
Input
great10

Output
7

Input
aaaaaaaaaa

Output
100
*/
package main

import (
	"fmt"
)

func main() {
	var s string
	fmt.Scanf("%v ", &s)

	aMap := make(map[byte]int, len(s))
	for i := 0; i < len(s); i++ {
		aMap[s[i]]++
	}

	total := 0
	for _, v := range aMap {
		total += v * v
	}
	fmt.Print(total)
}

// B. Choosing Symbol Pairs
/*
There is a given string S consisting of N symbols. Your task is to find the number of ordered pairs of integers i and j such that

1. 1 ≤ i, j ≤ N

2. S[i] = S[j], that is the i-th symbol of string S is equal to the j-th.

Input
The single input line contains S, consisting of lowercase Latin letters and digits. It is guaranteed that string S in not empty and its length does not exceed 105.

Output
Print a single number which represents the number of pairs i and j with the needed property. Pairs (x, y) and (y, x) should be considered different, i.e. the ordered pairs count.

Examples
Input
great10

Output
7

Input
aaaaaaaaaa

Output
100
*/
package main

import (
	"fmt"
)

func main() {
	var s string
	fmt.Scanf("%v\n", &s)

	aMap := make(map[byte]int, len(s))
	for i := 0; i < len(s); i++ {
		aMap[s[i]]++
	}

	total := 0
	for i := 0; i < len(s); i++ {
		total += aMap[s[i]]
	}
	fmt.Print(total)
}

//  URL: http://codeforces.com/contest/50/problem/B
//  Time: 4:11PM
//  PROBLEM RESTATEMENT
//      Given string s of length N, and 1 <= i, j <= N, count number of pairs
//      s.t s[i] == s[j]. i can equal j, and order matters (i, j) != (j, i).
//  APPROACH
//      1. Brute force approach is O(n^2), n can be an array of size 10^5, so
//         we need to find an analytical approach.
//      2. Let's say that a character appears in s `n` times.
//      3. How many ways can we pick two elements from a set of size `n`?
//             There are n choose 2 ways of picking two elements from a set.
//             Then, there are 2! ways of re-ordering each pair.
//             So, n C 2 * 2!. We can simplify n!/n-2!2! * 2! to n!/n-2! which
//             is n * (n - 1).
//             Since we can also set i == j, we need to add n to the answer.
//      4. Our approach is now simple. Create a map of type [rune, int].
//         Iterate through the map, and add value + value * (value - 1).

package main

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

func solve(s string) int {
	count := 0
	m := make(map[rune]int, 0)
	for _, elem := range s {
		m[elem] += 1
	}
	for _, value := range m {
		count += value + value*(value-1)
	}
	return count
}

func main() {
	reader, writer := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var s string
	fmt.Fscanf(reader, "%s\n", &s)
	fmt.Fprintf(writer, "%d\n", solve(s))
}