This is the hard version of the problem. The only difference between the versions is the constraints on $$$n$$$, $$$k$$$, $$$a_i$$$, and the sum of $$$n$$$ over all test cases. You can make hacks only if both versions of the problem are solved.Note the unusual memory limit.You are given an array of integers $$$a_1, a_2, \ldots, a_n$$$ of length $$$n$$$, and an integer $$$k$$$.The cost of an array of integers $$$p_1, p_2, \ldots, p_n$$$ of length $$$n$$$ is $$$$$$\max\limits_{1 \le i \le n}\left(\left \lfloor \frac{a_i}{p_i} \right \rfloor \right) - \min\limits_{1 \le i \le n}\left(\left \lfloor \frac{a_i}{p_i} \right \rfloor \right).$$$$$$Here, $$$\lfloor \frac{x}{y} \rfloor$$$ denotes the integer part of the division of $$$x$$$ by $$$y$$$. Find the minimum cost of an array $$$p$$$ such that $$$1 \le p_i \le k$$$ for all $$$1 \le i \le n$$$.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_1 \le a_2 \le \ldots \le a_n \le 10^5$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case, print a single integer — the minimum possible cost of an array $$$p$$$ satisfying the condition above.

NoteIn the first test case, the optimal array is $$$p = [1, 1, 1, 2, 2]$$$. The resulting array of values of $$$\lfloor \frac{a_i}{p_i} \rfloor$$$ is $$$[4, 5, 6, 4, 5]$$$. The cost of $$$p$$$ is $$$\max\limits_{1 \le i \le n}(\lfloor \frac{a_i}{p_i} \rfloor) - \min\limits_{1 \le i \le n}(\lfloor \frac{a_i}{p_i} \rfloor) = 6 - 4 = 2$$$. We can show that there is no array (satisfying the condition from the statement) with a smaller cost.In the second test case, one of the optimal arrays is $$$p = [12, 12, 12, 12, 12]$$$, which results in all $$$\lfloor \frac{a_i}{p_i} \rfloor$$$ being $$$0$$$.In the third test case, the only possible array is $$$p = [1, 1, 1]$$$.

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1706D2(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	ge := [1e5 + 1]int{}
	var T, n, k, v int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &k)
		up := -1
		for ; n > 0; n-- {
			Fscan(in, &v)
			for i := up + 1; i <= v; i++ {
				ge[i] = v
			}
			up = v
		}
		if up <= k {
			Fprintln(out, 0)
			continue
		}
		ans := int(1e9)
	o:
		for mx := up / k; mx <= up; mx++ {
			mi := mx
			for j := 1; (j-1)*(mx+1) <= up; j++ {
				v := ge[(j-1)*(mx+1)]
				if v >= j*(mx+1) {
					j = v/(mx+1) + 1
				}
				if v/j < mi {
					mi = v / j
					if mx-mi >= ans {
						continue o
					}
				}
			}
			ans = mx - mi
		}
		Fprintln(out, ans)
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1706D2(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	ge := [1e5 + 1]int{}
	var T, n, k, v int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &k)
		up := -1
		for ; n > 0; n-- {
			Fscan(in, &v)
			for i := up + 1; i <= v; i++ {
				ge[i] = v
			}
			up = v
		}
		if up <= k {
			Fprintln(out, 0)
			continue
		}
		ans := int(1e9)
	o:
		for mx := up / k; mx <= up; mx++ {
			mi := mx
			for j := 1; (j-1)*(mx+1) <= up; j++ {
				v := ge[(j-1)*(mx+1)]
				if v == 0 {
					break
				}
				if v >= j*(mx+1) {
					j = v/(mx+1) + 1
				}
				if v/j < mi {
					mi = v / j
					if mx-mi >= ans {
						continue o
					}
				}
			}
			ans = mx - mi
		}
		Fprintln(out, ans)
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1706D2(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	ge := [1e5 + 1]int{}
	var T, n, k, v int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &k)
		up := -1
		for ; n > 0; n-- {
			Fscan(in, &v)
			for i := up + 1; i <= v; i++ {
				ge[i] = v
			}
			up = v
		}
		if up <= k {
			Fprintln(out, 0)
			continue
		}
		ans := int(1e9)
	o:
		for mx := up / k; mx <= up; mx++ {
			mi := mx
			for j := 1; (j-1)*(mx+1) <= up; j++ {
				v := ge[(j-1)*(mx+1)]
				if v == 0 {
					break
				}
				if v >= j*(mx+1) {
					continue
				}
				if v/j < mi {
					mi = v / j
					if mx-mi >= ans {
						continue o
					}
				}
			}
			ans = mx - mi
		}
		Fprintln(out, ans)
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1706D2(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var T, n, k, v int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &k)
		ge := [1e5 + 1]int{}
		up := -1
		for ; n > 0; n-- {
			Fscan(in, &v)
			for i := up + 1; i <= v; i++ {
				ge[i] = v
			}
			up = v
		}
		if up <= k {
			Fprintln(out, 0)
			continue
		}
		ans := int(1e9)
	o:
		for mx := up / k; mx <= up; mx++ {
			mi := int(1e9)
			for j := 1; (j-1)*(mx+1) <= up; j++ {
				v := ge[(j-1)*(mx+1)]
				if v == 0 {
					break
				}
				if v >= j*(mx+1) {
					continue
				}
				if v/j < mi {
					mi = v / j
					if mx-mi >= ans {
						continue o
					}
				}
			}
			ans = min(ans, mx-mi)
		}
		Fprintln(out, ans)
	}
}

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

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1706D2(_r io.Reader, _w io.Writer) {
	out := bufio.NewWriter(_w)
	defer out.Flush()
	buf := make([]byte, 1<<12)
	_i := len(buf)
	rc := func() byte {
		if _i >= len(buf) {
			_r.Read(buf)
			_i = 0
		}
		b := buf[_i]
		_i++
		return b
	}
	r := func() (x int) {
		b := rc()
		for ; '0' > b; b = rc() {
		}
		for ; '0' <= b; b = rc() {
			x = x*10 + int(b&15)
		}
		return
	}
	for T := r(); T > 0; T-- {
		n, k := r(), r()
		ge := [1e5 + 1]int{}
		MX := -1
		for ; n > 0; n-- {
			v := r()
			for i := MX + 1; i <= v; i++ {
				ge[i] = v
			}
			MX = v
		}
		if MX <= k {
			Fprintln(out, 0)
			continue
		}
		ans := int(1e9)
	o:
		for mx := MX / k; mx <= MX; mx++ {
			mi := int(1e9)
			for j := 1; (j-1)*(mx+1) <= MX; j++ {
				v := ge[(j-1)*(mx+1)]
				if v == 0 {
					break
				}
				if v >= min(j*(mx+1), MX+1) {
					continue
				}
				mi = min(mi, v/j)
				if mx-mi >= ans {
					continue o
				}
			}
			ans = min(ans, mx-mi)
		}
		Fprintln(out, ans)
	}
}

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

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1706D2(_r io.Reader, out io.Writer) {
	const MX int = 1e5
	in := bufio.NewReader(_r)
	var T, n, k, v int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &k)
		ge := [MX + 1]int{}
		MX := -1
		for ; n > 0; n-- {
			Fscan(in, &v)
			for i := MX + 1; i <= v; i++ {
				ge[i] = v
			}
			MX = v
		}
		if MX <= k {
			Fprintln(out, 0)
			continue
		}
		ans := int(1e9)
	o:
		for mx := MX / k; mx <= MX; mx++ {
			mi := int(1e9)
			for j := 1; (j-1)*(mx+1) <= MX; j++ {
				v := ge[(j-1)*(mx+1)]
				if v == 0 {
					break
				}
				if v >= min(j*(mx+1), MX+1) {
					continue
				}
				mi = min(mi, v/j)
				if mx-mi >= ans {
					continue o
				}
			}
			ans = min(ans, mx-mi)
		}
		Fprintln(out, ans)
	}
}

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

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1706D2(_r io.Reader, out io.Writer) {
	const MX int = 1e5
	in := bufio.NewReader(_r)
	var T, n, k, v int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &k)
		ge := [MX + 1]int{}
		MX := -1
		for ; n > 0; n-- {
			Fscan(in, &v)
			for i := MX + 1; i <= v; i++ {
				ge[i] = v
			}
			MX = v
		}
		if MX <= k {
			Fprintln(out, 0)
			continue
		}
		ans := int(1e9)
	o:
		for mx := MX / k; mx <= MX; mx++ {
			mi := int(1e9)
			for j := 1; j <= k && (j-1)*(mx+1) <= MX; j++ {
				v := ge[(j-1)*(mx+1)]
				if v == 0 {
					break
				}
				if v >= min(j*(mx+1), MX+1) {
					continue
				}
				mi = min(mi, v/j)
				if mx-mi >= ans {
					continue o
				}
			}
			ans = min(ans, mx-mi)
		}
		Fprintln(out, ans)
	}
}

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

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1706D2(_r io.Reader, out io.Writer) {
	const MX int = 1e5
	in := bufio.NewReader(_r)
	var T, n, k, v int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &k)
		ge := [MX + 1]int{}
		pre := 0
		for ; n > 0; n-- {
			Fscan(in, &v)
			for i := pre; i <= v; i++ {
				ge[i] = v
			}
			pre = v + 1
		}
		ans := int(1e9)
		MX := pre - 1
		if MX <= k {
			Fprintln(out, 0)
			continue
		}
	o:
		for mx := MX / k; mx <= MX; mx++ {
			mi := int(1e9)
			for j := 1; j <= k && (j-1)*(mx+1) < MX; j++ {
				v := ge[(j-1)*(mx+1)]
				if v == 0 {
					break
				}
				if v >= min(j*(mx+1), MX+1) {
					continue
				}
				mi = min(mi, v/j)
				if mx-mi >= ans {
					continue o
				}
			}
			if mi <= mx {
				ans = min(ans, mx-mi)
			}
		}
		Fprintln(out, ans)
	}
}

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

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1706D2(_r io.Reader, out io.Writer) {
	const MX int = 1e5
	in := bufio.NewReader(_r)
	var T, n, k, v int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &k)
		ge := [MX + 1]int{}
		pre := 0
		for ; n > 0; n-- {
			Fscan(in, &v)
			for i := pre; i <= v; i++ {
				ge[i] = v
			}
			pre = v + 1
		}
		ans := int(1e9)
		MX := pre - 1
		if MX <= k {
			Fprintln(out, 0)
			continue
		}
		for mx := MX / k; mx <= MX; mx++ {
			mi := int(1e9)
			for j := 1; j <= k && (j-1)*(mx+1) < MX; j++ {
				v := ge[(j-1)*(mx+1)]
				if v == 0 {
					break
				}
				if v >= min(j*(mx+1), MX+1) {
					continue
				}
				mi = min(mi, v/j)
			}
			if mi <= mx {
				ans = min(ans, mx-mi)
			}
		}
		Fprintln(out, ans)
	}
}

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

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

package main

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

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

	var buf bytes.Buffer

	tc := readNum(reader)

	for tc > 0 {
		tc--
		n, k := readTwoNums(reader)
		A := readNNums(reader, n)
		res := solve(A, k)
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}

	fmt.Print(buf.String())
}

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

func normalize(s string) string {

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

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

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

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

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

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

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

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

	return i
}

const N = 100010

var closest [N]int

const INF = math.MaxInt64

func solve(A []int, k int) int {
	ps := make([]int, N)

	n := len(A)

	for i := 0; i < n; i++ {
		pv := 1 << 30
		for j := 1; j <= min2(A[i], k); j = (A[i] / (A[i] / j)) + 1 {
			nv := A[i] / j
			ps[nv+1] = max(ps[nv+1], pv)
			pv = nv
		}
		ps[0] = max(ps[0], pv)
	}

	ans := 1 << 30
	var cm int
	for i := 0; i <= A[0]; i++ {
		cm = max(cm, ps[i])
		ans = min2(ans, cm-i)
	}

	return ans
}

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

func solve1(A []int, k int) int {
	n := len(A)
	if n == 1 {
		return 0
	}

	arr := make([]int, n+1)
	copy(arr[1:], A)

	arr[0] = -1

	fill := func(s int, e int, v int) {
		for i := s; i < e; i++ {
			closest[i] = v
		}
	}

	for i := 1; i <= n; i++ {
		fill(arr[i-1]+1, arr[i]+1, arr[i])
	}

	fill(arr[n]+1, N, INF)

	v := make([]Pair, 2*k)

	var end int

	push := func(x Pair) {
		v[end] = x
		end++
	}

	back := func() Pair {
		return v[end-1]
	}

	var ans int64 = INF

	for mx := 1; mx <= 100000; mx++ {
		end = 0
		push(Pair{1, int64(mx)})
		for i := 2; i <= k; i++ {
			push(Pair{back().second + 1, int64(mx+1)*int64(i) - 1})
			if back().second > int64(arr[n]) {
				break
			}
		}
		if back().second >= int64(arr[n]) {
			var mn int64 = INF
			for i := 0; i < end; i++ {
				nxt := int64(closest[v[i].first])
				if nxt <= v[i].second {
					mn = min(mn, nxt/int64(i+1))
				}
			}
			ans = min(ans, int64(mx)-mn)
		}
	}

	return int(ans)
}

type Pair struct {
	first  int64
	second int64
}

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

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

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

