package main

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

func main() {
	var n int
	if _, err := fmt.Scanf("%d\n", &n); err != nil {
		return
	}

	var directions string
	coordinates := make([]int, n)
	reader := bufio.NewReader(os.Stdin)
	re := regexp.MustCompile(`\r?\n`)

	if _d, err := reader.ReadString('\n'); err != nil {
		if err != io.EOF {
			return
		}
	} else {
		directions = re.ReplaceAllString(_d, "")
	}

	if _c, err := reader.ReadString('\n'); err != nil {
		if err != io.EOF {
			return
		}
	} else {
		_c := re.ReplaceAllString(_c, "")
		ss := strings.Split(_c, " ")
		for i, s := range ss {
			x, err := strconv.Atoi(s)
			if err != nil {
				return
			}
			coordinates[i] = x
		}
	}

	min_distance := -1
	for i := 0; i < n-1; i++ {
		if directions[i] == 'R' && directions[i+1] == 'L' {
			dist := (coordinates[i+1] - coordinates[i]) / 2
			if dist < min_distance || min_distance < 0 {
				min_distance = dist
			}
		}
	}

	fmt.Println(min_distance)
}

package main

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

func readInt(buffer *bufio.Reader) (result int) {
	var negative, started bool
READER_LOOP:
	for {
		char, err := buffer.ReadByte()
		switch {
		case err != nil:
			break READER_LOOP
		case char == '-':
			negative = true
		case unicode.IsDigit(rune(char)):
			started = true
			result = result*10 + int(char&0xf)
		case started:
			break READER_LOOP
		}
	}
	if negative {
		result = -result
	}
	return
}

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

	var n int
	fmt.Fscanf(buffer, "%d\n", &n)
	var str string
	for {
		pref, isPrefix, err := buffer.ReadLine()
		if err != nil {
			panic(err)
		}
		str += string(pref)
		if !isPrefix {
			break
		}
	}
	if len(str) != n {
		panic(fmt.Errorf("Expected %d characters but got %d", n, len(str)))
	}

	maj := make([]int, n)
	for i := range maj {
		maj[i] = readInt(buffer)
	}

	best := -1
	for i := 1; i < n; i++ {
		if str[i-1] == 'R' && str[i] == 'L' {
			if prop := (maj[i] - maj[i-1]) / 2; best < 0 || best > prop {
				best = prop
			}
		}
	}
	fmt.Println(best)
}

package main

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

var n int
var s string
var a []int

func main() {
	buf := bufio.NewReaderSize(os.Stdin, 64*1024)

	fmt.Fscan(buf, &n)
	fmt.Fscan(buf, &s)

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

	tmin := -1
	for i := 1; i < n; i++ {
		if (s[i-1] == 'R') && (s[i] == 'L') {
			t := (a[i] - a[i-1]) / 2
			if (tmin == -1) || (t < tmin) {
				tmin = t
			}
		}
	}

	fmt.Println(tmin)
}

package main

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

func main() {
	var n int
	var moves string
	fmt.Scan(&n)
	fmt.Scan(&moves)

	xn := make([]int, n, n)
	bs := bufio.NewScanner(os.Stdin)
	bs.Split(bufio.ScanWords)
	for i := 0; i < n; i++ {
		bs.Scan()
		xn[i], _ = strconv.Atoi(bs.Text())
	}

	pre := -1
	ans := -1
	for i := 0; i < n; i++ {
		if moves[i] == 'R' {
			pre = i
		} else {
			if pre != -1 && (ans == -1 || ans > (xn[i]-xn[pre])/2) {
				ans = (xn[i] - xn[pre]) / 2
			}
		}
	}

	fmt.Println(ans)
}

package main

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

var n int
var s string
var a []int

func main() {
	buf := bufio.NewReaderSize(os.Stdin, 64*1024)
	fmt.Fscan(buf, &n)
	fmt.Fscan(buf, &s)

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

	res := -1
	p := -1

	for i := 0; i < n; i++ {
		if s[i] == 'R' {
			p = i
		} else if p >= 0 {
			t := (a[i] - a[p]) / 2
			if (res == -1) || (t < res) {
				res = t
			}
		}

	}

	fmt.Println(res)
}

package main

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

func readInt(buffer *bufio.Reader) (result int) {
	var negative, started bool
READER_LOOP:
	for {
		char, err := buffer.ReadByte()
		switch {
		case err != nil:
			break READER_LOOP
		case char == '-':
			negative = true
		case unicode.IsDigit(rune(char)):
			started = true
			result = result*10 + int(char&0xf)
		case started:
			break READER_LOOP
		}
	}
	if negative {
		result = -result
	}
	return
}

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

	var n int
	fmt.Fscanf(buffer, "%d\n", &n)
	str, isPrefix, err := buffer.ReadLine()
	if err != nil {
		panic(err)
	}
	if isPrefix {
		panic("Expected full line but found prefix")
	}
	if len(str) != n {
		panic(fmt.Errorf("Expected %d characters but got %d", n, len(str)))
	}

	maj := make([]int, n)
	for i := range maj {
		maj[i] = readInt(buffer)
	}

	for i := 1; i < n; i++ {
		if str[i-1] == 'R' && str[i] == 'L' {
			fmt.Println((maj[i] - maj[i-1]) / 2)
			return
		}
	}
	fmt.Println(-1)
}

package main

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

func main() {
	var n int
	var moves string
	fmt.Scan(&n)
	fmt.Scan(&moves)

	xn := make([]int, n, n)
	bs := bufio.NewScanner(os.Stdin)
	bs.Split(bufio.ScanWords)
	for i := 0; i < n; i++ {
		bs.Scan()
		xn[i], _ = strconv.Atoi(bs.Text())
	}

	pre := -1
	ans := -1
	for i := 0; i < n; i++ {
		if moves[i] == 'R' {
			pre = i
		} else {
			if pre != -1 && (ans == -1 || ans > xn[i]-xn[pre]) {
				ans = (xn[i] - xn[pre])
			}
		}
	}

	fmt.Println(ans / 2)
}

package main

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

func main() {
	var n int
	var moves string
	fmt.Scan(&n)
	fmt.Scan(&moves)

	xn := make([]int, n, n)
	bs := bufio.NewScanner(os.Stdin)
	bs.Split(bufio.ScanWords)
	for i := 0; i < n; i++ {
		bs.Scan()
		xn[i], _ = strconv.Atoi(bs.Text())
	}

	pre := -1
	ans := -1
	for i := 0; i < n; i++ {
		if moves[i] == 'R' {
			pre = i
		} else {
			if pre != -1 && (ans == -1 || ans > xn[i]-xn[pre]) {
				ans = (xn[i] - xn[pre]) / 2
			}
		}
	}

	fmt.Println(ans)
}

package main

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

func main() {
	var n int
	var moves string
	fmt.Scan(&n)
	fmt.Scan(&moves)

	xn := make([]int, n, n)
	bs := bufio.NewScanner(os.Stdin)
	bs.Split(bufio.ScanWords)
	for i := 0; i < n; i++ {
		bs.Scan()
		xn[i], _ = strconv.Atoi(bs.Text())
	}

	pre := -1
	ans := -1
	for i := 0; i < n; i++ {
		if moves[i] == 'R' {
			pre = i
		} else {
			if pre != -1 && (ans == -1 || ans > i-pre) {
				ans = (xn[i] - xn[pre]) / 2
			}
		}
	}

	fmt.Println(ans)
}

package main

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

func main() {
	var n int
	var moves string
	fmt.Scan(&n)
	fmt.Scan(&moves)

	xn := make([]int, n, n)
	bs := bufio.NewScanner(os.Stdin)
	bs.Split(bufio.ScanWords)
	for i := 0; i < n; i++ {
		bs.Scan()
		xn[i], _ = strconv.Atoi(bs.Text())
	}

	pre := -1
	ans := -1
	for i := 0; i < n; i++ {
		if moves[i] == 'R' {
			pre = i
		} else {
			if pre != -1 && (ans == -1 || ans > (xn[i]-xn[pre]/2)) {
				ans = (xn[i] - xn[pre]) / 2
			}
		}
	}

	fmt.Println(ans)
}


package main

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

var (
	sc = bufio.NewScanner(os.Stdin)
	wr = bufio.NewWriter(os.Stdout)
)

func scanInt() int {
	sc.Scan()
	a, _ := strconv.Atoi(sc.Text())
	return a
}

func scanInt64() int64 {
	sc.Scan()
	a, _ := strconv.ParseInt(sc.Text(), 10, 64)
	return a
}

func scanInts(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = scanInt()
	}
	return res
}

func scanText() string {
	sc.Scan()
	return sc.Text()
}

func printInts(a ...int) {
	for i, e := range a {
		fmt.Fprint(wr, e)
		if i != len(a)-1 {
			fmt.Fprint(wr, " ")
		}
	}
	fmt.Fprintln(wr)
	wr.Flush()
}

const LOG = 30

var (
	n       int
	a       []int
	ch      [][2]int
	trieCnt int
	root    int
)

func insert(x uint) {
	u := root
	for i := uint(LOG - 1); i >= 0; i-- {
		if ch[u][x>>i&1] == 0 {
			trieCnt++
			ch[u][x>>i&1] = trieCnt
		}
		u = ch[u][x>>i&1]
		if i == 0 {
			break
		}
	}
}

func check(x uint, u uint, d int) bool {
	if d < 0 {
		return true
	}
	if u == 0 && d != LOG-1 {
		return true
	}
	if x>>uint(d)&1 == 1 {
		return check(x, uint(ch[u][0]), d-1) || check(x, uint(ch[u][1]), d-1)
	}
	if ch[u][0] != 0 && ch[u][1] != 0 {
		return false
	}
	return check(x, uint(ch[u][0])|uint(ch[u][1]), d-1)
}

func main() {
	sc.Split(bufio.ScanWords)
	n = scanInt()
	a = scanInts(n)
	ch = make([][2]int, n*LOG+1)
	for _, x := range a {
		insert(uint(x))
	}
	low, high := uint(0), (uint(1)<<30)-1
	mid, ans := uint(0), high

	for low <= high && ans != 0 {
		mid = (low + high)>>1
		if check(mid, uint(root), LOG-1) {
			ans = mid
			high = mid - 1
		} else {
			low = mid + 1
		}
	}
	fmt.Println(ans)
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"sync"
)
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
var out chan string
var in *bufio.Scanner
var outWg *sync.WaitGroup
func ReadString() string {
	in.Scan()
	return in.Text()
}
func ReadStringSlice(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ReadString()
	}
	return s
}
func ReadInt() int {
	intStr := ReadString()
	i, _  := strconv.Atoi(intStr)
	return i
}
func ReadIntSlice(n int) []int {
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = ReadInt()
	}
	return arr
}
func init() {
	//set input
	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(2e+5))
	in.Split(bufio.ScanWords)

	//set output
	out = make(chan string, 16)
	outWg = &sync.WaitGroup{}
	outWg.Add(1)

	writer := bufio.NewWriterSize(os.Stdout, int(2e+5))
	go func(write *bufio.Writer) {
		defer outWg.Done()
		defer write.Flush()

		for line := range out {
			write.WriteString(line + "\n")
		}
	}(writer)
}
const maxn int = 100005
var ch [maxn*30][2]int
var sz int = 0
var ans int = 1 << 30
func dfs(o, pos, val int) {
	if pos < 0 {
		ans = min(ans, val)
		return
	}
	if (ch[o][0] > 0 && ch[o][1] > 0) {
		dfs(ch[o][0], pos - 1, val | int(1) << uint(pos))
		dfs(ch[o][1], pos - 1, val | int(1) << uint(pos))
	} else if (ch[o][1] > 0) {
		dfs(ch[o][1], pos - 1, val)
	} else {
		dfs(ch[o][0], pos - 1, val)
	}
}
func work() {
	n := ReadInt()
	a := ReadIntSlice(n)
	for i := 0; i < n * 30; i++ {
		ch[i][0], ch[i][1] = 0, 0
	}
	for i := 0; i < n; i++ {
		o := 0
		for j := 29; j >= 0; j-- {
			to := 0
			if (a[i] >> uint(j) & 1) > 0 {
				to = 1
			}
			if ch[o][to] == 0 {
				sz++
				ch[o][to] = sz
			}
			o = ch[o][to]
		}
	}
	dfs(0, 29, 0)
	fmt.Println(ans)

}
func main() {

	work()
	close(out)
}

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

