You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ positive integers. Initially you are standing at index $$$1$$$ and have a score equal to $$$a_1$$$. You can perform two kinds of moves:   move right — go from your current index $$$x$$$ to $$$x+1$$$ and add $$$a_{x+1}$$$ to your score. This move can only be performed if $$$x<n$$$.  move left — go from your current index $$$x$$$ to $$$x-1$$$ and add $$$a_{x-1}$$$ to your score. This move can only be performed if $$$x>1$$$. Also, you can't perform two or more moves to the left in a row. You want to perform exactly $$$k$$$ moves. Also, there should be no more than $$$z$$$ moves to the left among them.What is the maximum score you can achieve?

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The first line of each testcase contains three integers $$$n, k$$$ and $$$z$$$ ($$$2 \le n \le 10^5$$$, $$$1 \le k \le n - 1$$$, $$$0 \le z \le min(5, k)$$$) — the number of elements in the array, the total number of moves you should perform and the maximum number of moves to the left you can perform. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^4$$$) — the given array. The sum of $$$n$$$ over all testcases does not exceed $$$3 \cdot 10^5$$$.

Print $$$t$$$ integers — for each testcase output the maximum score you can achieve if you make exactly $$$k$$$ moves in total, no more than $$$z$$$ of them are to the left and there are no two or more moves to the left in a row.

NoteIn the first testcase you are not allowed to move left at all. So you make four moves to the right and obtain the score $$$a_1 + a_2 + a_3 + a_4 + a_5$$$.In the second example you can move one time to the left. So we can follow these moves: right, right, left, right. The score will be $$$a_1 + a_2 + a_3 + a_2 + a_3$$$.In the third example you can move four times to the left but it's not optimal anyway, you can just move four times to the right and obtain the score $$$a_1 + a_2 + a_3 + a_4 + a_5$$$.

package main

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

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

	T:= 0
	for Fscan(in, &T); T > 0; T-- {
		var n,k, z int
		Fscan(in, &n,&k,&z);
		a:=make([]int,n)
		for i:=0;i<n;i++{
			Fscan(in,&a[i])
		}

		ans:=0
		for l:=0;l<=z;l++{
			cnt,mx:=0,0
			for r:=0;r<=k-2*l;r++{
				cnt+=a[r]
				if r+1<n && mx < a[r]+a[r+1]{
					mx=a[r]+a[r+1]
				}
			}
			cnt+=l*mx
			if(cnt>ans){
				ans=cnt
			}
		}
		Fprintln(out,ans)
	}
}

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

package main

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

func main() {
	io := newFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	for t := io.nextInt(); t > 0; t-- {
		n, k, z := io.nextInt(), io.nextInt(), io.nextInt()
		a := io.nextInts(n)

		ans := 0
		for zz := 0; zz <= z; zz++ {
			s, mx := 0, 0
			for i := 0; i <= k-2*zz; i++ {
				s += a[i]
				if i+1 < n && a[i]+a[i+1] > mx {
					mx = a[i] + a[i+1]
				}
			}
			s += zz * mx
			if s > ans {
				ans = s
			}
		}

		io.println(ans)
	}
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p [1]byte
}

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

func (f *fastIO) nextInt() int {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	neg := false
	if f.p[0] == '-' {
		neg = true
		f.Read(f.p[:])
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p[:])
	}
	if neg {
		return -n
	}
	return n
}

func (f *fastIO) nextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.nextInt()
	}
	return a
}

func (f *fastIO) next() string {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p[:])
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

func (f *fastIO) printInts(a ...int) {
	if len(a) > 0 {
		f.print(a[0])
		for _, x := range a[1:] {
			f.print(" ", x)
		}
	}
}

func (f *fastIO) printlnInts(a ...int) {
	f.printInts(a...)
	f.println()
}

package main

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

type elem struct {
	score, pos int
	prevleft   bool
}

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

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

	for t := input(); t > 0; t-- {
		n, k, z := input(), input(), input()

		a := make([]int, n)

		for i := range a {
			a[i] = input()
		}

		dp := make([][]elem, k+1)
		for i := range dp {
			dp[i] = make([]elem, z+1)
		}

		dp[k][z].pos = 0
		dp[k][z].score = a[0]
		dp[k][z].prevleft = true

		for i := k; i > 0; i-- {
			for j := z; j >= 0 && j >= z-k+i; j-- {
				pos := dp[i][j].pos
				newScore := dp[i][j].score + a[pos+1]
				if newScore > dp[i-1][j].score {
					dp[i-1][j].score = newScore
					dp[i-1][j].pos = pos + 1
					dp[i-1][j].prevleft = false
				}

				if j != 0 && pos != 0 && !dp[i][j].prevleft {
					newScore = dp[i][j].score + a[pos-1]
					if newScore > dp[i-1][j-1].score {
						dp[i-1][j-1].score = newScore
						dp[i-1][j-1].prevleft = true
						dp[i-1][j-1].pos = pos - 1
					}
				}
			}
		}

		max := dp[0][0].score

		for _, e := range dp[0] {
			if e.score > max {
				max = e.score
			}
		}

		fmt.Println(max)
	}

}

package main

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

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

var n, z, k, cs int
var ara[100005] int
var dp[100005][7][3] int
var visit[100005][7][3] int

func F(pos int, lft int, flag int) int {
    mv := pos - 1 + 2 * (z - lft)
    if mv == k {
        return ara[pos];
    }

    if visit[pos][lft][flag] == cs {
        return dp[pos][lft][flag]
    }

    ret := F(pos + 1, lft, 0)
    if pos > 1 && lft > 0 && flag == 0 {
        ret = max(ret, F(pos - 1, lft - 1, 1))
    }

    visit[pos][lft][flag] = cs
    dp[pos][lft][flag] = ara[pos] + ret
    return dp[pos][lft][flag]
}

func main() {
    input := bufio.NewReader(os.Stdin)
	var T int;
    fmt.Fscan(input, &T)
	for t := 0; t < T; t++ {
        fmt.Fscan(input, &n)
        fmt.Fscan(input, &k)
        fmt.Fscan(input, &z)
		for i := 1; i <= n; i++ {
            fmt.Fscan(input, &ara[i])
		}
		cs++
		ret := F(1, z, 0)
		fmt.Printf("%d\n", ret)
	}
}

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

package main

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

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

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

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

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

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

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

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

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

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

var (
	t int

	n, k, z int64
	A       []int64
)

func main() {
	t = ReadInt()

	for tc := 0; tc < t; tc++ {
		n, k, z = ReadInt64_3()
		A = ReadInt64Slice(int(n))

		solve()
	}
}

func solve() {
	dp := [][]int64{}
	for i := int64(0); i < n+10; i++ {
		row := make([]int64, 8)
		dp = append(dp, row)
	}

	for i := int64(0); i < n; i++ {
		for j := int64(0); j <= z; j++ {
			dp[i][j] = -INF_BIT60
		}
	}

	dp[0][0] = A[0]
	for i := int64(0); i < n-1; i++ {
		for j := int64(0); j <= z; j++ {
			total := i + 2*j

			if total >= k {
				continue
			}

			// 右
			ChMax(&dp[i+1][j], dp[i][j]+A[i+1])

			// 右左右
			if k-total >= 3 {
				ChMax(&dp[i+1][j+1], dp[i][j]+A[i]+2*A[i+1])
			}

			// 右左
			if k-total >= 2 {
				ChMax(&dp[i][j+1], dp[i][j]+A[i]+A[i+1])
			}
		}
	}

	ans := int64(0)
	for i := int64(0); i < n; i++ {
		for j := int64(0); j <= z; j++ {
			ChMax(&ans, dp[i][j])
		}
	}
	fmt.Println(ans)
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

	return string(res)
}

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

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

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

	return string(res)
}

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

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