package main

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

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

	var buf bytes.Buffer

	tc := readNum(reader)

	for tc > 0 {
		tc--
		n, k := readTwoNums(reader)
		A := readNNums(reader, n)
		res := solve(A, k)
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}

	fmt.Print(buf.String())
}

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

func normalize(s string) string {

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

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

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

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

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

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

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

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

	return i
}

const N = 100010

var closest [N]int

const INF = math.MaxInt64

func solve(A []int, k int) int {
	n := len(A)
	if n == 1 {
		return 0
	}

	arr := make([]int, n+1)
	copy(arr[1:], A)

	arr[0] = -1

	fill := func(s int, e int, v int) {
		for i := s; i < e; i++ {
			closest[i] = v
		}
	}

	for i := 1; i <= n; i++ {
		fill(arr[i-1]+1, arr[i]+1, arr[i])
	}

	fill(arr[n]+1, N, INF)

	v := make([]Pair, 2*k)

	var end int

	push := func(x Pair) {
		v[end] = x
		end++
	}

	back := func() Pair {
		return v[end-1]
	}

	var ans int64 = INF

	for mx := 1; mx <= 100000; mx++ {
		end = 0
		push(Pair{1, int64(mx)})
		for i := 2; i <= k; i++ {
			push(Pair{back().second + 1, int64(mx+1)*int64(i) - 1})
			if back().second > int64(arr[n]) {
				break
			}
		}
		if back().second >= int64(arr[n]) {
			var mn int64 = INF
			for i := 0; i < end; i++ {
				nxt := int64(closest[v[i].first])
				if nxt <= v[i].second {
					mn = min(mn, nxt/int64(i+1))
				}
			}
			ans = min(ans, int64(mx)-mn)
		}
	}

	return int(ans)
}

type Pair struct {
	first  int64
	second int64
}

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

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

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1706D2(_r io.Reader, out io.Writer) {
	const MX int = 1e5
	in := bufio.NewReader(_r)
	var T, n, k, v int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &k)
		ge := [MX + 1]int{}
		pre := 0
		for ; n > 0; n-- {
			Fscan(in, &v)
			for i := pre; i <= v; i++ {
				ge[i] = v
			}
			pre = v + 1
		}
		ans := int(1e9)
		MX := pre - 1
		if MX <= k {
			Fprintln(out, 0)
			continue
		}
		for mx := MX / k; mx <= MX; mx++ {
			mi := int(1e9)
			for j := 1; j <= k && (j-1)*(mx+1) < MX; j++ {
				v := ge[(j-1)*(mx+1)]
				if v == 0 {
					break
				}
				if v >= min(j*(mx+1), MX+1) {
					continue
				}
				mi = min(mi, v/j)
			}
			ans = min(ans, mx-mi)
		}
		Fprintln(out, ans)
	}
}

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

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


Alice and Bob play a game. They have a binary string $$$s$$$ (a string such that each character in it is either $$$0$$$ or $$$1$$$). Alice moves first, then Bob, then Alice again, and so on.During their move, the player can choose any number (not less than one) of consecutive equal characters in $$$s$$$ and delete them.For example, if the string is $$$10110$$$, there are $$$6$$$ possible moves (deleted characters are bold):  $$$\textbf{1}0110 \to 0110$$$;  $$$1\textbf{0}110 \to 1110$$$;  $$$10\textbf{1}10 \to 1010$$$;  $$$101\textbf{1}0 \to 1010$$$;  $$$10\textbf{11}0 \to 100$$$;  $$$1011\textbf{0} \to 1011$$$. After the characters are removed, the characters to the left and to the right of the removed block become adjacent. I. e. the following sequence of moves is valid: $$$10\textbf{11}0 \to 1\textbf{00} \to 1$$$.The game ends when the string becomes empty, and the score of each player is the number of $$$1$$$-characters deleted by them.Each player wants to maximize their score. Calculate the resulting score of Alice.

The first line contains one integer $$$T$$$ ($$$1 \le T \le 500$$$) — the number of test cases. Each test case contains exactly one line containing a binary string $$$s$$$ ($$$1 \le |s| \le 100$$$).

For each test case, print one integer — the resulting score of Alice (the number of $$$1$$$-characters deleted by her).

NoteQuestions about the optimal strategy will be ignored.

package main

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

var in *bufio.Reader
var out *bufio.Writer

func printf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fprintf(out, x, a...)
}

func scanf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fscanf(in, x, a...)
}

var err error

func next() (n string) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return ""
		}
	}
}

func nextInt() (n int) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func nextLong() (n int64) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func nextDouble() (n float64) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func Fill(a []int, v int) {
	for i := range a {
		a[i] = v
	}
}

func FillB(a []bool, v bool) {
	for i := range a {
		a[i] = v
	}
}

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
}

const (
	oo  = 100000000
	mod = 1
)

func mult(a, b int64) int64 {
	return (a * b) % mod
}
func add(a, b int64) int64 {
	return (a + b) % mod
}
func sub(a, b int64) int64 {
	return (a - b + mod) % mod
}

var (
	base, a, b map[string]string
	dp         []map[string]int
	pat        string
)

func dfs(i int, x string) int {
	{
		a, test := dp[i][x]
		if test {
			return a
		}
	}
	res := i
	if base[x] != "" {
		for _, c := range []byte(base[x]) {
			if res < len(pat) && pat[res] == c {
				res++
			}
		}
	} else {
		res = dfs(dfs(i, a[x]), b[x])
	}
	dp[i][x] = res
	return res
}

func solve() {
	arr := []int{}
	{
		strin := []byte(next())
		num := 0
		for _, c := range strin {
			if c == '0' {
				if num != 0 {
					arr = append(arr, num)
				}
				num = 0
			} else {
				num++
			}
		}
		if num != 0 {
			arr = append(arr, num)
		}
	}
	sort.Ints(arr)
	tot := [2]int{}
	for i, c := range arr {
		tot[i%2] += c
	}
	printf("%d\n", tot[(len(arr)+1)%2])
}

func main() {
	in = bufio.NewReaderSize(os.Stdin, 8192)
	out = bufio.NewWriterSize(os.Stdout, 8192)
	if false {
		file1, err := os.Open("input.txt")
		if err != nil {
			log.Fatal(err)
		}
		file2, err := os.Create("output.txt")
		if err != nil {
			log.Fatal(err)
		}
		in = bufio.NewReaderSize(file1, 8192)
		out = bufio.NewWriterSize(file2, 8192)
	}
	defer out.Flush()

	// start := time.Now()

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

	// printf("%v\n", time.Now().Sub(start))
}

package main

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

func main() {
	var t int
	var s string
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &s)
		count := make([]int, 0)
		c := 0
		for _, v := range s {
			if v == '1' {
				c++
			} else if c != 0 {
				count = append(count, c)
				c = 0
			}
		}
		if c != 0 {
			count = append(count, c)
			c = 0
		}
		sort.Slice(count, func(i, j int) bool {
			return count[i] > count[j]
		})
		ans := 0
		for j := 0; j < len(count); j += 2 {
			ans += count[j]
		}
		fmt.Println(ans)
	}
}

package main

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

func printf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fprintf(out, x, a...)
}

func scanf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fscanf(in, x, a...)
}
func scan(a ...interface{}) (n int, err error) {
	return fmt.Fscan(in, a...)
}

var err error

func next() (n string) {
	for {
		_, err = scan(&n)
		L("next err: ", err)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return ""
		}
	}
}