func solve(v []int, bit int) int64 {
	if bit < 0 {
		return 0
	}
	var on, off []int
	for i := 0; i < len(v); i++ {
		x := (1 << bit) & v[i]
		if x == 0 {
			off = append(off, v[i])
		} else {
			on = append(on, v[i])
		}
	}
	if len(on) == 0 || len(off) == 0 {
		return solve(v, bit - 1)
	}
	x1 := solve(on, bit - 1)
	x2 := solve(off, bit - 1)
	if x1 < x2 {
		return x1 + (1 << bit)
	}
	return x2 + (1 << bit)
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var n, x int
	fmt.Fscanf(in, "%d\n", &n)
	var arr []int
	for i := 0; i < n; i++ {
		fmt.Fscanf(in, "%d ", &x)
		arr = append(arr, x)
	}
	fmt.Fprintf(out, "%v\n", solve(arr, 30))
}

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 := 0
		fmt.Fscanf(in, "%d\n", &n)
		a := make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d", &a[i])
		}
		fmt.Fscanf(in, "\n")
		fmt.Fprintf(out, "%d\n", solve(a, 31))
	}
	return
}

func solve(a []int64, pos int64) int64 {
	//fmt.Println(a, pos)
	if pos == 0 {
		for i := 1; i < len(a); i++ {
			if a[i]&1 != a[0]&1 {
				return 1
			}
		}
		return 0
	}
	b := []int64{}
	c := []int64{}
	cur := int64(1) << uint64(pos)
	for i := 0; i < len(a); i++ {
		if a[i]&cur > 0 {
			b = append(b, a[i])
		} else {
			c = append(c, a[i])
		}
	}
	if len(b) == 0 {
		return solve(c, pos-1)
	}
	if len(c) == 0 {
		return solve(b, pos-1)
	}
	return cur + min(solve(b, pos-1), solve(c, pos-1))
}

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

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"sync"
)
func gcd(a,b int64)int64{
	if(a==0){
		return b
	}
	return gcd(b%a,a)
}
func lcm(a,b int64)int64{
	return a/gcd(a,b)*b
}
func min(a,b int64)int64{
	if(a>b){
		return b
	}else{
		return a
	}
}
func max(a,b int64)int64{
	if(a>b){
		return a
	}
	return b
}
func dfs(x int64,a []int64)int64{
	if(x<0){
		return 0
	}
	var L,R []int64
	Len:=len(a)
	for i:=0;i<Len;i++{
		if (a[i]&(1<<uint64(x)))>0 {
			L=append(L, a[i])
		}else{
			R=append(R,a[i])
		}
	}
	if(len(L)==0){
		return dfs(x-1,R)
	}
	if(len(R)==0){
		return dfs(x-1,L)
	}
	return min(dfs(x-1,L),dfs(x-1,R))+(1<<uint64(x))
}
func main(){
	var n int64
	fmt.Scan(&n)
	a:=make([]int64,n)
	for i:=int64(0);i<n;i++{
		a[i]=ReadInt64()
	}
	fmt.Println(dfs(30,a))
}

/*********************** utility ***********************/

var out chan string
var in *bufio.Scanner
var outWg *sync.WaitGroup

func init() {
	//set input
	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(2e+5))
	in.Split(bufio.ScanWords)

	// set output
	out = make(chan string, 16)
	outWg = &sync.WaitGroup{}
	outWg.Add(1)

	writer := bufio.NewWriterSize(os.Stdout, int(2e+5))
	go func(writer *bufio.Writer) {
		defer outWg.Done()
		defer writer.Flush()

		for line := range out {
			writer.WriteString(line + "\n")
		}
	}(writer)
}

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

func ReadStringSlice(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ReadString()
	}
	return s
}

func ReadInt64() int64 {
	intStr := ReadString()
	i, _ := strconv.ParseInt(intStr,10,64)
	return i
}

func ReadIntSlice(n int64) []int64 {
	arr := make([]int64, n)
	for i := int64(0); i < n; i++ {
		arr[i] = ReadInt64()
	}
	return arr
}


package main

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

func main() {
	defer out.Flush()
	n := in.NextInt()
	count := map[int]int{}
	time := map[int]int{}
	for i := 0; i < n; i++ {
		a := in.NextInt()
		count[a]++
		time[a] = i
	}
	ans := 0
	for a, c := range count {
		if c > count[ans] || (c == count[ans] && time[a] < time[ans]) {
			ans = a
		}
	}
	fmt.Fprintln(out, ans)
}

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

type FastReader struct {
	r   *bufio.Reader
	buf string
}

func NewFastReader(r io.Reader) *FastReader {
	return &FastReader{
		r: bufio.NewReader(r),
	}
}

func (r *FastReader) advance() {
	for len(r.buf) != 0 && r.buf[0] == ' ' {
		r.buf = r.buf[1:]
	}
	for len(r.buf) == 0 {
		var buf bytes.Buffer
		for {
			chunk, more, _ := r.r.ReadLine()
			buf.Write(chunk)
			if !more {
				break
			}
		}
		r.buf = buf.String()
	}
}

func (r *FastReader) NextLine() string {
	r.advance()
	line := r.buf
	r.buf = ""
	return line
}

func (r *FastReader) NextWord() string {
	r.advance()
	var word string
	wordStart := 0
	for i := 0; i < len(r.buf); i++ {
		b := r.buf[i]
		if b == ' ' {
			if i != wordStart {
				word = r.buf[wordStart:i]
				r.buf = r.buf[i:]
				break
			}
			wordStart = i + 1
		}
	}
	if len(word) == 0 {
		word = r.buf
		r.buf = ""
	}
	return word
}

func (r *FastReader) NextInt() int {
	return parseInt(r.NextWord())
}

func parseInt(word string) int {
	sign := 1
	if word[0] == '-' {
		sign = -1
		word = word[1:]
	}
	result := 0
	for i := 0; i < len(word); i++ {
		result = result*10 + int(word[i]) - '0'
	}
	result *= sign
	return result
}

package main

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

var scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func ReadInt32() int {
	scanner.Scan()
	ans, _ := strconv.Atoi(scanner.Text())
	return ans
}

func PrintInts(ints ...int) {
	for _, value := range ints {
		writer.WriteString(strconv.Itoa(value))
		//writer.WriteByte(' ')
	}
}

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

	var cnt int = ReadInt32()
	var photos map[int]int
	photos = make(map[int]int)
	res := make(map[int][]int)
	top := 0

	for c := 0; c < cnt; c++ {
		like := ReadInt32()

		photos[like] = photos[like] + 1
		//if !res[photos[like]] {
		//	res[photos[like]] = make([]int, 1)
		//}
		res[photos[like]] = append(res[photos[like]], like)
		if top < photos[like] {
			top = photos[like]
		}
	}
	PrintInts(res[top][0])
	writer.WriteByte('\n')
}

package main

import "fmt"

func main()  {
  var N int
  fmt.Scanf("%d\n", &N)
  L := make(map[int]int)

  ret := 0
  max := 0

  for i := 0; i < N; i++ {
    var a int
    fmt.Scanf("%d", &a)
    L[a]++

    if L[a] > max {
      max = L[a]
      ret = a
    }
  }

  fmt.Println(ret)
}

package main

import (
	"fmt"
)

func main() {
	var n, max, winner int
	fmt.Scan(&n)
	var poll = make(map[int]int, n)

	for i := 0; i < n; i++ {
		var vote int
		fmt.Scan(&vote)
		poll[vote]++
		if poll[vote] > max {
			max = poll[vote]
			winner = vote
		}
	}
	fmt.Println(winner)
}


package main

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

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

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

func getLine() string {
	s, _ := reader.ReadString('\n')
	for len(s) > 0 && (s[len(s)-1] == '\n' || s[len(s)-1] == '\r') {
		s = s[:len(s)-1]
	}
	return s
}

func main() {
	// STDOUT MUST BE FLUSHED MANUALLY!!!
	defer writer.Flush()

	var n int
	scanf("%d\n", &n)

	var odda []int
	var evena []int
	for ; n >= 1; n-- {
		var a int
		scanf("%d", &a)
		if a%2 == 0 {
			evena = append(evena, a)
		} else {
			odda = append(odda, a)
		}
	}

	var longer []int
	var diffc int
	if len(odda)-len(evena) >= 0 {
		longer = odda
		diffc = len(odda) - len(evena)
	} else {
		longer = evena
		diffc = len(evena) - len(odda)
	}

	var sum int
	if diffc > 1 {
		sort.Ints(longer)
		for i := 0; i < diffc-1; i++ {
			sum += longer[i]
		}
	}
	printf("%d\n", sum)

}

package main

import (
	"fmt"
	"sort"
)
func abs(c int) int {
	if c < 0 {
		return -c
	}
	return c
}

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

type info struct {
	cnt int
	sum int64
}
func main()  {
	var n int
	fmt.Scan(&n)
	data := make([]int, n, n)
	for i := 0 ;i < n; i++ {
		fmt.Scan(&data[i])
	}
	sort.Ints(data)

	o := 0
	e := 0
	for i := 0 ; i < n ; i++ {
		if data[i] % 2 == 1 {
			e ++
		} else {
			o ++
		}
	}
	need :=  abs(o - e) - 1
	if need  <= 0 {
		fmt.Println(0)
		return
	}

	key := 0
	if o > e {
		key = 0
	} else {
		key = 1
	}

	wat := info{cnt:0, sum:0}

	for i := 0 ; i < n; i++ {
		if data[i] % 2 == key {
			wat.cnt ++
			wat.sum += int64(data[i])
			if wat.cnt == need {
				break
			}
 		}
	}
	fmt.Println(wat.sum)
}

// Author: sighduck
// URL: https://codeforces.com/contest/1144/problem/B

package main

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

func sumK(a []int, k int) int {
	sum := 0
	for i := 0; i < k; i++ {
		sum += a[i]
	}
	return sum
}

func Solve(n int, a []int) int {
	evens := []int{}
	odds := []int{}
	for _, elem := range a {
		if elem%2 == 0 {
			evens = append(evens, elem)
		} else {
			odds = append(odds, elem)
		}
	}
	sort.Ints(evens)
	sort.Ints(odds)
	evenCount, oddCount := len(evens), len(odds)
	if evenCount+1 == oddCount || oddCount+1 == evenCount || oddCount == evenCount {
		return 0
	}
	if evenCount > oddCount {
		return sumK(evens, evenCount-(oddCount+1))
	}
	return sumK(odds, oddCount-(evenCount+1))
}

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

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

	fmt.Fprintf(writer, "%d\n", Solve(n, a))
}

package main

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

func main() {
	var n, x, cnt, cnt1, sum int
	scr := bufio.NewReader(os.Stdin)
	// ocr := bufio.NewWriter(os.Stdout)

	fmt.Fscanln(scr, &n)
	even := make([]int, 0, n)
	odd := make([]int, 0, n)

	for i := 0; i < n; i++ {
		fmt.Fscanf(scr, "%d", &x)
		if x%2 == 0 {
			even = append(even, x)
		} else {
			odd = append(odd, x)
		}
	}

	sort.Sort(sort.IntSlice(odd))
	sort.Sort(sort.IntSlice(even))

	if len(odd) > len(even)+1 {
		cnt = len(odd) - len(even) - 1
	} else if len(even) > len(odd)+1 {
		cnt1 = len(even) - len(odd) - 1
	}
	if cnt > 0 {
		for i := 0; i < cnt; i++ {
			sum += odd[i]
		}
		fmt.Println(sum)
	} else if cnt1 > 0 {
		for i := 0; i < cnt1; i++ {
			sum += even[i]
		}
		fmt.Println(sum)
	} else {
		fmt.Println(0)
	}

}

package main

import (
. "fmt"
"sort"
)

