package main

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

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

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

func rw(r io.Reader, w io.Writer) {
	var t int
	fmt.Fscan(r, &t)
	for ti := 0; ti < t; ti++ {
		var n int
		fmt.Fscan(r, &n)
		var a = make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(r, &a[i])
		}
		w1, w2, w3 := do(a, n)
		fmt.Fprintln(w, w1, w2, w3)
	}
}

func do(a []int, n int) (int, int, int) {
	ans := 1
	l := 0
	r := 0
	d := a[0]
	m := map[int][]int{}
	for i, v := range a {
		m[v] = append(m[v], i)
	}
	// log.Print(a, n)
	for v, b := range m {
		c := 1
		k := b[0]
		if ans == 1 && v < d {
			d = v
			l = b[0]
			r = b[0]
		}
		for i := 1; i < len(b); i++ {
			if c2 := c + 1 - (b[i] - b[i-1] - 1); c2 > 1 {
				c = c2
			} else {
				c = 1
				k = b[i]
			}
			if ans < c || (ans == c && v < d) {
				ans = c
				d = v
				l = k
				r = b[i]
			}
			// log.Print(ans, d, l, r)
		}
	}
	return d, l + 1, r + 1
}

package main

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

func max(x int, num ...int) int {
	mx := x
	for _, j := range num {
		if j > mx {
			mx = j
		}
	}
	return mx
}

const maxn = 200100
var a []int

type Node struct {
	ans		int
	pref 	int
	suff 	int
	sum 	int
}

func newNode(v int) Node {
	var res Node
	res.sum = v
	res.pref = max(0, v)
	res.suff = max(0, v)
	res.ans = max(0, v)
	return res
}

func merge(l, r Node) Node {
	var res Node
	res.sum = l.sum + r.sum
	res.pref = max(l.pref, l.sum + r.pref)
	res.suff = max(r.suff, r.sum + l.suff)
	res.ans = max(l.ans, r.ans, l.suff + r.pref)
	return res
}

var t [4 * maxn]Node

func build(id, l, r int) {
	if (r - l == 1) {
		t[id] = newNode(-1)
		return
	}
	m := (r + l) / 2
	build(id * 2 + 1, l, m)
	build(id * 2 + 2, m, r)
	t[id] = merge(t[id * 2 + 1], t[id * 2 + 2])
}

func upd(id, l, r, p, v int) {
	if r - l == 1 {
		t[id] = newNode(v)
		return
	}
	m := (r + l) / 2
	if p < m {
		upd(id * 2 + 1, l, m, p, v)
	} else {
		upd(id * 2 + 2, m, r, p, v)
	}
	t[id] = merge(t[id * 2 + 1], t[id * 2 + 2])
}

func main() {
	in := bufio.NewReader(os.Stdin)
	var T int
	T = readInt(in)
	for ; T > 0; T-- {
		n := readInt(in)
		a = readArrInt(in)
		build(0, 0, n)
		mp := make(map[int] []int, n)
		for i := 0; i < n; i++ {
			if _, ok := mp[a[i]]; ok {
				mp[a[i]] = append(mp[a[i]], i)
			} else {
				mp[a[i]] = []int {i}
			}
		}
		var ans, ans_v int
		for k, idxs := range mp {
			for _, idx := range idxs {
				upd(0, 0, n, idx, 1)
			}
			cur := t[0].ans
			if cur > ans {
				ans = cur
				ans_v = k
			}
			for _, idx := range idxs {
				upd(0, 0, n, idx, -1)
			}
		}
		for i := 0; i < n; i++ {
			if a[i] == ans_v {
				a[i] = 1
			} else {
				a[i] = -1
			}
		}
		var ans_l, ans_r, mx int
		var cur_l, cur int
		for i := 0; i < n; i++ {
			if cur + a[i] < 0 {
				cur_l = i + 1
				cur = 0
			} else {
				cur += a[i]
				if cur > mx {
					mx = cur
					ans_l = cur_l
					ans_r = i
				}
			}
		}
		fmt.Println(ans_v, ans_l + 1, ans_r + 1)
	}
}

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

package main

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

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

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

type Elem struct {
	curr   int
	max    int
	start  int
	end    int
	liter  int
	pstart int
}

func NewElem(curr, max, start, end, liter, pstart int) *Elem {
	return &Elem{curr: curr, max: max, start: start, end: end, liter: liter, pstart: pstart}
}

func (e Elem) String() string {
	return fmt.Sprintf("CURR %d ;MAX %d; START: %d, END %d; LITER %d\n", e.curr, e.max, e.start, e.end, e.liter)
}

func main() {
	scanner := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var niter int
	fmt.Fscan(scanner, &niter)
	for iiter := 0; iiter < niter; iiter++ {
		var n int
		fmt.Fscan(scanner, &n)
		slc := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(scanner, &slc[i])
		}
		m := make(map[int]*Elem)
		for i := 0; i < n; i++ {
			if tmp, ok := m[slc[i]]; ok {
				if tmp.curr+2-(i-tmp.liter) > 1 {
					tmp.curr = tmp.curr + 2 - (i - tmp.liter)
				} else {
					tmp.curr = 1
					tmp.pstart = i
					//tmp.end = i
				}
				tmp.liter = i
				if tmp.curr > tmp.max {
					tmp.max = tmp.curr
					tmp.start = tmp.pstart
					tmp.end = i
				}
			} else {
				m[slc[i]] = NewElem(1, 1, i, i, i, i)
			}
		}
		rmax := 0
		res, l, r := 0, 0, 0
		//fmt.Println("!!!", m)
		for key, i := range m {
			if i.max >= rmax {
				rmax = i.max
				res = key
				r = i.end
				l = i.start
			}
		}
		fmt.Fprintln(writer, res, l+1, r+1)
	}
}

package main

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

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

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

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

		segs := make(map[int][][]int)
		for i := 0; i < n; i++ {
			s := []int{i, i}
			for j := i + 1; j < n; j++ {
				if a[i] != a[j] {
					break
				}
				s[1] = j
				i = j
			}

			segs[a[i]] = append(segs[a[i]], s)
		}

		var max int
		z, l, r := a[0], 0, 0
		for k, s2 := range segs {
			c := s2[0][1] - s2[0][0] + 1
			l2 := s2[0][0]
			r2 := s2[0][1]
			if c > max {
				z = k
				max = c
				l, r = l2, r2
			}

			for j := 1; j < len(s2); j++ {
				d := s2[j][0] - s2[j-1][1] - 1
				c -= d
				if c <= 0 {
					c = s2[j][1] - s2[j][0] + 1
					l2 = s2[j][0]
				} else {
					c += s2[j][1] - s2[j][0] + 1
				}
				if c > max {
					z = k
					max = c
					l, r = l2, s2[j][1]
				}
			}
		}
		fmt.Println(z, l+1, r+1)
	}
}

package main

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

// https://space.bilibili.com/206214/dynamic
func run(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var T, n, v int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		pos := map[int][]int{}
		for i := 1; i <= n; i++ {
			Fscan(in, &v)
			pos[v] = append(pos[v], i)
		}
		maxS, ans, l, r := 1, v, n, n
		for v, ps := range pos {
			for i, sum, st := 1, 1, ps[0]; i < len(ps); i++ {
				if sum -= ps[i] - ps[i-1] - 1; sum < 0 {
					sum, st = 0, ps[i]
				}
				if sum++; sum > maxS {
					maxS, ans, l, r = sum, v, st, ps[i]
				}
			}
		}
		Fprintln(out, ans, l, r)
	}
}

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

package main

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

// https://space.bilibili.com/206214/dynamic
func run(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var T, n, v int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		pos := map[int][]int{}
		for i := 1; i <= n; i++ {
			Fscan(in, &v)
			pos[v] = append(pos[v], i)
		}
		maxS, ans, l, r := 1, v, n, n
		for v, ps := range pos {
			for i, s, minS, minI := 1, 1, 0, ps[0]; i < len(ps); i++ {
				if s -= ps[i] - ps[i-1] - 1; s < minS {
					minS, minI = s, ps[i]
				}
				if s++; s-minS > maxS {
					maxS, ans, l, r = s-minS, v, minI, ps[i]
				}
			}
		}
		Fprintln(out, ans, l, r)
	}
}

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

package main

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

// https://space.bilibili.com/206214/dynamic
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	solve := func(curCase int) {
		var n int
		Fscan(in, &n)
		// int64
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}

		pos := map[int][]int{}
		for i, v := range a {
			pos[v] = append(pos[v], i)
		}

		mxs := 1
		mxv, mxl, mxr := a[0], 0,0
		for v, ps := range pos {
			mins := 0
			mi := 0
			s := 0
			for i, p := range ps {
				if i == 0 {
					s = 1
					mi = p - 1
				} else if p == ps[i-1]+1 {
					s++
					if s-mins > mxs {
						mxs = s - mins
						mxv, mxl, mxr = v, mi+1, p
					}
				} else {
					s -= p - ps[i-1] - 1
					if s < mins {
						mins = s
						mi = p-1
					}
					s++
					if s-mins > mxs {
						mxs = s - mins
						mxv, mxl, mxr =v, mi+1, p
					}
				}
			}
		}
		Fprintln(out, mxv, mxl+1, mxr+1)
	}

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

	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"
	"math"
	"os"
)