func nextInt() (n int) {
	for {
		_, err = scan(&n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func nextLong() (n int64) {
	for {
		_, err = scan(&n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func nextDouble() (n float64) {
	for {
		_, err = scan(&n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}
var (
	out *bufio.Writer
	in  *bufio.Reader
	printLog bool
)

func init() {
	out = bufio.NewWriterSize(os.Stdout, 8192)
	in = bufio.NewReaderSize(os.Stdin, 8192)

}
func L(a ...interface{}) {
	if printLog {
		log.Println(a)
	}
}
func longest1(s string) (l, r int) {
	L("s: ", s)
	l, r = -1, -1
	b, e := 0, 0
	mx := 0
	idx := 0
	for _, c := range s {
		if c == '1' {
			if s[b] != '1' {
				b = idx
			}
		}
		if c == '0' {
			e = idx
			L("b, e: ", b, e)
			if mx < e - b && s[b] == '1'{
				mx = e - b
				l, r = b, e
				L("l, r: ", l, r)
			}
			b, e = idx, idx
		}
		L("c, c == '0', c == '1': ",c, (bool)(c == '0'), (bool)(c == '1'), reflect.TypeOf(c))
		idx++
	}
	if s[b] == '1' {
		e = idx
		L("b, e: ", b, e)
		if mx < e - b && s[b] == '1'{
			mx = e - b
			l, r = b, e
			L("l, r: ", l, r)
		}
		b, e = idx, idx
	}
	return
}

func termi(s string) bool {
	// all 0
	if strings.Count(s, "0") == len(s) {
		return true
	}
	return false
}

func main() {
	printLog = false
	defer out.Flush()
	t := nextInt()
	L("t: ", t)
	for ; t > 0; t-- {
		s := next()
		L("s: ", s)
		ans := 0
		fl := 0
		for ; termi(s) == false; {
			l, r := longest1(s)
			L("l, r: ", l, r)

			s = s[:l] + s[r:]
			if fl == 0 {
				ans += r - l
			}
			L("s: ", s)
			fl = 1 - fl
		}
		fmt.Println(ans)
	}

}

package main

import (
	"fmt"
	"sort"
)

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

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

type Pair struct {
	first, second int
}

type byFirst []Pair

func (s byFirst) Len() int {
	return len(s)
}

func (s byFirst) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}

func (s byFirst) Less(i, j int) bool {
	return s[i].first < s[j].first
}

type bySecond []Pair

func (s bySecond) Len() int {
	return len(s)
}

func (s bySecond) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}

func (s bySecond) Less(i, j int) bool {
	return s[i].second < s[j].second
}

func main() {
	var t int
	fmt.Scan(&t)
	for t > 0 {
		var s string
		fmt.Scan(&s)

		n := len(s)

		blocks := make([]int, 0)

		j := -1
		for i := 0; i < n; i++ {
			if s[i] == '0' {
				if j == -1 {
					continue
				}
				blocks = append(blocks, i-j)
				j = -1
			} else {
				if j == -1 {
					j = i
				}
			}
		}
		if j != -1 {
			blocks = append(blocks, n-j)
		}

		sort.Ints(blocks)

		score := 0
		for i := len(blocks) - 1; i >= 0; i -= 2 {
			score += blocks[i]
		}

		fmt.Println(score)

		t--
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		var str string
		var arr []int
		fmt.Fscan(in, &str)

		count := 0
		for j := 0; j < len(str); j++ {
			if str[j] == '0' {
				if count > 0 {
					arr = append(arr, count)
				}
				count = 0
			} else {
				count++
			}
		}
		if count > 0 {
			arr = append(arr, count)
		}
		sort.Sort(sort.IntSlice(arr))

		alice := 0
		for j := len(arr) - 1; j >= 0; j -= 2 {
			alice += arr[j]
		}

		fmt.Println(alice)

	}
}

package main

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

func main(){
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	var s string
	fmt.Scan(&t)
	for ;t>0;t--{
		fmt.Scan(&s)
		fmt.Fprintln(out,solve(s))
	}
}

func solve(s string)int{
	var point int
	var m int
	for ;;m++{
		var n int
		s,n = move(s)
		if n==0{
			break
		}
		if m%2==0{
			point+=n
		}
	}
	return point
}

func move(s string)(string,int){
	var  start,end int
	var l int
	for i := range s{
		if s[i] != '1'{
			continue
		}
		j := i+1
		for ;j<len(s)&&s[j]=='1';j++{
		}
		if j-i > l{
			l = j-i
			start = i
			end = j
		}
	}
	s = s[:start]+s[end:]
	return s,l
}

package main

import (
	. "fmt"
	"sort"
	"strings"
)

// github.com/EndlessCheng/codeforces-go
func main() {
	T, s := 0, ""
	for Scan(&T); T > 0; T-- {
		Scan(&s)
		a := strings.Split(s, "0")
		sort.Strings(a)
		c := 0
		for i := len(a) - 1; i >= 0; i -= 2 {
			c += len(a[i])
		}
		Println(c)
	}
}

package main

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

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

	var n int
	var s string

	fmt.Fscan(io, &n)
	for ; n > 0; n-- {
		fmt.Fscan(io, &s)
		streak := 0
		streaks := []int{}
		for _, c := range s {
			if c == '1' {
				streak++
			} else if streak > 0 {
				streaks = append(streaks, streak)
				streak = 0
			}
		}
		if streak > 0 {
			streaks = append(streaks, streak)
			streak = 0
		}
		sort.Sort(sort.Reverse(sort.IntSlice(streaks)))
		alicePoints := 0
		for i, streak := range streaks {
			if i%2 == 0 {
				alicePoints += streak
			}
		}
		fmt.Println(alicePoints)
	}
}


Some large corporation where Polycarpus works has its own short message service center (SMSC). The center's task is to send all sorts of crucial information. Polycarpus decided to check the efficiency of the SMSC. For that, he asked to give him the statistics of the performance of the SMSC for some period of time. In the end, Polycarpus got a list of n tasks that went to the SMSC of the corporation. Each task was described by the time it was received by the SMSC and the number of text messages to send. More formally, the i-th task was described by two integers ti and ci — the receiving time (the second) and the number of the text messages, correspondingly.Polycarpus knows that the SMSC cannot send more than one text message per second. The SMSC uses a queue to organize its work. Consider a time moment x, the SMSC work at that moment as follows:  If at the time moment x the queue is not empty, then SMSC sends one message from the queue (SMSC gets the message from the head of the queue). Otherwise it doesn't send messages at the time moment x.  If at the time moment x SMSC receives a task, then it adds to the queue all the messages from this task (SMSC adds messages to the tail of the queue). Note, that the messages from the task cannot be send at time moment x. That's because the decision about sending message or not is made at point 1 before adding these messages to the queue. Given the information about all n tasks, Polycarpus wants to count two values: the time when the last text message was sent and the maximum size of the queue at some time. Help him count these two characteristics he needs to evaluate the efficiency of the SMSC.

The first line contains a single integer n (1 ≤ n ≤ 103) — the number of tasks of the SMSC. Next n lines contain the tasks' descriptions: the i-th line contains two space-separated integers ti and ci (1 ≤ ti, ci ≤ 106) — the time (the second) when the i-th task was received and the number of messages to send, correspondingly. It is guaranteed that all tasks were received at different moments of time. It is guaranteed that the tasks are sorted in the chronological order, that is, ti < ti + 1 for all integer i (1 ≤ i < n).

In a single line print two space-separated integers — the time when the last text message was sent and the maximum queue size at a certain moment of time.

NoteIn the first test sample:   second 1: the first message has appeared in the queue, the queue's size is 1;  second 2: the first message is sent, the second message has been received, the queue's size is 1;  second 3: the second message is sent, the queue's size is 0, Thus, the maximum size of the queue is 1, the last message was sent at the second 3.

// 292A
package main

import (
	"fmt"
)

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

func main() {
	var n, t, c, l, r, i int
	fmt.Scan(&n)
	for i = 0; i < n; i++ {
		fmt.Scan(&t, &c)
		l = max(t, l) + c
		r = max(l-t, r)
	}
	fmt.Println(l, r)
}


Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he decides to breed the chicken on his own farm.His farm is presented by a rectangle grid with $$$r$$$ rows and $$$c$$$ columns. Some of these cells contain rice, others are empty. $$$k$$$ chickens are living on his farm. The number of chickens is not greater than the number of cells with rice on the farm.Long wants to give his chicken playgrounds by assigning these farm cells to his chickens. He would like to satisfy the following requirements:  Each cell of the farm is assigned to exactly one chicken.  Each chicken is assigned at least one cell.  The set of cells assigned to every chicken forms a connected area. More precisely, if two cells $$$(x, y)$$$ and $$$(u, v)$$$ are assigned to the same chicken, this chicken is able to walk from $$$(x, y)$$$ to $$$(u, v)$$$ by passing only its cells and moving from each cell to another cell sharing a side. Long also wants to prevent his chickens from fighting for food. Hence he wants the difference between the maximum and the minimum number of cells with rice assigned to a chicken to be as small as possible. Please help him.

Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \le T \le 2 \cdot 10^4$$$). Description of the test cases follows. The first line of each test case contains three integers $$$r$$$, $$$c$$$ and $$$k$$$ ($$$1 \leq r, c \leq 100, 1 \leq k \leq 62$$$), representing the size of Long's farm and the number of chickens Long has.  Each of the next $$$r$$$ lines contains $$$c$$$ characters, each is either "." or "R", representing an empty cell or a cell with rice. It is guaranteed that the number of chickens is not greater than the number of cells with rice on the farm. It is guaranteed that the sum of $$$r \cdot c$$$ over all test cases does not exceed $$$2 \cdot 10^4$$$.

For each test case, print $$$r$$$ lines with $$$c$$$ characters on each line. Each character should be either a lowercase English character, an uppercase English character, or a digit. Two characters should be equal if and only if the two corresponding cells are assigned to the same chicken. Uppercase and lowercase characters are considered different, so "A" and "a" belong to two different chickens. If there are multiple optimal answers, print any.

4
3 5 3
..R..
...R.
....R
6 4 6
R..R
R..R
RRRR
RRRR
R..R
R..R
5 5 4
RRR..
R.R..
RRR..
R..R.
R...R
2 31 62
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR

11122
22223
33333
aacc
aBBc
aBBc
CbbA
CbbA
CCAA
11114
22244
32444
33344
33334
abcdefghijklmnopqrstuvwxyzABCDE
FGHIJKLMNOPQRSTUVWXYZ0123456789

NoteThese pictures explain the sample output. Each color represents one chicken. Cells filled with patterns (not solid colors) contain rice.In the first test case, each chicken has one cell with rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is $$$0$$$.In the second test case, there are $$$4$$$ chickens with $$$3$$$ cells of rice, and $$$2$$$ chickens with $$$2$$$ cells of rice. Hence, the difference between the maximum and the minimum number of cells with rice assigned to a chicken is $$$3 - 2 = 1$$$.In the third test case, each chicken has $$$3$$$ cells with rice. In the last test case, since there are $$$62$$$ chicken with exactly $$$62$$$ cells of rice, each chicken must be assigned to exactly one cell. The sample output is one of the possible way.

package main

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

func main() {
	defer os.Stdout.Sync()
	buf := bufio.NewReader(os.Stdin)
	symb := make([]byte, 62)
	for i := 0; i < 10; i++ {
		symb[i] = byte('0') + byte(i)
	}
	for i := 10; i < 10+26; i++ {
		symb[i] = byte('a') + byte(i-10)
	}
	for i := 10 + 26; i < 10+26+26; i++ {
		symb[i] = byte('A') + byte(i-10-26)
	}
	//fmt.Printf("%s\n",symb)
	var r, c, k, t int
	fmt.Fscanf(buf, "%d\n", &t)
	for ; t > 0; t-- {
		fmt.Fscanf(buf, "%d %d %d\n", &r, &c, &k)
		//fmt.Printf("%d %d %d %d\n",t,r,c,k)
		mat := make([][]byte, r)
		ans := make([][]byte,r)
		rcnt := 0
		for i := 0; i < r; i++ {
			mat[i] = make([]byte, c+1)
			ans[i] = make([]byte, c)
			fmt.Fscanf(buf, "%s\n", &mat[i])
			//fmt.Printf("--->%s\n",mat[i])
			for j := 0; j < c; j++ {
				if mat[i][j] == byte('R') {
					rcnt++
				}
			}
		}
		//fmt.Printf("rcnt=%d\n",rcnt)
		div := rcnt % k
		num := 1
		numrcnt := 0
		expectcnt := rcnt / k
		if num <= div {
			expectcnt++
		}
		x := 0
		y := 0
		front := 1

		for i := 0; i < r*c; i++ {
			//fmt.Printf("%c", symb[num-1])
			ans[x][y] = symb[num-1]
			//if (i+1)%c == 0{
			//	fmt.Printf("\n")
			//}
			if mat[x][y] == 'R' {
				numrcnt++
			}
			if numrcnt == expectcnt {
				if num < k {
					num++
					numrcnt = 0
					expectcnt = rcnt / k
					if num <= div {
						expectcnt++
					}
				}
			}
			if front > 0 {
				if y+front < c {
					y += front
				} else {
					front = -1
					x++
				}
			}else{
				if y+front >= 0{
					y += front
				}else{
					x++
					front = 1
				}
			}
		}
		for i:= 0;i< r;i++{
			for j:=0;j < c;j++{
				fmt.Printf("%c",ans[i][j])
			}
			fmt.Printf("\n")
		}

	}
}

/**
4
3 5 3
..R..
...R.
....R
6 4 6
R..R
R..R
RRRR
RRRR
R..R
R..R
2 31 62
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
 */

package main

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

var scanner *bufio.Scanner

func init() {
	scanner = bufio.NewScanner(bufio.NewReader(os.Stdin))
	//scanner.Buffer(make([]byte, 311000), 311000)
	scanner.Split(bufio.ScanWords)
}

func next() string {
	if !scanner.Scan() {
		panic("scanner.Scan() returned false")
	}
	return scanner.Text()
}

func nextInt() int {
	num, err := strconv.Atoi(next())
	if err != nil {
		panic(err)
	}
	return num
}

type PosGenerator struct{
	R, C int
	r, c int
	count int
}

func (pg *PosGenerator) hasNext() bool {
	return pg.count < pg.R*pg.C;
}

func (pg *PosGenerator) next() (int, int) {
	pg.count++;
	if pg.count==1 {
		return 0,0;
	}
	if pg.r%2==0 {
		if pg.c+1<pg.C {
			pg.c++;
		} else {
			pg.r++;
		}
	} else {
		if pg.c>0 {
			pg.c--;
		} else {
			pg.r++;
		}
	}

	return pg.r, pg.c;
}

func nextColor(b byte) byte {
	if b=='Z' {
		return 'a'
	}
	if b=='z' {
		return '0'
	}
	return b+1;
}

func shouldGet(gi, G, rice int) int {
	ricePerChicken := rice/G
	if gi<rice%G {
		ricePerChicken++
	}
	return ricePerChicken;
}

func main() {
	T := nextInt()
	for t:=0; t<T; t++ {
		R, C, G := nextInt(), nextInt(), nextInt()
		pg := PosGenerator{R:R, C:C}

		grid := make([]string, R)
		for i:= range grid {
			grid[i] = next();
		}

		rice := 0;
		field := make([][]byte, R)
		for i, row := range grid {
			field[i] = make([]byte, C)
			for _, c := range row {
				if c=='R' {
					rice++;
				}
			}
		}

		gi := 0;
		gc := 0;
		color := byte('A');

		for pg.hasNext() {
			r, c := pg.next()
			if grid[r][c] == 'R' {
				if gc==shouldGet(gi, G, rice) {
					color = nextColor(color)
					gi++
					gc=0
				}
				gc++;
			}

			field[r][c] = color;

		}

		for _, row := range field {
			fmt.Println(string(row))
		}
	}
}

package main

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

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

	var t, r, c, k, lastRi, lastRj, avg, nAvg int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &r, &c, &k)
		cells := make([][]byte, r)
		cnt := 0
		for i := range cells {
			Fscan(in, &cells[i])
			cntRow := bytes.Count(cells[i], []byte{'R'})
			if cntRow > 0 {
				cnt += cntRow
				lastRi = i
				if i & 1 == 0 {
					lastRj = bytes.LastIndex(cells[i], []byte{'R'})
				} else {
					lastRj = bytes.Index(cells[i], []byte{'R'})
				}
			}
		}
		cells[lastRi][lastRj] = '.'
		if r&1 == 1 {
			cells[r-1][c-1] = 'R'
		} else {
			cells[r-1][0] = 'R'
		}

		avg = cnt / k
		if cnt%k == 0 {
			nAvg = k
		} else {
			nAvg = (avg+1)*k - cnt
		}
		nowI := 0
		nowCnt := 0
		left := nAvg

		modify := func(i, j int, isRice bool) {
			cells[i][j] = alphabet[nowI]
			if isRice {
				nowCnt++
				if nowCnt == avg {
					nowI++
					nowCnt = 0
					left--
					if left == 0 {
						avg++
						left = k - nAvg
					}
				}
			}
		}
		for i, row := range cells {
			if i&1 == 0 {
				for j, ch := range row {
					modify(i, j, ch == 'R')
				}
			} else {
				for j := c - 1; j >= 0; j-- {
					modify(i, j, cells[i][j] == 'R')
				}
			}
		}
		for _, row := range cells {
			Fprintf(out, "%s\n", row)
		}
	}
}

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

package main

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

func main() {
	defer os.Stdout.Sync()
	buf := bufio.NewReader(os.Stdin)
	symb := make([]byte, 62)
	for i := 0; i < 10; i++ {
		symb[i] = byte('0') + byte(i)
	}
	for i := 10; i < 10+26; i++ {
		symb[i] = byte('a') + byte(i-10)
	}
	for i := 10 + 26; i < 10+26+26; i++ {
		symb[i] = byte('A') + byte(i-10-26)
	}
	//fmt.Printf("%s\n",symb)
	var r, c, k, t int
	fmt.Fscanf(buf, "%d\n", &t)
	for ; t > 0; t-- {
		fmt.Fscanf(buf, "%d %d %d\n", &r, &c, &k)
		//fmt.Printf("%d %d %d %d\n",t,r,c,k)
		mat := make([][]byte, r)
		ans := make([][]byte,r)
		rcnt := 0
		for i := 0; i < r; i++ {
			mat[i] = make([]byte, c+1)
			ans[i] = make([]byte, c)
			fmt.Fscanf(buf, "%s\n", &mat[i])
			//fmt.Printf("--->%s\n",mat[i])
			for j := 0; j < c; j++ {
				if mat[i][j] == byte('R') {
					rcnt++
				}
			}
		}
		//fmt.Printf("rcnt=%d\n",rcnt)
		div := rcnt % k
		num := 1
		numrcnt := 0
		expectcnt := rcnt / k
		if num <= div {
			expectcnt++
		}
		x := 0
		y := 0
		front := 1

		for i := 0; i < r*c; i++ {
			//fmt.Printf("%c", symb[num-1])
			ans[x][y] = symb[num-1]
			//if (i+1)%c == 0{
			//	fmt.Printf("\n")
			//}
			if mat[x][y] == 'R' {
				numrcnt++
			}
			if numrcnt == expectcnt {
				num++
				numrcnt = 0
				expectcnt = rcnt / k
				if num <= div {
					expectcnt++
				}
			}
			if front > 0 {
				if y+front < c {
					y += front
				} else {
					front = -1
					x++
				}
			}else{
				if y+front >= 0{
					y += front
				}else{
					x++
					front = 1
				}
			}
		}
		for i:= 0;i< r;i++{
			for j:=0;j < c;j++{
				fmt.Printf("%c",ans[i][j])
			}
			fmt.Printf("\n")
		}

	}
}

/**
4
3 5 3
..R..
...R.
....R
6 4 6
R..R
R..R
RRRR
RRRR
R..R
R..R
2 31 62
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
 */

package main

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

var scanner *bufio.Scanner

func init() {
	scanner = bufio.NewScanner(bufio.NewReader(os.Stdin))
	//scanner.Buffer(make([]byte, 311000), 311000)
	scanner.Split(bufio.ScanWords)
}

func next() string {
	if !scanner.Scan() {
		panic("scanner.Scan() returned false")
	}
	return scanner.Text()
}

func nextInt() int {
	num, err := strconv.Atoi(next())
	if err != nil {
		panic(err)
	}
	return num
}

type PosGenerator struct{
	R, C int
	r, c int
	count int
}

func (pg *PosGenerator) hasNext() bool {
	return pg.count < pg.R*pg.C;
}

func (pg *PosGenerator) next() (int, int) {
	pg.count++;
	if pg.count==1 {
		return 0,0;
	}
	if pg.r%2==0 {
		if pg.c+1<pg.C {
			pg.c++;
		} else {
			pg.r++;
		}
	} else {
		if pg.c>0 {
			pg.c--;
		} else {
			pg.r++;
		}
	}

	return pg.r, pg.c;
}

func nextColor(b byte) byte {
	if b=='Z' {
		return 'a'
	}
	if b=='z' {
		return '0'
	}
	return b+1;
}

func shouldGet(gi, G, rice int) int {
	ricePerChicken := rice/G
	if gi<rice%G {
		ricePerChicken++
	}
	return ricePerChicken;
}

func main() {
	T := nextInt()
	for t:=0; t<T; t++ {
		R, C, G := nextInt(), nextInt(), nextInt()
		pg := PosGenerator{R:R, C:C}

		grid := make([]string, R)
		for i:= range grid {
			grid[i] = next();
		}

		rice := 0;
		field := make([][]byte, R)
		for i, row := range grid {
			field[i] = make([]byte, C)
			for _, c := range row {
				if c=='R' {
					rice++;
				}
			}
		}

		gi := 0;
		gc := 0;
		color := byte('A');

		for pg.hasNext() {
			r, c := pg.next()
			field[r][c] = color;
			if grid[r][c] == 'R' {
				gc++;
				if gc==shouldGet(gi, G, rice) {
					color = nextColor(color)
					gi++
					gc=0
				}
			}
		}

		for _, row := range field {
			fmt.Println(string(row))
		}
	}
}

package main

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

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

	var t, r, c, k, lastRi, lastRj, avg, nAvg int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &r, &c, &k)
		cells := make([][]byte, r)
		cnt := 0
		for i := range cells {
			Fscan(in, &cells[i])
			cntRow := bytes.Count(cells[i], []byte{'R'})
			if cntRow > 0 {
				cnt += cntRow
				lastRi, lastRj = i, bytes.LastIndex(cells[i], []byte{'R'})
			}
		}
		cells[lastRi][lastRj] = '.'
		if r&1 == 1 {
			cells[r-1][c-1] = 'R'
		} else {
			cells[r-1][0] = 'R'
		}

		avg = cnt / k
		if cnt%k == 0 {
			nAvg = k
		} else {
			nAvg = (avg+1)*k - cnt
		}
		nowI := 0
		nowCnt := 0
		left := nAvg

		modify := func(i, j int, isRice bool) {
			cells[i][j] = alphabet[nowI]
			if isRice {
				nowCnt++
				if nowCnt == avg {
					nowI++
					nowCnt = 0
					left--
					if left == 0 {
						avg++
						left = k - nAvg
					}
				}
			}
		}
		for i, row := range cells {
			if i&1 == 0 {
				for j, ch := range row {
					modify(i, j, ch == 'R')
				}
			} else {
				for j := c - 1; j >= 0; j-- {
					modify(i, j, cells[i][j] == 'R')
				}
			}
		}
		for _, row := range cells {
			Fprintf(out, "%s\n", row)
		}
	}
}

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