func main() {
    var N int
    Scan(&N)
    ganjil := []int{}
    genap := []int{}
    for i := 0; i < N; i++ {
        var x int
        Scan(&x)
        if x%2 == 1 {
            ganjil = append(ganjil, x)
        } else {
            genap = append(genap, x)
        }
    }
    if len(ganjil) - len(genap) > 1 {
        sum := 0
        sort.Ints(ganjil)
        for i := 0; i < len(ganjil) - len(genap) - 1; i++ {
            sum += ganjil[i]
        }
        Print(sum)
    } else if len(genap) - len(ganjil) > 1 {
        sum := 0
        sort.Ints(genap)
        for i := 0; i < len(genap) - len(ganjil) - 1; i++ {
            sum += genap[i]
        }
        Print(sum)
    } else {
        Print("0")
    }
}

package main

import (
	"fmt"
	"sort"
)

func min(a,b int) int  {
	if(a < b){
		return a
		}
	return b
}
func katta(a,b int) int  {
	if(a < b){
		return b
	}
	return a
}
func ab(x int) int {
	if(x <= -1){
		x *= -1
	}
	return x
}

func main()  {

	var n int
	fmt.Scan(&n)
	m := make([]int,n)
	jf := 0
	for i := 0; i < n;i++{
		fmt.Scan(&m[i])
		if(m[i] % 2 == 0){
			jf ++
		}
	}
	toq := n - jf
	cnt := min(toq,jf)
	temp := cnt
	sort.Ints(m)
	for i := n - 1 ; i >= 0  && temp >= 1;i --{
		if(m[i] % 2 == 0 && m[i] >= 1){
			m[i] = 0
			temp --
		}
	}
	temp = cnt
	for i := n - 1 ;i >= 0 && temp >= 1 ; i--{
		if(m[i] % 2 != 0){
			m[i] = 0
			temp --
		}
	}
	for i := n - 1 ;i >= 0 ; i --{
		if(m[i] >= 1){
			m[i] = 0
			break
		}
	}
	ans := 0
	for i := 0 ; i < n;i ++{
		ans += m[i]
	}
	fmt.Println(ans)

}

// Codeforces 1144 B compet
package main

import (
	"bufio"
	"fmt"
	"strings"
	"strconv"
	"os"
	"sort"
)
var in *bufio.Reader

func readFive() (int,int,int,int,int) {
	var a [5]int
	s, err := in.ReadString('\n')
	if err != nil {
		fmt.Println("first read failure", err)
		panic(err)
	}
	ss := strings.Split(strings.Trim(s, " \n\r"), " ")
	for i:=0; i<len(ss); i++ {
		a[i], err = strconv.Atoi(ss[i])
		if err != nil {
			fmt.Println("n atoi failure", err)
			panic(err)
		}
	}
	return a[0],a[1],a[2],a[3],a[4]
}

func readLineNumbs(n int) []int {
	a := make([]int, n)
	delim := byte(' ')
	for i:=0; i<n; i++ {
		if i==(n-1) {
			delim = byte('\n')
		}
		s, err := in.ReadString(delim)
		s = strings.Trim(s, " \n\r")
		if err != nil {
			fmt.Println("read failure", err)
			panic(err)
		}
		aa,err := strconv.Atoi(s)
		if err != nil {
			fmt.Println("aa conv failed", err)
			panic(err)
		}
		a[i] = aa
	}
	return a
}

func readString() string {
	delim := byte('\n')
	s, err := in.ReadString(delim)
	s = strings.Trim(s, " \n\r")
	if err != nil {
		fmt.Println("read failure", err)
		panic(err)
	}
	return s
}

var n int
var a []int
var nonDel int64

func readData() {
	in = bufio.NewReader(os.Stdin)
	n,_,_,_,_ = readFive()
	a = readLineNumbs(n)
}

func solve() {
	var no,ne int
	no = 0
	ne = 0
	for i:=0; i<n; i++ {
		if a[i] %2 == 0 {
			ne++
		} else {
			no++
		}
	}
	ae := make([]int, ne)
	ao := make([]int, no)
	no=0; ne=0
	for i:=0; i<n; i++ {
		if a[i] %2 == 0 {
			ae[ne] = a[i];
			ne++
		} else {
			ao[no] = a[i];
			no++
		}
	}
	sort.Ints(ae)
	sort.Ints(ao)
	var ra []int
	var nrem int
	if no >= ne-1 && no <= ne+1 {
		nonDel = 0
		return
	} else if no < ne {
		ra = ae
		nrem = ne - no - 1
	} else {
		ra = ao
		nrem = no - ne - 1
	}
	nonDel = 0
	for i:=0; i<nrem; i++ {
		nonDel += int64(ra[i])
	}
}

func printRes() {
	fmt.Println(nonDel)
}

func main() {
	readData()
	solve()
	printRes()
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var n int
	fmt.Scanln(&n)

	even := make([]int, 0)
	odd := make([]int, 0)

	for i := 0; i < n; i++ {
		var val int
		fmt.Scan(&val)

		if val % 2 == 0 {
			even = append(even, val)
		} else {
			odd = append(odd, val)
		}
	}

	sort.Ints(even)
	sort.Ints(odd)

	var sum int
	var arr []int
	if len(even) > len(odd) {
		arr = even[:len(even) - len(odd) - 1]
	} else if len(odd) > len(even) {
		arr = odd[:len(odd) - len(even) - 1]
	}
	for _, m := range arr {
		sum += m
	}

	fmt.Println(sum)
}

package main

import (
	"fmt"
	"sort"
)

func min(a,b int) int  {
	if(a < b){
		return a
		}
	return b
}
func katta(a,b int) int  {
	if(a < b){
		return b
	}
	return a
}
func ab(x int) int {
	if(x <= -1){
		x *= -1
	}
	return x
}

func main()  {

	var n int
	fmt.Scan(&n)
	m := make([]int,n)
	jf := 0
	for i := 0; i < n;i++{
		fmt.Scan(&m[i])
		if(m[i] % 2 == 0){
			jf ++
		}
	}
	toq := n - jf
	cnt := min(toq,jf)
	cnt += cnt + 1
	n -= cnt
	sort.Ints(m)
	ans := 0
	for i := 0; i < n;i ++{
		ans += m[i]
	}
	fmt.Println(ans)

}

package main

import (
	"fmt"
	"sort"
)

func min(a,b int) int  {
	if(a < b){
		return a
		}
	return b
}
func katta(a,b int) int  {
	if(a < b){
		return b
	}
	return a
}
func ab(x int) int {
	if(x <= -1){
		x *= -1
	}
	return x
}

func main()  {

	var n int
	fmt.Scan(&n)
	m := make([]int,n)
	jf := 0
	for i := 0; i < n;i++{
		fmt.Scan(&m[i])
		if(m[i] % 2 == 0){
			jf ++
		}
	}
	toq := n - jf
	cnt := min(toq,jf)
	cnt += cnt + 1
	n -= cnt
	sort.Ints(m)
	var ans int64
	for i := 0; i < n;i ++{
		ans += int64(m[i])
	}
	fmt.Println(ans)

}

package main

import (
	"fmt"
	"sort"
)
func abs(c int) int {
	if c < 0 {
		return -c
	}
	return c
}

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

type info struct {
	cnt int
	sum int64
}
func main()  {
	var n int
	fmt.Scan(&n)
	data := make([]int, n, n)
	for i := 0 ;i < n; i++ {
		fmt.Scan(&data[i])
	}
	sort.Ints(data)

	o := 0
	e := 0
	for i := 0 ; i < n ; i++ {
		if data[i] % 2 == 1 {
			e ++
		} else {
			o ++
		}
	}
	need :=  abs(o - e) - 1
	fmt.Println(need)
	if need  <= 0 {
		fmt.Println(0)
		return
	}

	key := 0
	if o > e {
		key = 0
	} else {
		key = 1
	}

	wat := info{cnt:0, sum:0}

	for i := 0 ; i < n; i++ {
		if data[i] % 2 == key {
			wat.cnt ++
			wat.sum += int64(data[i])
			if wat.cnt == need {
				break
			}
 		}
	}
	fmt.Println(wat.sum)
}

package main

import (
	"fmt"
	"sort"
)
func abs(c int) int {
	if c < 0 {
		return -c
	}
	return c
}

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

type info struct {
	cnt int
	sum int64
}
func main()  {
	var n int
	fmt.Scan(&n)
	data := make([]int, n, n)
	for i := 0 ;i < n; i++ {
		fmt.Scan(&data[i])
	}
	sort.Ints(data)

	o := 0
	e := 0
	for i := 0 ; i < n ; i++ {
		if data[i] % 2 == 1 {
			e ++
		} else {
			o ++
		}
	}
	need :=  abs(o - e) - 1
	key := 0
	if o > e {
		key = 0
	} else {
		key = 1
	}

	if need  < 0 {
		fmt.Println(0)
		return
	}

	wat := info{cnt:0, sum:0}

	for i := 0 ; i < n; i++ {
		if data[i] % 2 == key {
			wat.cnt ++
			wat.sum += int64(data[i])
			if wat.cnt == need {
				break
			}
 		}
	}
	fmt.Println(wat.sum)
}

package main

import (
	"fmt"
	"sort"
)
func abs(c int) int {
	if c < 0 {
		return -c
	}
	return c
}

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

type info struct {
	cnt int
	sum int64
}
func main()  {
	var n int
	fmt.Scan(&n)
	data := make([]int, n, n)
	for i := 0 ;i < n; i++ {
		fmt.Scan(&data[i])
	}
	sort.Ints(data)

	o := 0
	e := 0
	for i := 0 ; i < n ; i++ {
		if data[i] % 2 == 1 {
			e ++
		} else {
			o ++
		}
	}
	need :=  abs(o - e) - 1
	if need  <= 0 {
		return
	}

	key := 0
	if o > e {
		key = 0
	} else {
		key = 1
	}

	wat := info{cnt:0, sum:0}

	for i := 0 ; i < n; i++ {
		if data[i] % 2 == key {
			wat.cnt ++
			wat.sum += int64(data[i])
			if wat.cnt == need {
				break
			}
 		}
	}
	fmt.Println(wat.sum)
}


package main

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

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

	n, m := readTwoNums(reader)
	D := readNNums(reader, n)

	solver := NewSolver(n, D)
	var buf bytes.Buffer
	for m > 0 {
		m--
		a, b := readTwoNums(reader)
		res := solver.solve(a, b)
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}
	fmt.Print(buf.String())
}

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
}

const MOD = 998244353

func pow(a, b int64) int64 {
	var r int64 = 1
	for b > 0 {
		if b&1 == 1 {
			r = (r * a) % MOD
		}
		a = (a * a) % MOD
		b >>= 1
	}
	return r
}

func inverse(num int64) int64 {
	return pow(num, MOD-2)
}

func modDiv(a, b int64) int64 {
	return a * inverse(b) % MOD
}

func modMul(a, b int64) int64 {
	return a * b % MOD
}

type Solver struct {
	sums []int64
	arr  []int
}

func NewSolver(n int, A []int) Solver {
	sort.Ints(A)
	sums := make([]int64, n+1)
	for i := 0; i < n; i++ {
		sums[i+1] = (sums[i] + int64(A[i])) % MOD
	}

	return Solver{sums, A}
}

func (solver Solver) solve(a, b int) int {
	n := len(solver.arr)

	i := sort.SearchInts(solver.arr, b)

	//if i < n && solver.arr[i] == b {
	//	i--
	//}

	big := n - i

	if a > big || a == n {
		return 0
	}
	tmp := (solver.sums[n] - solver.sums[i] + MOD) % MOD
	// tmp * (1 - a/big)
	res := modMul(tmp, modDiv(int64(big-a), int64(big)))

	tmp = solver.sums[i]

	res += modMul(tmp, modDiv(int64(big+1-a), int64(big+1)))

	res %= MOD

	return int(res)
}

package main

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