/*
1
10
2 1 2 2 2 4 2 2 1 4
---
2 3 8
*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n int
	scanf("%d\n", &n)

	numToIdcs := make(map[int][]int)

	for i := 0; i < n; i++ {
		var num int
		scanf("%d", &num)

		numToIdcs[num] = append(numToIdcs[num], i)
	}
	scanf("\n")

	bestSum := 0
	bestNum := -1
	bestL, bestR := -1, -1

	for num, idcs := range numToIdcs {
		if bestNum == -1 {
			bestSum = 1
			bestNum = num
			bestL, bestR = idcs[0], idcs[0]
		}

		sum := 1
		l := idcs[0]

		for i := 1; i < len(idcs); i++ {
			prevIdx, idx := idcs[i-1], idcs[i]
			toRemove := idx - prevIdx - 1

			sum += 1 - toRemove
			if sum <= 1 {
				sum = 1
				l = idx
			}

			if sum > bestSum {
				bestSum = sum
				bestNum = num
				bestL, bestR = l, idx
			}
		}
	}

	fmt.Fprintln(writer, bestNum, bestL+1, bestR+1)
}

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

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

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int) int {
	return nums[minIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func minIdx(nums ...int) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	return nums[maxIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func maxIdx(nums ...int) int {
	maxNumIdx := 0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = i
		}
	}

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func sum(nums ...int) (sum int) {
	for _, num := range nums {
		sum += num
	}
	return
}

//goland:noinspection GoUnusedFunction
func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int64) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := int64(5); i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

//goland:noinspection GoUnusedFunction
func factorize(num int64) []int64 {
	factors := make([]int64, 0)

	for ftr := int64(2); ftr*ftr <= num; ftr++ {
		for num%ftr == 0 {
			num /= ftr
			factors = append(factors, ftr)
		}
	}

	if num > 1 {
		factors = append(factors, num)
	}

	return factors
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	for t > 0 {
		t--
		var n int
		fmt.Fscan(in, &n)
		a := make([]int, n+5)
		for i := 1; i <= n; i++ {
			fmt.Fscan(in, &a[i])
		}
		mp := map[int][]int{}
		for i := 1; i <= n; i++ {
			mp[a[i]] = append(mp[a[i]], i)
		}
		num := -1
		l := 0
		r := 0
		cur := 0
		ans := -1
		al := 0
		ar := 0
		for v, now := range mp {
			l = 0
			r = 0
			for i := 0; i < len(now); i++ {
				if l == 0 && r == 0 {
					l = now[i]
					r = now[i]
					cur = 1
					if ans < cur {
						al = l
						ar = r
						num = v
						ans = cur
					}
					continue
				}
				cur += 1 - (now[i] - r - 1)
				if cur <= 0 {
					cur = 1
					l = now[i]
					r = now[i]
				} else {
					r = now[i]
				}
				if ans < cur {
					al = l
					ar = r
					num = v
					ans = cur
				}
			}
		}
		fmt.Fprintln(out, num, al, ar)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"runtime/debug"
)

/* begin of MyIO */
const eof = 0

type any = interface{}

type MyIO struct {
	in  *os.File
	out *bufio.Writer
	_i  int
	_n  int
	buf []byte
}

func NewIO(in *os.File, out *os.File) *MyIO {
	return &MyIO{
		in,
		bufio.NewWriter(out),
		0, 0, make([]byte, 1<<12),
	}
}

func (myio *MyIO) rc() byte {
	if myio._i == myio._n {
		myio._n, _ = myio.in.Read(myio.buf)
		if myio._n == 0 { // EOF
			return eof
		}
		myio._i = 0
	}
	b := myio.buf[myio._i]
	myio._i++
	return b
}

func (myio *MyIO) ReadInt() (x int) {
	rc := myio.rc
	b := rc()
	neg := false
	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
		if b == '-' {
			neg = true
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int(b&15)
	}
	if neg {
		return -x
	}
	return
}

func (myio *MyIO) ReadInt64() (x int64) {
	rc := myio.rc
	b := rc()
	neg := false

	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
		if b == '-' {
			neg = true
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int64(b&15)
	}
	if neg {
		return -x
	}
	return x
}

func (myio *MyIO) ReadBytes() (s []byte) {
	rc := myio.rc
	b := rc()
	for ; b <= ' '; b = rc() {
	}
	for ; b > ' '; b = rc() {
		s = append(s, b)
	}
	return
}

func (myio *MyIO) ReadStr() (s string) {
	return string(myio.ReadBytes())
}

func (myio *MyIO) Flush() {
	myio.out.Flush()
}

func (myio *MyIO) Printf(format string, a ...any) (n int, err error) {
	return fmt.Fprintf(myio.out, format, a...)
}

func (myio *MyIO) Println(a ...any) (n int, err error) {
	return fmt.Fprintln(myio.out, a...)
}

func (myio *MyIO) Print(a ...any) (n int, err error) {
	return fmt.Fprint(myio.out, a...)
}

var myio *MyIO

func ri() int {
	return myio.ReadInt()
}

func ri64() int64 {
	return myio.ReadInt64()
}

func ria(_n int) []int {
	a := make([]int, _n)
	for i := range a {
		a[i] = ri()
	}
	return a
}

/* End of MyIO */

func sol() {
	m := make(map[int][]int)
	n := ri()
	a := ria(n)
	for i, v := range a {
		if _, ok := m[v]; ok {
			m[v] = append(m[v], i)
		} else {
			m[v] = append([]int{}, i)
		}
	}
	ansv, ansl, anss, anse := a[0], 1, 0, 0
	for k, s := range m {
		ps, pv := 1, s[0]
		for i := 1; i < len(s); i++ {
			// last to cur
			ta := ps - (s[i] - s[i-1] - 1) + 1
			if ta > 1 {
				ps = ta
				if ps > ansl {
					ansv = k
					ansl = ps
					anss = pv
					anse = s[i]
				}
			} else {
				ps, pv = 1, s[i]
			}
		}
	}
	myio.Println(ansv, anss+1, anse+1)
}

func main() {
	debug.SetGCPercent(-1)
	in, out := os.Stdin, os.Stdout
	myio = NewIO(in, out)
	defer myio.Flush()

	var t = 1
	t = ri()
	for ; t > 0; t-- {
		sol()
	}
}

package main

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

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

func slove() {
	var n int
	fmt.Fscan(r, &n)
	nums := make([]int, n)
	hash := make(map[int][]int)
	for i:=0;i<n;i++ {
		fmt.Fscan(r, &nums[i])
		hash[nums[i]] = append(hash[nums[i]], i)
	}
	ans := 1
	a1, a2, a3 := nums[0], 0, 0
	for k, v := range hash {
		cur := 1
		left := v[0]
		for i:=1;i<len(v);i++ {
			cur += v[i-1] - v[i] + 2
			cur = max(1, cur)
			if cur == 1 {
				left = v[i]
			}
			if ans < cur {
				ans = cur
				a1 = k
				a2 = left
				a3 = v[i]
			}
		}
	}
	fmt.Fprintln(w, a1, a2+1, a3+1)

}

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

func main() {
	defer w.Flush()
	var n int
	fmt.Fscan(r, &n)
	for i:=1;i<=n;i++ {
		slove()
	}
}

package main

import "fmt"

func max(x int, num ...int) int {
	mx := x
	for _, j := range num {
		if j > mx {
			mx = j
		}
	}
	return mx
}

const maxn = 200100
var a[maxn]int

type Node struct {
	ans		int
	pref 	int
	suff 	int
	sum 	int
}

func newNode(v int) Node {
	var res Node
	res.sum = v
	res.pref = max(0, v)
	res.suff = max(0, v)
	res.ans = max(0, v)
	return res
}

func merge(l, r Node) Node {
	var res Node
	res.sum = l.sum + r.sum
	res.pref = max(l.pref, l.sum + r.pref)
	res.suff = max(r.suff, r.sum + l.suff)
	res.ans = max(l.ans, r.ans, l.suff + r.pref)
	return res
}

var t [4 * maxn]Node

func build(id, l, r int) {
	if (r - l == 1) {
		t[id] = newNode(-1)
		return
	}
	m := (r + l) / 2
	build(id * 2 + 1, l, m)
	build(id * 2 + 2, m, r)
	t[id] = merge(t[id * 2 + 1], t[id * 2 + 2])
}

func upd(id, l, r, p, v int) {
	if r - l == 1 {
		t[id] = newNode(v)
		return
	}
	m := (r + l) / 2
	if p < m {
		upd(id * 2 + 1, l, m, p, v)
	} else {
		upd(id * 2 + 2, m, r, p, v)
	}
	t[id] = merge(t[id * 2 + 1], t[id * 2 + 2])
}

func solve() {
	var n int
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
	}
	build(0, 0, n)
	mp := make(map[int] []int, n)
	for i := 0; i < n; i++ {
		if _, ok := mp[a[i]]; ok {
			mp[a[i]] = append(mp[a[i]], i)
		} else {
			mp[a[i]] = []int {i}
		}
	}
	var ans, ans_v int
	for k, idxs := range mp {
		for _, idx := range idxs {
			upd(0, 0, n, idx, 1)
		}
		cur := t[0].ans
		if cur > ans {
			ans = cur
			ans_v = k
		}
		for _, idx := range idxs {
			upd(0, 0, n, idx, -1)
		}
	}
	for i := 0; i < n; i++ {
		if a[i] == ans_v {
			a[i] = 1
		} else {
			a[i] = -1
		}
	}
	var ans_l, ans_r, mx int
	var cur_l, cur int
	for i := 0; i < n; i++ {
		if cur + a[i] < 0 {
			cur_l = i + 1
			cur = 0
		} else {
			cur += a[i]
			if cur > mx {
				mx = cur
				ans_l = cur_l
				ans_r = i
			}
		}
	}
	fmt.Println(ans_v, ans_l + 1, ans_r + 1)
	fmt.Println(ans, mx)
}

func main() {
	var t int
	fmt.Scan(&t)
	for ; t > 0; t-- {
		solve()
	}
}

package main

import "fmt"

func max(x int, num ...int) int {
	mx := x
	for _, j := range num {
		if j > mx {
			mx = j
		}
	}
	return mx
}

const maxn = 200100
var a[maxn]int

type Node struct {
	ans		int
	pref 	int
	suff 	int
	sum 	int
}

func newNode(v int) Node {
	var res Node
	res.sum = v
	res.pref = max(0, v)
	res.suff = max(0, v)
	res.ans = max(0, v)
	return res
}

func merge(l, r Node) Node {
	var res Node
	res.sum = l.sum + r.sum
	res.pref = max(l.pref, l.sum + r.pref)
	res.suff = max(r.suff, r.sum + l.suff)
	res.ans = max(l.ans, r.ans, l.suff + r.pref)
	return res
}

var t [4 * maxn]Node

func build(id, l, r int) {
	if (r - l == 1) {
		t[id] = newNode(-1)
		return
	}
	m := (r + l) / 2
	build(id * 2 + 1, l, m)
	build(id * 2 + 2, m, r)
	t[id] = merge(t[id * 2 + 1], t[id * 2 + 2])
}

func upd(id, l, r, p, v int) {
	if r - l == 1 {
		t[id] = newNode(v)
		return
	}
	m := (r + l) / 2
	if p < m {
		upd(id * 2 + 1, l, m, p, v)
	} else {
		upd(id * 2 + 2, m, r, p, v)
	}
	t[id] = merge(t[id * 2 + 1], t[id * 2 + 2])
}

func solve() {
	var n int
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
	}
	mp := make(map[int] []int, n)
	for i := 0; i < n; i++ {
		if _, ok := mp[a[i]]; ok {
			mp[a[i]] = append(mp[a[i]], i)
		} else {
			mp[a[i]] = []int {i}
		}
	}
	var ans, ans_v int
	for k, idxs := range mp {
		for _, idx := range idxs {
			upd(0, 0, n, idx, 1)
		}
		cur := t[0].ans
		if cur >= ans {
			ans = cur
			ans_v = k
		}
		for _, idx := range idxs {
			upd(0, 0, n, idx, -1)
		}
	}
	for i := 0; i < n; i++ {
		if a[i] == ans_v {
			a[i] = 1
		} else {
			a[i] = -1
		}
	}
	var ans_l, ans_r, mx int
	var cur_l, cur int
	for i := 0; i < n; i++ {
		if cur + a[i] < 0 {
			cur_l = i + 1
			cur = 0
		} else {
			cur += a[i]
			if cur > mx {
				mx = cur
				ans_l = cur_l
				ans_r = i
			}
		}
	}
	if mx != ans {
		panic("123")
	}
	fmt.Println(ans_v, ans_l + 1, ans_r + 1)
}