package main

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

func solve() {
	n, k, z := readInt64(), readInt64(), readInt64()
	array := make([]int64, n)
	for i := range array {
		array[i] = readInt64()
	}

	var ans, i int64
	for i = 0; i <= z; i++ {
		if i > k/2 {
			continue
		}

		var tmp, maxGain, j int64
		tmp = array[0]
		for j = 1; j <= k-i*2; j++ {
			maxGain = maxInt64(maxGain, array[j-1]+array[j])
			tmp += array[j]
		}

		if k-i*2+1 < n {
			maxGain = maxInt64(maxGain, array[k-i*2]+array[k-i*2+1])
		}
		tmp += maxGain * i
		ans = maxInt64(ans, tmp)
		// println(i, j, maxGain, ans)
	}
	fmt.Println(ans)
}

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

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

type xxx struct {
	x int
}

type sortArray []xxx

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

package main

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

var rw = newFastIO(os.Stdin, os.Stdout)

func main() {
	defer rw.Flush()
	nTest := 1
	nTest = rw.nextInt()
	for i := 0; i < nTest; i++ {
		solveCase()
	}
}

func solveCase() {
	n := rw.nextInt()
	k := rw.nextInt()
	z := rw.nextInt()
	a := rw.nextInts(n)
	ans := 0
	for t := 0; t <= z; t++ {
		max := 0
		cur := 0
		for i := 0; i <= k-t*2; i++ {
			cur += a[i]
			if i+1 < n && a[i]+a[i+1] > max {
				max = a[i] + a[i+1]
			}
		}
		val := cur + max*t
		if val > ans {
			ans = val
		}
	}
	rw.println(ans)
}

// @meooow template uwu
type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p [1]byte
}

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

func (f *fastIO) nextInt() int {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	neg := false
	if f.p[0] == '-' {
		neg = true
		f.Read(f.p[:])
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p[:])
	}
	if neg {
		return -n
	}
	return n
}

func (f *fastIO) nextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.nextInt()
	}
	return a
}

func (f *fastIO) next() string {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p[:])
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

func (f *fastIO) printInts(a ...int) {
	if len(a) > 0 {
		f.print(a[0])
		for _, x := range a[1:] {
			f.print(" ", x)
		}
	}
}

func (f *fastIO) printlnInts(a ...int) {
	f.printInts(a...)
	f.println()
}

package main

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

const (
	SCANNER_BUF_SIZE = 2 << 20

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

var (
	writer  *bufio.Writer
	scanner *bufio.Scanner
)

func init() {
	writer = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, SCANNER_BUF_SIZE), SCANNER_BUF_SIZE)
}

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

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

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

func sstring() string {
	scanner.Scan()
	return scanner.Text()
}

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

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

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

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

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

func main() {
	defer writer.Flush()
	t := sint64()
	for ; t > 0; t-- {
		n := sint64()
		k := sint64()
		z := sint64()
		a := make([]int64, n)
		for i := range a {
			a[i] = sint64()
		}
		printf("%d\n", sol(n, k, z, a))
	}
}

func sol(n, k, z int64, a []int64) int64 {
	if k-(z*2) >= n || len(a) == 0 {
		panic("") // should not happen
	}
	prefSums := make([]int64, len(a)+1)
	for i := 1; i < len(prefSums); i++ {
		prefSums[i] = prefSums[i-1] + a[i-1]
	}
	// printf("%v\n", prefSums)
	ret := int64(0)
	base := a[0]
	for osc := int64(1); osc < n; osc++ {
		base += a[osc]
		curr := base
		remainSteps := k - osc
		if remainSteps < 0 {
			break
		}
		// printf("remainsteps %d\n", remainSteps)
		// if we can oscillate and end
		if z*2 >= remainSteps {
			if remainSteps&1 == 0 {
				curr += a[osc-1] * (remainSteps / 2)
				curr += a[osc] * (remainSteps / 2)
				ret = max(ret, curr)
				// printf("even osc %d %d\n", osc, curr)
				continue
			}
			curr += a[osc-1] * (remainSteps/2 + 1)
			curr += a[osc] * (remainSteps / 2)
			ret = max(ret, curr)
			// printf("odd osc %d %d\n", osc, curr)
			continue
		}
		// oscillate and fill up remaining steps
		curr += a[osc-1] * z
		curr += a[osc] * z
		remainSteps -= z * 2
		// printf("pref = %d %d\n", osc, remainSteps)
		curr += prefSums[osc+1+remainSteps] - prefSums[osc+1]
		// printf("remain osc %d %d\n", osc, curr)
		ret = max(ret, curr)
	}
	return ret
}

package main

import (
	"fmt"
	"os"
)

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

var n, z, k, cs int;
var ara[100005] int;
var dp[100005][7][3] int;
var visit[100005][7][3] int;

func F(pos int, lft int, flag int) int {
    mv := pos - 1 + 2 * (z - lft);
    if mv == k {
        os.Exit(0);
        return ara[pos];
    }

    if visit[pos][lft][flag] == cs {
        return dp[pos][lft][flag];
    }

    ret := F(pos + 1, lft, 0);
    if pos > 1 && lft > 0 && flag == 0 {
        ret = max(ret, F(pos - 1, lft - 1, 1));
    }

    visit[pos][lft][flag] = cs;
    dp[pos][lft][flag] = ara[pos] + ret;
    return dp[pos][lft][flag];
}

func main() {
	var T int;
	fmt.Scanf("%d\n", &T);
	for t := 0; t < T; t++ {
		fmt.Scanf("%d %d %d\n", &n, &k, &z);
		for i := 1; i <= n; i++ {
			if i < n {
			    fmt.Scanf("%d", &ara[i]);
			} else {
			    fmt.Scanf("%d\n", &ara[i]);
			}
		}
		cs++;
		ret := F(1, z, 0);
		fmt.Printf("%d\n", ret);
	}
}

package main

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

func solve() {
	n, k, z := readInt64(), readInt64(), readInt64()
	array := make([]int64, n)
	for i := range array {
		array[i] = readInt64()
	}

	var ans, i int64
	for i = 0; i <= z; i++ {
		var tmp, maxGain, j int64
		tmp = array[0]
		for j = 1; j <= k-i*2; j++ {
			maxGain = maxInt64(maxGain, array[j-1]+array[j])
			tmp += array[j]
		}
		tmp += maxGain * i
		ans = maxInt64(ans, tmp)
	}
	fmt.Println(ans)
}

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

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

type xxx struct {
	x int
}

type sortArray []xxx

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

package main

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

const (
	SCANNER_BUF_SIZE = 2 << 20

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

var (
	writer  *bufio.Writer
	scanner *bufio.Scanner
)

func init() {
	writer = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, SCANNER_BUF_SIZE), SCANNER_BUF_SIZE)
}

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

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

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