// github.com/EndlessCheng/codeforces-go
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	const mod int64 = 998244353
	pow := func(x, n int64) (res int64) {
		res = 1
		for ; n > 0; n >>= 1 {
			if n&1 == 1 {
				res = res * x % mod
			}
			x = x * x % mod
		}
		return res
	}
	div := func(a, b int64) int64 { return a * pow(b, mod-2) % mod }

	var n, q, def int
	var dur int64
	Fscan(in, &n, &q)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	sort.Ints(a)
	sum := make([]int64, n+1)
	for i, v := range a {
		sum[i+1] = (sum[i] + int64(v)) % mod
	}
	for ; q > 0; q-- {
		Fscan(in, &dur, &def)
		p := sort.SearchInts(a, def)
		cnt := int64(n - p)
		if cnt < dur {
			Fprintln(out, 0)
			continue
		}
		ans := div(cnt+1-dur, cnt+1) * sum[p]
		if cnt > dur {
			ans += div(cnt-dur, cnt) * (sum[n] - sum[p] + mod)
		}
		Fprintln(out, ans%mod)
	}
}

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

package main

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

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

	n, m := readTwoNums(reader)
	D := readNNums(reader, n)

	solver := NewSolver(n, D)
	var buf bytes.Buffer
	for m > 0 {
		m--
		a, b := readTwoNums(reader)
		res := solver.solve(a, b)
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}
	fmt.Print(buf.String())
}

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
}

const MOD = 998244353

func pow(a, b int64) int64 {
	var r int64 = 1
	for b > 0 {
		if b&1 == 1 {
			r = (r * a) % MOD
		}
		a = (a * a) % MOD
		b >>= 1
	}
	return r
}

func inverse(num int64) int64 {
	return pow(num, MOD-2)
}

func modDiv(a, b int64) int64 {
	return a * inverse(b) % MOD
}

func modMul(a, b int64) int64 {
	return a * b % MOD
}

type Solver struct {
	sums []int64
	arr  []int
}

func NewSolver(n int, A []int) Solver {
	sort.Ints(A)
	sums := make([]int64, n+1)
	for i := 0; i < n; i++ {
		sums[i+1] = (sums[i] + int64(A[i])) % MOD
	}

	return Solver{sums, A}
}

func (solver Solver) solve(a, b int) int {
	n := len(solver.arr)

	i := sort.SearchInts(solver.arr, b)

	//if i < n && solver.arr[i] == b {
	//	i--
	//}

	big := n - i

	if a > big || a == n {
		return 0
	}
	tmp := (solver.sums[n] - solver.sums[i] + MOD) % MOD
	// tmp * (1 - a/big)
	res := modMul(tmp, modDiv(int64(big-a), int64(big)))

	tmp = solver.sums[i]

	res += modMul(tmp, modDiv(int64(big+1-a), int64(big+1)))

	return int(res)
}

package main

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

// github.com/EndlessCheng/codeforces-go
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	const mod int64 = 998244353
	pow := func(x, n int64) (res int64) {
		res = 1
		for ; n > 0; n >>= 1 {
			if n&1 == 1 {
				res = res * x % mod
			}
			x = x * x % mod
		}
		return res
	}
	div := func(a, b int64) int64 { return a * pow(b, mod-2) % mod }

	var n, q, def int
	var dur int64
	Fscan(in, &n, &q)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	sort.Ints(a)
	sum := make([]int64, n+1)
	for i, v := range a {
		sum[i+1] = sum[i] + int64(v)
	}
	for ; q > 0; q-- {
		Fscan(in, &dur, &def)
		p := sort.SearchInts(a, def)
		cnt := int64(n - p)
		if cnt < dur {
			Fprintln(out, 0)
			continue
		}
		ans := div(cnt+1-dur, cnt+1) * sum[p]
		if cnt > dur {
			ans += div(cnt-dur, cnt) * (sum[n] - sum[p])
		}
		Fprintln(out, ans%mod)
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	const mod int64 = 998244353
	pow := func(x, n int64) (res int64) {
		res = 1
		for ; n > 0; n >>= 1 {
			if n&1 == 1 {
				res = res * x % mod
			}
			x = x * x % mod
		}
		return res
	}
	invP := func(a int64) int64 { return pow(a, mod-2) }
	divP := func(a, b int64) int64 { return a * invP(b) % mod }

	var n, q, def int
	var dur int64
	Fscan(in, &n, &q)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	sort.Ints(a)
	sum := make([]int64, n+1)
	for i, v := range a {
		sum[i+1] = sum[i] + int64(v)
	}
	for ; q > 0; q-- {
		Fscan(in, &dur, &def)
		p := sort.SearchInts(a, def)
		big := int64(n - p)
		ans := int64(0)
		if big > dur {
			ans += divP(big-dur, big) * (sum[n] - sum[p]) % mod
		}
		if big >= dur {
			ans += divP(big+1-dur, big+1) * sum[p] % mod
		}
		Fprintln(out, ans%mod)
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	const mod int64 = 998244353
	pow := func(x, n int64) (res int64) {
		res = 1
		for ; n > 0; n >>= 1 {
			if n&1 == 1 {
				res = res * x % mod
			}
			x = x * x % mod
		}
		return res
	}
	div := func(a, b int64) int64 { return a * pow(b, mod-2) % mod }

	var n, q, def int
	var dur int64
	Fscan(in, &n, &q)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	sort.Ints(a)
	sum := make([]int64, n+1)
	for i, v := range a {
		sum[i+1] = (sum[i] + int64(v)) % mod
	}
	for ; q > 0; q-- {
		Fscan(in, &dur, &def)
		p := sort.SearchInts(a, def)
		cnt := int64(n - p)
		if cnt < dur {
			Fprintln(out, 0)
			continue
		}
		ans := div(cnt+1-dur, cnt+1) * sum[p]
		if cnt > dur {
			ans += div(cnt-dur, cnt) * (sum[n] - sum[p])
		}
		Fprintln(out, ans%mod)
	}
}

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


package main

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

const NN = 20

func readInt(in *bufio.Scanner) (x int) {
	in.Scan()
	x, _ = strconv.Atoi(string(in.Bytes()))
	// if err != nil {
	// 	fmt.Println(err)
	// }
	return
}

// func readPositiveInt(in *bufio.Scanner) (x int) {
// 	in.Scan()
// 	for _, b := range in.Bytes() {
// 		x = x*10 + int(b&15)
// 	}
// 	return
// }

func readString(in *bufio.Scanner) (s string) {
	in.Scan()
	s = string(in.Bytes())
	return
}

func readBytes(in *bufio.Scanner) (s []byte) {
	in.Scan()
	s = in.Bytes()
	return
}

func main() {
	in := bufio.NewScanner(os.Stdin)
	in.Split(bufio.ScanWords)
	n := readInt(in)
	m := readInt(in)
	q := readInt(in)
	ss := make([][]byte, 0)
	for i := 0; i < n; i++ {
		s := readBytes(in)
		ss = append(ss, []byte(string(s)))
	}
	// fmt.Println(ss)
	cnt := 0
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if ss[i][j] == '*' {
				cnt++
			}
		}
	}
	val := 0
	for j := 0; j < m; j++ {
		for i := 0; i < n; i++ {
			if j*n+i >= cnt && ss[i][j] == '*' {
				val++
			}
		}
	}

	for i := 0; i < q; i++ {
		a := readInt(in)
		b := readInt(in)
		a--
		b--
		// if n == 999 {
		// 	if i > NN {
		// 		fmt.Println(a, b, cnt, val, cnt%n, cnt/n)
		// 	}
		// }
		j := a + b*n
		if j >= cnt {
			if ss[a][b] == '*' {
				val--
			} else {
				val++
			}
		}
		if ss[a][b] == '*' {
			// cnt-1
			ss[a][b] = '.'
			cnt--
			if ss[cnt%n][cnt/n] == '*' {
				val++
			}
		} else {
			// cnt
			ss[a][b] = '*'
			if ss[cnt%n][cnt/n] == '*' {
				val--
			}
			cnt++
		}
		// if n == 999 {
		// 	if i > NN {
		// 		fmt.Println(i, val)
		// 	}
		// } else {
		fmt.Println(val)
		// }
	}
}

package main

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

const N = 1000

var z [N][N + 1]bool

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

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

	var c int
	for i := 0; i < n; i++ {
		var s string
		fmt.Fscan(in, &s)

		for j, ch := range s {
			if ch == '*' {
				z[i][j] = true
				c++
			}
		}
	}

	var req int
	for i := 0; i < c; i++ {
		if !z[i%n][i/n] {
			req++
		}
	}

	for i := 0; i < q; i++ {
		var x, y int
		fmt.Fscan(in, &x, &y)

		x--
		y--
		if z[x][y] {
			// was valid, now off.
			if y*n+x < c {
				req++
			}
			z[x][y] = false
			c--
			if !z[c%n][c/n] {
				req--
			}

		} else {
			if !z[c%n][c/n] {
				req++
			}
			c++
			z[x][y] = true
			if y*n+x < c {
				req--
			}
		}

		fmt.Fprintln(out, req)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)
var wrtr = bufio.NewWriterSize(os.Stdout, 10_000_000)
var rdr = bufio.NewScanner(os.Stdin)
func I(a int) int64 { return int64(a) }
func gs() string  { rdr.Scan(); return rdr.Text() }
func gi() int64 { i,e := strconv.ParseInt(gs(),10,64); if e != nil {panic(e)}; return i }
func gi2() (int64,int64) { return gi(),gi() }
func gi3() (int64,int64,int64) { return gi(),gi(),gi() }
func gi4() (int64,int64,int64,int64) { return gi(),gi(),gi(),gi() }
func gis(n int64) []int64 { res := make([]int64,n); for i:=I(0);i<n;i++ { res[i] = gi() }; return res }
func gf() float64 { f,e := strconv.ParseFloat(gs(),64); if e != nil {panic(e)}; return f }
func gbs() []byte { return []byte(gs()) }
func gfs(n int64) []float64  { res := make([]float64,n); for i:=I(0);i<n;i++ { res[i] = gf() }; return res }
func gss(n int64) []string  { res := make([]string,n); for i:=I(0);i<n;i++ { res[i] = gs() }; return res }
func ia(m int64) []int64 { return make([]int64,m) }
func iai(m int64,v int64) []int64 { a := make([]int64,m); for i:=I(0);i<m;i++ { a[i] = v }; return a }
func twodi(n int64,m int64,v int64) [][]int64 {
	r := make([][]int64,n); for i:=I(0);i<n;i++ { x := make([]int64,m); for j:=I(0);j<m;j++ { x[j] = v }; r[i] = x }; return r
}
func fill2(m int64) ([]int64,[]int64) { a,b := ia(m),ia(m); for i:=I(0);i<m;i++ {a[i],b[i] = gi(),gi()}; return a,b }
func fill3(m int64) ([]int64,[]int64,[]int64) { a,b,c := ia(m),ia(m),ia(m); for i:=I(0);i<m;i++ {a[i],b[i],c[i] = gi(),gi(),gi()}; return a,b,c }
func fill4(m int64) ([]int64,[]int64,[]int64,[]int64) { a,b,c,d := ia(m),ia(m),ia(m),ia(m); for i:=I(0);i<m;i++ {a[i],b[i],c[i],d[i] = gi(),gi(),gi(),gi()}; return a,b,c,d }
func abs(a int64) int64 { if a < 0 { return -a }; return a }
func rev(a []int64) { i,j := I(0),I(len(a)-1); for i < j { a[i],a[j] = a[j],a[i]; i++; j-- } }
func max(a,b int64) int64 { if a > b { return a }; return b }
func min(a,b int64) int64 { if a > b { return b }; return a }
func tern(cond bool, a int64, b int64) int64 { if cond { return a }; return b }
func terns(cond bool, a string, b string) string { if cond { return a }; return b }
func maxarr(a []int64) int64 { ans := a[0]; for _,aa := range(a) { if aa > ans { ans = aa } }; return ans }
func minarr(a []int64) int64 { ans := a[0]; for _,aa := range(a) { if aa < ans { ans = aa } }; return ans }
func sumarr(a []int64) int64 { ans := I(0); for _,aa := range(a) { ans += aa }; return ans }
func zeroarr(a []int64) { for i:=I(0); i<I(len(a)); i++ { a[i] = 0 } }
func powmod(a,e,mod int64) int64 { res, m := I(1), a; for e > 0 { if e&1 != 0 { res = res * m % mod }; m = m * m % mod; e >>= 1 }; return res }
func powint(a,e int64) int64 { res, m := I(1), a; for e > 0 { if e&1 != 0 { res = res * m }; m = m * m; e >>= 1 }; return res }
func gcd(a,b int64) int64 { for b != 0 { t:=b; b=a%b; a=t }; return a }
func gcdExtended(a,b int64) (int64,int64,int64) { if a == 0 { return b,0,1 }; gcd,x1,y1 := gcdExtended(b%a,a); return gcd, y1-(b/a)*x1,x1 }
func modinv(a,m int64) (int64,bool) { g,x,_ := gcdExtended(a,m); if g != 1 { return 0,false }; return (x % m + m) % m,true  }
func makefact(n int64,mod int64) ([]int64,[]int64) {
	fact,factinv := make([]int64,n+1),make([]int64,n+1)
	fact[0] = 1; for i:=I(1);i<=n;i++ { fact[i] = fact[i-1] * i % mod }
	factinv[n] = powmod(fact[n],mod-2,mod); for i:=n-1;i>=0;i-- { factinv[i] = factinv[i+1] * (i+1) % mod }
	return fact,factinv
}
const inf int64 = 1000000000000000000
func main() {
	//f1, _ := os.Create("cpu.prof"); pprof.StartCPUProfile(f1); defer pprof.StopCPUProfile()
	defer wrtr.Flush()
	infn := ""; if infn == "" && len(os.Args) > 1 {	infn = os.Args[1] }
	if infn != "" {	f, e := os.Open(infn); if e != nil { panic(e) }; rdr = bufio.NewScanner(f) }
	rdr.Split(bufio.ScanWords); rdr.Buffer(make([]byte,1024),1_000_000_000)
	// PROGRAM STARTS HERE
	N,M,Q := gi(),gi(),gi()
	bd := make([][]byte,N)
	for i:=I(0);i<N;i++ { bd[i] = gbs() }
	X,Y := fill2(Q); for i:=I(0);i<Q;i++ { X[i]--; Y[i]-- }
	nicons := I(0)
	for i:=I(0);i<N;i++ { for j:=I(0);j<M;j++ { if bd[i][j] == '*' { nicons++ }}}
	bc,br := nicons/N,nicons % N // First non-icon
	goodicons := I(0)
	for i:=I(0);i<N;i++ { for j:=I(0);j<M;j++ { if (j < bc || j == bc && i < br) && bd[i][j] == '*' { goodicons++ } } }
	add := func(ii,jj int64) {
		// Add element -- check good -- expand good -- check added element
		bd[ii][jj] = '*'
		nicons++
		if jj < bc || jj == bc && ii < br { goodicons++ }
		if bc < M && bd[br][bc] == '*' { goodicons++ }
		br++; if br==N { bc++; br = 0}
	}
	sub := func(ii,jj int64) {
		bd[ii][jj] = '.'
		nicons--
		if jj < bc || jj == bc && ii < br { goodicons-- }
		br--; if br < 0 { bc--; br = N-1}
		if bd[br][bc] == '*' { goodicons-- }
	}
	for i:=I(0);i<Q;i++ {
		x,y := X[i],Y[i]
		if bd[x][y] == '*' { sub(x,y) } else { add(x,y) }
		fmt.Fprintln(wrtr,nicons-goodicons)
	}
}