func main() {
	build(0, 0, maxn)
	var t int
	fmt.Scan(&t)
	for ; t > 0; t-- {
		solve()
	}
}

package main

import "fmt"

func max(x int, num ...int) int {
	mx := x
	for _, j := range num {
		if j > mx {
			mx = j
		}
	}
	return mx
}

const maxn = 200100
var a[maxn]int

type Node struct {
	ans		int
	pref 	int
	suff 	int
	sum 	int
}

func newNode(v int) Node {
	var res Node
	res.sum = v
	res.pref = max(0, v)
	res.suff = max(0, v)
	res.ans = max(0, v)
	return res
}

func merge(l, r Node) Node {
	var res Node
	res.sum = l.sum + r.sum
	res.pref = max(l.pref, l.sum + r.pref)
	res.suff = max(r.suff, r.sum + l.suff)
	res.ans = max(l.ans, r.ans, l.suff + r.pref)
	return res
}

var t [4 * maxn]Node

func build(id, l, r int) {
	if (r - l == 1) {
		t[id] = newNode(-1)
		return
	}
	m := (r + l) / 2
	build(id * 2 + 1, l, m)
	build(id * 2 + 2, m, r)
	t[id] = merge(t[id * 2 + 1], t[id * 2 + 2])
}

func upd(id, l, r, p, v int) {
	if r - l == 1 {
		t[id] = newNode(v)
		return
	}
	m := (r + l) / 2
	if p < m {
		upd(id * 2 + 1, l, m, p, v)
	} else {
		upd(id * 2 + 2, m, r, p, v)
	}
	t[id] = merge(t[id * 2 + 1], t[id * 2 + 2])
}

func solve() {
	var n int
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
	}
	mp := make(map[int] []int, n)
	for i := 0; i < n; i++ {
		if _, ok := mp[a[i]]; ok {
			mp[a[i]] = append(mp[a[i]], i)
		} else {
			mp[a[i]] = []int {i}
		}
	}
	var ans, ans_v int
	for k, idxs := range mp {
		for _, idx := range idxs {
			upd(0, 0, n, idx, 1)
		}
		cur := t[0].ans
		if cur >= ans {
			ans = cur
			ans_v = k
		}
		for _, idx := range idxs {
			upd(0, 0, n, idx, -1)
		}
	}
	for i := 0; i < n; i++ {
		if a[i] == ans_v {
			a[i] = 1
		} else {
			a[i] = -1
		}
	}
	var ans_l, ans_r, mx int
	var cur_l, cur int
	for i := 0; i < n; i++ {
		if cur + a[i] < 0 {
			cur_l = i + 1
			cur = 0
		} else {
			cur += a[i]
			if cur > mx {
				mx = cur
				ans_l = cur_l
				ans_r = i
			}
		}
	}
	fmt.Println(ans_v, ans_l + 1, ans_r + 1)
}

func main() {
	build(0, 0, maxn)
	var t int
	fmt.Scan(&t)
	for ; t > 0; t-- {
		solve()
	}
}

package main

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

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

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

type Elem struct {
	curr  int
	max   int
	start int
	end   int
	liter int
}

func NewElem(curr, max, start, end, liter int) *Elem {
	return &Elem{curr: curr, max: max, start: start, end: end, liter: liter}
}

func (e Elem) String() string {
	return fmt.Sprintf("CURR %d ;MAX %d; START: %d, END %d; LITER %d\n", e.curr, e.max, e.start, e.end, e.liter)
}

func main() {
	scanner := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var niter int
	fmt.Fscan(scanner, &niter)
	for iiter := 0; iiter < niter; iiter++ {
		var n int
		fmt.Fscan(scanner, &n)
		slc := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(scanner, &slc[i])
		}
		m := make(map[int]*Elem)
		for i := 0; i < n; i++ {
			if tmp, ok := m[slc[i]]; ok {
				if tmp.curr+2-(i-tmp.liter) > 1 {
					tmp.curr = tmp.curr + 2 - (i - tmp.liter)
				} else {
					tmp.curr = 1
					tmp.start = i
					tmp.end = i
				}
				tmp.liter = i
				if tmp.curr > tmp.max {
					tmp.max = tmp.curr
					tmp.end = i
				}
			} else {
				m[slc[i]] = NewElem(1, 1, i, i, i)
			}
		}
		rmax := 0
		res, l, r := 0, 0, 0
		//fmt.Println("!!!", m)
		for key, i := range m {
			if i.max >= rmax {
				rmax = i.max
				res = key
				r = i.end
				l = i.start
			}
		}
		fmt.Fprintln(writer, res, l+1, r+1)
	}
}

package main

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

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

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

type Elem struct {
	curr  int
	max   int
	start int
	end   int
	liter int
}

func NewElem(curr, max, start, end, liter int) *Elem {
	return &Elem{curr: curr, max: max, start: start, end: end, liter: liter}
}

func (e Elem) String() string {
	return fmt.Sprintf("CURR %d ;MAX %d; START: %d, END %d; LITER %d\n", e.curr, e.max, e.start, e.end, e.liter)
}

func main() {
	scanner := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var niter int
	fmt.Fscan(scanner, &niter)
	for iiter := 0; iiter < niter; iiter++ {
		var n int
		fmt.Fscan(scanner, &n)
		slc := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(scanner, &slc[i])
		}
		m := make(map[int]*Elem)
		for i := 0; i < n; i++ {
			if tmp, ok := m[slc[i]]; ok {
				if tmp.curr+2-(i-tmp.liter) > 1 {
					tmp.curr = tmp.curr + 2 - (i - tmp.liter)
				} else {
					tmp.curr = 1
					tmp.start = i
					tmp.end = i
				}
				tmp.liter = i
				if tmp.curr > tmp.max {
					tmp.max = tmp.curr
					tmp.end = i
				}
			} else {
				m[slc[i]] = NewElem(1, 1, i, i, i)
			}
		}
		rmax := 0
		res, l, r := 0, 0, 0
		fmt.Println("!!!", m)
		for key, i := range m {
			if i.max >= rmax {
				rmax = i.max
				res = key
				r = i.end
				l = i.start
			}
		}
		fmt.Fprintln(writer, res, l+1, r+1)
	}
}

package main

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

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

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

type Elem struct {
	curr  int
	max   int
	start int
	end   int
	liter int
}

func NewElem(curr, max, start, end, liter int) *Elem {
	return &Elem{curr: curr, max: max, start: start, end: end, liter: liter}
}

func (e Elem) String() string {
	return fmt.Sprintf("CURR %d ;MAX %d; START: %d, END %d; LITER %d\n", e.curr, e.max, e.start, e.end, e.liter)
}

func main() {
	scanner := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var niter int
	fmt.Fscan(scanner, &niter)
	for iiter := 0; iiter < niter; iiter++ {
		var n int
		fmt.Fscan(scanner, &n)
		slc := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(scanner, &slc[i])
		}
		m := make(map[int]*Elem)
		for i := 0; i < n; i++ {
			if tmp, ok := m[slc[i]]; ok {
				if tmp.curr+2-(i-tmp.liter) > 1 {
					tmp.curr = tmp.curr + 2 - (i - tmp.liter)
				} else {
					tmp.curr = 1
					tmp.start = i
					tmp.end = i
				}
				tmp.liter = i
				if tmp.curr > tmp.max {
					tmp.max = tmp.curr
					tmp.end = i
				}
			} else {
				m[slc[i]] = NewElem(1, 1, i, i, i)
			}
		}
		res, l, r := 0, 0, 0
		//fmt.Println("!!!", m)
		for key, i := range m {
			if i.max >= res {
				res = key
				r = i.end
				l = i.start
			}
		}
		fmt.Fprintln(writer, res, l+1, r+1)
	}
}

package main

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

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

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

type Elem struct {
	curr  int
	max   int
	start int
	end   int
	liter int
}

func NewElem(curr, max, start, end, liter int) *Elem {
	return &Elem{curr: curr, max: max, start: start, end: end, liter: liter}
}

func (e Elem) String() string {
	return fmt.Sprintf("CURR %d ;MAX %d; START: %d, END %d; LITER %d\n", e.curr, e.max, e.start, e.end, e.liter)
}

func main() {
	scanner := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var niter int
	fmt.Fscan(scanner, &niter)
	for iiter := 0; iiter < niter; iiter++ {
		var n int
		fmt.Fscan(scanner, &n)
		slc := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(scanner, &slc[i])
		}
		m := make(map[int]*Elem)
		for i := 0; i < n; i++ {
			if tmp, ok := m[slc[i]]; ok {
				if tmp.curr+2-(i-tmp.liter) > 1 {
					tmp.curr = tmp.curr + 2 - (i - tmp.liter)
				} else {
					tmp.curr = 1
					tmp.start = i
					tmp.end = i
				}
				tmp.liter = i
				if tmp.curr > tmp.max {
					tmp.max = tmp.curr
					tmp.end = i
				}
			} else {
				m[slc[i]] = NewElem(1, 1, i, i, i)
			}
		}
		res, l, r := 0, 0, 0
		fmt.Println("!!!", m)
		for key, i := range m {
			if i.max >= res {
				res = key
				r = i.end
				l = i.start
			}
		}
		fmt.Fprintln(writer, res, l+1, r+1)
	}
}

package main

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

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

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

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

		segs := make(map[int][][]int)
		for i := 0; i < n; i++ {
			s := []int{i, i}
			for j := i + 1; j < n; j++ {
				if a[i] != a[j] {
					break
				}
				s[1] = j
				i = j
			}
			if s[0] != s[1] {
				segs[a[i]] = append(segs[a[i]], s)
			}
		}

		var max int
		z, l, r := a[0], 0, 0
		for k, s2 := range segs {
			c := s2[0][1] - s2[0][0] + 1
			l2 := s2[0][0]
			r2 := s2[0][1]
			if c > max {
				z = k
				max = c
				l, r = l2, r2
			}

			for j := 1; j < len(s2); j++ {
				d := s2[j][0] - s2[j-1][1] - 1
				c -= d
				if c <= 0 {
					c = s2[j][1] - s2[j][0] + 1
					l2 = s2[j][0]
				} else {
					c += s2[j][1] - s2[j][0] + 1
				}
				if c > max {
					z = k
					max = c
					l, r = l2, s2[j][1]
				}
			}
		}
		fmt.Println(z, l+1, r+1)
	}
}