package main

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

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

	var t, r, c, k, avg, nAvg int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &r, &c, &k)
		cells := make([][]byte, r)
		cnt := 0
		for i := range cells {
			Fscan(in, &cells[i])
			cnt += bytes.Count(cells[i], []byte{'R'})
		}

		avg = cnt / k
		if cnt%k == 0 {
			nAvg = k
		} else {
			nAvg = (avg+1)*k - cnt
		}
		nowI := 0
		nowCnt := 0
		left := nAvg

		modify := func(i, j int, isRice bool) {
			cells[i][j] = alphabet[nowI]
			if isRice {
				nowCnt++
				if nowCnt == avg {
					nowI++
					nowCnt = 0
					left--
					if left == 0 {
						left = k - nAvg
					}
				}
			}
		}
		for i, row := range cells {
			if i&1 == 0 {
				for j, ch := range row {
					modify(i, j, ch == 'R')
				}
			} else {
				for j := c - 1; j >= 0; j-- {
					modify(i, j, cells[i][j] == 'R')
				}
			}
		}
		for _, row := range cells {
			Fprintf(out, "%s\n", row)
		}
	}
}

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

package main

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

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

	var t, r, c, k, lastRi, lastRj, avg, nAvg int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &r, &c, &k)
		cells := make([][]byte, r)
		cnt := 0
		for i := range cells {
			Fscan(in, &cells[i])
			cntRow := bytes.Count(cells[i], []byte{'R'})
			if cntRow > 0 {
				cnt += cntRow
				lastRi = i
				if i & 1 == 1 {
					lastRj = bytes.LastIndex(cells[i], []byte{'R'})
				} else {
					lastRj = bytes.Index(cells[i], []byte{'R'})
				}
			}
		}
		cells[lastRi][lastRj] = '.'
		if r&1 == 1 {
			cells[r-1][c-1] = 'R'
		} else {
			cells[r-1][0] = 'R'
		}

		avg = cnt / k
		if cnt%k == 0 {
			nAvg = k
		} else {
			nAvg = (avg+1)*k - cnt
		}
		nowI := 0
		nowCnt := 0
		left := nAvg

		modify := func(i, j int, isRice bool) {
			cells[i][j] = alphabet[nowI]
			if isRice {
				nowCnt++
				if nowCnt == avg {
					nowI++
					nowCnt = 0
					left--
					if left == 0 {
						avg++
						left = k - nAvg
					}
				}
			}
		}
		for i, row := range cells {
			if i&1 == 0 {
				for j, ch := range row {
					modify(i, j, ch == 'R')
				}
			} else {
				for j := c - 1; j >= 0; j-- {
					modify(i, j, cells[i][j] == 'R')
				}
			}
		}
		for _, row := range cells {
			Fprintf(out, "%s\n", row)
		}
	}
}

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

package main

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

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

	var t, r, c, k, avg, nAvg int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &r, &c, &k)
		cells := make([][]byte, r)
		cnt := 0
		for i := range cells {
			Fscan(in, &cells[i])
			cnt += bytes.Count(cells[i], []byte{'R'})
		}

		avg = cnt / k
		if cnt%k == 0 {
			nAvg = k
		} else {
			nAvg = (avg+1)*k - cnt
		}
		nowI := 0
		nowCnt := 0
		left := nAvg

		modify := func(i, j int, isRice bool) {
			cells[i][j] = alphabet[nowI]
			if isRice {
				nowCnt++
				if nowCnt == avg {
					nowI++
					nowCnt = 0
					left--
					if left == 0 {
						avg++
						left = k - nAvg
					}
				}
			}
		}
		for i, row := range cells {
			if i&1 == 0 {
				for j, ch := range row {
					modify(i, j, ch == 'R')
				}
			} else {
				for j := c - 1; j >= 0; j-- {
					modify(i, j, cells[i][j] == 'R')
				}
			}
		}
		for _, row := range cells {
			Fprintf(out, "%s\n", row)
		}
	}
}

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

package main

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

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

	var t, r, c, k, lastRi, lastRj, avg, nAvg int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &r, &c, &k)
		cells := make([][]byte, r)
		cnt := 0
		for i := range cells {
			Fscan(in, &cells[i])
			cntRow := bytes.Count(cells[i], []byte{'R'})
			if cntRow > 0 {
				cnt += cntRow
				lastRi, lastRj = i, bytes.LastIndex(cells[i], []byte{'R'})
			}
		}
		cells[lastRi][lastRj] = '.'
		cells[r-1][c-1] = 'R'

		avg = cnt / k
		if cnt%k == 0 {
			nAvg = k
		} else {
			nAvg = (avg+1)*k - cnt
		}
		nowI := 0
		nowCnt := 0
		left := nAvg

		modify := func(i, j int, isRice bool) {
			cells[i][j] = alphabet[nowI]
			if isRice {
				nowCnt++
				if nowCnt == avg {
					nowI++
					nowCnt = 0
					left--
					if left == 0 {
						avg++
						left = k - nAvg
					}
				}
			}
		}
		for i, row := range cells {
			if i&1 == 0 {
				for j, ch := range row {
					modify(i, j, ch == 'R')
				}
			} else {
				for j := c - 1; j >= 0; j-- {
					modify(i, j, cells[i][j] == 'R')
				}
			}
		}
		for _, row := range cells {
			Fprintf(out, "%s\n", row)
		}
	}
}

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


Simon has an array a1, a2, ..., an, consisting of n positive integers. Today Simon asked you to find a pair of integers l, r (1 ≤ l ≤ r ≤ n), such that the following conditions hold:  there is integer j (l ≤ j ≤ r), such that all integers al, al + 1, ..., ar are divisible by aj;  value r - l takes the maximum value among all pairs for which condition 1 is true; Help Simon, find the required pair of numbers (l, r). If there are multiple required pairs find all of them.

The first line contains integer n (1 ≤ n ≤ 3·105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 106).

Print two integers in the first line — the number of required pairs and the maximum value of r - l. On the following line print all l values from optimal pairs in increasing order.

NoteIn the first sample the pair of numbers is right, as numbers 6, 9, 3 are divisible by 3.In the second sample all numbers are divisible by number 1.In the third sample all numbers are prime, so conditions 1 and 2 are true only for pairs of numbers (1, 1), (2, 2), (3, 3), (4, 4), (5, 5).

package main

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

//Standart bufio.Scanner clone with no errors and
//with MaxTokenSize == 16777216 instead of 65536
type Scanner struct {
	r            io.Reader
	split        bufio.SplitFunc
	maxTokenSize int
	token, buf   []byte
	start, end   int
}

func NewScanner(r io.Reader) *Scanner {
	return &Scanner{
		r:            r,
		split:        bufio.ScanWords,
		maxTokenSize: 16777216,
		buf:          make([]byte, 4096),
	}
}

func (s *Scanner) advance(n int) bool {
	if n < 0 || n > s.end-s.start {
		return false
	}
	s.start += n
	return true
}

func (s *Scanner) Text() string {
	return string(s.token)
}

func (s *Scanner) Scan() bool {
	for {
		if s.end > s.start {
			advance, token, err := s.split(s.buf[s.start:s.end], false)
			if err != nil || !s.advance(advance) {
				return false
			}
			s.token = token
			if token != nil {
				return true
			}
		}
		if s.start > 0 && (s.end == len(s.buf) || s.start > len(s.buf)/2) {
			copy(s.buf, s.buf[s.start:s.end])
			s.end -= s.start
			s.start = 0
		}
		if s.end == len(s.buf) {
			if len(s.buf) >= s.maxTokenSize {
				return false
			}
			newSize := len(s.buf) * 2
			if newSize > s.maxTokenSize {
				newSize = s.maxTokenSize
			}
			newBuf := make([]byte, newSize)
			copy(newBuf, s.buf[s.start:s.end])
			s.buf = newBuf
			s.end -= s.start
			s.start = 0
			continue
		}
		for loop := 0; ; {
			n, _ := s.r.Read(s.buf[s.end:len(s.buf)])
			s.end += n
			if n > 0 {
				break
			}
			loop++
			if loop > 100 {
				break
			}
		}
	}
}

func (s *Scanner) Split(split bufio.SplitFunc) {
	s.split = split
}

var w *bufio.Writer = bufio.NewWriter(os.Stdout)
var sc *Scanner = NewScanner(os.Stdin)

func next() string { sc.Scan(); return sc.Text() }

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

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

func pri32(i ...int) {
	for _, ii := range i {
		w.WriteString(strconv.Itoa(ii))
		sp()
	}
}
func pri64(i ...int64) {
	for _, ii := range i {
		w.WriteString(strconv.FormatInt(ii, 10))
		sp()
	}
}
func sp()   { w.WriteByte(' ') }
func endl() { w.WriteByte('\n') }

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

type SparseTable struct {
	l     []int
	table [][]int
	Data  []int
}