func sstring() string {
	scanner.Scan()
	return scanner.Text()
}

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

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

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

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

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

func main() {
	defer writer.Flush()
	t := sint64()
	for ; t > 0; t-- {
		n := sint64()
		k := sint64()
		z := sint64()
		a := make([]int64, n)
		for i := range a {
			a[i] = sint64()
		}
		printf("%d\n", sol(n, k, z, a))
	}
}

func sol(n, k, z int64, a []int64) int64 {
	if k-(z*2) >= n || len(a) == 0 {
		panic("") // should not happen
	}
	prefSums := make([]int64, len(a)+1)
	for i := 1; i < len(prefSums); i++ {
		prefSums[i] = prefSums[i] + a[i-1]
	}
	ret := int64(0)
	base := a[0]
	for end := int64(1); end < int64(len(a)); end++ {
		base += a[end]
		remainSteps := k - end
		if remainSteps < 0 {
			break
		}
		nback := z
		if remainSteps%2 == 1 {
			nback = min(nback, remainSteps/2+1)
		} else {
			nback = min(nback, remainSteps/2)
		}
		nfront := min(nback, remainSteps-nback)
		curr := base + (nback * a[end-1]) + (nfront * a[end])
		if nfront+nback+end < k {
			remain := k - end - nfront - nback
			realEnd := end + remain
			if realEnd < n {
				curr += prefSums[realEnd+1] - prefSums[end+1]
			}
		}
		ret = max(ret, curr)
	}
	return ret
}

package main

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

const (
	SCANNER_BUF_SIZE = 2 << 20

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

var (
	writer  *bufio.Writer
	scanner *bufio.Scanner
)

func init() {
	writer = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, SCANNER_BUF_SIZE), SCANNER_BUF_SIZE)
}

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

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

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

func sstring() string {
	scanner.Scan()
	return scanner.Text()
}

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

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

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

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

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

func main() {
	defer writer.Flush()
	t := sint64()
	for ; t > 0; t-- {
		n := sint64()
		k := sint64()
		z := sint64()
		a := make([]int64, n)
		for i := range a {
			a[i] = sint64()
		}
		printf("%d\n", sol(n, k, z, a))
	}
}

func sol(n, k, z int64, a []int64) int64 {
	if k-(z*2) >= n || len(a) == 0 {
		panic("") // should not happen
	}
	prefSums := make([]int64, len(a)+1)
	for i := 1; i < len(prefSums); i++ {
		prefSums[i] = prefSums[i-1] + a[i-1]
	}
	// printf("%v\n", prefSums)
	ret := int64(0)
	base := a[0]
	for osc := int64(1); osc < n; osc++ {
		base += a[osc]
		curr := base
		remainSteps := k - osc
		if remainSteps < 0 {
			continue
		}
		if z*2 >= remainSteps {
			// we can oscillate and end
			if remainSteps & 1 == 0 {
				curr += a[osc-1] * remainSteps / 2
				curr += a[osc] * remainSteps / 2
				ret = max(ret, curr)
				continue
			}
			curr += a[osc-1] * (remainSteps / 2 + 1)
			curr += a[osc-1] * (remainSteps / 2)
			ret = max(ret, curr)
			continue
		}
		// oscillate and fill up remaining steps
		curr += a[osc-1] * z
		curr += a[osc] * z
		remainSteps -= z*2
		// printf("pref = %d %d\n", osc, remainSteps)
		curr += prefSums[osc+1+remainSteps] - prefSums[osc+1]
		ret = max(ret, curr)
	}
	return ret
}

package main

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

const (
	SCANNER_BUF_SIZE = 2 << 20

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

var (
	writer  *bufio.Writer
	scanner *bufio.Scanner
)

func init() {
	writer = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, SCANNER_BUF_SIZE), SCANNER_BUF_SIZE)
}

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

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

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

func sstring() string {
	scanner.Scan()
	return scanner.Text()
}

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

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

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

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

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

func main() {
	defer writer.Flush()
	t := sint64()
	for ; t > 0; t-- {
		n := sint64()
		k := sint64()
		z := sint64()
		a := make([]int64, n)
		for i := range a {
			a[i] = sint64()
		}
		printf("%d\n", sol(n, k, z, a))
	}
}

func sol(n, k, z int64, a []int64) int64 {
	if k - (z*2) >= n || len(a) == 0 {
		panic("") // should not happen
	}
	ret := int64(0)
	base := a[0]
	for end := int64(1); end < int64(len(a)); end++ {
		base += a[end]
		remainSteps := k - end
		if remainSteps < 0 {
			break
		}
		var nback int64
		if remainSteps % 2 == 1 {
			nback = remainSteps / 2 + 1
		} else {
			nback = remainSteps / 2
		}
		if nback > z {
			continue
		}
		nfront := remainSteps / 2
		curr := base + (nback * a[end-1]) + (nfront * a[end])
		ret = max(ret, curr)
	}
	return ret
}

package main

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

const (
	SCANNER_BUF_SIZE = 2 << 20

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

var (
	writer  *bufio.Writer
	scanner *bufio.Scanner
)

func init() {
	writer = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, SCANNER_BUF_SIZE), SCANNER_BUF_SIZE)
}

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

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

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

func sstring() string {
	scanner.Scan()
	return scanner.Text()
}

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

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

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

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

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

func main() {
	defer writer.Flush()
	t := sint64()
	for ; t > 0; t-- {
		n := sint64()
		k := sint64()
		z := sint64()
		a := make([]int64, n)
		for i := range a {
			a[i] = sint64()
		}
		printf("%d\n", sol(n, k, z, a))
	}
}

func sol(n, k, z int64, a []int64) int64 {
	if k-(z*2) >= n || len(a) == 0 {
		panic("") // should not happen
	}
	prefSums := make([]int64, len(a)+1)
	for i := 1; i < len(prefSums); i++ {
		prefSums[i] = prefSums[i-1] + a[i-1]
	}
	// printf("%v\n", prefSums)
	ret := int64(0)
	base := a[0]
	for osc := int64(1); osc < n; osc++ {
		base += a[osc]
		curr := base
		remainSteps := k - osc
		if remainSteps < 0 {
			break
		}
		if z*2 >= remainSteps {
			// we can oscillate and end
			if remainSteps & 1 == 0 {
				curr += a[osc-1] * remainSteps / 2
				curr += a[osc] * remainSteps / 2
				ret = max(ret, curr)
				continue
			}
			curr += a[osc-1] * (remainSteps / 2 + 1)
			curr += a[osc-1] * (remainSteps / 2)
			continue
		}
		// oscillate and fill up remaining steps
		curr += a[osc-1] * z
		curr += a[osc] * z
		remainSteps -= z*2
		// printf("pref = %d %d\n", osc, remainSteps)
		curr += prefSums[osc+1+remainSteps] - prefSums[osc+1]
		ret = max(ret, curr)
	}
	return ret
}