package main

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

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

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

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

		segs := make(map[int][][]int)
		for i := 0; i < n; i++ {
			s := []int{i, i}
			for j := i + 1; j < n; j++ {
				if a[i] != a[j] {
					break
				}
				s[1] = j
				i = j
			}
			if s[0] != s[1] {
				segs[a[i]] = append(segs[a[i]], s)
			}
		}

		var max int
		z, l, r := a[0], 0, 0
		for k, s2 := range segs {
			c := s2[0][1] - s2[0][0] + 1
			l2 := s2[0][0]
			r2 := s2[0][1]
			if c > max {
				z = k
				max = c
				l, r = l2, r2
			}

			for j := 1; j < len(s2); j++ {
				d := s2[j][0] - s2[j-1][1] - 1
				c -= d
				if c-d <= 0 {
					c = s2[j][1] - s2[j][0] + 1
					l2, r2 = s2[j][0], s2[j][1]
				}

				if c > max {
					z = k
					max = c
					l, r = l2, r2
				}
			}
		}
		fmt.Println(z, l+1, r+1)
	}
}

package main

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

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

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

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

		m := make([]bool, n)
		var max int
		z, l, r := a[0], 0, 0
		for i := 0; i < n; i++ {
			if m[i] || counts[a[i]] <= max {
				continue
			}
			counts[a[i]]--
			c := 1
			for j := i + 1; j < n; j++ {
				if a[i] == a[j] {
					m[i] = true
					counts[a[i]]--
					c++
				} else {
					c--
					if c == 0 {
						break
					}
				}

				if c > max {
					max = c
					z, l, r = a[i], i, j
				}
			}
		}

		fmt.Fprintln(out, z, l+1, r+1)
	}
}

package main

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

// https://space.bilibili.com/206214/dynamic
func run(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var T, n, v int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		pos := map[int][]int{}
		for i := 0; i < n; i++ {
			Fscan(in, &v)
			pos[v] = append(pos[v], i)
		}
		maxS, ans, l, r := 1, v, n-1, n-1
		for v, ps := range pos {
			for i, s, minS, minI := 1, 1, 0, ps[0]; i < len(ps); i++ {
				if ps[i]-ps[i-1] > 1 {
					s -= ps[i] - ps[i-1] - 1
					if s < minS {
						minS, minI = s, ps[i-1]
					}
				}
				s++
				if s-minS > maxS {
					maxS, ans, l, r = s-minS, v, minI, ps[i]
				}
			}
		}
		Fprintln(out, ans, l+1, r+1)
	}
}

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

package main

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

// https://space.bilibili.com/206214/dynamic
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	solve := func(curCase int) {
		var n int
		Fscan(in, &n)
		// int64
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}

		pos := map[int][]int{}
		for i, v := range a {
			pos[v] = append(pos[v], i)
		}

		mxs := 1
		mxv, mxl, mxr := a[0], 0,0
		for v, ps := range pos {
			mins := 0
			mi := 0
			s := 0
			for i, p := range ps {
				if i == 0 {
					s = 1
					mi = p - 1
				} else if p == ps[i]+1 {
					s++
					if s-mins > mxs {
						mxs = s - mins
						mxv, mxl, mxr = v, mi+1, p
					}
				} else {
					s -= p - ps[i] - 1
					if s < mins {
						mins = s
						mi = p-1
					}
					s++
					if s-mins > mxs {
						mxs = s - mins
						mxv, mxl, mxr =v, mi+1, p
					}
				}
			}
		}
		Fprintln(out, mxv, mxl+1, mxr+1)
	}

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

	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"
	"math"
	"os"
)