package main

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

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

	var n, m, q, c, x, y int
	Fscan(in, &n, &m, &q)
	a := make([][]byte, n)
	for i := range a {
		Fscan(in, &a[i])
		c += bytes.Count(a[i], []byte{'*'})
	}

	ans := c
	for i, r := range a {
		j := c / n
		if i < c%n {
			j++
		}
		ans -= bytes.Count(r[:j], []byte{'*'})
	}

	for ; q > 0; q-- {
		Fscan(in, &x, &y)
		x--
		y--
		p := y*n + x
		if a[x][y] == '*' {
			c--
			if p > c {
				ans--
			}
			if p != c && a[c%n][c/n] == '*' {
				ans++
			}
		} else {
			if p > c {
				ans++
			}
			if p != c && a[c%n][c/n] == '*' {
				ans--
			}
			c++
		}
		Fprintln(out, ans)
		a[x][y] ^= 4
	}
}

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

package main

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

type IO struct {
	in  io.Reader
	out io.Writer
}

func NewIO(in io.Reader, out io.Writer) *IO {
	return &IO{in: bufio.NewReader(in), out: out}
}

func (io *IO) ReadInt() int {
	var res int
	_, _ = fmt.Fscanf(io.in, "%d\n", &res)
	return res
}

func (io *IO) ReadPairInt() (int, int) {
	var a, b int
	_, _ = fmt.Fscanf(io.in, "%d %d\n", &a, &b)
	return a, b
}

func (io *IO) ReadString() string {
	var res string
	_, _ = fmt.Fscanf(io.in, "%s\n", &res)
	return res
}

func (io *IO) ReadIntSlice(n int) sort.IntSlice {
	var t int
	res := make(sort.IntSlice, 0, n)
	for i := 0; i < n; i++ {
		_, _ = fmt.Fscanf(io.in, "%d", &t)
		res = append(res, t)
	}
	_, _ = fmt.Fscanf(io.in, "\n")
	return res
}

func (io *IO) Println(v ...interface{}) {
	_, _ = fmt.Fprintln(io.out, v...)
}

func (io *IO) Print(v ...interface{}) {
	_, _ = fmt.Fprint(io.out, v...)
}

func (io *IO) Printf(format string, v ...interface{}) {
	_, _ = fmt.Fprintf(io.out, format, v...)
}

func (io *IO) PrintIntSlice(s sort.IntSlice) {
	for _, v := range s {
		io.Printf("%d ", v)
	}
}

func (io *IO) PrintlnIntSlice(s sort.IntSlice) {
	io.PrintIntSlice(s)
	io.Printf("\n")
}

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

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

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

func main() {
	io := NewIO(os.Stdin, os.Stdout)
	params := io.ReadIntSlice(3)
	n, m, q := params[0], params[1], params[2]

	field := make([]bool, n*m)

	cnt := 0
	prefixCnt := 0

	for i := 0; i < n; i++ {
		s := io.ReadString()
		for j := 0; j < len(s); j++ {
			if s[j] == '*' {
				field[j*n+i] = true
				cnt++
			}
		}
	}

	for i := 0; i < cnt; i++ {
		if field[i] {
			prefixCnt++
		}
	}

	for i := 0; i < q; i++ {
		params = io.ReadIntSlice(2)
		pos := (params[1]-1)*n + (params[0] - 1)
		//fmt.Println("DEBUG", pos, field, prefixCnt, cnt)
		if field[pos] {
			field[pos] = false
			if pos < cnt {
				prefixCnt--
			}
			if pos != cnt-1 && field[cnt-1] {
				prefixCnt--
			}
			cnt--
		} else {
			field[pos] = true
			cnt++
			if pos < cnt {
				prefixCnt++
			}
			if pos != cnt-1 && field[cnt-1] {
				prefixCnt++
			}
		}
		fmt.Println(cnt - prefixCnt)
	}
}

package main

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

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

func main() {
	defer w.Flush()
	var n, m, q int
	fmt.Fscan(r, &n, &m, &q)
	grid := make([][]byte, n)
	cnt := 0
	for i:=0;i<n;i++ {
		var s string
		fmt.Fscan(r, &s)
		grid[i] = []byte(s)
		for j := range grid[i] {
			if grid[i][j] == '.' {
				grid[i][j] = 0
			} else {
				grid[i][j] = 1
				cnt++
			}
		}
	}
	fit := 0
	x, y := (cnt-1)%n, (cnt-1)/n
	for i:=0;i<n;i++ {
		for j := range grid[i] {
			if grid[i][j] == 1 && (j < y || (j==y&&i<=x)) {
				fit++
			}
		}
	}
	for k:=0;k<q;k++ {
		a, b := 0, 0
		fmt.Fscan(r, &a, &b)
		a, b = a-1, b-1
		if grid[a][b] == 0 {
			grid[a][b] = 1
			cnt++
			x++
			if x == n {
				x = 0
				y++
			}
			if x == a && y == b {
				fit++
			} else {
				if grid[x][y] == 1 {
					fit++
				}
				if b<y || (b==y&&a<=x) {
					fit++
				}
			}
		} else {
			grid[a][b] = 0
			cnt--
			if x == a && y == b {
				fit--
			} else {
				if grid[x][y] == 1 {
					fit--
				}
				if b<y || (b==y&&a<=x) {
					fit--
				}
			}
			x--
			if x == -1 {
				x = n-1
				y--
			}
		}
		fmt.Fprintln(w, cnt - fit)
	}
}

package main

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

func readInt(in *bufio.Scanner) (x int) {
	in.Scan()
	x, _ = strconv.Atoi(string(in.Bytes()))
	return
}

func readPositiveInt(in *bufio.Scanner) (x int) {
	in.Scan()
	for _, b := range in.Bytes() {
		x = x*10 + int(b&15)
	}
	return
}

func readString(in *bufio.Scanner) (s string) {
	in.Scan()
	s = string(in.Bytes())
	return
}

func readBytes(in *bufio.Scanner) (s []byte) {
	in.Scan()
	s = in.Bytes()
	return
}

func main() {
	in := bufio.NewScanner(os.Stdin)
	in.Split(bufio.ScanWords)
	n := readInt(in)
	m := readInt(in)
	q := readInt(in)
	ss := make([][]byte, 0)
	for i := 0; i < n; i++ {
		s := readBytes(in)
		st := make([]byte, m)
		copy(st, s)
		ss = append(ss, st)
	}
	cnt := 0
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if ss[i][j] == '*' {
				cnt++
			}
		}
	}
	val := 0
	for j := 0; j < m; j++ {
		for i := 0; i < n; i++ {
			if j*n+i >= cnt && ss[i][j] == '*' {
				val++
			}
		}
	}

	for i := 0; i < q; i++ {
		a := readPositiveInt(in)
		b := readPositiveInt(in)
		a--
		b--
		j := a + b*n
		if j >= cnt {
			if ss[a][b] == '*' {
				val--
			} else {
				val++
			}
		}
		if ss[a][b] == '*' {
			// cnt-1
			ss[a][b] = '.'
			cnt--
			if ss[cnt%n][cnt/n] == '*' {
				val++
			}
		} else {
			// cnt
			ss[a][b] = '*'
			if ss[cnt%n][cnt/n] == '*' {
				val--
			}
			cnt++
		}
		fmt.Println(val)
	}
}

package main

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

const NN = 20

func readAllInt(in *bufio.Scanner) (x int) {
	in.Scan()
	x, _ = strconv.Atoi(string(in.Bytes()))
	return
}

func readInt(in *bufio.Scanner) (x int) {
	in.Scan()
	for _, b := range in.Bytes() {
		x = x*10 + int(b&15)
	}
	return
}

func readString(in *bufio.Scanner) (s string) {
	in.Scan()
	s = string(in.Bytes())
	return
}

func readBytes(in *bufio.Scanner) (s []byte) {
	in.Scan()
	s = in.Bytes()
	return
}

func main() {
	in := bufio.NewScanner(os.Stdin)
	in.Split(bufio.ScanWords)
	n := readInt(in)
	m := readInt(in)
	q := readInt(in)
	ss := make([][]byte, 0)
	for i := 0; i < n; i++ {
		s := readBytes(in)
		st := make([]byte, m)
		copy(st, s)
		ss = append(ss, st)
	}
	cnt := 0
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if ss[i][j] == '*' {
				cnt++
			}
		}
	}
	val := 0
	for j := 0; j < m; j++ {
		for i := 0; i < n; i++ {
			if j*n+i >= cnt && ss[i][j] == '*' {
				val++
			}
		}
	}

	for i := 0; i < q; i++ {
		a := readInt(in)
		b := readInt(in)
		a--
		b--
		j := a + b*n
		if j >= cnt {
			if ss[a][b] == '*' {
				val--
			} else {
				val++
			}
		}
		if ss[a][b] == '*' {
			// cnt-1
			ss[a][b] = '.'
			cnt--
			if ss[cnt%n][cnt/n] == '*' {
				val++
			}
		} else {
			// cnt
			ss[a][b] = '*'
			if ss[cnt%n][cnt/n] == '*' {
				val--
			}
			cnt++
		}
		fmt.Println(val)
	}
}