You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it.  You have a fence consisting of $$$n$$$ planks, where the $$$i$$$-th plank has the color $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.You've invited $$$m$$$ painters for this purpose. The $$$j$$$-th painter will arrive at the moment $$$j$$$ and will recolor exactly one plank to color $$$c_j$$$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank.Can you get the coloring $$$b$$$ you want? If it's possible, print for each painter which plank he must paint.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \dots, c_m$$$ ($$$1 \le c_j \le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.

For each test case, output "NO" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print "YES" and $$$m$$$ integers $$$x_1, x_2, \dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer).

YES
1
YES
2 2
YES
1 1 1
YES
2 1 9 5 9
NO
NO

package main

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

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

	var T, n, m, w int
o:
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &m)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		pa := make([]int, n+1)
		pb := make([][]int, n+1)
		for i, v := range a {
			if Fscan(in, &w); w == v {
				pa[w] = i + 1
			} else {
				pb[w] = append(pb[w], i+1)
			}
		}
		c := make([]int, m)
		for i := range c {
			Fscan(in, &c[i])
		}
		ans := make([]interface{}, m)
		for i := m - 1; i >= 0; i-- {
			if v := c[i]; len(pb[v]) > 0 {
				ans[i] = pb[v][0]
				pb[v] = pb[v][1:]
			} else if i < m-1 {
				ans[i] = ans[m-1]
			} else if pa[v] > 0 {
				ans[i] = pa[v]
			} else {
				Fprintln(out, "NO")
				continue o
			}
		}
		for _, p := range pb {
			if len(p) > 0 {
				Fprintln(out, "NO")
				continue o
			}
		}
		Fprintln(out, "YES")
		Fprintln(out, ans...)
	}
}

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

package main

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

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

	solve := func(Case int) {
		var n, m int
		Fscan(in, &n, &m)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		fix := map[int][]int{}
		bPos := make([]int, n+1)
		b := make([]int, n)
		for i := range b {
			Fscan(in, &b[i])
			if b[i] != a[i] {
				fix[b[i]] = append(fix[b[i]], i+1)
				bPos[b[i]] = i + 1
			}
			if bPos[b[i]] == 0 {
				bPos[b[i]] = i + 1
			}
		}
		cc := make([]int, m)
		leftcc := make([]int, n+1)
		leftI := 1
		for i := range cc {
			Fscan(in, &cc[i])
			if bPos[cc[i]] > 0 {
				leftcc[cc[i]]++
			}
		}
		ans := make([]int, m)
		//badPos := -1
		for i, c := range cc {
			if len(fix[c]) > 0 {
				ans[i] = fix[c][0]
				fix[c] = fix[c][1:]
				if len(fix[c]) == 0 {
					delete(fix, c)
				}
				//badPos = -1
			} else if bPos[c] > 0 {
				ans[i] = bPos[c]
			} else {
				if len(fix) == 0 {
					for ; leftI <= n && leftcc[leftI] <= 0; leftI++ {
					}
					if leftI > n {
						Fprintln(out, "NO")
						return
					}
					fix[leftI] = append(fix[leftI], bPos[leftI])
					ans[i] = bPos[leftI]
					//badPos = 1
				} else {
					for _, vs := range fix {
						ans[i] = vs[0]
						break
					}
				}
			}
			leftcc[c]--
		}
		for _, f := range fix {
			if len(f) > 0 {
				Fprintln(out, "NO")
				return
			}
		}
		Fprintln(out, "YES")
		for _, v := range ans {
			Fprint(out, v, " ")
		}
		Fprintln(out)
	}

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

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

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

package main

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

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

func init() {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 4096), 1e9)
}

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

func sint64() int64 {
	s := sstring()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

func sint64sl(n int64) []int64 {
	sl := make([]int64, n)
	for i := range sl {
		sl[i] = sint64()
	}
	return sl
}

func suint64() uint64 {
	s := sstring()
	i := uint64(0)
	for _, r := range s {
		i *= 10
		i += uint64(r - '0')
	}
	return i
}

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

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		m := sint64()
		a := sint64sl(n)
		b := sint64sl(n)
		c := sint64sl(m)
		res := sol(n, m, a, b, c)
		if res == nil {
			printf("NO\n")
			continue
		}
		printf("YES\n")
		for _, d := range res {
			printf("%d ", d)
		}
		printf("\n")
		// judge(n, m, a, b, c, res)
	}
}

func sol(n, m int64, a, b, c []int64) []int64 {
	diff := map[int64][]int64{} // desiredcolor: []position
	for i := range a {
		if a[i] == b[i] {
			continue
		}
		diff[b[i]] = append(diff[b[i]], int64(i))
	}
	if len(diff) == 0 {
		return solSame(n, m, a, b, c)
	}
	// printf("diff=%v\n", diff)
	return solDiff(n, m, a, b, c, diff)
}

func solSame(n, m int64, a, b, c []int64) []int64 {
	set := map[int64]int64{}
	for i := range a {
		set[a[i]] = int64(i) + 1
	}
	// printf("set=%v\n", set)
	res := make([]int64, m)
	if v, ok := set[c[len(c)-1]]; !ok {
		return nil
	} else {
		for i := range res {
			res[i] = v
		}
	}
	return res
}

func solDiff(n, m int64, a, b, c []int64, diff map[int64][]int64) []int64 {
	set := map[int64]int64{}
	for i := range b {
		set[b[i]] = int64(i)
	}
	res := make([]int64, m)
	for i := len(c) - 1; i >= 0; i-- {
		if len(diff[c[i]]) == 0 {
			if i == len(c)-1 {
				if pos, ok := set[c[i]]; !ok {
					return nil
				} else {
					res[i] = pos + 1 // paint any plank of same desired color
				}
				continue
			}
			res[i] = res[len(res)-1] // paint last painter's plank, for it to be repainted later
			continue
		}
		res[i] = diff[c[i]][len(diff[c[i]])-1] + 1
		diff[c[i]] = diff[c[i]][:len(diff[c[i]])-1]
		if len(diff[c[i]]) == 0 {
			delete(diff, c[i])
		}
	}
	// printf("diff2=%v\n", diff)
	if len(diff) != 0 {
		return nil
	}
	return res
}