/*
1
10
2 1 2 2 2 4 2 2 1 4
---
2 3 8
*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n int
	scanf("%d\n", &n)

	numToIdcs := make(map[int][]int)

	for i := 0; i < n; i++ {
		var num int
		scanf("%d", &num)

		numToIdcs[num] = append(numToIdcs[num], i)
	}
	scanf("\n")

	bestSum := 0
	bestNum := -1
	bestL, bestR := -1, -1

	for num, idcs := range numToIdcs {
		if bestNum == -1 {
			bestSum = 1
			bestNum = num
			bestL, bestR = idcs[0], idcs[0]
		}

		sum := 1
		l := idcs[0]

		for i := 1; i < len(idcs); i++ {
			prevIdx, idx := idcs[i-1], idcs[i]
			toRemove := idx - prevIdx - 1

			if toRemove >= sum {
				sum = 1
				l = idx
			} else {
				sum += 1 + toRemove
			}

			if sum > bestSum {
				bestSum = sum
				bestNum = num
				bestL, bestR = l, idx
			}
		}
	}

	fmt.Fprintln(writer, bestNum, bestL+1, bestR+1)
}

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

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

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int) int {
	return nums[minIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func minIdx(nums ...int) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	return nums[maxIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func maxIdx(nums ...int) int {
	maxNumIdx := 0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = i
		}
	}

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func sum(nums ...int) (sum int) {
	for _, num := range nums {
		sum += num
	}
	return
}

//goland:noinspection GoUnusedFunction
func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int64) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := int64(5); i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

//goland:noinspection GoUnusedFunction
func factorize(num int64) []int64 {
	factors := make([]int64, 0)

	for ftr := int64(2); ftr*ftr <= num; ftr++ {
		for num%ftr == 0 {
			num /= ftr
			factors = append(factors, ftr)
		}
	}

	if num > 1 {
		factors = append(factors, num)
	}

	return factors
}

package main

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

/*
1
10
2 1 2 2 2 4 2 2 1 4
---
2 3 8
*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n int
	scanf("%d\n", &n)

	numToIdcs := make(map[int][]int)

	for i := 0; i < n; i++ {
		var num int
		scanf("%d", &num)

		numToIdcs[num] = append(numToIdcs[num], i)
	}
	scanf("\n")

	bestSum := 0
	bestNum := -1
	bestL, bestR := -1, -1

	for num, idcs := range numToIdcs {
		sum := 1
		l := idcs[0]

		if bestNum == -1 {
			bestSum = 1
			bestNum = num
			bestL, bestR = idcs[0], idcs[0]
		}

		for i := 1; i < len(idcs); i++ {
			prevIdx, idx := idcs[i-1], idcs[i]
			toRemove := idx - prevIdx - 1

			if toRemove >= sum {
				sum = 1
				l = idx
			} else {
				sum += 1
			}

			if sum > bestSum {
				bestSum = sum
				bestNum = num
				bestL, bestR = l, idx
			}
		}
	}

	fmt.Fprintln(writer, bestNum, bestL+1, bestR+1)
}

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

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

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int) int {
	return nums[minIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func minIdx(nums ...int) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	return nums[maxIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func maxIdx(nums ...int) int {
	maxNumIdx := 0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = i
		}
	}

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func sum(nums ...int) (sum int) {
	for _, num := range nums {
		sum += num
	}
	return
}

//goland:noinspection GoUnusedFunction
func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int64) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := int64(5); i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

//goland:noinspection GoUnusedFunction
func factorize(num int64) []int64 {
	factors := make([]int64, 0)

	for ftr := int64(2); ftr*ftr <= num; ftr++ {
		for num%ftr == 0 {
			num /= ftr
			factors = append(factors, ftr)
		}
	}

	if num > 1 {
		factors = append(factors, num)
	}

	return factors
}


package main

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

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

func max(i, j int) int {
    if i > j {
        return i
    }
    return j
}

func solve() {
    var n int
    var s string
    Fscan(in, &n, &s)

    a := make([]int, n + 1)
    for i := 1; i <= n; i++ {
        Fscan(in, &a[i]);
        a[i] += a[i - 1];
    }

    ans := 0
    for i := 1; i <= n; i++ {
        if string(s[i - 1]) != "1" {
            continue;
        }

        j := i;
        for ; j < n && string(s[j]) == "1"; j++ {

        }

        res := a[j] - a[i - 1];
        for k := i; k <= j; k++ {
            v := 0
            if i >= 2 {
                v = a[i - 2]
            }
            res = max(res, a[k - 1] - v + a[j] - a[k])
        }
        ans += res

        i = j;
    }

    Fprintln(out, ans)
}

func main() {
    var T int
    Fscan(in, &T)
    for ; T > 0; T-- {
        solve()
    }
    out.Flush()
}

package main

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

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

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

type BestAns struct {
	withLid    int64
	withoutLid int64
}

var MOD int64 = 1000*1000*1000 + 7

func max(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}
func main() {
	defer writer.Flush()

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

	for q := 0; q < t; q++ {
		var n int
		var boxes string

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

		boxStates := make([]bool, n, n)
		arr := make([]int64, n, n)

		scanf("%s\n", &boxes)
		for i := 0; i < n; i++ {
			if boxes[i] == '1' {
				boxStates[i] = true
			}
		}

		for i := 0; i < n; i++ {
			if i != n-1 {
				scanf("%d ", &arr[i])
			} else {
				scanf("%d\n", &arr[i])
			}
		}

		dp := make([]BestAns, n, n)

		if boxStates[0] {
			dp[0] = BestAns{
				withLid:    arr[0],
				withoutLid: 0,
			}
		}

		for i := 1; i < n; i++ {
			if boxStates[i] {
				dp[i].withLid = arr[i] + max(dp[i-1].withLid, dp[i-1].withoutLid)
				dp[i].withoutLid = max(dp[i-1].withLid, dp[i-1].withoutLid+arr[i-1])
			} else {
				dp[i].withLid = -math.MaxInt64
				dp[i].withoutLid = max(dp[i-1].withLid, dp[i-1].withoutLid)
			}
		}
		ans := max(dp[n-1].withLid, dp[n-1].withoutLid)

		printf("%d\n", ans)
	}
}

package main

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

var sc = bufio.NewScanner(os.Stdin)

const mod int = 998244353
const N int = 5e5 + 5

func main() {
	sc.Buffer(make([]byte, 500000*1+1), 500000)
	sc.Split(bufio.ScanWords)

	T := nextInt()
	for T > 0 {
		T--
		ans := 0
		n, s := nextInt(), nextString()
		a := make([]int, n)
		for i := 0; i < n; i++ {
			a[i] = nextInt()
		}
		var i int
		for i = 0; i < n && s[i] == '1'; i++ {
			ans += a[i]
		}
		if i < n {
			sum, mn := a[i], a[i]
			for i = i + 1; i < n; i++ {
				if s[i] == '0' {
					ans += sum - mn
					sum = a[i]
					mn = a[i]
				} else {
					sum += a[i]
					mn = min(mn, a[i])
				}
			}
			fmt.Println(ans + sum - mn)
		} else {
			fmt.Println(ans)
		}
	}
}

func max(args ...int) int {
	res := args[0]
	for _, v := range args {
		if v > res {
			res = v
		}
	}
	return res
}

func min(args ...int) int {
	res := args[0]
	for _, v := range args {
		if v < res {
			res = v
		}
	}
	return res
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

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

package main

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

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

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

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

		aa := make([]int64, n)
		for i := range aa {
			fmt.Fscan(in, &aa[i])
		}

		last := -1
		var tot int64
		for i := 0; i < n; i++ {
			if s[i] == '1' {
				if last >= 0 && aa[i] < aa[last] {
					tot += aa[last]
					s[last] = '1'
					s[i] = '0'
					last = i
				} else {
					tot += aa[i]
				}
			} else if s[i] == '0' {
				last = i
			}
		}

		fmt.Fprintln(out, tot)
	}
}

package main

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

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

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

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

		aa := make([]int64, n)
		for i := range aa {
			fmt.Fscan(in, &aa[i])
		}

		last := -1
		if s[0] == '0' {
			last = 0
		}

		for i := 1; i < n; i++ {
			if s[i] == '1' && last >= 0 && aa[i] < aa[last] {
				s[last] = '1'
				s[i] = '0'
				last = i
			} else if s[i] == '0' {
				last = i
			}
		}

		var tot int64
		for i, ch := range s {
			if ch == '1' {
				tot += aa[i]
			}
		}
		fmt.Fprintln(out, tot)
	}
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
)
var r *bufio.Reader
var w *bufio.Writer
func max(nums ...int) int{
	res:=math.MinInt
	for _,i:=range nums{
		if i>res{
			res=i
		}
	}
	return res
}
func solve(){
	defer w.Flush()
	var n int
	var good []byte
	fmt.Fscan(r, &n, &good)
	nums:=make([]int, n)

	take:=make([]int, n) //take[i], ans of [i..n-1], if i has used its 1 on itself
	leave:=make([]int, n) //leave[i], ans of [i..n-1], if i has not used its own 1

	for i:=0;i<n;i++{
		fmt.Fscan(r, &nums[i])
	}
	if good[n-1]==49{
		take[n-1]=nums[n-1]
	}
	for i:=n-2;i>=0;i--{
		leave[i]= max(leave[i+1], take[i+1])
		take[i] = max(leave[i+1], take[i+1])
		if good[i]==49{
			take[i]=max(take[i], nums[i]+take[i+1])
			if good[i+1]==48{
				take[i]=max(take[i], nums[i]+leave[i+1])
			}
		}
		if good[i+1]==49{
			leave[i]=max(leave[i], nums[i]+leave[i+1])
		}
	}
	//fmt.Println("take ", take)
	//fmt.Println("leave", leave)
	fmt.Fprintln(w, max(take[0],leave[0]));
}
func init(){
	r = bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)
}
func main(){
	defer  w.Flush()
	var t int;
	fmt.Fscan(r, &t);
	for t>0{
		solve();
		t--;
	}
}

package main

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

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

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

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

	var T int
	var n int
	var s string
	var x int

	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		Fscan(in, &s)

		r := []int{}
		for i := 0; i < n; i++ {
			Fscan(in, &x)
			r = append(r, x)
		}

		a, b := 0, 0
		if s[0] == '1' {
			a = r[0]
		}

		for i := 1; i < n; i++ {
			if s[i] == '0' {
				continue
			}
			tb := b
			if s[i-1] == '0' {
				b = max(a, b) + r[i-1]
			} else {
				b = b + r[i-1]
			}
			a = max(a, tb) + r[i]
			//Fprintln(out, "i ", i, a, b)
		}

		Fprintln(out, max(a, b))
	}
}

package main

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

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

	var T int
	for fmt.Fscan(r, &T); T > 0; T-- {

		var n int
		var b []byte
		fmt.Fscan(r, &n, &b)
		for i := range b { b[i] -= '0' }
		b = append([]byte{0}, b...)

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

		i := 0
		res := 0
		for i <= n {
			for i <= n && b[i] == 0 { i++ }
			j := i
			sum_ := a[j-1]
			min_ := a[j-1]
			for j <= n && b[j] == 1 {
				sum_ += a[j]
				if a[j] < min_ { min_ = a[j] }
				j++
			}
			if j > i { res += sum_ - min_ }
			i = j
		}
		fmt.Fprintln(w, res)
	}

}

package main

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

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

func sum_min(a []int, l, r int) (int, int) {
	_sum := 0
	_min := a[l]
	for j := l; j < r; j++ {
		_sum += a[j]
		if a[j] < _min { _min = a[j] }
	}
	return _sum, _min
}

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

	var T int
	for fmt.Fscan(r, &T); T > 0; T-- {

		var n int
		var b []byte
		fmt.Fscan(r, &n, &b)
		for i := range b { b[i] -= '0' }

		a := make([]int, n)
		for i := range a { fmt.Fscan(r, &a[i]) }

		i := 0
		res := 0
		for i < n {
			for i < n && b[i] == 0 { i++ }
			j := i
			for j < n && b[j] == 1 { j++ }
			if i == 0 {
				res += sum(a, j)
			} else if j > i {
				_sum, _min := sum_min(a, i-1, j)
				res += _sum - _min
			}
			i = j
		}
		fmt.Fprintln(w, res)
	}

}

package main

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

var sc = bufio.NewScanner(os.Stdin)

const mod int = 998244353
const N int = 5e5 + 5

func main() {
	sc.Buffer(make([]byte, 500000*1+1), 500000)
	sc.Split(bufio.ScanWords)

	T := nextInt()
	for T > 0 {
		T--
		ans := 0
		n, s := nextInt(), nextString()
		a := make([]int, n)
		sum, mn := 0, N
		flag := false
		for i := 0; i < n; i++ {
			a[i] = nextInt()
			if s[i] == '0' {
				if !flag {
					if s[0] == '1' {
						ans += sum
					} else {
						ans += max(0, sum-mn)
					}
					flag = true
				} else {
					ans += max(0, sum-mn)
				}
				sum = a[i]
				mn = a[i]
			} else {
				sum += a[i]
				mn = min(mn, a[i])
			}
		}
		ans += max(0, sum-mn)
		fmt.Println(ans)
	}
}

func max(args ...int) int {
	res := args[0]
	for _, v := range args {
		if v > res {
			res = v
		}
	}
	return res
}

func min(args ...int) int {
	res := args[0]
	for _, v := range args {
		if v < res {
			res = v
		}
	}
	return res
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

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

package main

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

var sc = bufio.NewScanner(os.Stdin)

const mod int = 998244353
const N int = 5e5 + 5

func main() {
	sc.Buffer(make([]byte, 500000*1+1), 500000)
	sc.Split(bufio.ScanWords)

	T := nextInt()
	for T > 0 {
		T--
		ans := 0
		n, s := nextInt(), nextString()
		a := make([]int, n)
		sum, mn := 0, N
		flag := false
		for i := 0; i < n; i++ {
			a[i] = nextInt()
			if s[i] == '0' {
				if !flag {
					ans += sum
					flag = true
				} else {
					ans += max(0, sum-mn)
				}
				sum = a[i]
				mn = a[i]
			} else {
				sum += a[i]
				mn = min(mn, a[i])
			}
		}
		ans += max(0, sum-mn)
		fmt.Println(ans)
	}
}

func max(args ...int) int {
	res := args[0]
	for _, v := range args {
		if v > res {
			res = v
		}
	}
	return res
}

func min(args ...int) int {
	res := args[0]
	for _, v := range args {
		if v < res {
			res = v
		}
	}
	return res
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

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

package main

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

var sc = bufio.NewScanner(os.Stdin)

const mod int = 998244353
const N int = 5e5 + 5

func main() {
	sc.Buffer(make([]byte, 500000*1+1), 500000)
	sc.Split(bufio.ScanWords)

	T := nextInt()
	for T > 0 {
		T--
		ans := 0
		n, s := nextInt(), nextString()
		a := make([]int, n)
		sum, mn := 0, N
		flag := false
		for i := 0; i < n; i++ {
			a[i] = nextInt()
			if s[i] == '0' {
				if !flag {
					ans += sum
					flag = true
				} else {
					ans += sum - mn
				}
				sum = a[i]
				mn = a[i]
			} else {
				sum += a[i]
				mn = min(mn, a[i])
			}
		}
		ans += sum - mn
		fmt.Println(ans)
	}
}

func max(args ...int) int {
	res := args[0]
	for _, v := range args {
		if v > res {
			res = v
		}
	}
	return res
}

func min(args ...int) int {
	res := args[0]
	for _, v := range args {
		if v < res {
			res = v
		}
	}
	return res
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

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

package main

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

var sc = bufio.NewScanner(os.Stdin)

const mod int = 998244353
const N int = 5e5 + 5

func main() {
	sc.Buffer(make([]byte, 500000*1+1), 500000)
	sc.Split(bufio.ScanWords)

	T := nextInt()
	for T > 0 {
		T--
		ans := 0
		n, s := nextInt(), nextString()
		a := make([]int, n)
		sum, mn := 0, N
		for i := 0; i < n; i++ {
			a[i] = nextInt()
			if s[i] == '0' {
				if i-1 >= 0 {
					ans += sum - mn
				}
				sum = a[i]
				mn = a[i]
			} else {
				sum += a[i]
				mn = min(mn, a[i])
			}
		}
		ans += sum - mn
		fmt.Println(ans)
	}
}

func min(args ...int) int {
	res := args[0]
	for _, v := range args {
		if v < res {
			res = v
		}
	}
	return res
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

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

package main

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

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

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

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

		aa := make([]int64, n)
		for i := range aa {
			fmt.Fscan(in, &aa[i])
		}

		last := -1
		if s[0] == '0' {
			last = 0
		}

		var tot int64
		for i := 1; i < n; i++ {
			if s[i] == '1' {
				if last >= 0 && aa[i] < aa[last] {
					tot += aa[last]
					s[last] = '1'
					s[i] = '0'
					last = i
				} else {
					tot += aa[i]
				}
			} else if s[i] == '0' {
				last = i
			}
		}

		fmt.Fprintln(out, tot)
	}
}

package main

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

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

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

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

		aa := make([]int64, n)
		for i := range aa {
			fmt.Fscan(in, &aa[i])
		}

		last := -1
		if s[0] == '0' {
			last = 0
		}

		for i := 1; i < n; i++ {
			if s[i] == '1' && aa[i] < aa[i-1] && last >= 0 {
				s[last] = '1'
				s[i] = '0'
				last = i
			} else if s[i] == '0' {
				last = i
			}
		}

		var tot int64
		for i, ch := range s {
			if ch == '1' {
				tot += aa[i]
			}
		}
		fmt.Fprintln(out, tot)
	}
}

package main

import (
	"fmt"
	"math"
)
func max(nums ...int) int{
	res:=math.MinInt
	for _,i:=range nums{
		if i>res{
			res=i
		}
	}
	return res
}
func solve(){
	var n int
	fmt.Scan(&n)
	var s string
	fmt.Scan(&s)
	good:=[]byte(s)
	nums:=make([]int, n)

	take:=make([]int, n) //take[i], ans of [i..n-1], if i has used its 1 on itself
	leave:=make([]int, n) //leave[i], ans of [i..n-1], if i has not used its own 1

	for i:=0;i<n;i++{
		fmt.Scan(&nums[i])
	}
	if good[n-1]==49{
		take[n-1]=nums[n-1]
	}
	for i:=n-2;i>=0;i--{
		leave[i]= max(leave[i+1], take[i+1])
		take[i] = max(leave[i+1], take[i+1])
		if good[i]==49{
			take[i]=max(take[i], nums[i]+take[i+1])
		}
		if good[i+1]==49{
			leave[i]=max(leave[i], nums[i]+leave[i+1])
		}
	}
	//fmt.Println("take ", take)
	//fmt.Println("leave", leave)
	fmt.Println(max(take[0],leave[0]));
}
func main(){
	var t int;
	fmt.Scan(&t);
	for t>0{
		solve();
		t--;
	}
}

package main

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

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

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

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

	var T int
	var n int
	var s string
	var x int

	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		Fscan(in, &s)

		r := []int{}
		for i := 0; i < n; i++ {
			Fscan(in, &x)
			r = append(r, x)
		}

		a, b := 0, 0
		if s[0] == '1' {
			a = r[0]
			b = r[0]
		}

		for i := 1; i < n; i++ {
			if s[i] == '0' {
				continue
			}
			tb := b
			if s[i-1] == '0' {
				b = max(a, b) + r[i-1]
			} else {
				b = b + r[i-1]
			}
			a = max(a, tb) + r[i]
			//Fprintln(out, "i ", i, a, b)
		}

		Fprintln(out, max(a, b))
	}
}

package main

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

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

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

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

	var T int
	var n int
	var s string
	var x int

	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		Fscan(in, &s)

		r := []int{}
		for i := 0; i < n; i++ {
			Fscan(in, &x)
			r = append(r, x)
		}

		a, b := 0, 0
		if s[0] == '1' {
			a = r[0]
			b = r[0]
		}

		for i := 1; i < n; i++ {
			if s[i] == '0' {
				continue
			}
			if s[i-1] == '0' {
				a, b = max(a, b)+r[i], max(a, b)+r[i-1]
			} else {
				a, b = max(a, b)+r[i], b+r[i-1]
			}
			//Fprintln(out, "i ", i, a, b)
		}

		Fprintln(out, max(a, b))
	}
}


package main

import (
	"fmt"
	"math"
	"sort"
)

type Point struct {
	x    int
	y    int
	dist float64
}

type PointList []Point

func (p PointList) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }
func (p PointList) Len() int           { return len(p) }
func (p PointList) Less(i, j int) bool { return p[i].dist < p[j].dist }

func main() {

	var t, n int
	fmt.Scanln(&t)

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

		var plist PointList
		for j := 0; j < n; j++ {
			var x, y int
			fmt.Scanf("%d %d\n", &x, &y)

			point := Point{x: x, y: y, dist: math.Sqrt(float64(x*x + y*y))}
			plist = append(plist, point)
		}

		sort.Sort(plist)

		steps := ""
		currX := 0
		currY := 0

		for j := 0; j < n; j++ {
			p := plist[j]

			if p.x >= currX {
				for k := 0; k < p.x-currX; k++ {
					steps += "R"
				}
				currX = p.x
			} else {
				if currX != 0 {
					steps = ""
					break
				}
			}

			if p.y >= currY {
				for k := 0; k < p.y-currY; k++ {
					steps += "U"
				}
				currY = p.y
			} else {
				if currY != 0 {
					steps = ""
					break
				}
			}
		}

		if steps == "" {
			fmt.Println("NO")
		} else {
			fmt.Println("YES")
			fmt.Println(steps)
		}
	}
}

package main

import (
	"fmt"
	"strings"
)

func CollectingPackages(iList [][]int) (string, string) {
	rs := ""
	xTemp := 0
	yTemp := 0
	count := 0
	for {
		ySub := yTemp
		for j := 0; j < len(iList); j++ {
			if iList[j][0] == xTemp {
				if iList[j][1] < ySub {
					return "NO", ""
				}
				count++
				if iList[j][1] > yTemp {
					rs += strings.Repeat("U", iList[j][1]-yTemp)
					yTemp = iList[j][1]
				}
			}
		}
		xTemp++
		if count == len(iList) {
			return "YES", rs
		}
		rs += "R"
	}
}

func main() {
	var t int
	fmt.Scanf("%d\n", &t)
	for i := 0; i < t; i++ {
		var n int
		var iList [][]int
		fmt.Scanf("%d\n", &n)
		for j := 0; j < n; j++ {
			var x, y int
			var temp []int
			fmt.Scanf("%d %d\n", &x, &y)
			temp = append(temp, x, y)
			iList = append(iList, temp)
		}
		ok, rs := CollectingPackages(iList)
		if ok == "YES" {
			fmt.Println(ok)
			fmt.Println(rs)
		} else {
			fmt.Println(ok)
		}
	}
}

package main

import (
	"fmt"
	"sort"
)

type point struct {
	x, y int
}

func main() {
	var (
		n int
		t int
	)
	fmt.Scan(&t)
	for ; t > 0; t-- {
		fmt.Scan( &n)
		arr := make([] point, n)

		for i := 0; i < n; i++ {
			var x , y int
			fmt.Scan(&x, &y)
			arr[i] = point{x, y}
		}
		sort.SliceStable(arr, func(i, j int) bool{
			if arr[i].x != arr[j].x {
				return arr[i].x < arr[j].x
			}
			return arr[i].y < arr[j].y
		})

		start := point{0,0}
		succ := true
		ans := ""
		for i := 0; i < n; i++ {
			if start.x > arr[i].x || start.y > arr[i].y {
				succ = false
				break
			}
			for j := 0; j < arr[i].x - start.x; j++ {
				ans = ans + "R"
			}
			for j := 0; j < arr[i].y - start.y; j++ {
				ans = ans + "U"
			}
			start = arr[i];
		}
		if (succ) {
			fmt.Println("YES")
			fmt.Println(ans)
		} else {
			fmt.Println("NO")
		}
	}

}

package main

import (
	"fmt"
	"sort"
)

type Point struct {
	x,y int
}

func main() {
	var t int
	fmt.Scan(&t)
	for T := 0 ; T < t ; T ++ {
		var n int
		fmt.Scan(&n)
		 var pts []Point
		pts = make([]Point , n)
		for i := 0 ; i < n ; i ++ {
			fmt.Scan(&pts[i].x,&pts[i].y)
		}
		var zer Point
		zer.x = 0
		zer.y = 0
		pts = append(pts , zer)
		sort.Slice(pts , func(i,j int) bool{
			if pts[i].x < pts[j].x {
				return true
			} else if pts[i].x == pts[j].x {
				if pts[i].y < pts[j].y {
					return true
				} else{
					return false
				}
			} else {
				return false
			}
		})
		ok := true
		var output string
		output = ""
		for i := 1 ; i <= n ; i ++ {
			if pts[i-1].x <= pts[i].x && pts[i-1].y <= pts[i].y {
				var r,u int
				r = pts[i].x - pts[i-1].x
				u = pts[i].y - pts[i-1].y
				for j := 0 ; j < r ; j ++ {
					output = output + "R"
				}
				for j := 0 ; j < u ; j ++ {
					output = output + "U"
				}
			} else{
				ok = false
				break
			}
		}
		if ok == true {
			fmt.Println("YES")
			fmt.Println(output)
		}else{
			fmt.Println("NO")
		}
	}
}

package main

import "fmt"

type atom struct{x,y int}

func Main(){
	var n int
	fmt.Scanf("%d\n",&n)
	a:=make([]atom,n)
	for i:=0;i<n;i++{
		var x,y int
		fmt.Scanf("%d %d\n",&x,&y)
		a[i]=atom{x,y}
	}
	for rd:=0;rd<n;rd++{
		for i:=0;i+1<n;i++{
			if a[i].x+a[i].y>a[i+1].x+a[i+1].y{
				a[i],a[i+1]=a[i+1],a[i]
			}
		}
	}
	nowx,nowy:=0,0
	for i:=0;i+1<n;i++{
		if a[i].x>a[i+1].x || a[i].y>a[i+1].y{
			fmt.Println("NO")
			return
		}
	}
	fmt.Println("YES")
	for i:=0;i<n;i++{
		for ;nowx<a[i].x;nowx++{
			fmt.Print("R")
		}
		for ;nowy<a[i].y;nowy++{
			fmt.Print("U")
		}
	}
	fmt.Print("\n")
}

func main(){
	var t int
	fmt.Scanf("%d\n",&t)
	for ;t>0;t--{
		Main()
	}
}

package main

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

type point struct {
	x, y, d int
}

type points []point

func (p points) Len() int {
	return len(p)
}
func (p points) Less(i, j int) bool {
	return p[i].d < p[j].d
}
func (p points) Swap(i, j int) {
	p[i], p[j] = p[j], p[i]
}

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

func main() {
	var t int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		var n int
		fmt.Scan(&n)
		p := make([]point, n)
		for j := 0; j < n; j++ {
			fmt.Scan(&p[j].x, &p[j].y)
			p[j].d = p[j].x + p[j].y
		}
		sort.Sort(points(p))
		// 近い順に全て辿れるか？
		// fmt.Println("debug - points", p)
		now := point{0, 0, 0}
		ans := ""
		ok := true
		for j := 0; j < n; j++ {
			if abs(p[j].x-now.x)+abs(p[j].y-now.y) != abs(p[j].d-now.d) {
				ok = false
				break
			} else {
				ans += strings.Repeat("R", p[j].x-now.x)
				ans += strings.Repeat("U", p[j].y-now.y)
				now = p[j]
			}
		}
		if ok {
			fmt.Println("YES")
			fmt.Println(ans)
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import(
	"fmt"
	"sort"
)
type point struct{
	x int
	y int
}
type newlist []*point

func check(list newlist,n int) bool {
	for i:=1;i<n;i++{
		if list[i].y < list[i-1].y{
			return false
		}
	}
	return true
}
func main(){
	var t,n int
	fmt.Scan(&t)
	for ;t>0;t--{
		fmt.Scan(&n)
		var list newlist
		for i:=0;i<n;i++{
			var x,y int
			fmt.Scan(&x,&y)
			list = append(list,&point{x,y})
		}
		sort.Sort(list)
		if check(list,n)==false{
			fmt.Println("NO")
		}else{
			fmt.Println("YES")
			x,y:=0,0
			for i:=0;i<n;i++{
				for j:=x;j<list[i].x;j++{
					fmt.Printf("R")
				}
				for j:=y;j<list[i].y;j++{
					fmt.Printf("U")
				}
				x,y = list[i].x,list[i].y
			}
			fmt.Printf("\n")
		}
	}
}

func (a newlist) Len() int{
	return len(a)
}
func (a newlist) Less(i,j int) bool{
	if a[i].x == a[j].x{
		return a[i].y<a[j].y
	}else{
		return a[i].x < a[j].x
	}
}
func (a newlist) Swap(i,j int){
	a[i],a[j] = a[j],a[i]
}

package main

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

type Point struct {
	x, y int
}

type PointArr []Point

func (p PointArr) Len() int {
	return len(p)
}

func (p PointArr) Less(i, j int) bool {
	return p[i].x < p[j].x || (p[i].x == p[j].x && p[i].y <= p[j].y)
}

func (p PointArr) Swap(i, j int) {
	p[i], p[j] = p[j], p[i]
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var t, n, x, y int
	fmt.Fscanln(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscanln(reader, &n)
		coords := make([]Point, n)
		for i := 0; i < n; i++ {
			fmt.Fscanln(reader, &x, &y)
			coords[i] = Point{x: x, y: y}
		}
		sort.Sort(PointArr(coords))
		var ans strings.Builder
		has_error := false
		prev := Point{x: 0, y: 0}
		for i := 0; i < n; i++ {
			b := coords[i]
			if prev.y > b.y {
				// Can't move down
				has_error = true
				break
			}
			for prev.x < b.x {
				ans.WriteString("R")
				prev.x += 1
			}
			for prev.y < b.y {
				ans.WriteString("U")
				prev.y += 1
			}
			prev = b
		}
		if has_error {
			fmt.Fprintln(writer, "NO")
			continue
		}
		fmt.Fprintln(writer, "YES")
		fmt.Fprintln(writer, ans.String())
	}
}

package main

import "fmt"

import "sort"

type point struct {
	x, y int
}

func (a point) Less(p point) bool {
	if a.x == p.x {
		return a.y < p.y
	}
	return a.x < p.x
}

// ByXY is used for sort point
type ByXY []point

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

func main() {
	var t int
	fmt.Scan(&t)
	for tt := 0; tt < t; tt++ {
		var n int
		fmt.Scan(&n)
		p := make([]point, n)
		for i := range p {
			fmt.Scan(&p[i].x, &p[i].y)
		}
		sort.Sort(ByXY(p))
		flag := false
		var lx, ly int
		for i := range p {
			if p[i].x < lx || p[i].y < ly {
				flag = true
				break
			}
			lx, ly = p[i].x, p[i].y
		}
		if flag {
			fmt.Println("NO")
		} else {
			fmt.Println("YES")
			var lx, ly int
			for i := range p {
				for lx < p[i].x {
					fmt.Print("R")
					lx++
				}
				for ly < p[i].y {
					fmt.Print("U")
					ly++
				}
			}
			fmt.Println()
		}
	}
}

package main

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

type Vertex struct {
	x, y int
}

func getMove(from, to Vertex) string{
	var move string
	for i := 0; i < to.x - from.x; i++ {
		move = move + "R"
	}
	for j := 0; j < to.y - from.y; j++ {
		move = move + "U"
	}
	return move
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	var ans []string
	for i := 0; i < t; i++ {
		var n int
		var input []Vertex
		fmt.Fscan(in, &n)
		for j := 0; j < n; j++ {
			var x, y int
			fmt.Fscan(in, &x, &y)
			input = append(input, Vertex{x, y})
		}
		input = append(input, Vertex{0, 0})
		sort.Slice(input, func(i, j int)bool {
			if input[i].x < input[j].x{
				return true;
			}else{
				if input[i].y < input[j].y{
					return true;
				}else{
					return false;
				}
			}
		})
		ok := true
		n = n + 1
		var tmp string
		//fmt.Println(input)
		for j := 1; j < n; j++ {
			if input[j].x>=input[j-1].x&&input[j].y>=input[j-1].y {
				tmp = tmp + getMove(input[j-1], input[j])
			}else{
				ok = false
			}
		}
		if ok {
			ans = append(ans, "YES")
			ans = append(ans, tmp)
		}else{
			ans = append(ans, "NO")
		}
	}
	fmt.Fprintln(out, strings.Join(ans, "\n"));
}

package main

import (
	"fmt"
	"strings"
)

func CollectingPackages(iList [][]int) (string, string) {
	rs := ""
	xTemp := 0
	yTemp := 0
	count := 0
	for {
		flag := 0
		for j := 0; j < len(iList); j++ {
			if iList[j][0] == xTemp {
				flag++
				if flag == 1 {
					if iList[j][1] < yTemp {
						return "NO", ""
					}
				}
				count++
				if iList[j][1] > yTemp {
					rs += strings.Repeat("U", iList[j][1] - yTemp)
					yTemp = iList[j][1]
				}
			}
		}
		xTemp++
		if count == len(iList) {
			return "YES", rs
		}
		rs += "R"
	}
}

func main() {
	var t int
	fmt.Scanf("%d\n", &t)
	for i := 0; i < t; i++ {
		var n int
		var iList [][]int
		fmt.Scanf("%d\n", &n)
		for j := 0; j < n; j++ {
			var x, y int
			var temp []int
			fmt.Scanf("%d %d\n", &x, &y)
			temp = append(temp, x, y)
			iList = append(iList, temp)
		}
		ok, rs := CollectingPackages(iList)
		if ok == "YES" {
			fmt.Println(ok)
			fmt.Println(rs)
		} else {
			fmt.Println(ok)
		}
	}
}

package main

import (
	"fmt"
	"strings"
)

func CollectingPackages(iList [][]int) (string, string) {
	rs := ""
	xTemp := 0
	yTemp := 0
	count := 0
	for {
		flag := 0
		for j := 0; j < len(iList); j++ {
			if iList[j][0] == xTemp {
				flag++
				if flag == 1 {
					if iList[j][1] < yTemp {
						return "NO", ""
					}
					if iList[j][1] == yTemp {
						flag--
					}
				}
				count++
				if iList[j][1] > yTemp {
					rs += strings.Repeat("U", iList[j][1] - yTemp)
					yTemp = iList[j][1]
				}
			}
		}
		xTemp++
		if count == len(iList) {
			return "YES", rs
		}
		rs += "R"
	}
}

func main() {
	var t int
	fmt.Scanf("%d\n", &t)
	for i := 0; i < t; i++ {
		var n int
		var iList [][]int
		fmt.Scanf("%d\n", &n)
		for j := 0; j < n; j++ {
			var x, y int
			var temp []int
			fmt.Scanf("%d %d\n", &x, &y)
			temp = append(temp, x, y)
			iList = append(iList, temp)
		}
		ok, rs := CollectingPackages(iList)
		if ok == "YES" {
			fmt.Println(ok)
			fmt.Println(rs)
		} else {
			fmt.Println(ok)
		}
	}
}

package main

import (
	"fmt"
	"strings"
)

//
//func swap(xp *float64, yp *float64) {
//	temp := *xp;
//	*xp = *yp;
//	*yp = temp;
//}
//
//func swapArr(xp *[]int, yp *[]int) {
//	temp := *xp;
//	*xp = *yp;
//	*yp = temp;
//}
//
//
//// A function to implement bubble sort
//func bubbleSort(arr []float64, iList [][]int)  [][]int {
//	for i := 0; i < len(arr)-1; i++ {
//		for j := 0; j < len(arr)-i-1; j++ {
//			if arr[j] > arr[j+1] {
//				swap(&arr[j], &arr[j+1])
//				swapArr(&iList[j], &iList[j+1])
//			}
//		}
//	}
//	return iList
//}
//
//func calDistance(x1, y1 int) float64 {
//	return math.Sqrt(math.Pow(float64(x1), 2) + math.Pow(float64(y1), 2))
//}
//

func CollectingPackages(iList [][]int) (string, string) {
	rs := ""
	xTemp := 0
	yTemp := 0
	count := 0
	for {
		flag := 0
		for j := 0; j < len(iList); j++ {
			if iList[j][0] == xTemp {
				flag++
				if flag == 1 {
					if iList[j][1] < yTemp {
						return "NO", ""
					}
				}
				count++
				if iList[j][1] > yTemp {
					rs += strings.Repeat("U", iList[j][1] - yTemp)
					yTemp = iList[j][1]
				}
			}
		}
		xTemp++
		if count == len(iList) {
			return "YES", rs
		}
		rs += "R"
	}
}

func main() {
	var t int
	var iList [][]int
	fmt.Scanf("%d\n", &t)
	for i := 0; i < t; i++ {
		var n int
		fmt.Scanf("%d\n", &n)
		for j := 0; j < n; j++ {
			var x, y int
			var temp []int
			fmt.Scanf("%d %d\n", &x, &y)
			temp = append(temp, x, y)
			iList = append(iList, temp)
		}
	}
	ok, rs := CollectingPackages(iList)
	if ok == "YES" {
		fmt.Println(ok)
		fmt.Println(rs)
	} else {
		fmt.Println(ok)
	}
}

package main

import (
	"fmt"
	"math"
	"sort"
)

type Point struct {
	x    int
	y    int
	dist float64
}

type PointList []Point

func (p PointList) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }
func (p PointList) Len() int           { return len(p) }
func (p PointList) Less(i, j int) bool { return p[i].dist < p[j].dist }

func main() {

	var t, n int
	fmt.Scanln(&t)

	//reader := bufio.NewReader(os.Stdin)
	//scanner := bufio.NewScanner(os.Stdin)
	for i := 0; i < t; i++ {
		fmt.Scanln(&n)

		var plist PointList
		for j := 0; j < n; j++ {
			//var str string
			/*if scanner.Scan() {
				str = scanner.Text()
			}
			//str, _ := reader.ReadString('\n')
			//str = str[0 : len(str)-1]
			coords := strings.Split(str, " ")

			x, _ := strconv.Atoi(coords[0])
			y, _ := strconv.Atoi(coords[1])*/
			var x, y int
			fmt.Scan(&x)
			fmt.Scan(&y)
			//fmt.Scanln()

			point := Point{x: x, y: y, dist: math.Sqrt(float64(x*x + y*y))}
			plist = append(plist, point)
		}

		sort.Sort(plist)

		steps := ""
		currX := 0
		currY := 0
		prevDist := float64(0)
		for j := 0; j < n; j++ {
			p := plist[j]

			if p.dist == prevDist {
				steps = ""
				break
			} else {
				prevDist = p.dist
			}

			if p.x > currX {
				for k := 0; k < p.x-currX; k++ {
					steps += "R"
				}
				currX = p.x
			}

			if p.y > currY {
				for k := 0; k < p.y-currY; k++ {
					steps += "U"
				}
				currY = p.y
			}
		}

		if steps == "" {
			fmt.Println("NO")
		} else {
			fmt.Println("YES")
			fmt.Println(steps)
		}
	}
}