package main

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

const NN = 20

func readInt(in *bufio.Scanner) (x int) {
	in.Scan()
	x, _ = strconv.Atoi(string(in.Bytes()))
	// if err != nil {
	// 	fmt.Println(err)
	// }
	return
}

// func readPositiveInt(in *bufio.Scanner) (x int) {
// 	in.Scan()
// 	for _, b := range in.Bytes() {
// 		x = x*10 + int(b&15)
// 	}
// 	return
// }

func readString(in *bufio.Scanner) (s string) {
	in.Scan()
	s = string(in.Bytes())
	return
}

func readBytes(in *bufio.Scanner) (s []byte) {
	in.Scan()
	s = in.Bytes()
	return
}

func main() {
	in := bufio.NewScanner(os.Stdin)
	in.Split(bufio.ScanWords)
	n := readInt(in)
	m := readInt(in)
	q := readInt(in)
	ss := make([][]byte, 0)
	for i := 0; i < n; i++ {
		s := readBytes(in)
		ss = append(ss, []byte(string(s)))
	}
	// fmt.Println(ss)
	cnt := 0
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if ss[i][j] == '*' {
				cnt++
			}
		}
	}
	val := 0
	for j := 0; j < m; j++ {
		for i := 0; i < n; i++ {
			if j*n+i >= cnt && ss[i][j] == '*' {
				val++
			}
		}
	}

	for i := 0; i < q; i++ {
		a := readInt(in)
		b := readInt(in)
		a--
		b--
		// if n == 999 {
		// 	if i > NN {
		// 		fmt.Println(a, b, cnt, val, cnt%n, cnt/n)
		// 	}
		// }
		j := a + b*n
		if j >= cnt {
			if ss[a][b] == '*' {
				val--
			} else {
				val++
			}
		}
		if ss[a][b] == '*' {
			// cnt-1
			ss[a][b] = '.'
			cnt--
			if ss[cnt%n][cnt/n] == '*' {
				val++
			}
		} else {
			// cnt
			if ss[cnt%n][cnt/n] == '*' {
				val--
			}
			ss[a][b] = '*'
			cnt++
		}
		// if n == 999 {
		// 	if i > NN {
		// 		fmt.Println(i, val)
		// 	}
		// } else {
		fmt.Println(val)
		// }
	}
}

package main

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

const NN = 20

func readInt(in *bufio.Scanner) (x int) {
	in.Scan()
	x, err := strconv.Atoi(string(in.Bytes()))
	if err != nil {
		fmt.Println(err)
	}
	return
}

// func readPositiveInt(in *bufio.Scanner) (x int) {
// 	in.Scan()
// 	for _, b := range in.Bytes() {
// 		x = x*10 + int(b&15)
// 	}
// 	return
// }

func readString(in *bufio.Scanner) (s string) {
	in.Scan()
	s = string(in.Bytes())
	return
}

func readBytes(in *bufio.Scanner) (s []byte) {
	in.Scan()
	s = in.Bytes()
	return
}

func main() {
	in := bufio.NewScanner(os.Stdin)
	in.Split(bufio.ScanWords)
	n := readInt(in)
	m := readInt(in)
	q := readInt(in)
	ss := make([][]byte, 0)
	for i := 0; i < n; i++ {
		s := readBytes(in)
		ss = append(ss, []byte(string(s)))
	}
	// fmt.Println(ss)
	cnt := 0
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if ss[i][j] == '*' {
				cnt++
			}
		}
	}
	val := 0
	for j := 0; j < m; j++ {
		for i := 0; i < n; i++ {
			if j*n+i >= cnt && ss[i][j] == '*' {
				val++
			}
		}
	}

	for i := 0; i < q; i++ {
		a := readInt(in)
		b := readInt(in)
		a--
		b--
		if n == 999 {
			if i > NN {
				fmt.Println(a, b, cnt, val, cnt%n, cnt/n)
			}
		}
		j := a + b*n
		if j >= cnt {
			if ss[a][b] == '*' {
				val--
			} else {
				val++
			}
		}
		if ss[a][b] == '*' {
			// cnt-1
			ss[a][b] = '.'
			cnt--
			if ss[cnt%n][cnt/n] == '*' {
				val++
			}
		} else {
			// cnt
			if ss[cnt%n][cnt/n] == '*' {
				val--
			}
			ss[a][b] = '*'
			cnt++
		}
		if n == 999 {
			if i > NN {
				fmt.Println(i, val)
			}
		} else {
			fmt.Println(val)
		}
	}
}

package main

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

var z [1000003]bool

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

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

	var c int
	for i := 0; i < n; i++ {
		var s string
		fmt.Fscan(in, &s)

		for j, ch := range s {
			if ch == '*' {
				z[j*n+i] = true
				c++
			}
		}
	}

	var o int
	for i := 0; i < c; i++ {
		if z[i] {
			o++
		}
	}

	for i := 0; i < q; i++ {
		var x, y int
		fmt.Fscan(in, &x, &y)

		x--
		y--
		pos := y*n + x
		if z[pos] {
			// was valid, now off.
			if pos < c {
				o--
			}

			c--
			if pos != c && z[c] {
				o--
			}

		} else {
			if pos < c {
				o++
			}
			if z[c] {
				o++
			}
			c++
		}
		z[pos] = !z[pos]

		fmt.Fprintln(out, c-o)
	}
}

package main

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

var z [1000003]bool

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

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

	var c int
	for i := 0; i < n; i++ {
		var s string
		fmt.Fscan(in, &s)

		for j, ch := range s {
			if ch == '*' {
				z[j*n+i] = true
				c++
			}
		}
	}

	var o int
	for i := 0; i < c; i++ {
		if z[i] {
			o++
		}
	}

	for i := 0; i < q; i++ {
		var x, y int
		fmt.Fscan(in, &x, &y)

		x--
		y--
		pos := y*n + x
		if z[pos] {
			// was valid, now off.
			if pos < c {
				o--
			}

			c--
			if pos != c && z[c] {
				o--
			}

		} else {
			if pos < c {
				o++
			}
			if pos != c && z[c] {
				o++
			}
			c++
		}
		z[pos] = !z[pos]

		fmt.Fprintln(out, c-o)
	}
}

package main

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

var z [1000003]bool

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

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

	var c int
	for i := 0; i < n; i++ {
		var s string
		fmt.Fscan(in, &s)

		for j, ch := range s {
			if ch == '*' {
				z[j*n+i] = true
				c++
			}
		}
	}

	var o int
	for i := 0; i < c; i++ {
		if z[i] {
			o++
		}
	}

	for i := 0; i < q; i++ {
		var x, y int
		fmt.Fscan(in, &x, &y)

		x--
		y--
		pos := y*n + x
		if z[pos] {
			// was valid, now off.
			if pos < c {
				o--
			}

			c--
			if pos != c && z[c] {
				o--
			}

		} else {
			if pos < c {
				o++
			}
			if pos+1 != c && z[c] {
				o++
			}
			c++
		}
		z[pos] = !z[pos]

		fmt.Fprintln(out, c-o)
	}
}


package main

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

type node struct {
	l, r   int
	val    int
	needOR bool
}
type segmentTree []node

func (t segmentTree) _pushUp(o int) {
	lo, ro := t[o<<1], t[o<<1|1]
	if lo.needOR {
		t[o].val = lo.val | ro.val
	} else {
		t[o].val = lo.val ^ ro.val
	}
}

func (t segmentTree) _build(arr []int, o, l, r int) {
	t[o].l, t[o].r = l, r
	if l == r {
		t[o].val = arr[l]
		t[o].needOR = true
		return
	}
	mid := (l + r) >> 1
	t._build(arr, o<<1, l, mid)
	t._build(arr, o<<1|1, mid+1, r)
	t[o].needOR = !t[o<<1].needOR
	t._pushUp(o)
}

func (t segmentTree) _update(o, idx int, val int) {
	if t[o].l == t[o].r {
		t[o].val = val
		return
	}
	if idx <= (t[o].l+t[o].r)>>1 {
		t._update(o<<1, idx, val)
	} else {
		t._update(o<<1|1, idx, val)
	}
	t._pushUp(o)
}

func (t segmentTree) init(arr []int)          { t._build(arr, 1, 1, len(arr)-1) }
func (t segmentTree) update(idx int, val int) { t._update(1, idx, val) }

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

	var n, m int
	Fscan(in, &n, &m)
	n = 1 << uint(n)
	arr := make([]int, n+1)
	for i := 1; i <= n; i++ {
		Fscan(in, &arr[i])
	}

	t := make(segmentTree, 2*n)
	t.init(arr)
	for ; m > 0; m-- {
		var idx, val int
		Fscan(in, &idx, &val)
		t.update(idx, val)
		Fprintln(out, t[1].val)
	}
}

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

package main

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

var (
	sc   *bufio.Scanner = bufio.NewScanner(os.Stdin)
	n, m int
	tr   []int
)

func nextString() string {
	sc.Scan()
	return sc.Text()
}

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