func NewSparseTable(a []int) *SparseTable {
	t := &SparseTable{}
	n := len(a)
	t.Data = a
	t.l = make([]int, n+1)
	for i := 2; i <= n; i++ {
		t.l[i] = t.l[i>>1] + 1
	}

	t.table = make([][]int, t.l[n]+1)
	for i := 0; i < t.l[n]+1; i++ {
		t.table[i] = make([]int, n)
	}

	for i := 0; i < n; i++ {
		t.table[0][i] = a[i]
	}

	for k := uint(1); (1 << k) <= n; k++ {
		for i := 0; i+(1<<k) <= n; i++ {
			x := t.table[k-1][i]
			y := t.table[k-1][i+(1<<(k-1))]
			t.table[k][i] = gcd(x, y)
		}
	}
	return t
}

func (t *SparseTable) Query(i, j int) int {
	k := uint(t.l[j-i])
	x := t.table[k][i]
	y := t.table[k][j-(1<<k)+1]
	return gcd(x, y)
}

func main() {
	n := ni32()
	a := make([]int, n)
	f := make([]int, n)
	b := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = ni32()
	}
	gcdsparsetable := NewSparseTable(a)
	res := 0
	for i := 0; i < n; i++ {
		b[i] = sort.Search(i, func(j int) bool {
			return gcdsparsetable.Query(j, i) == a[i]
		})
		f[i] = i + sort.Search(n-i, func(j int) bool {
			return gcdsparsetable.Query(i, i+j) < a[i]
		}) - 1
		if res < f[i]-b[i] {
			res = f[i] - b[i]
		}
	}
	var ans, ans2 []int
	for i := 0; i < n; i++ {
		if f[i]-b[i] == res {
			ans = append(ans, b[i]+1)
		}
	}
	sort.Ints(ans)
	i := 0
	for i < len(ans) {
		ans2 = append(ans2, ans[i])
		j := i
		for ; j < len(ans) && ans[j] == ans[i]; j++ {
		}
		i = j
	}
	pri32(len(ans2), res)
	endl()
	pri32(ans2...)
	w.Flush()
}

package main

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

//Standart bufio.Scanner clone with no errors and
//with MaxTokenSize == 16777216 instead of 65536
type Scanner struct {
	r            io.Reader
	split        bufio.SplitFunc
	maxTokenSize int
	token, buf   []byte
	start, end   int
}

func NewScanner(r io.Reader) *Scanner {
	return &Scanner{
		r:            r,
		split:        bufio.ScanWords,
		maxTokenSize: 16777216,
		buf:          make([]byte, 4096),
	}
}

func (s *Scanner) advance(n int) bool {
	if n < 0 || n > s.end-s.start {
		return false
	}
	s.start += n
	return true
}

func (s *Scanner) Text() string {
	return string(s.token)
}

func (s *Scanner) Scan() bool {
	for {
		if s.end > s.start {
			advance, token, err := s.split(s.buf[s.start:s.end], false)
			if err != nil || !s.advance(advance) {
				return false
			}
			s.token = token
			if token != nil {
				return true
			}
		}
		if s.start > 0 && (s.end == len(s.buf) || s.start > len(s.buf)/2) {
			copy(s.buf, s.buf[s.start:s.end])
			s.end -= s.start
			s.start = 0
		}
		if s.end == len(s.buf) {
			if len(s.buf) >= s.maxTokenSize {
				return false
			}
			newSize := len(s.buf) * 2
			if newSize > s.maxTokenSize {
				newSize = s.maxTokenSize
			}
			newBuf := make([]byte, newSize)
			copy(newBuf, s.buf[s.start:s.end])
			s.buf = newBuf
			s.end -= s.start
			s.start = 0
			continue
		}
		for loop := 0; ; {
			n, _ := s.r.Read(s.buf[s.end:len(s.buf)])
			s.end += n
			if n > 0 {
				break
			}
			loop++
			if loop > 100 {
				break
			}
		}
	}
}

func (s *Scanner) Split(split bufio.SplitFunc) {
	s.split = split
}

var w *bufio.Writer = bufio.NewWriter(os.Stdout)
var sc *Scanner = NewScanner(os.Stdin)

func next() string { sc.Scan(); return sc.Text() }

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

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

func pri32(i ...int) {
	for _, ii := range i {
		w.WriteString(strconv.Itoa(ii))
		sp()
	}
}
func pri64(i ...int64) {
	for _, ii := range i {
		w.WriteString(strconv.FormatInt(ii, 10))
		sp()
	}
}
func sp()   { w.WriteByte(' ') }
func endl() { w.WriteByte('\n') }

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

func GetNeutralValue() int { return 0 }

func CombineValues(value1, value2 int) int {
	return gcd(value1, value2)
}

func CombineValueAndDelta(value int, delta int) int {
	return delta
}

//Segment Tree with no range modification
type STree []int

func NewSTree(n int) STree {
	tree := make([]int, n<<1)
	for i := 0; i < n<<1; i++ {
		tree[i] = GetNeutralValue()
	}
	return tree
}

func (tree STree) Modify(idx int, delta int) {
	idx += len(tree) >> 1
	tree[idx] = CombineValueAndDelta(tree[idx], delta)
	for ; idx > 1; idx = idx >> 1 {
		tree[idx>>1] = CombineValues(tree[idx], tree[idx^1])
	}
}

func (tree STree) Query(l, r int) int {
	query := GetNeutralValue()
	for l, r = l+len(tree)>>1, r+len(tree)>>1; l <= r; l, r = (l+1)>>1, (r-1)>>1 {
		if l&1 != 0 {
			query = CombineValues(query, tree[l])
		}
		if r&1 == 0 {
			query = CombineValues(query, tree[r])
		}
	}
	return query
}

func main() {
	n := ni32()
	a := make([]int, n)
	f := make([]int, n)
	b := make([]int, n)
	tree := NewSTree(n)
	for i := 0; i < n; i++ {
		a[i] = ni32()
		tree.Modify(i, a[i])
	}
	res := 0
	for i := 0; i < n; i++ {
		b[i] = sort.Search(i, func(j int) bool {
			return tree.Query(j, i) == a[i]
		})
		f[i] = i + sort.Search(n-i, func(j int) bool {
			return tree.Query(i, i+j) < a[i]
		}) - 1
		if res < f[i]-b[i] {
			res = f[i] - b[i]
		}
	}
	var ans []int
	for i := 0; i < n; i++ {
		if f[i]-b[i] == res {
			ans = append(ans, b[i]+1)
		}
	}
	pri32(len(ans), res)
	endl()
	pri32(ans...)
	w.Flush()
}


Mole is hungry again. He found one ant colony, consisting of n ants, ordered in a row. Each ant i (1 ≤ i ≤ n) has a strength si.In order to make his dinner more interesting, Mole organizes a version of «Hunger Games» for the ants. He chooses two numbers l and r (1 ≤ l ≤ r ≤ n) and each pair of ants with indices between l and r (inclusively) will fight. When two ants i and j fight, ant i gets one battle point only if si divides sj (also, ant j gets one battle point only if sj divides si). After all fights have been finished, Mole makes the ranking. An ant i, with vi battle points obtained, is going to be freed only if vi = r - l, or in other words only if it took a point in every fight it participated. After that, Mole eats the rest of the ants. Note that there can be many ants freed or even none.In order to choose the best sequence, Mole gives you t segments [li, ri] and asks for each of them how many ants is he going to eat if those ants fight.

The first line contains one integer n (1 ≤ n ≤ 105), the size of the ant colony.  The second line contains n integers s1, s2, ..., sn (1 ≤ si ≤ 109), the strengths of the ants.  The third line contains one integer t (1 ≤ t ≤ 105), the number of test cases.  Each of the next t lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n), describing one query.

Print to the standard output t lines. The i-th line contains number of ants that Mole eats from the segment [li, ri].

NoteIn the first test battle points for each ant are v = [4, 0, 2, 0, 2], so ant number 1 is freed. Mole eats the ants 2, 3, 4, 5.In the second test case battle points are v = [0, 2, 0, 2], so no ant is freed and all of them are eaten by Mole.In the third test case battle points are v = [2, 0, 2], so ants number 3 and 5 are freed. Mole eats only the ant 4.In the fourth test case battle points are v = [0, 1], so ant number 5 is freed. Mole eats the ant 4.

package main

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

var gcd, ll, rr, minv, cnt []int

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

func Gcd(a, b int) int {
	if b == 0 {
		return a
	} else {
		return Gcd(b, a%b)
	}
}

func BuildInternal(s []int, l, r, n int) {
	ll[n] = l
	rr[n] = r
	if l == r {
		gcd[n] = s[l]
		minv[n] = s[l]
		cnt[n] = 1
		return
	}
	var mid int = (l + r) / 2
	BuildInternal(s, l, mid, n*2)
	BuildInternal(s, mid+1, r, n*2+1)
	if minv[n*2] < minv[n*2+1] {
		cnt[n] = cnt[n*2]
	} else if minv[n*2] > minv[n*2+1] {
		cnt[n] = cnt[n*2+1]
	} else {
		cnt[n] = cnt[n*2] + cnt[n*2+1]
	}
	minv[n] = IntMin(minv[n*2], minv[n*2+1])
	gcd[n] = Gcd(gcd[n*2], gcd[n*2+1])
}

func Build(n int, s []int) {
	gcd = make([]int, n*4)
	ll = make([]int, n*4)
	rr = make([]int, n*4)
	minv = make([]int, n*4)
	cnt = make([]int, n*4)
	BuildInternal(s, 0, n-1, 1)
}

func Query(l, r, n int) (_gcd, _minv, _cnt int) {
	if l == ll[n] && r == rr[n] {
		_gcd = gcd[n]
		_minv = minv[n]
		_cnt = cnt[n]
		return _gcd, _minv, _cnt
	}
	mid := (ll[n] + rr[n]) / 2
	if r <= mid {
		return Query(l, r, n*2)
	} else if l > mid {
		return Query(l, r, n*2+1)
	} else {
		gl, ml, cl := Query(l, mid, n*2)
		gr, mr, cr := Query(mid+1, r, n*2+1)
		_gcd = Gcd(gl, gr)
		if ml < mr {
			_minv = ml
			_cnt = cl
		} else if ml > mr {
			_minv = mr
			_cnt = cr
		} else {
			_minv = mr
			_cnt = cl + cr
		}
		return _gcd, _minv, _cnt
	}
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	var n, t, l, r int
	fmt.Fscan(reader, &n)
	strength := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &strength[i])
	}
	Build(n, strength)
	fmt.Fscan(reader, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(reader, &l, &r)
		l -= 1
		r -= 1
		_gcd, _minv, _cnt := Query(l, r, 1)
		if _gcd == _minv {
			fmt.Fprintln(writer, r-l+1-_cnt)
		} else {
			fmt.Fprintln(writer, r-l+1)
		}
	}
	writer.Flush()
}

package main

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

var gcd, ll, rr, minv []int

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

func Gcd(a, b int) int {
	if b == 0 {
		return a
	} else {
		return Gcd(b, a%b)
	}
}

func BuildInternal(s []int, l, r, n int) {
	ll[n] = l
	rr[n] = r
	if l == r {
		gcd[n] = s[l]
		minv[n] = s[l]
		return
	}
	var mid int = (l + r) / 2
	BuildInternal(s, l, mid, n*2)
	BuildInternal(s, mid+1, r, n*2+1)
	minv[n] = IntMin(minv[n*2], minv[n*2+1])
	gcd[n] = Gcd(gcd[n*2], gcd[n*2+1])
}

func Build(n int, s []int) {
	gcd = make([]int, n*4)
	ll = make([]int, n*4)
	rr = make([]int, n*4)
	minv = make([]int, n*4)
	BuildInternal(s, 0, n-1, 1)
}

func Query(l, r, n int) (_gcd, _minv, _cnt int) {
	if l == ll[n] && r == rr[n] {
		_gcd = gcd[n]
		_minv = minv[n]
		_cnt = 1
		return _gcd, _minv, _cnt
	}
	mid := (ll[n] + rr[n]) / 2
	if r <= mid {
		return Query(l, r, n*2)
	} else if l > mid {
		return Query(l, r, n*2+1)
	} else {
		gl, ml, cl := Query(l, mid, n*2)
		gr, mr, cr := Query(mid+1, r, n*2+1)
		_gcd = Gcd(gl, gr)
		if ml < mr {
			_minv = ml
			_cnt = cl
		} else if ml > mr {
			_minv = mr
			_cnt = cr
		} else {
			_minv = mr
			_cnt = cl + cr
		}
		return _gcd, _minv, _cnt
	}
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	var n, t, l, r int
	fmt.Fscan(reader, &n)
	strength := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &strength[i])
	}
	Build(n, strength)
	fmt.Fscan(reader, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(reader, &l, &r)
		l -= 1
		r -= 1
		_gcd, _minv, _cnt := Query(l, r, 1)
		if _gcd == _minv {
			fmt.Fprintln(writer, r-l+1-_cnt)
		} else {
			fmt.Fprintln(writer, r-l+1)
		}
	}
	writer.Flush()
}