func judge(n, m int64, a, b, c, res []int64) {
	o := make([]int64, n)
	copy(o, a)
	for i := range res {
		pos := res[i] - 1
		col := c[i]
		a[pos] = col
	}
	for i := range a {
		if a[i] != b[i] {
			panic(fmt.Sprintf("unexpected sol:\no=%v\na=%v\nb=%v\nc=%v\n", o, a, b, c))
		}
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := os.Stdout
	var t int
	fmt.Fscan(in,&t)
	for u:=1;u<=t;u++{
		var n, m int
		fmt.Fscan(in,&n,&m)
		a := make([]int,n+5)
		b := make([]int,n+5)
		c := make([]int,m+5)
		ned := map[int][]int{}
		des := map[int]int{}
		for i:=1;i<=n;i++{
			fmt.Fscan(in,&a[i])
			if des[a[i]]==0{
				des[a[i]]=i
			}
		}
		for i:=1;i<=n;i++{
			fmt.Fscan(in,&b[i])
			if b[i]!=a[i]{
				ned[b[i]] = append(ned[b[i]],i)
			}
		}
		for i:=1;i<=m;i++{
			fmt.Fscan(in,&c[i])
		}
		flag := true
		ops := []int{}
		er := []int{}
		for i:=1;i<=m;i++{
			if len(ned[c[i]])>0{
				tar := ned[c[i]][0]
				ned[c[i]] = ned[c[i]][1:]
				if len(er)>0{
					for j:=0;j<len(er);j++{
						ops = append(ops,tar)
					}
					er = []int{}
				}
				a[tar]=c[i]
				ops = append(ops,tar)
			}else{
				er = append(er,c[i])
			}
		}
		for _,v := range ned{
			if len(v)>0{
				flag = false
			}
		}
		if len(er)>0{
			fin := map[int]int{}
			for i:=1;i<=n;i++{
				fin[a[i]]=i
			}
			nol := []int{}
			for i:=0;i<len(er);i++ {
				if fin[er[i]]==0{
					nol = append(nol,er[i])
				}else{
					if len(nol)>0{
						for j:=0;j<len(nol);j++{
							ops = append(ops,fin[er[i]])
						}
						nol = []int{}
					}
					ops = append(ops,fin[er[i]])
				}
			}
			if len(nol)>0{
				flag = false
			}
		}
		if flag{
			fmt.Fprintln(out,"YES")
			for i:=0;i<len(ops);i++{
				fmt.Fprintf(out,"%d ",ops[i])
			}
			fmt.Fprintln(out)
		}else{
			fmt.Fprintln(out,"NO")
		}
	}
}

package main

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

func solve(a, b, c []int) {
	hasColor := make(map[int]int, 0)
	needColor := make(map[int][]int, 0)
	totalNeed := 0
	ans := []int{}

	for i := range a {
		hasColor[b[i]] = i + 1
		if a[i] != b[i] {
			if len(needColor[b[i]]) == 0 {
				totalNeed++
			}
			needColor[b[i]] = append(needColor[b[i]], i)
		}
	}

	for i := range c {
		if totalNeed == 0 {
			if hasColor[c[len(c)-1]] > 0 {
				idx := hasColor[c[len(c)-1]]
				printf("yes\n")
				for _, x := range ans {
					printf("%d ", x+1)
				}
				for j := i; j < len(c); j++ {
					printf("%d ", idx)
				}
				printf("\n")
			} else {
				printf("no\n")
			}
			return
		}

		if len(needColor[c[i]]) > 0 {
			ans = append(ans, needColor[c[i]][len(needColor[c[i]])-1])
			needColor[c[i]] = needColor[c[i]][:len(needColor[c[i]])-1]
			if len(needColor[c[i]]) == 0 {
				totalNeed--
			}
		} else {
			v := -1
		L:
			for _, val := range needColor {
				for _, x := range val {
					v = x
					break L
				}
			}
			ans = append(ans, v)
		}
	}

	if totalNeed == 0 {
		printf("yes\n")
		for _, x := range ans {
			printf("%d ", x+1)
		}
		printf("\n")
	} else {
		printf("no\n")
	}
}

func main() {
	//RedirectInputToFile(reader, "input.txt")
	t := 1
	scan(&t)

	for T := 0; T < t; T++ {
		var n, m int
		scan(&n, &m)

		var a, b, c []int
		a = make([]int, n)
		b = make([]int, n)
		c = make([]int, m)
		for i := range a {
			scan(&a[i])
		}
		for i := range b {
			scan(&b[i])
		}
		for i := range c {
			scan(&c[i])
		}

		solve(a, b, c)
	}

	writer.Flush()
}

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

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

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

package main

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

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

var n, m int
var color [100005][]int
var a, b, c, same, ans, cnt [100005]int
var colored int

func main() {
	var t int
	fmt.Fscan(in, &t)
	for t > 0 {
		t--
		fmt.Fscan(in, &n, &m)
		for i := 1; i <= n; i++ {
			fmt.Fscan(in, &a[i])
		}
		for i := 1; i <= n; i++ {
			fmt.Fscan(in, &b[i])
			if a[i] == b[i] {
				same[a[i]] = i
			} else {
				color[b[i]] = append(color[b[i]], i)
				cnt[b[i]]++
			}
		}
		for i := 1; i <= m; i++ {
			fmt.Fscan(in, &c[i])
		}
		no := false
		colored = 0
		for i := m; i >= 1; i-- {
			x := c[i]
			if cnt[x] > 0 {
				pos := color[x][cnt[x]-1]
				ans[i] = pos
				a[pos] = x
				cnt[x]--
				colored = pos
			} else if same[x] != 0 {
				// fmt.Println("same", same[x])
				ans[i] = same[x]
				colored = same[x]
			} else if colored != 0 {
				ans[i] = colored
			} else {
				no = true
				break
			}
			// fmt.Print(i, " ", ans[i], "|")
		}
		for i := 1; i <= n; i++ {
			if a[i] != b[i] {
				no = true
			}
		}

		if no {
			fmt.Fprintln(out, "NO")
		} else {
			fmt.Fprintln(out, "YES")
			for i := 1; i <= m; i++ {
				fmt.Fprint(out, ans[i], " ")
			}
			fmt.Fprintln(out)
		}
		for i := 1; i <= n; i++ {
			same[a[i]] = 0
			cnt[b[i]] = 0
			color[b[i]] = nil
		}
		// out.Flush()
	}
	out.Flush()
}

package main

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

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

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

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

		changedCnt := make([]int, n+1)
		for i := range b {
			if a[i] != b[i] {
				changedCnt[b[i]]++
			}
		}
		changed := make([][]int, n+1)
		for i := 0; i < n+1; i++ {
			if changedCnt[i] > 0 {
				changed[i] = make([]int, 0, changedCnt[i])
			}
		}
		notChanged := make([]int, n+1)
		for i := range notChanged {
			notChanged[i] = -1
		}
		for i := range b {
			if a[i] == b[i] {
				notChanged[b[i]] = i + 1
			} else {
				changed[b[i]] = append(changed[b[i]], i+1)
			}
		}

		work := make([]int, m)
		if notChanged[c[m-1]] < 0 && changedCnt[c[m-1]] == 0 {
			fmt.Fprintln(out, "NO")
			out.Flush()
			continue
		}
		for i := m - 1; i >= 0; i-- {
			if changedCnt[c[i]] > 0 {
				work[i] = changed[c[i]][changedCnt[c[i]]-1]
				if changedCnt[c[i]] > 1 {
					changed[c[i]] = changed[c[i]][:changedCnt[c[i]]-1]
				} else {
					changed[c[i]] = nil
				}
				changedCnt[c[i]]--
			} else if notChanged[c[i]] > 0 {
				work[i] = notChanged[c[i]]
			} else {
				work[i] = work[i+1]
			}
		}

		for i := range changedCnt {
			if changedCnt[i] > 0 {
				fmt.Fprintln(out, "NO")
				out.Flush()
				continue MainLoop
			}
		}
		fmt.Fprintln(out, "YES")
		for i := 0; i < m-1; i++ {
			fmt.Fprint(out, work[i], " ")
		}
		fmt.Fprintln(out, work[m-1])
		out.Flush()
	}
}