package main

import (
	"fmt"
	"math"
	"sort"
)

type Point struct {
	x    int
	y    int
	dist float64
}

type PointList []Point

func (p PointList) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }
func (p PointList) Len() int           { return len(p) }
func (p PointList) Less(i, j int) bool { return p[i].dist < p[j].dist }

func main() {

	var t, n int
	fmt.Scanln(&t)

	//reader := bufio.NewReader(os.Stdin)
	//scanner := bufio.NewScanner(os.Stdin)
	for i := 0; i < t; i++ {
		fmt.Scanln(&n)

		var plist PointList
		for j := 0; j < n; j++ {
			//var str string
			/*if scanner.Scan() {
				str = scanner.Text()
			}
			//str, _ := reader.ReadString('\n')
			//str = str[0 : len(str)-1]
			coords := strings.Split(str, " ")

			x, _ := strconv.Atoi(coords[0])
			y, _ := strconv.Atoi(coords[1])*/
			var x, y int
			fmt.Scanf("%d %d\n", &x, &y)

			point := Point{x: x, y: y, dist: math.Sqrt(float64(x*x + y*y))}
			plist = append(plist, point)
		}

		sort.Sort(plist)

		steps := ""
		currX := 0
		currY := 0
		prevDist := float64(0)
		for j := 0; j < n; j++ {
			p := plist[j]

			if p.dist == prevDist {
				steps = ""
				break
			} else {
				prevDist = p.dist
			}

			if p.x > currX {
				for k := 0; k < p.x-currX; k++ {
					steps += "R"
				}
				currX = p.x
			}

			if p.y > currY {
				for k := 0; k < p.y-currY; k++ {
					steps += "U"
				}
				currY = p.y
			}
		}

		if steps == "" {
			fmt.Println("NO")
		} else {
			fmt.Println("YES")
			fmt.Println(steps)
		}
	}
}