Ivan is playing yet another roguelike computer game. He controls a single hero in the game. The hero has $$$n$$$ equipment slots. There is a list of $$$c_i$$$ items for the $$$i$$$-th slot, the $$$j$$$-th of them increases the hero strength by $$$a_{i,j}$$$. The items for each slot are pairwise distinct and are listed in the increasing order of their strength increase. So, $$$a_{i,1} < a_{i,2} < \dots < a_{i,c_i}$$$.For each slot Ivan chooses exactly one item. Let the chosen item for the $$$i$$$-th slot be the $$$b_i$$$-th item in the corresponding list. The sequence of choices $$$[b_1, b_2, \dots, b_n]$$$ is called a build.The strength of a build is the sum of the strength increases of the items in it. Some builds are banned from the game. There is a list of $$$m$$$ pairwise distinct banned builds. It's guaranteed that there's at least one build that's not banned.What is the build with the maximum strength that is not banned from the game? If there are multiple builds with maximum strength, print any of them.

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10$$$) — the number of equipment slots. The $$$i$$$-th of the next $$$n$$$ lines contains the description of the items for the $$$i$$$-th slot. First, one integer $$$c_i$$$ ($$$1 \le c_i \le 2 \cdot 10^5$$$) — the number of items for the $$$i$$$-th slot. Then $$$c_i$$$ integers $$$a_{i,1}, a_{i,2}, \dots, a_{i,c_i}$$$ ($$$1 \le a_{i,1} < a_{i,2} < \dots < a_{i,c_i} \le 10^8$$$). The sum of $$$c_i$$$ doesn't exceed $$$2 \cdot 10^5$$$. The next line contains a single integer $$$m$$$ ($$$0 \le m \le 10^5$$$) — the number of banned builds. Each of the next $$$m$$$ lines contains a description of a banned build — a sequence of $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le c_i$$$). The builds are pairwise distinct, and there's at least one build that's not banned.

Print the build with the maximum strength that is not banned from the game. If there are multiple builds with maximum strength, print any of them.

package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
)

type item struct {
	item  Item
	index int
}

type heapImpl struct {
	p               *pq
	items           map[interface{}]*item
	key             func(Item) interface{}
	DisableIndexing bool
}

func (h *heapImpl) Push(i Item) {
	it := &item{i, 0}
	h.addItem(it)
	heap.Push(h.p, it)
}

func (h *heapImpl) Pop() Item {
	res := heap.Pop(h.p).(*item)
	h.removeItem(res.item)
	return res.item
}

func (h *heapImpl) Top() Item {
	if len(h.p.items) > 0 {
		return h.p.items[0].item
	}
	return nil
}

func (h *heapImpl) Len() int {
	return len(h.p.items)
}

func (h *heapImpl) Update(o Item, n Item) {
	old := h.items[h.key(o)]
	new := &item{n, old.index}
	h.removeItem(old.item)
	h.p.items[old.index] = new
	heap.Fix(h.p, old.index)
	h.addItem(new)
}

func (h *heapImpl) Remove(i Item) {
	index := h.items[h.key(i)].index
	h.removeItem(i)
	heap.Remove(h.p, index)
}

func (h *heapImpl) Get(key interface{}) Item {
	return h.items[key].item
}

func (h *heapImpl) removeItem(item Item) {
	if !h.DisableIndexing {
		delete(h.items, h.key(item))
	}
}

func (h *heapImpl) addItem(item *item) {
	if !h.DisableIndexing {
		h.items[h.key(item.item)] = item
	}
}

// A PriorityQueue implements heap.Interface and holds Items.
type pq struct {
	items []*item
}

func (pq pq) Len() int { return len(pq.items) }

func (pq pq) Less(i, j int) bool {
	// We want Pop to give us the highest, not lowest, priority so we use greater than here.
	return pq.items[i].item.Compare(pq.items[j].item)
}

func (pq pq) Swap(i, j int) {
	pq.items[i], pq.items[j] = pq.items[j], pq.items[i]
	pq.items[i].index = i
	pq.items[j].index = j
}

func (pq *pq) Push(x interface{}) {
	n := len(pq.items)
	item := x.(*item)
	item.index = n
	pq.items = append(pq.items, item)
}

func (pq *pq) Pop() interface{} {
	old := pq.items
	n := len(old)
	item := old[n-1]
	old[n-1] = nil  // avoid memory leak
	item.index = -1 // for safety
	pq.items = old[0 : n-1]
	return item
}

func NewHeapWithKey(getKey func(Item) interface{}) Heap {
	return &heapImpl{
		&pq{
			[]*item{},
		},
		make(map[interface{}]*item),
		getKey,
		true,
	}
}

func NewHeap() Heap {
	return NewHeapWithKey(func(i Item) interface{} { return i })
}

type Item interface {
	Compare(left Item) bool
}

type Heap interface {
	Push(i Item)
	Pop() Item
	Top() Item
	Len() int
	Update(old Item, new Item)
	Remove(item Item)
	Get(key interface{}) Item
}

type MinInt int

func (i MinInt) Compare(j Item) bool {
	return i < j.(MinInt)
}

type MaxInt int

func (i MaxInt) Compare(j Item) bool {
	return i > j.(MaxInt)
}

type idx struct {
	value int
	arr   [10]int
}

func (left *idx) Compare(right Item) bool {
	return left.value > right.(*idx).value
}

func main() {
	r := bufio.NewReaderSize(os.Stdin, 8*1024*1024)

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

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

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

	seen := make(map[idx]bool)
	for i := 0; i < b; i++ {
		cur := idx{}
		for j := 0; j < n; j++ {
			fmt.Fscanf(r, "%d", &cur.arr[j])
			cur.arr[j]--
			cur.value += arr[j][cur.arr[j]]
		}
		fmt.Fscanf(r, "\n")
		seen[cur] = true
	}

	cur := idx{0, [10]int{}}
	for i := 0; i < n; i++ {
		cur.arr[i] = len(arr[i]) - 1
		cur.value += arr[i][cur.arr[i]]
	}

	h := NewHeap()
	h.Push(&cur)

	alloc := [1001000]idx{}
	a := 0

	used := make(map[idx]bool)

	for {
		cur = *h.Pop().(*idx)
		if !seen[cur] {
			break
		}
		for i := 0; i < n; i++ {
			if cur.arr[i] == 0 {
				continue
			}

			next := &alloc[a]
			a++

			next.arr = cur.arr
			next.value = cur.value - arr[i][cur.arr[i]] + arr[i][cur.arr[i]-1]
			next.arr[i]--

			if !used[*next] {
				used[*next] = true
				h.Push(next)
			}
		}
	}

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

package main

import (
	"bufio"
	"os"
	"fmt"
	"math/rand"
	"time"

	"container/heap"
	"strconv"
)

var n int

const MAX = 1e5 + 5

const MOD int64 = 1e9 + 7

var a map[int][]int

var m int

var banned [][]int

var garb []int

var w *bufio.Writer

const WRITE = false

type pair struct {
	v int64
	id int
}

type byV []*pair

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

type Item struct {
	priority int
	index int
	idx int
	idxElem int
}

// A PriorityQueue implements heap.Interface and holds Items.
type PriorityQueue []*Item

func (pq PriorityQueue) Len() int { return len(pq) }

func (pq PriorityQueue) Less(i, j int) bool {
	return pq[i].priority > pq[j].priority
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = i
	pq[j].index = j
}

func (pq *PriorityQueue) Push(x interface{}) {
	n := len(*pq)
	item := x.(*Item)
	item.index = n
	*pq = append(*pq, item)
}

func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	old[n-1] = nil  // avoid memory leak
	item.index = -1 // for safety
	*pq = old[0 : n-1]
	return item
}

type value struct {
	value int
	indexes []int
}

func main() {
	f, _ := os.OpenFile("D:\\GO\\src\\codeforces\\edu114\\4\\in.txt", os.O_RDWR, 0)
	defer f.Close()

	if WRITE {
		f.WriteString("2\n")

		f.WriteString("114239 ")

		for i := 1; i <= 114239; i++ {
			f.WriteString(strconv.Itoa(i))
			f.WriteString(" ")
		}
		f.WriteString("\n")

		f.WriteString("85761 ")

		for i := 1; i <= 85761; i++ {
			f.WriteString(strconv.Itoa(i))
			f.WriteString(" ")
		}
		f.WriteString("\n")

		f.WriteString("0")
		return
	}

	rand.Seed(time.Now().UnixNano())

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

	rand.Seed(time.Now().UnixNano())

	fmt.Fscan(r, &n)

	garb = make([]int, n)

	for i := 0; i < n; i++ {
		garb[i] =  rand.Intn(1e6)
	}

	a = make(map[int][]int)

	for j := 0; j < n; j++ {
		var c int
		fmt.Fscan(r, &c)

		cc := make([]int, c)

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

	fmt.Fscan(r, &m)

	banned = make([][]int, m)

	for j := 0; j < m ; j++ {
		banned[j] = make([]int, n)

		for k := 0; k < n; k++ {
			var v int
			fmt.Fscan(r, &v)
			v--
			banned[j][k] = v
		}
	}

	solve()

}

func solve()  {
	hashes := make(map[int64][]int)

	for i := 0; i < m; i++ {
		b := banned[i]

		h := hash(b)

		hashes[h] = append(hashes[h], i)
	}

	var arr []*value

	a0 := a[0]
	for i := 0; i < len(a0); i++ {
		var indexes []int
		indexes = append(indexes, i)
		arr = append(arr, &value{a0[i], indexes})
	}

	for i := 1; i < n; i ++ {
		slot := a[i]

		biggest := arr[len(arr) - 1].value

		var pq PriorityQueue
		heap.Init(&pq)

		for j := len(slot) - 1; j >= 0; j-- {
			heap.Push(&pq, &Item{priority: slot[j] + biggest, idx: len(arr) - 1, idxElem : j})
		}

		var temp []*value

		counter := 0

		for ;pq.Len()  > 0 && counter <= int(2e5) + 100; {

			it := heap.Pop(&pq).(*Item)
			elemIndex := it.idxElem

			idx := it.idx

			indexes := arr[idx].indexes

			var elems []int

			for i := 0; i < len(indexes); i++ {
				elems = append(elems, indexes[i])
			}

			elems = append(elems, elemIndex)

			temp = append(temp, &value{value: slot[elemIndex] + arr[idx].value, indexes: elems})

			counter++

			if idx > 0 {
				t := idx - 1

				heap.Push(&pq, &Item{priority: slot[elemIndex] + arr[t].value, idx: t, idxElem : elemIndex})
			}
		}

		idx := len(temp) - 1

		for i := 0; i < len(arr); i++ {
			arr[i] = temp[idx]
			idx--
		}

		for j := idx; j >= 0; j-- {
			arr = append(arr, temp[j])
		}
	}

	for i := len(arr) - 1; i >= 0; i-- {
		e := arr[i]
		indexes := e.indexes

		h := hash(indexes)

		if _, ok := hashes[h]; !ok {

			for j := 0; j < len(indexes); j++ {
				fmt.Print(indexes[j] + 1)
				fmt.Print(" ")
			}

			fmt.Println()
			return
		}
	}
}

func hash(indexes []int) int64 {
	var ans int64

	arr := make([]int, n)

	for j := 0; j < len(indexes); j++ {
		slot := a[j]
		index := indexes[j]

		arr[j] = slot[index]
	}

	for i := 0; i < len(arr); i++ {

		pos := i + 1

		var temp = int64(pos) * int64(arr[i]) * int64(garb[i])

		ans ^= temp
	}

	return ans % MOD
}

package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
)

type Item interface {
	Compare(left Item) bool
}

type Heap interface {
	Push(i Item)
	Pop() Item
	Top() Item
	Len() int
	Update(old Item, new Item)
	Remove(item Item)
	Get(key interface{}) Item
}

type MinInt int

func (i MinInt) Compare(j Item) bool {
	return i < j.(MinInt)
}

type MaxInt int

func (i MaxInt) Compare(j Item) bool {
	return i > j.(MaxInt)
}

type item struct {
	item  Item
	index int
}

type heapImpl struct {
	p               *pq
	items           map[interface{}]*item
	key             func(Item) interface{}
	DisableIndexing bool
}

func (h *heapImpl) Push(i Item) {
	it := &item{i, 0}
	h.addItem(it)
	heap.Push(h.p, it)
}

func (h *heapImpl) Pop() Item {
	res := heap.Pop(h.p).(*item)
	h.removeItem(res.item)
	return res.item
}

func (h *heapImpl) Top() Item {
	if len(h.p.items) > 0 {
		return h.p.items[0].item
	}
	return nil
}

func (h *heapImpl) Len() int {
	return len(h.p.items)
}

func (h *heapImpl) Update(o Item, n Item) {
	old := h.items[h.key(o)]
	new := &item{n, old.index}
	h.removeItem(old.item)
	h.p.items[old.index] = new
	heap.Fix(h.p, old.index)
	h.addItem(new)
}

func (h *heapImpl) Remove(i Item) {
	index := h.items[h.key(i)].index
	h.removeItem(i)
	heap.Remove(h.p, index)
}

func (h *heapImpl) Get(key interface{}) Item {
	return h.items[key].item
}

func (h *heapImpl) removeItem(item Item) {
	if !h.DisableIndexing {
		delete(h.items, h.key(item))
	}
}

func (h *heapImpl) addItem(item *item) {
	if !h.DisableIndexing {
		h.items[h.key(item.item)] = item
	}
}

// A PriorityQueue implements heap.Interface and holds Items.
type pq struct {
	items []*item
}