package main

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

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

	tc := readNum(reader)

	var buf bytes.Buffer

	for tc > 0 {
		tc--
		n, m := readTwoNums(reader)
		A := readNNums(reader, n)
		B := readNNums(reader, n)
		C := readNNums(reader, m)
		ok, res := solve(n, m, A, B, C)
		if !ok {
			buf.WriteString("NO\n")
		} else {
			buf.WriteString("YES\n")
			for i := 0; i < len(res); i++ {
				buf.WriteString(fmt.Sprintf("%d ", res[i]))
			}
			buf.WriteByte('\n')
		}
	}
	fmt.Print(buf.String())
}

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

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

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

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

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

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

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

	return i
}
func solve(n int, m int, A []int, B []int, C []int) (bool, []int) {
	// 对于颜色x, cntb[x] >= cntc[x]
	diff := make([][]int, n+1)
	for i := 0; i <= n; i++ {
		diff[i] = make([]int, 0, 2)
	}

	for i := 0; i < n; i++ {
		if A[i] != B[i] {
			b := B[i]
			diff[b] = append(diff[b], i+1)
		}
	}
	x := C[m-1]
	y := -1

	if len(diff[x]) > 0 {
		y = diff[x][len(diff[x])-1]
	} else {
		for i := n - 1; i >= 0; i-- {
			if B[i] == x {
				y = i + 1
				diff[x] = append(diff[x], i+1)
				break
			}
		}
	}

	if y < 0 {
		return false, nil
	}

	pos := make([]int, n+1)
	// then we always has a solution
	res := make([]int, m)
	for i := 0; i < m; i++ {
		c := C[i]
		if pos[c] >= len(diff[c]) {
			res[i] = y
		} else {
			res[i] = diff[c][pos[c]]
			pos[c]++
		}
	}

	for i := 0; i <= n; i++ {
		if pos[i] < len(diff[i]) {
			return false, nil
		}
	}

	return true, res
}

package main

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

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

	var T, n, m, w int
o:
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &m)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		pa := make([]int, n+1)
		pb := make([][]int, n+1)
		for i, v := range a {
			if Fscan(in, &w); w == v {
				pa[w] = i + 1
			} else {
				pb[w] = append(pb[w], i+1)
			}
		}
		c := make([]int, m)
		for i := range c {
			Fscan(in, &c[i])
		}
		ans := make([]interface{}, m)
		for i := m - 1; i >= 0; i-- {
			if v := c[i]; len(pb[v]) > 0 {
				ans[i] = pb[v][0]
				pb[v] = pb[v][1:]
			} else if i < m-1 {
				ans[i] = ans[m-1]
			} else if pa[v] > 0 {
				ans[i] = pa[v]
			} else {
				Fprintln(out, "NO")
				continue o
			}
		}
		Fprintln(out, "YES")
		Fprintln(out, ans...)
	}
}

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

package main

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

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

	solve := func(Case int) {
		var n, m int
		Fscan(in, &n, &m)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		fix := make([][]int, n+1)
		bPos := make([]int, n+1)
		b := make([]int, n)
		for i := range b {
			Fscan(in, &b[i])
			if b[i] != a[i] {
				fix[b[i]] = append(fix[b[i]], i+1)
				bPos[b[i]] = i + 1
			}
			if bPos[b[i]] == 0 {
				bPos[b[i]] = i + 1
			}
		}
		fixI := 1
		cc := make([]int, m)
		leftcc := make([]int, n+1)
		for i := range cc {
			Fscan(in, &cc[i])
			if bPos[cc[i]] > 0 {
				leftcc[cc[i]]++
			}
		}
		leftI := 1
		ans := make([]int, m)
		//badPos := -1
		for i, c := range cc {
			if len(fix[c]) > 0 {
				ans[i] = fix[c][0]
				fix[c] = fix[c][1:]
				//badPos = -1
			} else if bPos[c] > 0 {
				ans[i] = bPos[c]
			} else {
				for ; fixI <= n && len(fix[fixI]) == 0; fixI++ {
				}
				if fixI > n {
					for ; leftI <= n && leftcc[leftI] <= 0; leftI++ {
					}
					if leftI > n {
						Fprintln(out, "NO")
						return
					}
					fix[leftI] = append(fix[leftI], bPos[leftI])
					ans[i] = bPos[leftI]
					//badPos = 1
				} else {
					ans[i] = fix[fixI][0]
				}
			}
			leftcc[c]--
		}
		for _, f := range fix {
			if len(f) > 0 {
				Fprintln(out, "NO")
				return
			}
		}
		Fprintln(out, "YES")
		for _, v := range ans {
			Fprint(out, v, " ")
		}
		Fprintln(out)
	}

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

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

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

package main

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

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

	solve := func(Case int) {
		var n, m int
		Fscan(in, &n, &m)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		fix := make([][]int, n+1)
		final := make([]int, n+1)
		b := make([]int, n)
		for i := range b {
			Fscan(in, &b[i])
			if b[i] != a[i] {
				fix[b[i]] = append(fix[b[i]], i+1)
			}
			final[b[i]] = i + 1
		}
		fixI := 1
		cc := make([]int, m)
		leftcc := make([]int, n+1)
		for i := range cc {
			Fscan(in, &cc[i])
			if final[cc[i]]> 0 {
				leftcc[cc[i]]++
			}
		}
		leftI := 1
		ans := make([]int, m)
		//badPos := -1
		for i, c := range cc {
			if len(fix[c]) > 0 {
				ans[i] = fix[c][0]
				fix[c] = fix[c][1:]
				//badPos = -1
			} else if final[c] > 0 {
				ans[i] = final[c]
			} else {
				for ; fixI <= n && len(fix[fixI]) == 0; fixI++ {
				}
				if fixI > n {
					for ; leftI <= n && leftcc[leftI] <= 0; leftI++ {
					}
					if leftI > n {
						Fprintln(out, "NO")
						return
					}
					fix[leftI] = append(fix[leftI], final[leftI])
					ans[i] = final[leftI]
					//badPos = 1
				} else {
					ans[i] = fix[fixI][0]
				}
			}
			leftcc[c]--
		}
		for _, f := range fix {
			if len(f) > 0 {
				Fprintln(out, "NO")
				return
			}
		}
		Fprintln(out, "YES")
		for _, v := range ans {
			Fprint(out, v, " ")
		}
		Fprintln(out)
	}

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

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

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

package main

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

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

func init() {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 4096), 1e9)
}

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

func sint64() int64 {
	s := sstring()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

func sint64sl(n int64) []int64 {
	sl := make([]int64, n)
	for i := range sl {
		sl[i] = sint64()
	}
	return sl
}

func suint64() uint64 {
	s := sstring()
	i := uint64(0)
	for _, r := range s {
		i *= 10
		i += uint64(r - '0')
	}
	return i
}

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

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		m := sint64()
		a := sint64sl(n)
		b := sint64sl(n)
		c := sint64sl(m)
		res := sol(n, m, a, b, c)
		if res == nil {
			printf("NO\n")
			continue
		}
		printf("YES\n")
		for _, d := range res {
			printf("%d ", d)
		}
		printf("\n")
	}
}

func sol(n, m int64, a, b, c []int64) []int64 {
	diff := map[int64][]int64{} // desiredcolor: []position
	for i := range a {
		if a[i] == b[i] {
			continue
		}
		diff[b[i]] = append(diff[b[i]], int64(i))
	}
	if len(diff) == 0 {
		return solSame(n, m, a, b, c)
	}
	// printf("diff=%v\n", diff)
	return solDiff(n, m, a, b, c, diff)
}