func calcNode(i, lev int) {
	if lev%2 == 0 {
		tr[i] = tr[2*i] | tr[2*i+1]
	} else {
		tr[i] = tr[2*i] ^ tr[2*i+1]
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	n, m = 1<<uint(nextInt()), nextInt()
	tr = make([]int, 2*n)
	for i := 0; i < n; i++ {
		tr[n+i] = nextInt()
	}
	for lev, levSz, i := 0, n/2, n-1; levSz >= 1; levSz /= 2 {
		for j := 0; j < levSz; j++ {
			calcNode(i-j, lev)
		}
		i -= levSz
		lev++
	}
	for i := 0; i < m; i++ {
		ix, v := nextInt()+n-1, nextInt()
		tr[ix] = v
		for j, lev := ix/2, 0; j >= 1; j /= 2 {
			calcNode(j, lev)
			lev++
		}
		fmt.Println(tr[1])
	}
}

package main

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

type node struct {
	val    int
	needOR bool
}
type segmentTree []node

func (t segmentTree) _pushUp(o int) {
	lo, ro := t[o<<1], t[o<<1|1]
	if lo.needOR {
		t[o].val = lo.val | ro.val
	} else {
		t[o].val = lo.val ^ ro.val
	}
}

func (t segmentTree) _build(arr []int, o, l, r int) {
	if l == r {
		t[o].val = arr[l]
		t[o].needOR = true
		return
	}
	mid := (l + r) >> 1
	t._build(arr, o<<1, l, mid)
	t._build(arr, o<<1|1, mid+1, r)
	t[o].needOR = !t[o<<1].needOR
	t._pushUp(o)
}

func (t segmentTree) _update(o, l, r int, idx int, val int) {
	if l == r {
		t[o].val = val
		return
	}
	mid := (l + r) >> 1
	if idx <= mid {
		t._update(o<<1, l, mid, idx, val)
	} else {
		t._update(o<<1|1, mid+1, r, idx, val)
	}
	t._pushUp(o)
}

func (t segmentTree) init(arr []int)                 { t._build(arr, 1, 1, len(arr)-1) }
func (t segmentTree) update(n int, idx int, val int) { t._update(1, 1, n, idx, val) }

func Sol339D(reader io.Reader, writer io.Writer) {
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n, m int
	Fscan(in, &n, &m)
	n = 1 << uint(n)
	arr := make([]int, n+1)
	for i := 1; i <= n; i++ {
		Fscan(in, &arr[i])
	}

	t := make(segmentTree, 2*n)
	t.init(arr)
	for ; m > 0; m-- {
		var idx, val int
		Fscan(in, &idx, &val)
		t.update(n, idx, val)
		Fprintln(out, t[1].val)
	}
}

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

// 339D
package main

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

var (
	i, j, m, n uint64
	f          [1 << 19]uint64
)

func modify(x, v, p uint64) {
	if p == 1 {
		f[x] = v
	} else {
		if p%2 != 0 {
			f[x] = f[x<<1|1] ^ f[x<<1]
		} else {
			f[x] = f[x<<1|1] | f[x<<1]
		}
	}
	if x > 1 {
		modify(x>>1, v, p+1)
	}
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	fmt.Scan(&n, &m)
	n = uint64(1 << n)
	for i = 0; i < n; i++ {
		var c uint64
		fmt.Fscan(in, &c)
		modify(n+i, c, 1)
	}
	for ; m > 0; m-- {
		var pos, b uint64
		fmt.Fscan(in,&pos, &b)
		modify(n+pos-1, b, 1)
		fmt.Fprintln(out, f[1])
	}
	out.Flush()
	return
}

// 339D
package main

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

var (
	i, j, m, n uint64
	f          [1 << 19]uint64
)

func modify(x, v, p uint64) {
	if p == 1 {
		f[x] = v
	} else {
		if p%2 != 0 {
			f[x] = f[x<<1|1] ^ f[x<<1]
		} else {
			f[x] = f[x<<1|1] | f[x<<1]
		}
	}
	if x > 1 {
		modify(x>>1, v, p+1)
	}
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	fmt.Scan(&n, &m)
	n = uint64(1 << n)
	for i = 0; i < n; i++ {
		var c uint64
		fmt.Fscan(in, &c)
		modify(n+i, c, 1)
	}
	for ; m > 0; m-- {
		var pos, b uint64
		fmt.Scan(&pos, &b)
		modify(n+pos-1, b, 1)
		fmt.Fprintln(out, f[1])
	}
	out.Flush()
	return
}

// 339D
package main

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

var (
	i, j, m, n uint64
	f          [1 << 19]uint64
)

func modify(x, v, p uint64) {
	if p == 1 {
		f[x] = v
	} else {
		if p%2 != 0 {
			f[x] = f[x<<1|1] ^ f[x<<1]
		} else {
			f[x] = f[x<<1|1] | f[x<<1]
		}
	}
	if x > 1 {
		modify(x>>1, v, p+1)
	}
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	fmt.Scan(&n, &m)
	n = uint64(1 << n)
	for i = 0; i < n; i++ {
		var c uint64
		fmt.Fscan(in, &c)
		modify(n+i, c, 1)
	}
	for ; m > 0; m-- {
		var pos, b uint64
		fmt.Scan(&pos, &b)
		modify(n+pos-1, b, 1)
		fmt.Fprintln(out, f[1])
	}
	return
}


package main

import "fmt"

func main() {
	var n int
	_, err := fmt.Scan(&n)
	if err != nil {
		return
	}
	cardsNumber := make([]int, n)
	max := 0

	for i := 0; i < n; i++ {
		var number int
		_, err := fmt.Scan(&number)
		if err != nil {
			return
		}
		cardsNumber[i] = number
		if max < number {
			max = number
		}
	}

	pyramids := make([]int, 0, 10)
	h := 2
	cardsCount := 2
	for cardsCount <= max {
		pyramids = append(pyramids, cardsCount)
		cardsCount += h * 2 + h - 1
		h++
	}

	for _, k := range cardsNumber {
		pyramidsNumber := 0
		for k >= pyramids[0] {
			i := 0
			for i != len(pyramids) && k >= pyramids[i] {
				i++
			}
			if i > 0 {
				i -= 1
			}
			k -= pyramids[i]
			pyramidsNumber++
		}
		fmt.Println(pyramidsNumber)
	}

}

// https://codeforces.com/contest/1345/problem/B
package main

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

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

func main() {
	var (
		T int
	)

	f := make([]int, 25821 + 1)
	f[1] = 2
	for i := 2; i < len(f); i++ {
		f[i] =  f[i - 1] + (i - 1) + 2 * i
	}

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input = bufio.NewReader(os.Stdin)
	fmt.Fscanf(input, "%d", &T)

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

		ans := 0
		for n > 1 {
			idx := sort.Search(len(f), func(x int) bool { return f[x] > n})
			n -= f[idx - 1]
			ans++
		}

		fmt.Println(ans)
	}
}

/*
URL:
https://codeforces.com/contest/1345/problem/B
*/

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

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

func solve(n int) {
	resi := n
	ans := 0

	for resi >= 2 {
		i := 1
		for {
			num := sub(i)
			if num <= resi {
				i++
			} else {
				break
			}
		}

		resi -= sub(i - 1)
		ans++
	}

	fmt.Println(ans)
}

func sub(i int) int {
	return (3*i*i + i) / 2
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

	return string(res)
}

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

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

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

	return string(res)
}

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

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

package main

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

func calc(m int64) int64 {
	return m * (3*m + 1) / 2
}

func solve() {
	n := readInt64()

	var l, r int64
	ans := 0
	for {
		if n < 2 {
			break
		}

		l, r = 1, n
		for r > l {
			m := (l+r)/2 + 1
			tot := calc(m)
			if tot > n {
				r = m - 1
			} else {
				l = m
			}
		}

		// log.Println(l, r, n)

		ans++
		n -= calc(l)
	}

	fmt.Println(ans)
}

func main() {
	k := readInt()
	for i := 0; i < k; i++ {
		solve()
	}
}

// sort ------------------------------------------------------------

type xxx struct {
	x int
}

type sortArray []xxx

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

// -----------------------------------------------------------------

var (
	readString func() string
	readBytes  func() []byte
)

func init() {
	log.SetFlags(log.Lshortfile)
	readString, readBytes = newReadString(os.Stdin)
}

func newReadString(ior io.Reader) (func() string, func() []byte) {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), math.MaxInt32)
	r.Split(bufio.ScanWords)

	f1 := func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
	f2 := func() []byte {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Bytes()
	}
	return f1, f2
}

func readInt() int {
	return int(readInt64())
}

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

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

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

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

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

func sum(a []int) int {
	var ret int
	for i := range a {
		ret += a[i]
	}
	return ret
}

func sumFloat64(a []float64) float64 {
	var ret float64
	for i := range a {
		ret += a[i]
	}
	return ret
}

func gcd(m, n int) int {
	for m%n != 0 {
		m, n = n, m%n
	}
	return n
}

func lcm(m, n int) int {
	return m / gcd(m, n) * n
}

package main

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

func main() {
	for t := ScanInt(); t > 0; t-- {
		n := ScanInt()
		P(solve(n))
	}

	close(O)
	oWg.Wait()
}

func solve(n int) int {
	arr := make([]int64, 37838)
	arr[1] = 2
	i := 2
	for arr[i-1]+int64(3*i-1) < math.MaxInt32 && i < 37838 {
		arr[i] = arr[i-1] + int64(3*i-1)
		i++
	}

	c := 0
	sum := int64(0)
	for n > 1 {
		c++
		floor := bsearch(int64(n), arr)
		sum += floor
		n -= int(floor)
	}
	return c
}

func bsearch(x int64, arr []int64) int64 {
	l := 1
	r := len(arr)
	floor := int64(-1)
	for l <= r {
		mid := l + (r-l)/2
		if arr[mid] == x {
			return arr[mid]
		} else if arr[mid] > x {
			r = mid - 1
		} else {
			floor = arr[mid]
			l = mid + 1
		}
	}
	return floor
}

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

var O chan string
var in *bufio.Scanner
var oWg *sync.WaitGroup
var P func(a ...interface{}) (n int, err error)

func init() {
	P = fmt.Println

	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(1e+9))
	in.Split(bufio.ScanWords)

	O = make(chan string, 16)
	oWg = &sync.WaitGroup{}
	oWg.Add(1)

	writer := bufio.NewWriter(os.Stdout)
	go func() {
		defer oWg.Done()
		defer writer.Flush()

		for line := range O {
			writer.WriteString(line + "\n")
		}
	}()
}

func ScanWord() string { in.Scan(); return in.Text() }
func ScanInt() int     { i, _ := strconv.Atoi(ScanWord()); return i }
func ScanInt64() int64 { i, _ := strconv.ParseInt(ScanWord(), 10, 64); return i }
func ScanStringSlice(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ScanWord()
	}
	return s
}
func ScanIntSlice(n int) []int {
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = ScanInt()
	}
	return arr
}
func ScanInt64Slice(n int) []int64 {
	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		arr[i] = ScanInt64()
	}
	return arr
}

/*********************** 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"
	"math"
	"os"
	"sort"
	"strconv"
	"strings"
	"sync"
)

func main() {
	arr := make([]int64, 37838)
	arr[1] = 2
	i := 2
	for arr[i-1]+int64(3*i-1) < math.MaxInt32 && i < 37838 {
		arr[i] = arr[i-1] + int64(3*i-1)
		i++
	}

	for t := ScanInt(); t > 0; t-- {
		n := ScanInt()
		P(solve(n, arr))
	}

	close(O)
	oWg.Wait()
}

func solve(n int, arr []int64) int {

	c := 0
	sum := int64(0)
	for n > 1 {
		c++
		floor := bsearch(int64(n), arr)
		sum += floor
		n -= int(floor)
	}
	return c
}

func bsearch(x int64, arr []int64) int64 {
	l := 1
	r := len(arr)
	floor := int64(-1)
	for l <= r {
		mid := l + (r-l)/2
		if arr[mid] == x {
			return arr[mid]
		} else if arr[mid] > x {
			r = mid - 1
		} else {
			floor = arr[mid]
			l = mid + 1
		}
	}
	return floor
}

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

var O chan string
var in *bufio.Scanner
var oWg *sync.WaitGroup
var P func(a ...interface{}) (n int, err error)

func init() {
	P = fmt.Println

	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(1e+9))
	in.Split(bufio.ScanWords)

	O = make(chan string, 16)
	oWg = &sync.WaitGroup{}
	oWg.Add(1)

	writer := bufio.NewWriter(os.Stdout)
	go func() {
		defer oWg.Done()
		defer writer.Flush()

		for line := range O {
			writer.WriteString(line + "\n")
		}
	}()
}

func ScanWord() string { in.Scan(); return in.Text() }
func ScanInt() int     { i, _ := strconv.Atoi(ScanWord()); return i }
func ScanInt64() int64 { i, _ := strconv.ParseInt(ScanWord(), 10, 64); return i }
func ScanStringSlice(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ScanWord()
	}
	return s
}
func ScanIntSlice(n int) []int {
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = ScanInt()
	}
	return arr
}
func ScanInt64Slice(n int) []int64 {
	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		arr[i] = ScanInt64()
	}
	return arr
}

/*********************** 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"
import "math"

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

	var n []int = make([]int, t)

	max := 0
	for i:=0; i< t ; i++ {
		fmt.Scan(&n[i])
		if n[i] > max {
			max = n[i]
		}
	}

	for i:=0; i< t ; i++ {
		fmt.Println(getMinPyra(int64(n[i])))
	}
}

func getMinPyra(n int64) int64{
	if n < 2 {
		return 0
	} else if n < 4 {
		return 1
	}

	var count int64

	for n > 1 {
		size :=	(int64)(math.Floor((-1+math.Sqrt((float64)(1+24*n)))/6.0))
		count++;
		n -= size*(3*size+1)/2;
	}

	return count
}

package main

import "fmt"

func cards_from_height(h int64) int64 {
	a1 := h * (h + 1)
	a2 := (h - 1) * h / 2
	return a1 + a2
}

func calc(cards_num int64) int64 {
	if cards_num <= 1 {
		return 0
	}

	var h = int64(1)
	var temp = cards_from_height(h)
	for temp < cards_num {
		h += 1
		temp += 2*h + (h - 1)
	}

	if temp == cards_num {
		return 1
	}
	temp -= 2*h + (h - 1)
	return 1 + calc(cards_num-temp)
}

func main() {
	var n int64
	var result = []int64{}
	fmt.Scanf("%d\n", &n)
	for j := int64(0); j < n; j++ {
		var n1 int64
		fmt.Scanf("%d\n", &n1)
		rr := calc(n1)
		result = append(result, rr)
	}

	for _, rr := range result {
		fmt.Println(rr)
	}
}

package main

import (
	_ "bufio"
	"fmt"
)

func main() {
	p1345B()
}

var lookup [100000]int
var rightMax int

func p1345B() {
	var (
		test int
		num  int
	)

	// cards array returns max right side for given constraint
	rightMax = build1345B(100000)

	fmt.Scanf("%d\n", &test)
	for i := 0; i < test; i++ {
		fmt.Scanf("%d\n", &num)
		s1345B(num)
	}

	// fmt.Println(rightMax)
}

func s1345B(n int) {

	fmt.Println(cal1345B(n))
}

func build1345B(len int) int {
	//fmt.Println("build1345B", len)
	lookup[0] = 0
	// fmt.Println(lookup[0])
	for i := 1; i < len; i++ {
		lookup[i] = lookup[i-1] + 3*i - 1
		//fmt.Println(lookup[i])
		if lookup[i] > 1000000000 {
			return i
		}
	}
	return 0
}

/*
// finds number in maxrectangles that can be formed
	1. find height of max rectangle find function

*/
func cal1345B(n int) int {
	// base condition ensures n >= 2 always
	if n < lookup[1] {
		return 0
	} else {
		maxHeight := findMaxHeight(n, 0, rightMax)
		return n/lookup[maxHeight] + cal1345B(n%lookup[maxHeight])
	}

	//	return 0
}