package main

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

type Vertex struct {
	x, y int
}

func getMove(from, to Vertex) string{
	var move string
	for i := 0; i < to.x - from.x; i++ {
		move = move + "R"
	}
	for j := 0; j < to.y - from.y; j++ {
		move = move + "U"
	}
	return move
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	var ans []string
	for i := 0; i < t; i++ {
		var n int
		var input []Vertex
		fmt.Fscan(in, &n)
		for j := 0; j < n; j++ {
			var x, y int
			fmt.Fscan(in, &x, &y)
			input = append(input, Vertex{x, y})
		}
		input = append(input, Vertex{0, 0})
		sort.Slice(input, func(i, j int)bool {
			if input[i].x < input[j].x{
				return true;
			}else{
				if input[i].y < input[j].y{
					return true;
				}else{
					return false;
				}
			}
		})
		ok := true
		n = n + 1
		var tmp string
		for j := 1; j < n; j++ {
			if (input[j].x+input[j].y)>(input[j-1].x+input[j-1].y) {
				tmp = tmp + getMove(input[j-1], input[j])
			}else{
				ok = false
			}
		}
		if ok {
			ans = append(ans, "YES")
			ans = append(ans, tmp)
		}else{
			ans = append(ans, "NO")
		}
	}
	fmt.Fprintln(out, strings.Join(ans, "\n"));
}