func (pq pq) Len() int { return len(pq.items) }

func (pq pq) Less(i, j int) bool {
	// We want Pop to give us the highest, not lowest, priority so we use greater than here.
	return pq.items[i].item.Compare(pq.items[j].item)
}

func (pq pq) Swap(i, j int) {
	pq.items[i], pq.items[j] = pq.items[j], pq.items[i]
	pq.items[i].index = i
	pq.items[j].index = j
}

func (pq *pq) Push(x interface{}) {
	n := len(pq.items)
	item := x.(*item)
	item.index = n
	pq.items = append(pq.items, item)
}

func (pq *pq) Pop() interface{} {
	old := pq.items
	n := len(old)
	item := old[n-1]
	old[n-1] = nil  // avoid memory leak
	item.index = -1 // for safety
	pq.items = old[0 : n-1]
	return item
}

func NewHeapWithKey(getKey func(Item) interface{}) Heap {
	return &heapImpl{
		p:               &pq{
			items: []*item{},
		},
		items:           make(map[interface{}]*item),
		key:             getKey,
		DisableIndexing: true,
	}
}

func NewHeap() Heap {
	return NewHeapWithKey(func(i Item) interface{} { return i })
}

type idx struct {
	value int
	arr   [10]int
}

func (left *idx) Compare(right Item) bool {
	return left.value > right.(*idx).value
}

func main() {
	r := bufio.NewReaderSize(os.Stdin, 8*1024*1024)

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

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

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

	seen := make(map[idx]bool)
	for i := 0; i < b; i++ {
		cur := idx{}
		for j := 0; j < n; j++ {
			fmt.Fscanf(r, "%d", &cur.arr[j])
			cur.arr[j]--
			cur.value += arr[j][cur.arr[j]]
		}
		fmt.Fscanf(r, "\n")
		seen[cur] = true
	}

	cur := idx{0, [10]int{}}
	for i := 0; i < n; i++ {
		cur.arr[i] = len(arr[i]) - 1
		cur.value += arr[i][cur.arr[i]]
	}

	h := NewHeap()
	h.Push(&cur)

	//alloc := [1001000]idx{}
	//a := 0

	used := make(map[idx]bool)

	for ; h.Len() > 0; {
		cur = *h.Pop().(*idx)
		if !seen[cur] {
			break
		}

		for i := 0; i < n; i++ {
			if cur.arr[i] == 0 {
				continue
			}
			next := cur
			next.arr = cur.arr
			next.value = cur.value - arr[i][cur.arr[i]] + arr[i][cur.arr[i]-1]
			next.arr[i]--

			if !used[next] {
				used[next] = true
				h.Push(&next)
			}
		}
	}

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

package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
)

type item struct {
	item  Item
	index int
}

type heapImpl struct {
	p               *pq
	items           map[interface{}]*item
	key             func(Item) interface{}
	DisableIndexing bool
}

func (h *heapImpl) Push(i Item) {
	it := &item{i, 0}
	h.addItem(it)
	heap.Push(h.p, it)
}

func (h *heapImpl) Pop() Item {
	res := heap.Pop(h.p).(*item)
	h.removeItem(res.item)
	return res.item
}

func (h *heapImpl) Top() Item {
	if len(h.p.items) > 0 {
		return h.p.items[0].item
	}
	return nil
}

func (h *heapImpl) Len() int {
	return len(h.p.items)
}

func (h *heapImpl) Update(o Item, n Item) {
	old := h.items[h.key(o)]
	new := &item{n, old.index}
	h.removeItem(old.item)
	h.p.items[old.index] = new
	heap.Fix(h.p, old.index)
	h.addItem(new)
}

func (h *heapImpl) Remove(i Item) {
	index := h.items[h.key(i)].index
	h.removeItem(i)
	heap.Remove(h.p, index)
}

func (h *heapImpl) Get(key interface{}) Item {
	return h.items[key].item
}

func (h *heapImpl) removeItem(item Item) {
	if !h.DisableIndexing {
		delete(h.items, h.key(item))
	}
}

func (h *heapImpl) addItem(item *item) {
	if !h.DisableIndexing {
		h.items[h.key(item.item)] = item
	}
}

// A PriorityQueue implements heap.Interface and holds Items.
type pq struct {
	items []*item
}

func (pq pq) Len() int { return len(pq.items) }

func (pq pq) Less(i, j int) bool {
	// We want Pop to give us the highest, not lowest, priority so we use greater than here.
	return pq.items[i].item.Compare(pq.items[j].item)
}

func (pq pq) Swap(i, j int) {
	pq.items[i], pq.items[j] = pq.items[j], pq.items[i]
	pq.items[i].index = i
	pq.items[j].index = j
}

func (pq *pq) Push(x interface{}) {
	n := len(pq.items)
	item := x.(*item)
	item.index = n
	pq.items = append(pq.items, item)
}

func (pq *pq) Pop() interface{} {
	old := pq.items
	n := len(old)
	item := old[n-1]
	old[n-1] = nil  // avoid memory leak
	item.index = -1 // for safety
	pq.items = old[0 : n-1]
	return item
}

func NewHeapWithKey(getKey func(Item) interface{}) Heap {
	return &heapImpl{
		&pq{
			[]*item{},
		},
		make(map[interface{}]*item),
		getKey,
		true,
	}
}

func NewHeap() Heap {
	return NewHeapWithKey(func(i Item) interface{} { return i })
}

type Item interface {
	Compare(left Item) bool
}

type Heap interface {
	Push(i Item)
	Pop() Item
	Top() Item
	Len() int
	Update(old Item, new Item)
	Remove(item Item)
	Get(key interface{}) Item
}

type MinInt int

func (i MinInt) Compare(j Item) bool {
	return i < j.(MinInt)
}

type MaxInt int

func (i MaxInt) Compare(j Item) bool {
	return i > j.(MaxInt)
}

type idx struct {
	value int
	arr   [10]int
}

func (left *idx) Compare(right Item) bool {
	return left.value > right.(*idx).value
}

func main() {
	r := bufio.NewReaderSize(os.Stdin, 8*1024*1024)

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

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

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

	seen := make(map[idx]bool)
	for i := 0; i < b; i++ {
		cur := idx{}
		for j := 0; j < n; j++ {
			fmt.Fscanf(r, "%d", &cur.arr[j])
			cur.arr[j]--
			cur.value += arr[j][cur.arr[j]]
		}
		fmt.Fscanf(r, "\n")
		seen[cur] = true
	}

	cur := idx{0, [10]int{}}
	for i := 0; i < n; i++ {
		cur.arr[i] = len(arr[i]) - 1
		cur.value += arr[i][cur.arr[i]]
	}

	h := NewHeap()
	h.Push(&cur)

	alloc := [2000000]idx{}
	a := 0

	for {
		cur = *h.Pop().(*idx)
		if !seen[cur] {
			break
		}
		for i := 0; i < n; i++ {
			if cur.arr[i] == 0 {
				continue
			}
			if h.Len() > 200000 || a >= len(alloc) {
				continue
			}

			next := &alloc[a]
			a++

			next.arr = cur.arr
			next.value = cur.value - arr[i][cur.arr[i]] + arr[i][cur.arr[i]-1]
			next.arr[i]--

			h.Push(next)
		}
	}

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

package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
)

type item struct {
	item  Item
	index int
}

type heapImpl struct {
	p               *pq
	items           map[interface{}]*item
	key             func(Item) interface{}
	DisableIndexing bool
}

func (h *heapImpl) Push(i Item) {
	it := &item{i, 0}
	h.addItem(it)
	heap.Push(h.p, it)
}

func (h *heapImpl) Pop() Item {
	res := heap.Pop(h.p).(*item)
	h.removeItem(res.item)
	return res.item
}

func (h *heapImpl) Top() Item {
	if len(h.p.items) > 0 {
		return h.p.items[0].item
	}
	return nil
}

func (h *heapImpl) Len() int {
	return len(h.p.items)
}

func (h *heapImpl) Update(o Item, n Item) {
	old := h.items[h.key(o)]
	new := &item{n, old.index}
	h.removeItem(old.item)
	h.p.items[old.index] = new
	heap.Fix(h.p, old.index)
	h.addItem(new)
}

func (h *heapImpl) Remove(i Item) {
	index := h.items[h.key(i)].index
	h.removeItem(i)
	heap.Remove(h.p, index)
}

func (h *heapImpl) Get(key interface{}) Item {
	return h.items[key].item
}

func (h *heapImpl) removeItem(item Item) {
	if !h.DisableIndexing {
		delete(h.items, h.key(item))
	}
}

func (h *heapImpl) addItem(item *item) {
	if !h.DisableIndexing {
		h.items[h.key(item.item)] = item
	}
}

// A PriorityQueue implements heap.Interface and holds Items.
type pq struct {
	items []*item
}

func (pq pq) Len() int { return len(pq.items) }

func (pq pq) Less(i, j int) bool {
	// We want Pop to give us the highest, not lowest, priority so we use greater than here.
	return pq.items[i].item.Compare(pq.items[j].item)
}

func (pq pq) Swap(i, j int) {
	pq.items[i], pq.items[j] = pq.items[j], pq.items[i]
	pq.items[i].index = i
	pq.items[j].index = j
}

func (pq *pq) Push(x interface{}) {
	n := len(pq.items)
	item := x.(*item)
	item.index = n
	pq.items = append(pq.items, item)
}

func (pq *pq) Pop() interface{} {
	old := pq.items
	n := len(old)
	item := old[n-1]
	old[n-1] = nil  // avoid memory leak
	item.index = -1 // for safety
	pq.items = old[0 : n-1]
	return item
}

func NewHeapWithKey(getKey func(Item) interface{}) Heap {
	return &heapImpl{
		&pq{
			[]*item{},
		},
		make(map[interface{}]*item),
		getKey,
		true,
	}
}

func NewHeap() Heap {
	return NewHeapWithKey(func(i Item) interface{} { return i })
}

type Item interface {
	Compare(left Item) bool
}

type Heap interface {
	Push(i Item)
	Pop() Item
	Top() Item
	Len() int
	Update(old Item, new Item)
	Remove(item Item)
	Get(key interface{}) Item
}

type MinInt int

func (i MinInt) Compare(j Item) bool {
	return i < j.(MinInt)
}

type MaxInt int

func (i MaxInt) Compare(j Item) bool {
	return i > j.(MaxInt)
}

type idx struct {
	value int
	arr   [10]int
}

func (left *idx) Compare(right Item) bool {
	return left.value > right.(*idx).value
}

func main() {
	r := bufio.NewReaderSize(os.Stdin, 8*1024*1024)

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

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

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

	seen := make(map[idx]bool)
	for i := 0; i < b; i++ {
		cur := idx{}
		for j := 0; j < n; j++ {
			fmt.Fscanf(r, "%d", &cur.arr[j])
			cur.arr[j]--
			cur.value += arr[j][cur.arr[j]]
		}
		fmt.Fscanf(r, "\n")
		seen[cur] = true
	}

	cur := idx{0, [10]int{}}
	for i := 0; i < n; i++ {
		cur.arr[i] = len(arr[i]) - 1
		cur.value += arr[i][cur.arr[i]]
	}

	h := NewHeap()
	h.Push(&cur)

	alloc := [2000000]idx{}
	a := 0

	for {
		cur = *h.Pop().(*idx)
		if !seen[cur] {
			break
		}
		for i := 0; i < n; i++ {
			if cur.arr[i] == 0 {
				continue
			}
			if h.Len() > 100000 || a >= len(alloc) {
				continue
			}

			next := &alloc[a]
			a++

			next.arr = cur.arr
			next.value = cur.value - arr[i][cur.arr[i]] + arr[i][cur.arr[i]-1]
			next.arr[i]--

			h.Push(next)
		}
	}

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

package main

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

type idx struct {
	arr [10]int
}

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

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

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

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

	seen := make(map[idx]bool)
	for i := 0; i < b; i++ {
		cur := idx{[10]int{}}
		for j := 0; j < n; j++ {
			fmt.Fscanf(r, "%d", &cur.arr[j])
			cur.arr[j]--
		}
		fmt.Fscanf(r, "\n")
		seen[cur] = true
	}

	cur := idx{[10]int{}}
	for i := 0; i < n; i++ {
		cur.arr[i] = len(arr[i]) - 1
	}

	for seen[cur] {
		bi := -1
		for i := 0; i < n; i++ {
			if cur.arr[i] > 0 && (bi == -1 ||
				(arr[i][cur.arr[i]]-arr[i][cur.arr[i]-1]) <
					(arr[bi][cur.arr[bi]]-arr[bi][cur.arr[bi]-1])) {
				bi = i
			}
		}
		//fmt.Println(bi, arr[bi][cur.arr[bi] ])
		cur.arr[bi]--
	}

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

package main

import (
	"bufio"
	"os"
	"fmt"
	"math/rand"
	"time"

	"container/heap"
	"strconv"
)

var n int

const MAX = 1e5 + 5

const MOD int64 = 1e9 + 7

var a map[int][]int

var m int

var banned [][]int

var garb []int

var w *bufio.Writer

const WRITE = false

type pair struct {
	v int64
	id int
}

type byV []*pair

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

type Item struct {
	priority int
	index int
	idx int
	idxElem int
}

// A PriorityQueue implements heap.Interface and holds Items.
type PriorityQueue []*Item

func (pq PriorityQueue) Len() int { return len(pq) }

func (pq PriorityQueue) Less(i, j int) bool {
	return pq[i].priority > pq[j].priority
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = i
	pq[j].index = j
}

func (pq *PriorityQueue) Push(x interface{}) {
	n := len(*pq)
	item := x.(*Item)
	item.index = n
	*pq = append(*pq, item)
}

func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	old[n-1] = nil  // avoid memory leak
	item.index = -1 // for safety
	*pq = old[0 : n-1]
	return item
}