func solSame(n, m int64, a, b, c []int64) []int64 {
	set := map[int64]int64{}
	for i := range a {
		set[a[i]] = int64(i) + 1
	}
	// printf("set=%v\n", set)
	res := make([]int64, m)
	if v, ok := set[c[len(c)-1]]; !ok {
		return nil
	} else {
		for i := range res {
			res[i] = v
		}
	}
	return res
}

func solDiff(n, m int64, a, b, c []int64, diff map[int64][]int64) []int64 {
	set := map[int64]int64{}
	for i := range a {
		set[a[i]] = int64(i)
	}
	res := make([]int64, m)
	for i := len(c) - 1; i >= 0; i-- {
		color := c[i]
		if len(diff[color]) == 0 {
			if i == len(c)-1 {
				if pos, ok := set[color]; !ok {
					return nil
				} else {
					res[i] = pos + 1
				}
				continue
			}
			res[i] = res[len(res)-1] // paint last painter's plank, for it to be repainted later
			continue
		}
		res[i] = diff[color][len(diff[color])-1] + 1
		diff[color] = diff[color][:len(diff[color])-1]
		if len(diff[color]) == 0 {
			delete(diff, color)
		}
	}
	// printf("diff2=%v\n", diff)
	if len(diff) != 0 {
		return nil
	}
	return res
}

package main

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

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

func init() {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 4096), 1e9)
}

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

func sint64() int64 {
	s := sstring()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

func sint64sl(n int64) []int64 {
	sl := make([]int64, n)
	for i := range sl {
		sl[i] = sint64()
	}
	return sl
}

func suint64() uint64 {
	s := sstring()
	i := uint64(0)
	for _, r := range s {
		i *= 10
		i += uint64(r - '0')
	}
	return i
}

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

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		m := sint64()
		a := sint64sl(n)
		b := sint64sl(n)
		c := sint64sl(m)
		res := sol(n, m, a, b, c)
		if res == nil {
			printf("NO\n")
			continue
		}
		printf("YES\n")
		for _, d := range res {
			printf("%d ", d)
		}
		printf("\n")
	}
}

func sol(n, m int64, a, b, c []int64) []int64 {
	diff := map[int64][]int64{} // desiredcolor: []position
	for i := range a {
		if a[i] == b[i] {
			continue
		}
		diff[b[i]] = append(diff[b[i]], int64(i))
	}
	if len(diff) == 0 {
		return solSame(n, m, a, b, c)
	}
	// printf("diff=%v\n", diff)
	return solDiff(n, m, a, b, c, diff)
}

func solSame(n, m int64, a, b, c []int64) []int64 {
	set := map[int64]int64{}
	for i := range a {
		set[a[i]] = int64(i) + 1
	}
	// printf("set=%v\n", set)
	res := make([]int64, m)
	if v, ok := set[c[len(c)-1]]; !ok {
		return nil
	} else {
		for i := range res {
			res[i] = v
		}
	}
	return res
}

func solDiff(n, m int64, a, b, c []int64, diff map[int64][]int64) []int64 {
	set := map[int64]int64{}
	for i := range a {
		set[a[i]] = int64(i)
	}
	res := make([]int64, m)
	for i := len(c) - 1; i >= 0; i-- {
		color := c[i]
		if len(diff[color]) == 0 {
			if i == len(c)-1 {
				if pos, ok := set[color]; !ok {
					return nil
				} else {
					res[i] = pos + 1
				}
			}
			res[i] = res[len(res)-1] // paint last painter's plank, for it to be repainted later
			continue
		}
		res[i] = diff[color][len(diff[color])-1] + 1
		diff[color] = diff[color][:len(diff[color])-1]
		if len(diff[color]) == 0 {
			delete(diff, color)
		}
	}
	// printf("diff2=%v\n", diff)
	if len(diff) != 0 {
		return nil
	}
	return res
}

package main

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

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

func init() {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 4096), 1e9)
}

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

func sint64() int64 {
	s := sstring()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

func sint64sl(n int64) []int64 {
	sl := make([]int64, n)
	for i := range sl {
		sl[i] = sint64()
	}
	return sl
}

func suint64() uint64 {
	s := sstring()
	i := uint64(0)
	for _, r := range s {
		i *= 10
		i += uint64(r - '0')
	}
	return i
}

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

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		m := sint64()
		a := sint64sl(n)
		b := sint64sl(n)
		c := sint64sl(m)
		res := sol(n, m, a, b, c)
		if res == nil {
			printf("NO\n")
			continue
		}
		printf("YES\n")
		for _, d := range res {
			printf("%d ", d)
		}
		printf("\n")
	}
}

func sol(n, m int64, a, b, c []int64) []int64 {
	diff := map[int64][]int64{} // desiredcolor: []position
	for i := range a {
		if a[i] == b[i] {
			continue
		}
		diff[b[i]] = append(diff[b[i]], int64(i))
	}
	if len(diff) == 0 {
		return solSame(n, m, a, b, c)
	}
	// printf("diff=%v\n", diff)
	return solDiff(n, m, a, b, c, diff)
}

func solSame(n, m int64, a, b, c []int64) []int64 {
	set := map[int64]int64{}
	for i := range a {
		set[a[i]] = int64(i) + 1
	}
	// printf("set=%v\n", set)
	res := make([]int64, m)
	if v, ok := set[c[len(c)-1]]; !ok {
		return nil
	} else {
		for i := range res {
			res[i] = v
		}
	}
	return res
}

func solDiff(n, m int64, a, b, c []int64, diff map[int64][]int64) []int64 {
	res := make([]int64, m)
	for i := len(c) - 1; i >= 0; i-- {
		color := c[i]
		if len(diff[color]) == 0 {
			if i == len(c)-1 {
				return nil
			}
			res[i] = res[len(res)-1] // paint last plank, for it to be repainted later
			continue
		}
		res[i] = diff[color][len(diff[color])-1]+1
		diff[color] = diff[color][:len(diff[color])-1]
		if len(diff[color]) == 0 {
			delete(diff, color)
		}
	}
	// printf("diff2=%v\n", diff)
	if len(diff) != 0 {
		return nil
	}
	return res
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := os.Stdout
	var t int
	fmt.Fscan(in,&t)
	for u:=1;u<=t;u++{
		var n, m int
		fmt.Fscan(in,&n,&m)
		a := make([]int,n+5)
		b := make([]int,n+5)
		c := make([]int,m+5)
		ned := map[int][]int{}
		des := map[int]int{}
		for i:=1;i<=n;i++{
			fmt.Fscan(in,&a[i])
			if des[a[i]]==0{
				des[a[i]]=i
			}
		}
		for i:=1;i<=n;i++{
			fmt.Fscan(in,&b[i])
			if b[i]!=a[i]{
				ned[b[i]] = append(ned[b[i]],i)
			}
		}
		for i:=1;i<=m;i++{
			fmt.Fscan(in,&c[i])
		}
		flag := true
		ops := []int{}
		er := []int{}
		for i:=1;i<=m;i++{
			if len(ned[c[i]])>0{
				tar := ned[c[i]][0]
				ned[c[i]] = ned[c[i]][1:]
				if len(er)>0{
					for j:=0;j<len(er);j++{
						ops = append(ops,tar)
					}
					er = []int{}
				}
				ops = append(ops,tar)
				des[c[i]]=tar
			}else{
				if des[c[i]]!=0{
					if len(er)>0{
						for j:=0;j<len(er);j++{
							ops = append(ops,des[c[i]])
						}
						er = []int{}
					}
					ops = append(ops,des[c[i]])
				}else{
					er = append(er,1)
				}
			}
		}
		for _,v := range ned{
			if len(v)>0{
				flag = false
			}
		}
		if len(er)>0{
			flag = false
		}
		if flag{
			fmt.Fprintln(out,"YES")
			for i:=0;i<len(ops);i++{
				fmt.Fprintf(out,"%d ",ops[i])
			}
			fmt.Fprintln(out)
		}else{
			fmt.Fprintln(out,"NO")
		}
	}
}