// index of number maximum number less than or equal to n
func findMaxHeight(n, l, r int) int {
	// try-2 assumes n lies between [l,r]
	// working
	// case where mid will become left each time
	if r-l <= 1 {
		switch r - l {
		case 0:
			return l
		case 1:
			if lookup[r] <= n {
				return r
			} else {
				return l
			}
		}
	}
	mid := (l + r) / 2
	if lookup[mid] > n {
		return findMaxHeight(n, l, mid-1)
	} else if lookup[mid] == n {
		return mid
	} else {
		return findMaxHeight(n, mid, r)
	}
}

/*
func findMaxHeight(n, l, r int) int {

	// try-1 binary search
	if l == r {
		return r
	}
	// case when mid might get stuck in infinite loop
	if r-l == 1 {
		if lookup[r] <= n {
			return r
		} else {
			return l
		}
	}
	mid := (r + l) / 2

	// left half
	if lookup[mid] >= n {
		return findMaxHeight(n, l, mid)
	} else {
		return findMaxHeight(n, mid, r)
	}
}
*/

package main

import (
	_ "bufio"
	"fmt"
)

func main() {
	p1345B()
}

var lookup [100000]int
var rightMax int

func p1345B() {
	var (
		test int
		num  int
	)

	// cards array returns max right side for given constraint
	rightMax = build1345B(100000)

	fmt.Scanf("%d\n", &test)
	for i := 0; i < test; i++ {
		fmt.Scanf("%d\n", &num)
		s1345B(num)
	}

	// fmt.Println(rightMax)
}

func s1345B(n int) {

	fmt.Println(cal1345B(n))
}

func build1345B(len int) int {
	//fmt.Println("build1345B", len)
	lookup[0] = 0
	// fmt.Println(lookup[0])
	for i := 1; i < len; i++ {
		lookup[i] = lookup[i-1] + 3*i - 1
		//fmt.Println(lookup[i])
		if lookup[i] > 1000000000 {
			return i
		}
	}
	return 0
}

/*
// finds number in maxrectangles that can be formed
	1. find height of max rectangle find function

*/
func cal1345B(n int) int {
	// base condition ensures n >= 2 always
	if n < lookup[1] {
		return 0
	} else {
		maxHeight := findMaxHeight(n, 0, rightMax)
		return n/lookup[maxHeight] + cal1345B(n%lookup[maxHeight])
	}

	//	return 0
}

/*
// index of number maximum number less than or equal to n
func findMaxHeight(n, l, r int) int {
	// try-2 assumes n lies between [l,r]
	// working
	// case where mid will become left each time
	if r-l <= 1 {
		switch r - l {
		case 0:
			return l
		case 1:
			if lookup[r] <= n {
				return r
			} else {
				return l
			}
		}
	}
	mid := (l + r) / 2
	if lookup[mid] > n {
		return findMaxHeight(n, l, mid-1)
	} else if lookup[mid] == n {
		return mid
	} else {
		return findMaxHeight(n, mid, r)
	}
}
*/

func findMaxHeight(n, l, r int) int {

	// try-1 binary search
	if l == r {
		return r
	}
	// case when mid might get stuck in infinite loop
	if r-l == 1 {
		if lookup[r] <= n {
			return r
		} else {
			return l
		}
	}
	mid := (r + l) / 2

	// left half
	if lookup[mid] >= n {
		return findMaxHeight(n, l, mid)
	} else {
		return findMaxHeight(n, mid, r)
	}
}

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 {
	NextInt64() int64
	NextUInt64() uint64
	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) 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) NextUInt64() uint64 {
	impl.Scan()
	bb := impl.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

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

func gcd(x, y int64) int64 {
	for y != 0 {
		x, y = y, x%y
	}
	return x
}

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()
	dp := []int64{2}
	for dp[len(dp)-1] <= 1e9 {
		currHeight := int64(len(dp))
		nxtTents := int64(2) * (currHeight + 1)
		nxtFloors := currHeight
		dp = append(dp, dp[len(dp)-1]+nxtTents+nxtFloors)
	}
	//writer.Printf("%v\n", dp)
	t := reader.NextInt64()
	for ; t > 0; t-- {
		res := int64(0)
		n := reader.NextInt64()
		i := len(dp) - 1
		for i >= 0 {
			if dp[i] <= n {
				n -= dp[i]
				//writer.Printf("%d\n", dp[i])
				res++
				continue
			}
			if n == 0 {
				break
			}
			i--
		}
		writer.Printf("%d\n", res)
	}
}

package main

import (
    "fmt"
)

func index(arr []int, val int) int {
	for idx, elem := range arr {
		if elem == val {
			return idx
		} else if elem > val {
			return idx - 1
		}
	}
	return -1
}

func main() {
    var t int
	fmt.Scan(&t)
	var arr = make([]int, 1)
	arr[0] = 0
	for i := 0; i < t; i++ {
		var n int
		fmt.Scan(&n)

		count := 0
		for ; n > 1; count++ {
			for {
				last := arr[len(arr) - 1]
				if last >= n {
					break
				}
				newelem := last + 2 * len(arr) + len(arr) - 1
				arr = append(arr, newelem)
			}
			idx := index(arr, n)
			if idx == -1 {
				count--;
			} else {
				n -= arr[idx]
			}
		}
		fmt.Println(count)
	}
}

package main

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

var reader io.Reader
var scanner *bufio.Scanner

func Init(io io.Reader, max_token_size int) {
	reader = bufio.NewReader(io)
	scanner = bufio.NewScanner(reader)
	// max token size
	scanner.Split(bufio.ScanWords)
	scanner.Buffer([]byte{}, max_token_size)
}

func stderr(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
}

func die(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
	os.Exit(1)
}

func NextInt() int {
	bytes := NextBytes()
	i, err := Atoi(bytes)
	if err != nil {
		die(err)
	}
	return i
}
func NextInt64() int64 {
	bytes := NextBytes()
	i, err := Atoi64(bytes)
	if err != nil {
		die(err)
	}
	return i
}

func NextBytes() []byte {
	if !scanner.Scan() {
		die(scanner.Err())
	}
	bytes := scanner.Bytes()
	copied := make([]byte, len(bytes))
	copy(copied, bytes)
	return copied
}

func Atoi(b []byte) (int, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	n := 0
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func Atoi64(b []byte) (int64, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	var n int64
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int64(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func ExtendByteArray(a []byte, size int) []byte {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]byte, size-len(a))...)
	}
}

func ExtendIntArray(a []int, size int) []int {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]int, size-len(a))...)
	}
}

func reverseInts(a []int) {
	for l, r := 0, len(a)-1; l < r; l, r = l+1, r-1 {
		a[l], a[r] = a[r], a[l]
	}
}

func unique(a []int) int {
	newLen := 0
	n := len(a)
	for i := 0; i < n; i++ {
		if newLen == 0 || a[i] != a[newLen-1] {
			a[newLen] = a[i]
			newLen++
		}
	}
	return newLen
}

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

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

func main() {
	inf := os.Stdin
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	// inf, err := os.Open("a.in")
	// if err != nil {
	// 	die(err)
	// }
	// out, err := os.Create("a.out")
	// if err != nil {
	// 	die(err)
	// }
	// defer out.Close()
	Init(inf, 1000010)

	T := NextInt()

	f := make([]int, 26001)
	for i := 1; i <= 26000; i++ {
		f[i] = (i*i*3 + i) / 2
	}

	for t := 0; t < T; t++ {
		n := NextInt()
		var res int
		for n > 0 {
			ind := sort.SearchInts(f, n)
			if f[ind] > n {
				ind--
			}
			if ind == 0 {
				break
			}
			res++
			n -= f[ind]
		}
		fmt.Fprintln(out, res)
	}
}

// https://codeforces.com/contest/1345/problem/B
package main

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

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

func main() {
	var (
		T int
	)

	f := make([]int, 25821)
	f[0] = 2
	for i := 1; i < len(f); i++ {
		f[i] =  f[i-1] + i + (i + 1)*2
	}

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input = bufio.NewReader(os.Stdin)
	fmt.Fscanf(input, "%d", &T)

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

		ans := 0
		for k := len(f) - 1; k >= 0; k-- {
			if f[k] <= n {
				n -= f[k]
				ans++
				if n < 2 {
					break
				}
			}
		}

		fmt.Println(ans)
	}
}

// https://codeforces.com/contest/1345/problem/B
package main

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

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

func main() {
	var (
		T int
	)

	f := make([]int, 25821)
	f[0] = 2
	for i := 1; i < len(f); i++ {
		f[i] =  f[i-1]+i-1+i*2
	}

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input = bufio.NewReader(os.Stdin)
	fmt.Fscanf(input, "%d", &T)

	for i := 0; i < T; i++ {
		var n int
		fmt.Scan(&n)

		ans := 0
		for k := len(f) - 1; k >= 0; k-- {
			if f[k] < n {
				n -= f[k]
				ans++
				if n < 2 {
					break
				}
			}
		}

		fmt.Println(ans)
	}
}

// https://codeforces.com/contest/1345/problem/B
package main

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

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

func main() {
	var (
		T int
	)

	f := make([]int, 25821)
	f[0] = 2
	for i := 1; i < len(f); i++ {
		f[i] =  f[i-1]+i-1+i*2
	}

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input = bufio.NewReader(os.Stdin)
	fmt.Fscanf(input, "%d", &T)

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

		ans := 0
		for k := len(f) - 1; k >= 0; k-- {
			if f[k] < n {
				n -= f[k]
				ans++
				if n < 2 {
					break
				}
			}
		}

		fmt.Println(ans)
	}
}

package main

import "fmt"

func main() {
	var n int
	_, err := fmt.Scan(&n)
	if err != nil {
		return
	}
	cardsNumber := make([]int, n)
	max := 0

	for i := 0; i < n; i++ {
		var number int
		_, err := fmt.Scan(&number)
		if err != nil {
			return
		}
		cardsNumber[i] = number
		if max < number {
			max = number
		}
	}

	pyramids := make([]int, 0, 10)
	h := 2
	cardsCount := 2
	for cardsCount < max {
		pyramids = append(pyramids, cardsCount)
		cardsCount += h * 2 + h - 1
		h++
	}

	for _, k := range cardsNumber {
		pyramidsNumber := 0
		for k >= pyramids[0] {
			i := 0
			for i != len(pyramids) && k >= pyramids[i] {
				i++
			}
			if i > 0 {
				i -= 1
			}
			k -= pyramids[i]
			pyramidsNumber++
		}
		fmt.Println(pyramidsNumber)
	}

}

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 {
	NextInt64() int64
	NextUInt64() uint64
	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) 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) NextUInt64() uint64 {
	impl.Scan()
	bb := impl.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

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

func gcd(x, y int64) int64 {
	for y != 0 {
		x, y = y, x%y
	}
	return x
}

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()
	dp := []int64{2}
	for dp[len(dp)-1] <= 1e9 {
		currHeight := int64(len(dp))
		nxtTents := int64(2) * (currHeight+1)
		nxtFloors := currHeight
		dp = append(dp, dp[len(dp)-1] + nxtTents + nxtFloors)
	}
	t := reader.NextInt64()
	for ; t > 0; t-- {
		res := int64(0)
		n := reader.NextInt64()
		for i := len(dp)-1; i > -1; i-- {
			if dp[i] <= n {
				n -= dp[i]
				res++
			}
			if n == 0 {
				break
			}
		}
		writer.Printf("%d\n", res)
	}
}