type value struct {
	value int
	indexes []int
}

func main() {
	f, _ := os.OpenFile("D:\\GO\\src\\codeforces\\edu114\\4\\in.txt", os.O_RDWR, 0)
	defer f.Close()

	if WRITE {
		f.WriteString("2\n")

		f.WriteString("114239 ")

		for i := 1; i <= 114239; i++ {
			f.WriteString(strconv.Itoa(i))
			f.WriteString(" ")
		}
		f.WriteString("\n")

		f.WriteString("85761 ")

		for i := 1; i <= 85761; i++ {
			f.WriteString(strconv.Itoa(i))
			f.WriteString(" ")
		}
		f.WriteString("\n")

		f.WriteString("0")
		return
	}

	rand.Seed(time.Now().UnixNano())

	r := bufio.NewReader(f)
	w = bufio.NewWriter(os.Stdout)

	rand.Seed(time.Now().UnixNano())

	fmt.Fscan(r, &n)

	garb = make([]int, n)

	for i := 0; i < n; i++ {
		garb[i] =  rand.Intn(1e6)
	}

	a = make(map[int][]int)

	for j := 0; j < n; j++ {
		var c int
		fmt.Fscan(r, &c)

		cc := make([]int, c)

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

	fmt.Fscan(r, &m)

	banned = make([][]int, m)

	for j := 0; j < m ; j++ {
		banned[j] = make([]int, n)

		for k := 0; k < n; k++ {
			var v int
			fmt.Fscan(r, &v)
			v--
			banned[j][k] = v
		}
	}

	solve()

}

func solve()  {
	hashes := make(map[int64][]int)

	for i := 0; i < m; i++ {
		b := banned[i]

		h := hash(b)

		hashes[h] = append(hashes[h], i)
	}

	var arr []*value

	a0 := a[0]
	for i := 0; i < len(a0); i++ {
		var indexes []int
		indexes = append(indexes, i)
		arr = append(arr, &value{a0[i], indexes})
	}

	for i := 1; i < n; i ++ {
		slot := a[i]

		biggest := arr[len(arr) - 1].value

		var pq PriorityQueue
		heap.Init(&pq)

		for j := len(slot) - 1; j >= 0; j-- {
			heap.Push(&pq, &Item{priority: slot[j] + biggest, idx: len(arr) - 1, idxElem : j})
		}

		var temp []*value

		counter := 0

		for ;pq.Len()  > 0 && counter <= int(2e5) + 100; {

			it := heap.Pop(&pq).(*Item)
			elemIndex := it.idxElem

			idx := it.idx

			indexes := arr[idx].indexes

			var elems []int

			for i := 0; i < len(indexes); i++ {
				elems = append(elems, indexes[i])
			}

			elems = append(elems, elemIndex)

			temp = append(temp, &value{value: slot[elemIndex] + arr[idx].value, indexes: elems})

			counter++

			if idx > 0 {
				t := idx - 1

				heap.Push(&pq, &Item{priority: slot[elemIndex] + arr[t].value, idx: t, idxElem : elemIndex})
			}
		}

		idx := len(temp) - 1

		for i := 0; i < len(arr); i++ {
			arr[i] = temp[idx]
			idx--
		}

		for j := idx; j >= 0; j-- {
			arr = append(arr, temp[j])
		}
	}

	for i := len(arr) - 1; i >= 0; i-- {
		e := arr[i]
		indexes := e.indexes

		h := hash(indexes)

		if _, ok := hashes[h]; !ok {

			for j := 0; j < len(indexes); j++ {
				fmt.Print(indexes[j] + 1)
				fmt.Print(" ")
			}

			fmt.Println()
			return
		}
	}
}

func hash(indexes []int) int64 {
	var ans int64

	arr := make([]int, n)

	for j := 0; j < len(indexes); j++ {
		slot := a[j]
		index := indexes[j]

		arr[j] = slot[index]
	}

	for i := 0; i < len(arr); i++ {

		pos := i + 1

		var temp = int64(pos) * int64(arr[i]) * int64(garb[i])

		ans ^= temp
	}

	return ans % MOD
}

package main

import (
	"bufio"
	"os"
	"fmt"
	"math/rand"
	"time"

	"container/heap"
)

var n int

const MAX = 1e5 + 5

const MOD int64 = 1e9 + 7

var a map[int][]int

var m int

var banned [][]int

var garb []int

type pair struct {
	v int64
	id int
}

type byV []*pair

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

type Item struct {
	priority int
	index int
	idx int
	idxElem int
}

// A PriorityQueue implements heap.Interface and holds Items.
type PriorityQueue []*Item

func (pq PriorityQueue) Len() int { return len(pq) }

func (pq PriorityQueue) Less(i, j int) bool {
	return pq[i].priority > pq[j].priority
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = i
	pq[j].index = j
}

func (pq *PriorityQueue) Push(x interface{}) {
	n := len(*pq)
	item := x.(*Item)
	item.index = n
	*pq = append(*pq, item)
}

func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	old[n-1] = nil  // avoid memory leak
	item.index = -1 // for safety
	*pq = old[0 : n-1]
	return item
}

type value struct {
	value int
	indexes []int
}

func main() {

	r := bufio.NewReader(os.Stdin)

	rand.Seed(time.Now().UnixNano())

	fmt.Fscan(r, &n)

	garb = make([]int, n)

	for i := 0; i < n; i++ {
		garb[i] =  rand.Intn(1e6)
	}

	a = make(map[int][]int)

	for j := 0; j < n; j++ {
		var c int
		fmt.Fscan(r, &c)

		cc := make([]int, c)

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

	fmt.Fscan(r, &m)

	banned = make([][]int, m)

	for j := 0; j < m ; j++ {
		banned[j] = make([]int, n)

		for k := 0; k < n; k++ {
			var v int
			fmt.Fscan(r, &v)
			v--
			banned[j][k] = v
		}
	}

	solve()

}

func solve()  {
	hashes := make(map[int64][]int)

	for i := 0; i < m; i++ {
		b := banned[i]

		h := hash(b)

		hashes[h] = append(hashes[h], i)
	}

	var arr []*value

	a0 := a[0]
	for i := 0; i < len(a0); i++ {
		var indexes []int
		indexes = append(indexes, i)
		arr = append(arr, &value{a0[i], indexes})
	}

	for i := 1; i < n; i ++ {
		slot := a[i]

		biggest := arr[len(arr) - 1].value

		var pq PriorityQueue
		heap.Init(&pq)

		for j := len(slot) - 1; j >= 0; j-- {
			heap.Push(&pq, &Item{priority: slot[j] + biggest, idx: len(arr) - 1, idxElem : j})
		}

		var temp []*value

		counter := 0

		for ;pq.Len()  > 0 && counter <= int(1e5) + 100; {

			it := heap.Pop(&pq).(*Item)
			elemIndex := it.idxElem

			idx := it.idx

			indexes := arr[idx].indexes
			indexes = append(indexes, elemIndex)

			temp = append(temp, &value{value: slot[elemIndex] + arr[idx].value, indexes: indexes})

			counter++

			if idx > 0 {
				t := idx - 1

				heap.Push(&pq, &Item{priority: slot[elemIndex] + arr[t].value, idx: t, idxElem : elemIndex})
			}
		}

		idx := len(temp) - 1

		for i := 0; i < len(arr); i++ {
			arr[i] = temp[idx]
			idx--
		}

		for j := idx; j >= 0; j-- {
			arr = append(arr, temp[j])
		}
	}

	for i := len(arr) - 1; i >= 0; i-- {
		e := arr[i]
		indexes := e.indexes

		h := hash(indexes)

		if _, ok := hashes[h]; !ok {

			for j := 0; j < len(indexes); j++ {
				fmt.Print(indexes[j] + 1)
				fmt.Print(" ")
			}

			fmt.Println()
			return
		}
	}
}

func hash(indexes []int) int64 {
	var ans int64

	arr := make([]int, n)

	for j := 0; j < len(indexes); j++ {
		slot := a[j]
		index := indexes[j]

		arr[j] = slot[index]
	}

	for i := 0; i < len(arr); i++ {

		pos := i + 1

		var temp = int64(pos) * int64(arr[i]) * int64(garb[i])

		ans ^= temp
	}

	return ans % MOD
}

package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
)

type Item interface {
	Compare(left Item) bool
}

type Heap interface {
	Push(i Item)
	Pop() Item
	Top() Item
	Len() int
	Update(old Item, new Item)
	Remove(item Item)
	Get(key interface{}) Item
}

type MinInt int

func (i MinInt) Compare(j Item) bool {
	return i < j.(MinInt)
}

type MaxInt int

func (i MaxInt) Compare(j Item) bool {
	return i > j.(MaxInt)
}

type item struct {
	item  Item
	index int
}

type heapImpl struct {
	p               *pq
	items           map[interface{}]*item
	key             func(Item) interface{}
	DisableIndexing bool
}

func (h *heapImpl) Push(i Item) {
	it := &item{i, 0}
	h.addItem(it)
	heap.Push(h.p, it)
}

func (h *heapImpl) Pop() Item {
	res := heap.Pop(h.p).(*item)
	h.removeItem(res.item)
	return res.item
}

func (h *heapImpl) Top() Item {
	if len(h.p.items) > 0 {
		return h.p.items[0].item
	}
	return nil
}

func (h *heapImpl) Len() int {
	return len(h.p.items)
}

func (h *heapImpl) Update(o Item, n Item) {
	old := h.items[h.key(o)]
	new := &item{n, old.index}
	h.removeItem(old.item)
	h.p.items[old.index] = new
	heap.Fix(h.p, old.index)
	h.addItem(new)
}

func (h *heapImpl) Remove(i Item) {
	index := h.items[h.key(i)].index
	h.removeItem(i)
	heap.Remove(h.p, index)
}

func (h *heapImpl) Get(key interface{}) Item {
	return h.items[key].item
}

func (h *heapImpl) removeItem(item Item) {
	if !h.DisableIndexing {
		delete(h.items, h.key(item))
	}
}

func (h *heapImpl) addItem(item *item) {
	if !h.DisableIndexing {
		h.items[h.key(item.item)] = item
	}
}

// A PriorityQueue implements heap.Interface and holds Items.
type pq struct {
	items []*item
}

func (pq pq) Len() int { return len(pq.items) }

func (pq pq) Less(i, j int) bool {
	// We want Pop to give us the highest, not lowest, priority so we use greater than here.
	return pq.items[i].item.Compare(pq.items[j].item)
}

func (pq pq) Swap(i, j int) {
	pq.items[i], pq.items[j] = pq.items[j], pq.items[i]
	pq.items[i].index = i
	pq.items[j].index = j
}

func (pq *pq) Push(x interface{}) {
	n := len(pq.items)
	item := x.(*item)
	item.index = n
	pq.items = append(pq.items, item)
}

func (pq *pq) Pop() interface{} {
	old := pq.items
	n := len(old)
	item := old[n-1]
	old[n-1] = nil  // avoid memory leak
	item.index = -1 // for safety
	pq.items = old[0 : n-1]
	return item
}

func NewHeapWithKey(getKey func(Item) interface{}) Heap {
	return &heapImpl{
		p:               &pq{
			items: []*item{},
		},
		items:           make(map[interface{}]*item),
		key:             getKey,
		DisableIndexing: true,
	}
}

func NewHeap() Heap {
	return NewHeapWithKey(func(i Item) interface{} { return i })
}

type idx struct {
	value int
	arr   [10]int
}

func (left *idx) Compare(right Item) bool {
	return left.value > right.(*idx).value
}

func main() {
	r := bufio.NewReaderSize(os.Stdin, 8*1024*1024)

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

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

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

	seen := make(map[idx]bool)
	for i := 0; i < b; i++ {
		cur := idx{}
		for j := 0; j < n; j++ {
			fmt.Fscanf(r, "%d", &cur.arr[j])
			cur.arr[j]--
			cur.value += arr[j][cur.arr[j]]
		}
		fmt.Fscanf(r, "\n")
		seen[cur] = true
	}

	cur := idx{0, [10]int{}}
	for i := 0; i < n; i++ {
		cur.arr[i] = len(arr[i]) - 1
		cur.value += arr[i][cur.arr[i]]
	}

	h := NewHeap()
	h.Push(&cur)

	//alloc := [1001000]idx{}
	//a := 0

	used := make(map[idx]bool)

	for ; h.Len() > 0; {
		cur = *h.Pop().(*idx)
		if !seen[cur] {
			break
		}

		next := cur
		for i := 0; i < n; i++ {
			if cur.arr[i] == 0 {
				continue
			}

			next.arr = cur.arr
			next.value = cur.value - arr[i][cur.arr[i]] + arr[i][cur.arr[i]-1]
			next.arr[i]--

			if !used[next] {
				used[next] = true
				h.Push(&next)
			}
		}
	}

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