package main

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

type Vertex struct {
	x, y int
}

func getMove(from, to Vertex) string{
	var move string
	for i := 0; i < to.x - from.x; i++ {
		move = move + "R"
	}
	for j := 0; j < to.y - from.y; j++ {
		move = move + "U"
	}
	return move
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	var ans []string
	for i := 0; i < t; i++ {
		var n int
		var input []Vertex
		fmt.Fscan(in, &n)
		for j := 0; j < n; j++ {
			var x, y int
			fmt.Fscan(in, &x, &y)
			input = append(input, Vertex{x, y})
		}
		input = append(input, Vertex{0, 0})
		sort.Slice(input, func(i, j int)bool {
			if input[i].x < input[j].x{
				return true;
			}else{
				if input[i].y < input[j].y{
					return true;
				}else{
					return false;
				}
			}
		})
		ok := true
		n = n + 1
		var tmp string
		for j := 1; j < n; j++ {
			if input[j].x>input[j-1].x&&input[j].y>input[j-1].y {
				tmp = tmp + getMove(input[j-1], input[j])
			}else{
				ok = false
			}
		}
		if ok {
			ans = append(ans, "YES")
			ans = append(ans, tmp)
		}else{
			ans = append(ans, "NO")
		}
	}
	fmt.Fprintln(out, strings.Join(ans, "\n"));
}


package main

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

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

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

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

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

	return res
}

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

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

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

// DigitSum returns digit sum of a decimal number.
// DigitSum only accept a positive integer.
func DigitSum(n int) int {
	if n < 0 {
		return -1
	}

	res := 0

	for n > 0 {
		res += n % 10
		n /= 10
	}

	return res
}

// DigitNumOfDecimal returns digits number of n.
// n is non negative number.
func DigitNumOfDecimal(n int) int {
	res := 0

	for n > 0 {
		n /= 10
		res++
	}

	return res
}

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

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

	return s
}

// Kiriage returns Ceil(a/b)
// a >= 0, b > 0
func Kiriage(a, b int) int {
	return (a + (b - 1)) / b
}

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

	if e == 0 {
		return 1
	}

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

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

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

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

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

	return a
}

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

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

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

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

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

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

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

	return string(res)
}

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

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

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

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

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

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

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

var q int

func main() {
	q = ReadInt()

	for i := 0; i < q; i++ {
		S := ReadRuneSlice()
		T := ReadRuneSlice()

		smemo := [ALPHABET_NUM]int{}
		tmemo := [ALPHABET_NUM]int{}

		for j := 0; j < len(S); j++ {
			s, t := S[j], T[j]
			smemo[s-'a']++
			tmemo[t-'a']++
		}

		flag := false
		for j := 0; j < ALPHABET_NUM; j++ {
			if smemo[j] > 0 && tmemo[j] > 0 {
				flag = true
				break
			}
		}
		if flag {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

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

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

package main

import (
	"fmt"
	"strings"
)

func main() {
	var n int
	fmt.Scan(&n)
	var (
		first string
		second string
	)
	can := make([]bool, n)
	for i := range can {
		fmt.Scan(&first)
		fmt.Scan(&second)
		if strings.ContainsAny(first, second) {
			can[i] = true
		}
	}
	for _, e := range can {
		if e {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

//HOWDY! IT'S ME! A HARD BLACKISH DICK MOVE!
package main

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

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

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

//str := PrintlnInts(X...)

/*******************************************************************/
func equalizable(s, t string) bool {
	table := make(map[byte]bool)
	n := len(s)
	for i := 0; i < n; i++ {
		table[s[i]] = true
	}
	for i := 0; i < n; i++ {
		if table[t[i]] {
			return true
		}
	}
	return false
}

func main() {
	for q := ReadInt(); q > 0; q-- {
		s := ReadString()
		t := ReadString()
		ans := "NO"
		if equalizable(s, t) {
			ans = "YES"
		}
		fmt.Println(ans)
	}
}

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

func min(args ...int) int {
	if len(args) == 0 {
		panic("empty!")
	}
	min := args[0]
	for i := 1; i < len(args); i++ {
		if args[i] < min {
			min = args[i]
		}
	}
	return min
}

func max(args ...int) int {
	if len(args) == 0 {
		panic("empty!")
	}
	max := args[0]
	for i := 1; i < len(args); i++ {
		if args[i] > max {
			max = args[i]
		}
	}
	return max
}

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

func gcd(args ...int) int {
	if len(args) == 0 {
		panic("empty!")
	}
	gcd := args[0]
	for i := 1; i < len(args); i++ {
		gcd = _gcd(gcd, args[i])
	}
	return gcd
}

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

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

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

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

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

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

func readInt_64() int64 {
	return readIntNbits(64)
}

func ReadInt2_64() (int64, int64) {
	return readInt_64(), readInt_64()
}

func ReadInt3_64() (int64, int64, int64) {
	return readInt_64(), readInt_64(), readInt_64()
}

func readInt() int {
	return int(readIntNbits(32))
}

func readIntNbits(n int) int64 {
	i, err := strconv.ParseInt(ReadString(), 0, n)
	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
}

// 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())
}

// PrintIntsLine returns integers string delimited by a space.
func PrintlnInts(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)
}

package main

import (
	"fmt"
)

func main() {
	var tt int
	var a, b string
	var res bool
	fmt.Scan(&tt)
	for tt != 0 {
		res = false
		arr := make([]bool, 30)
		fmt.Scan(&a, &b)
		for i := 0; i < len(a); i++ {
			arr[int(a[i])-97] = true
		}

		for i := 0; i < len(b); i++ {
			if arr[int(b[i])-97] == true {
				res = true
				break
			}
		}
		if res {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
		tt--
	}
}

package main

import (
	"fmt"
)

func main() {
	var tt int
	var a, b string
	var res bool
	fmt.Scan(&tt)
	for tt != 0 {
		res = false
		arr := make([]bool, 30)
		fmt.Scan(&a, &b)
		for i := 0; i < len(a); i++ {
			arr[int(a[i])-97] = true
		}

		for i := 0; i < len(b); i++ {
			if arr[int(b[i])-97] == true {
				res = true
				break
			}
		}
		if res {
			println("YES")
		} else {
			println("NO")
		}
		tt--
	}
}

package main

import (
	"fmt"
)

func main() {
	var tt int
	var a, b string
	var res bool
	fmt.Scan(&tt)
	for tt != 0 {
		res = false
		arr := make([]bool, 30)
		fmt.Scan(&a, &b)
		for i := 0; i < len(a); i++ {
			arr[int(a[i])-97] = true
		}

		for i := 0; i < len(b); i++ {
			if arr[int(b[i])-97] == true {
				res = true
				break
			}
		}
		if res {
			print("YES\n")
		} else {
			print("NO\n")
		}
		tt--
	}
	return
}