package main

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

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

var n, m int
var color [100005][]int
var a, b, c, same, ans, cnt [100005]int
var colored int

func main() {
	var t int
	fmt.Fscan(in, &t)
	for t > 0 {
		t--
		fmt.Fscan(in, &n, &m)
		for i := 1; i <= n; i++ {
			fmt.Fscan(in, &a[i])
		}
		for i := 1; i <= n; i++ {
			fmt.Fscan(in, &b[i])
			if a[i] == b[i] {
				same[a[i]] = i
			} else {
				color[b[i]] = append(color[b[i]], i)
				cnt[b[i]]++
			}
		}
		for i := 1; i <= m; i++ {
			fmt.Fscan(in, &c[i])
		}
		no := false
		colored = 0
		for i := m; i >= 1; i-- {
			x := c[i]
			if cnt[x] > 0 {
				pos := color[x][cnt[x]-1]
				ans[i] = pos
				a[pos] = x
				cnt[x]--
				colored = pos
			} else if same[x] != 0 {
				fmt.Println("same", same[x])
				ans[i] = same[x]
				colored = same[x]
			} else if colored != 0 {
				ans[i] = colored
			} else {
				no = true
				break
			}
			// fmt.Print(i, " ", ans[i], "|")
		}
		for i := 1; i <= n; i++ {
			if a[i] != b[i] {
				no = true
			}
		}

		if no {
			fmt.Fprintln(out, "NO")
		} else {
			fmt.Fprintln(out, "YES")
			for i := 1; i <= m; i++ {
				fmt.Fprint(out, ans[i], " ")
			}
			fmt.Fprintln(out)
		}
		for i := 1; i <= n; i++ {
			same[a[i]] = 0
			cnt[b[i]] = 0
			color[b[i]] = nil
		}
		// out.Flush()
	}
	out.Flush()
}


After a long day, Alice and Bob decided to play a little game. The game board consists of $$$n$$$ cells in a straight line, numbered from $$$1$$$ to $$$n$$$, where each cell contains a number $$$a_i$$$ between $$$1$$$ and $$$n$$$. Furthermore, no two cells contain the same number. A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $$$i$$$ to cell $$$j$$$ only if the following two conditions are satisfied:   the number in the new cell $$$j$$$ must be strictly larger than the number in the old cell $$$i$$$ (i.e. $$$a_j > a_i$$$), and  the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $$$|i-j|\bmod a_i = 0$$$). Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of numbers. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le n$$$). Furthermore, there are no pair of indices $$$i \neq j$$$ such that $$$a_i = a_j$$$.

Print $$$s$$$ — a string of $$$n$$$ characters, where the $$$i$$$-th character represents the outcome of the game if the token is initially placed in the cell $$$i$$$. If Alice wins, then $$$s_i$$$ has to be equal to "A"; otherwise, $$$s_i$$$ has to be equal to "B".

NoteIn the first sample, if Bob puts the token on the number (not position):   $$$1$$$: Alice can move to any number. She can win by picking $$$7$$$, from which Bob has no move.  $$$2$$$: Alice can move to $$$3$$$ and $$$5$$$. Upon moving to $$$5$$$, Bob can win by moving to $$$8$$$. If she chooses $$$3$$$ instead, she wins, as Bob has only a move to $$$4$$$, from which Alice can move to $$$8$$$.  $$$3$$$: Alice can only move to $$$4$$$, after which Bob wins by moving to $$$8$$$.  $$$4$$$, $$$5$$$, or $$$6$$$: Alice wins by moving to $$$8$$$.  $$$7$$$, $$$8$$$: Alice has no move, and hence she loses immediately.

package main

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

var in = bufio.NewScanner(os.Stdin)

func readInt() int {
	if !in.Scan() {
		panic(in.Err())
	}
	res := 0
	for _, b := range in.Bytes() {
		res = res*10 + int(b-'0')
	}
	return res
}

type pair struct {
	pos, val int
}

type byVal []pair

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

func main() {
	in.Split(bufio.ScanWords)

	n := readInt()
	a := make([]int, n)
	pv := make([]pair, n)
	for i := range a {
		a[i] = readInt()
		pv[i].pos = i
		pv[i].val = a[i]
	}
	ans := make([]byte, n)
	sort.Sort(sort.Reverse(byVal(pv)))
	for _, pv := range pv {
		for i := pv.pos - pv.val; i >= 0; i -= pv.val {
			if ans[i] == 'B' {
				ans[pv.pos] = 'A'
				break
			}
		}
		if ans[pv.pos] == 0 {
			for i := pv.pos + pv.val; i < n; i += pv.val {
				if ans[i] == 'B' {
					ans[pv.pos] = 'A'
					break
				}
			}
		}
		if ans[pv.pos] == 0 {
			ans[pv.pos] = 'B'
		}
	}
	fmt.Printf("%s\n", ans)
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1033C(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n int
	Fscan(in, &n)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	dp := make([]byte, n)
	var f func(int) byte
	f = func(p int) (res byte) {
		d := &dp[p]
		if *d > 0 {
			return *d
		}
		defer func() { *d = res }()
		for i := p % a[p]; i < n; i += a[p] {
			if a[i] > a[p] && f(i) == 'B' {
				return 'A'
			}
		}
		return 'B'
	}
	for i := 0; i < n; i++ {
		f(i)
	}
	Fprint(out, string(dp))
}

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

package main

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

var n int
var a, dp []int

func f(index int) int {
	if a[index] == n {
		return 2
	}
	if dp[index] != 0 {
		return dp[index]
	}
	for _, k := range []int{-1, 1} {
		for i := 1; ; i++ {
			newIndex := index + k*i*a[index]
			if newIndex < 0 || newIndex >= n {
				break
			}
			if a[newIndex] < a[index] {
				continue
			}
			if f(newIndex) == 2 {
				dp[index] = 1
				return dp[index]
			}
		}
	}
	dp[index] = 2
	return dp[index]
}

func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d\n", &n)
	a = make([]int, n)
	dp = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &a[i])
	}

	outBuf := bufio.NewWriter(os.Stdout)
	defer func() { outBuf.Flush() }()

	for i := 0; i < n; i++ {
		fmt.Fprintf(outBuf, "%c", "AB"[f(i)-1])
	}
}