package main

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

const N = 2020
const INF = int64(1e18)
var n int
var x[N] int
var y[N] int
var c[N] int
var k[N] int

type Edge struct {
	to int
	dist int64
}

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

func getDist(i int, j int) int64 {
	if i == 0 {
		return int64(c[j])
	}
	if j == 0 {
		return int64(c[i])
	}
	var diff = int64(k[i] + k[j]) * int64(abs(x[i], x[j]) + abs(y[i], y[j]))
	return diff
}

var visited[N] bool

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

	fmt.Fscanf(reader, "%d\n", &n)
	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d %d\n", &x[i], &y[i])
	}

	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d", &c[i])
	}
	fmt.Fscanf(reader, "\n")
	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d", &k[i])
	}

	var ret = int64(0)
	var burn = make([]int, 0)
	var edx = make([]int, 0)
	var edy = make([]int, 0)

	var edges = make([]Edge, n + 1)

	for i := 1; i <= n; i++ {
		edges[i].dist = INF
		edges[i].to = -1
	}
	edges[0].dist = 0

	for i := 1; i <= n + 1; i++ {
		var f = -1
		for j := 0; j <= n; j++ {
			if visited[j] == false && (f == -1 || edges[j].dist < edges[f].dist) {
				f = j
			}
		}

		if f == -1 {
			break
		}

		ret += edges[f].dist

		if edges[f].to != -1 {
			var u = f
			var v = edges[f].to
			if u > v {
				u, v = v, u
			}
			if u == 0 {
				if v > 0 {
					burn = append(burn, v)
				}
			} else {
				edx = append(edx, u)
				edy = append(edy, v)
			}
		}

		visited[f] = true
		for j := 0; j <= n; j++ {
			if !visited[j] {
				var canDist = getDist(f, j)
				if edges[j].dist > canDist {
					edges[j].dist = canDist
					edges[j].to = f
				}
			}
		}
	}

	fmt.Fprintf(writer, "%d\n", ret)

	fmt.Fprintf(writer, "%d\n", len(burn))
	for _, u := range burn {
		fmt.Fprintf(writer, "%d ", u)
	}
	fmt.Fprintf(writer,"\n")

	fmt.Fprintf(writer, "%d\n", len(edx))
	for i := 0; i < len(edx); i++ {
		fmt.Fprintf(writer, "%d %d\n", edx[i], edy[i])
	}

}

package main

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

/* solution */

type edge struct {
	a    int
	b    int
	cost int64
}

type edgeArray []edge

func (e edgeArray) Len() int           { return len(e) }
func (e edgeArray) Swap(i, j int)      { e[i], e[j] = e[j], e[i] }
func (e edgeArray) Less(i, j int) bool { return e[i].cost < e[j].cost }

var stations []int

func solve() {

	n := readInt()
	coords := make([][2]int64, n)
	c := make([]int64, n)
	k := make([]int64, n)
	for i := 0; i < n; i++ {
		coords[i][0], coords[i][1] = readInt64(), readInt64()
	}
	for i := 0; i < n; i++ {
		c[i] = readInt64()
	}
	for i := 0; i < n; i++ {
		k[i] = readInt64()
	}

	edges := make(edgeArray, 0)
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if i == j {
				continue
			}
			cost := (k[i] + k[j]) * dist(coords, i, j)
			edges = append(edges, edge{a: i, b: j, cost: cost})
		}
	}

	sort.Sort(edges)

	y := int64(0)

	stations = make([]int, n)
	for i := 0; i < n; i++ {
		stations[i] = i
		y += c[i]
	}

	v := n
	e := 0
	ans := make(edgeArray, 0)

	for i := 0; i < len(edges); i++ {
		edge := edges[i]
		pa := snm(edge.a)
		pb := snm(edge.b)
		if pa == pb {
			continue
		}
		pmax := pa
		if c[pa] < c[pb] {
			pmax = pb
		}
		if c[pmax] > edge.cost {
			stations[pmax] = snm(pa + pb - pmax)
			ans = append(ans, edge)
			y = y - c[pmax] + edge.cost
			e++
			v--
		}
	}

	printInt64(y)
	ln()

	printInt(v)
	ln()

	for i := 0; i < n; i++ {
		if i == snm(i) {
			printInt(i + 1)
		}
	}
	ln()

	printInt(e)
	ln()

	for i := 0; i < len(ans); i++ {
		printInt(ans[i].a + 1)
		printInt(ans[i].b + 1)
		ln()
	}

}

func snm(k int) int {
	if stations[k] == k {
		return k
	}
	ret := snm(stations[k])
	stations[k] = ret
	return ret
}

func dist(coords [][2]int64, i, j int) int64 {
	x1, y1, x2, y2 := coords[i][0], coords[i][1], coords[j][0], coords[j][1]
	dx := x1 - x2
	if dx < 0 {
		dx = -dx
	}
	dy := y1 - y2
	if dy < 0 {
		dy = -dy
	}
	return dx + dy
}

/* stubs */

func main() {

	var f *os.File
	if runtime.GOROOT() == "C:\\Soft\\Go" {
		f, _ = os.Open("input.txt")
		defer f.Close()
	} else {
		f = os.Stdin
	}

	writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	reader = bufio.NewScanner(f)

	const maxCapacity = 1024 * 1024
	buf := make([]byte, maxCapacity)
	reader.Buffer(buf, maxCapacity)

	reader.Split(bufio.ScanWords)

	solve()

}

var reader *bufio.Scanner
var writer *bufio.Writer

func read() string {
	reader.Scan()
	return reader.Text()
}

func readInt() int {
	reader.Scan()
	ret, _ := strconv.Atoi(reader.Text())
	return ret
}

func print(s string) {
	writer.WriteString(s)
	writer.WriteString(" ")
}

func ln() {
	writer.WriteString("\n")
}

func printInt(i int) {
	print(strconv.Itoa(i))
}

func printInt64(i int64) {
	print(strconv.FormatInt(i, 10))
}

func readInt64() int64 {
	reader.Scan()
	ret, _ := strconv.ParseInt(reader.Text(), 10, 64)
	return ret
}

package main

import (
	"bufio"
	"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
}

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

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

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

/*
ASCII code

ASCII   10進数  ASCII   10進数  ASCII   10進数
!       33      "       34      #       35
$       36      %       37      &       38
'       39      (       40      )       41
*       42      +       43      ,       44
-       45      .       46      /       47
0       48      1       49      2       50
3       51      4       52      5       53
6       54      7       55      8       56
9       57      :       58      ;       59
<       60      =       61      >       62
?       63      @       64      A       65
B       66      C       67      D       68
E       69      F       70      G       71
H       72      I       73      J       74
K       75      L       76      M       77
N       78      O       79      P       80
Q       81      R       82      S       83
T       84      U       85      V       86
W       87      X       88      Y       89
Z       90      [       91      \       92
]       93      ^       94      _       95
`       96      a       97      b       98
c       99      d       100     e       101
f       102     g       103     h       104
i       105     j       106     k       107
l       108     m       109     n       110
o       111     p       112     q       113
r       114     s       115     t       116
u       117     v       118     w       119
x       120     y       121     z       122
{       123     |       124     }       125
~       126             127
*/

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

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

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

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

type City struct {
	id, x, y int64
}

var n int64
var cities []City
var C []int64
var K []int64

var ans int64
var ansCityIds []int64
var connectedCities [][]int64

const NUM = 2000 + 5

func main() {
	n = ReadInt64()
	for i := int64(0); i < n; i++ {
		x, y := ReadInt64_2()
		cities = append(cities, City{id: i + 1, x: x, y: y})
	}
	C = ReadInt64Slice(int(n))
	K = ReadInt64Slice(int(n))

	// エッジを作る
	for i := int64(0); i < n; i++ {
		for j := i + 1; j < n; j++ {
			ci, cj := cities[i], cities[j]
			dist := AbsInt(ci.x-cj.x) + AbsInt(ci.y-cj.y)
			coef := K[ci.id-1] + K[cj.id-1]
			ecost := coef * dist

			M[ci.id][cj.id] = ecost
			M[cj.id][ci.id] = ecost
		}
	}
	// id:0のダミーノードにダミーエッジをはる
	for i := int64(0); i < n; i++ {
		M[0][i+1] = C[i]
		M[i+1][0] = C[i]
	}

	ans := prim()
	fmt.Println(ans)
	fmt.Println(len(ansCityIds))
	fmt.Println(PrintInts64Line(ansCityIds...))
	fmt.Println(len(connectedCities))
	for i := 0; i < len(connectedCities); i++ {
		fmt.Printf("%d %d\n", connectedCities[i][0], connectedCities[i][1])
	}
}

const MAX = 2000 + 5

var M [MAX][MAX]int64

// O(|V|^2)
func prim() int64 {
	var u, minv int64
	var d, p, color [MAX]int64

	for i := int64(0); i <= n; i++ {
		d[i] = INF_BIT60
		p[i] = -1
		color[i] = WHITE
	}

	d[0] = 0

	// すべてのノードがBLACKになるまで続くのでn回ループする
	for {
		// 次にTに加えるべきノードuを選定する
		minv = INF_BIT60
		u = -1
		for i := int64(0); i <= n; i++ {
			if minv > d[i] && color[i] != BLACK {
				u = i
				minv = d[i]
			}
		}
		if u == -1 {
			break // すべての頂点がTに属していたら終了
		}

		color[u] = BLACK // uをTに所属させる
		if p[u] != -1 {
			if p[u] == 0 {
				ansCityIds = append(ansCityIds, u)
			} else {
				connectedCities = append(connectedCities, []int64{p[u], u})
			}
		}

		for v := int64(0); v <= n; v++ {
			if color[v] != BLACK && M[u][v] != INF_BIT60 {
				if d[v] > M[u][v] {
					d[v] = M[u][v]
					p[v] = u
					color[v] = GRAY // ここをコメントアウトしてもACする
				}
			}
		}
	}

	sum := int64(0)
	for i := int64(0); i <= n; i++ {
		if p[i] != -1 {
			sum += M[i][p[i]]
		}
		// sum += d[i] // これでもACする
	}
	return sum
}

/*
- まずは全探索を検討しましょう
- MODは最後にとりましたか？
- ループを抜けた後も処理が必要じゃありませんか？
- 和・積・あまりを求められたらint64が必要ではありませんか？
- いきなりオーバーフローはしていませんか？
- MOD取る系はint64必須ですよ？
*/

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

package main

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

func solve(reader io.Reader, writer io.Writer) {
	abs := func(x int) int {
		if x < 0 {
			return -x
		}
		return x
	}
	var fa []int
	initFa := func(n int) {
		fa = make([]int, n)
		for i := range fa {
			fa[i] = i
		}
	}
	var find func(int) int
	find = func(x int) int {
		if fa[x] != x {
			fa[x] = find(fa[x])
		}
		return fa[x]
	}
	merge := func(from, to int) { fa[find(from)] = find(to) }
	same := func(x, y int) bool { return find(x) == find(y) }
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n int
	Fscan(in, &n)
	type pair struct{ x, y int }
	ps := make([]pair, n)
	for i := range ps {
		Fscan(in, &ps[i].x, &ps[i].y)
	}
	costCity := make([]int, n)
	for i := range costCity {
		Fscan(in, &costCity[i])
	}
	costLine := make([]int, n)
	for i := range costLine {
		Fscan(in, &costLine[i])
	}

	type dist struct {
		v, w int
		d    int64
	}
	ds := make([]dist, 0, n*n)
	for i, pi := range ps {
		for j, pj := range ps {
			if j == i {
				ds = append(ds, dist{i, j, int64(costCity[i])})
			} else {
				ds = append(ds, dist{i, j, int64(abs(pi.x-pj.x)+abs(pi.y-pj.y)) * int64(costLine[i]+costLine[j])})
			}
		}
	}
	sort.Slice(ds, func(i, j int) bool { return ds[i].d < ds[j].d })

	ans := int64(0)
	ansCity := []interface{}{}
	ansConn := []pair{}
	initFa(n + 1)
	for _, d_ := range ds {
		v, w, d := d_.v, d_.w, d_.d
		if v == w {
			if !same(v, n) {
				ans += d
				ansCity = append(ansCity, v+1)
				merge(v, n)
			}
		} else {
			if !same(v, w) {
				ans += d
				ansConn = append(ansConn, pair{v + 1, w + 1})
				merge(v, w)
			}
		}
	}
	Fprintln(out, ans)
	Fprintln(out, len(ansCity))
	Fprintln(out, ansCity...)
	Fprintln(out, len(ansConn))
	for _, p := range ansConn {
		if p.x > p.y {
			p.x, p.y = p.y, p.x
		}
		Fprintln(out, p.x, p.y)
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func solve(reader io.Reader, writer io.Writer) {
	abs := func(x int) int {
		if x < 0 {
			return -x
		}
		return x
	}
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n int
	Fscan(in, &n)
	type pair struct{ x, y int }
	ps := make([]pair, n)
	for i := range ps {
		Fscan(in, &ps[i].x, &ps[i].y)
	}
	costCity := make([]int, n)
	for i := range costCity {
		Fscan(in, &costCity[i])
	}
	costLine := make([]int, n)
	for i := range costLine {
		Fscan(in, &costLine[i])
	}

	dist := [2001][2001]int64{}
	for i := 0; i < n; i++ {
		dist[i][n] = int64(costCity[i])
		dist[n][i] = dist[i][n]
		for j := i + 1; j < n; j++ {
			dist[i][j] = int64(abs(ps[i].x-ps[j].x)+abs(ps[i].y-ps[j].y)) * int64(costLine[i]+costLine[j])
			dist[j][i] = dist[i][j]
		}
	}

	ans := int64(0)
	ansCity := []interface{}{}
	ansConn := []pair{}
	type distCity struct {
		d int64
		v int
	}
	minCost := make([]distCity, n+1)
	for i := range minCost {
		minCost[i].d = int64(1e18)
	}
	minCost[0].d = 0
	used := make([]bool, n+1)
	for {
		v := -1
		for w := 0; w <= n; w++ {
			if !used[w] && (v == -1 || minCost[w].d < minCost[v].d) {
				v = w
			}
		}
		if v == -1 {
			break
		}
		used[v] = true
		if minCost[v].d > 0 {
			ans += minCost[v].d
			if v == n {
				ansCity = append(ansCity, minCost[v].v+1)
			} else if minCost[v].v == n {
				ansCity = append(ansCity, v+1)
			} else {
				ansConn = append(ansConn, pair{minCost[v].v + 1, v + 1})
			}
		}
		for w := 0; w <= n; w++ {
			if dist[v][w] < minCost[w].d {
				minCost[w] = distCity{dist[v][w], v}
			}
		}
	}
	Fprintln(out, ans)
	Fprintln(out, len(ansCity))
	Fprintln(out, ansCity...)
	Fprintln(out, len(ansConn))
	for _, p := range ansConn {
		if p.x > p.y {
			p.x, p.y = p.y, p.x
		}
		Fprintln(out, p.x, p.y)
	}
}

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

package main

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

type point struct {
	x, y int
}

var ans int64

const (
	mod = int(1e9 + 7)
)

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

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

type node struct {
	from int
	val  int64
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	tt := 1
	//fmt.Fscanf(in, "%d\n", &tt)
	for ii := 0; ii < tt; ii++ {
		n := 0
		fmt.Fscanf(in, "%d\n", &n)
		c := make([]int64, n)
		x := make([]int, n)
		y := make([]int, n)
		k := make([]int64, n)
		a := make([]node, n)
		used := make([]bool, n)
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d %d\n", &x[i], &y[i])
		}
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d", &c[i])
		}
		fmt.Fscanf(in, "\n")
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d", &k[i])
		}
		fmt.Fscanf(in, "\n")
		for i := 0; i < n; i++ {
			a[i] = node{-1, c[i]}
		}
		ans := int64(0)
		anss := []int{}
		ansv := make([][]int, 0)
		for i := 0; i < n; i++ {
			selected := -1
			for j := 0; j < n; j++ {
				if !used[j] {
					if selected == -1 {
						selected = j
					} else if a[j].val < a[selected].val {
						selected = j
					}
				}
			}
			if a[selected].from == -1 {
				anss = append(anss, selected)
			} else {
				ansv = append(ansv, []int{a[selected].from, selected})
			}
			ans += a[selected].val
			used[selected] = true
			for j := 0; j < n; j++ {
				if !used[j] {
					dis := dis(x[selected], y[selected], x[j], y[j])
					newcost := int64(dis) * (int64(k[selected]) + int64(k[j]))
					if newcost < a[j].val {
						a[j].val = newcost
						a[j].from = selected
					}
				}
			}
		}
		fmt.Fprintf(out, "%d\n", ans)
		fmt.Fprintf(out, "%d\n", len(anss))
		for i := 0; i < len(anss)-1; i++ {
			fmt.Fprintf(out, "%d ", anss[i]+1)
		}
		if len(anss) > 0 {
			fmt.Fprintf(out, "%d\n", anss[len(anss)-1]+1)
		}
		fmt.Fprintf(out, "%d\n", len(ansv))
		for i := 0; i < len(ansv); i++ {
			fmt.Fprintf(out, "%d %d\n", ansv[i][0]+1, ansv[i][1]+1)
		}
	}
	return
}

func dis(x1, y1, x2, y2 int) int {
	return abs(x1-x2) + abs(y1-y2)
}

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

// if median can be larger or equal to mid
func ok(mid int, a [][]int, s int64) bool {
	n := len(a)
	b := make([]int, n) // salary
	cost := int64(0)
	c := make([]int, 0) // which can be larger then mid
	for i := 0; i < n; i++ {
		if a[i][1] >= mid {
			if a[i][0] <= mid {
				b[i] = mid
			} else {
				b[i] = a[i][0]
			}
			c = append(c, i)
		} else {
			b[i] = a[i][0]
		}
		cost += int64(b[i])
	}
	//fmt.Println("debug3", c)
	if len(c) < (n+1)/2 {
		return false
	}
	save := make([]int, 0)
	for i := 0; i < len(c); i++ {
		id := c[i]
		if a[id][0] <= mid {
			save = append(save, b[id]-a[id][0])
		}
	}

	sort.Ints(save)
	needSave := n/2 - (n - len(c))
	if needSave > len(save) {
		needSave = len(save)
	}
	for i := 0; i < needSave; i++ {
		cost -= int64(save[len(save)-1-i])
	}
	if cost <= s {
		return true
	}
	return false
}

package main

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

/* solution */

type edge struct {
	a    int
	b    int
	cost int64
}

type edgeArray []edge

func (e edgeArray) Len() int           { return len(e) }
func (e edgeArray) Swap(i, j int)      { e[i], e[j] = e[j], e[i] }
func (e edgeArray) Less(i, j int) bool { return e[i].cost < e[j].cost }

var stations []int

func solve() {

	n := readInt()
	coords := make([][2]int64, n)
	c := make([]int64, n)
	k := make([]int64, n)
	for i := 0; i < n; i++ {
		coords[i][0], coords[i][1] = readInt64(), readInt64()
	}
	for i := 0; i < n; i++ {
		c[i] = readInt64()
	}
	for i := 0; i < n; i++ {
		k[i] = readInt64()
	}

	edges := make(edgeArray, n*n)
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			cost := int64(100000000000000)
			if i != j {
				cost = (k[i] + k[j]) * dist(coords, i, j)
			}
			edges[i*n+j] = edge{a: i, b: j, cost: cost}
		}
	}

	sort.Sort(edges)

	y := int64(0)

	stations = make([]int, n)
	for i := 0; i < n; i++ {
		stations[i] = i
		y += c[i]
	}

	v := n
	e := 0
	ans := make(edgeArray, 0)

	for i := 0; i < n*(n-1); i++ {
		edge := edges[i]
		pa := snm(edge.a)
		pb := snm(edge.b)
		pmax := pa
		if c[pa] < c[pb] {
			pmax = pb
		}
		if c[pmax] > edge.cost {
			stations[pmax] = snm(pa + pb - pmax)
			ans = append(ans, edge)
			y = y - c[pmax] + edge.cost
			e++
			v--
		}
	}

	printInt64(y)
	ln()

	printInt(v)
	ln()

	for i := 0; i < n; i++ {
		if i == snm(i) {
			printInt(i + 1)
		}
	}
	ln()

	printInt(e)
	ln()

	for i := 0; i < len(ans); i++ {
		printInt(ans[i].a + 1)
		printInt(ans[i].b + 1)
		ln()
	}

}

func snm(k int) int {
	if stations[k] == k {
		return k
	}
	ret := snm(stations[k])
	stations[k] = ret
	return ret
}

func dist(coords [][2]int64, i, j int) int64 {
	x1, y1, x2, y2 := coords[i][0], coords[i][1], coords[j][0], coords[j][1]
	dx := x1 - x2
	if dx < 0 {
		dx = -dx
	}
	dy := y1 - y2
	if dy < 0 {
		dy = -dy
	}
	return dx + dy
}

/* stubs */

func main() {

	var f *os.File
	if runtime.GOROOT() == "C:\\Soft\\Go" {
		f, _ = os.Open("input.txt")
		defer f.Close()
	} else {
		f = os.Stdin
	}

	writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	reader = bufio.NewScanner(f)

	const maxCapacity = 1024 * 1024
	buf := make([]byte, maxCapacity)
	reader.Buffer(buf, maxCapacity)

	reader.Split(bufio.ScanWords)

	solve()

}

var reader *bufio.Scanner
var writer *bufio.Writer

func read() string {
	reader.Scan()
	return reader.Text()
}

func readInt() int {
	reader.Scan()
	ret, _ := strconv.Atoi(reader.Text())
	return ret
}

func print(s string) {
	writer.WriteString(s)
	writer.WriteString(" ")
}

func ln() {
	writer.WriteString("\n")
}

func printInt(i int) {
	print(strconv.Itoa(i))
}

func printInt64(i int64) {
	print(strconv.FormatInt(i, 10))
}

func readInt64() int64 {
	reader.Scan()
	ret, _ := strconv.ParseInt(reader.Text(), 10, 64)
	return ret
}

package main

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

/* solution */

type edge struct {
	a    int
	b    int
	cost int64
}

type edgeArray []edge

func (e edgeArray) Len() int           { return len(e) }
func (e edgeArray) Swap(i, j int)      { e[i], e[j] = e[j], e[i] }
func (e edgeArray) Less(i, j int) bool { return e[i].cost < e[j].cost }

var stations []int

const inf = int64(9223372036854775807)

func solve() {

	n := readInt()
	coords := make([][2]int64, n)
	c := make([]int64, n)
	k := make([]int64, n)
	for i := 0; i < n; i++ {
		coords[i][0], coords[i][1] = readInt64(), readInt64()
	}
	for i := 0; i < n; i++ {
		c[i] = readInt64()
	}
	for i := 0; i < n; i++ {
		k[i] = readInt64()
	}

	edges := make(edgeArray, n*n)
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			cost := inf
			if i != j {
				cost = (k[i] + k[j]) * dist(coords, i, j)
			}
			edges[i*n+j] = edge{a: i, b: j, cost: cost}
		}
	}

	sort.Sort(edges)

	y := int64(0)

	stations = make([]int, n)
	for i := 0; i < n; i++ {
		stations[i] = i
		y += c[i]
	}

	v := n
	e := 0
	ans := make(edgeArray, 0)

	for i := 0; i < n*(n-1); i++ {
		edge := edges[i]
		pa := snm(edge.a)
		pb := snm(edge.b)
		pmax := pa
		if c[pa] < c[pb] {
			pmax = pb
		}
		if c[pmax] > edge.cost {
			stations[pmax] = snm(pa + pb - pmax)
			ans = append(ans, edge)
			y = y - c[pmax] + edge.cost
			e++
			v--
		}
	}

	printInt64(y)
	ln()

	printInt(v)
	ln()

	for i := 0; i < n; i++ {
		if i == snm(i) {
			printInt(i + 1)
		}
	}
	ln()

	printInt(e)
	ln()

	for i := 0; i < len(ans); i++ {
		printInt(ans[i].a + 1)
		printInt(ans[i].b + 1)
		ln()
	}

}

func snm(k int) int {
	if stations[k] == k {
		return k
	}
	ret := snm(stations[k])
	stations[k] = ret
	return ret
}

func dist(coords [][2]int64, i, j int) int64 {
	x1, y1, x2, y2 := coords[i][0], coords[i][1], coords[j][0], coords[j][1]
	dx := x1 - x2
	if dx < 0 {
		dx = -dx
	}
	dy := y1 - y2
	if dy < 0 {
		dy = -dy
	}
	return dx + dy
}

/* stubs */

func main() {

	var f *os.File
	if runtime.GOROOT() == "C:\\Soft\\Go" {
		f, _ = os.Open("input.txt")
		defer f.Close()
	} else {
		f = os.Stdin
	}

	writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	reader = bufio.NewScanner(f)

	const maxCapacity = 1024 * 1024
	buf := make([]byte, maxCapacity)
	reader.Buffer(buf, maxCapacity)

	reader.Split(bufio.ScanWords)

	solve()

}

var reader *bufio.Scanner
var writer *bufio.Writer

func read() string {
	reader.Scan()
	return reader.Text()
}

func readInt() int {
	reader.Scan()
	ret, _ := strconv.Atoi(reader.Text())
	return ret
}

func print(s string) {
	writer.WriteString(s)
	writer.WriteString(" ")
}

func ln() {
	writer.WriteString("\n")
}

func printInt(i int) {
	print(strconv.Itoa(i))
}

func printInt64(i int64) {
	print(strconv.FormatInt(i, 10))
}

func readInt64() int64 {
	reader.Scan()
	ret, _ := strconv.ParseInt(reader.Text(), 10, 64)
	return ret
}

package main

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

const N = 2020
var n int
var x[N] int
var y[N] int
var lab[N] int
var labx [N] int

var c[N] int
var k[N] int
var dt[N][] int

type Edge struct {
	u int
	v int
	dist int64
}

func uni(r1 int, r2 int)  {
	var x = lab[r1] + lab[r2]
	if lab[r1] < lab[r2] {
		lab[r1] = x
		lab[r2] = r1
	} else {
		lab[r2] = x
		lab[r1] = r2
	}
}

func unix(r1 int, r2 int)  {
	var x = labx[r1] + labx[r2]
	if labx[r1] < labx[r2] {
		labx[r1] = x
		labx[r2] = r1
	} else {
		labx[r2] = x
		labx[r1] = r2
	}
}

func getx(x int) int {
	if labx[x] < 0 {
		return x
	}
	labx[x] = getx(labx[x])
	return labx[x]
}

func getParent(x int) int {
	if lab[x] < 0 {
		return x
	}
	lab[x] = getParent(lab[x])
	return lab[x]
}

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

func getDist(i int, j int) int64 {
	var diff = int64(k[i] + k[j]) * int64(abs(x[i], x[j]) + abs(y[i], y[j]))
	return diff
}

func shouldConnect(i int, j int) bool {
	var diff = getDist(i, j)
	return int64(c[i]) > diff || int64(c[j]) > diff
}

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

	fmt.Fscanf(reader, "%d\n", &n)
	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d %d\n", &x[i], &y[i])
	}

	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d", &c[i])
	}
	fmt.Fscanf(reader, "\n")
	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d", &k[i])
	}

	for i := 1; i <= n; i++ {
		lab[i] = -1
	}

	for i := 1; i < n; i++ {
		for j := i + 1; j <= n; j++ {
			if shouldConnect(i, j) {
				var r1 = getParent(i)
				var r2 = getParent(j)
				if r1 != r2 {
					uni(r1, r2)
				}
			}
		}
	}

	for i := 1; i <= n; i++ {
		dt[i] = make([]int, 0)
	}

	for i := 1; i <= n; i++ {
		var parent = getParent(i)
		dt[parent] = append(dt[parent], i)
	}

	var burn = make([]int, 0)
	var edge_from = make([]int, 0)
	var edge_to = make([]int, 0)

	var ret = int64(0)

	for i := 1; i <= n; i++ {
		var sz = len(dt[i])
		if sz > 0 {

			var f = dt[i][0]
			for _, u := range dt[i] {
				if c[u] < c[f] {
					f = u
				}
			}
			burn = append(burn, f)
			ret += int64(c[f])

			for j := 1; j <= n; j++ {
				labx[j] = -1
			}

			var krusk = make([]Edge, 0)

			for u := 0; u < sz - 1; u++ {
				var uu = dt[i][u]
				var pick = dt[i][u + 1]
				var mindiff = getDist(uu, pick)

				for v := u + 1; v < sz; v++ {
					var vv = dt[i][v]
					var dist = getDist(uu, vv)
					if dist < mindiff {
						mindiff = dist
						pick = vv
					}
				}
				krusk = append(krusk, Edge{uu, pick, mindiff})
			}

			sort.SliceStable(krusk, func(i, j int) bool {
				return krusk[i].dist < krusk[j].dist
			})

			for _, ed := range krusk {
				var r1 = getx(ed.u)
				var r2 = getx(ed.v)
				if r1 != r2 {
					ret += ed.dist
					unix(r1, r2)
					edge_from = append(edge_from, ed.u)
					edge_to = append(edge_to, ed.v)
				}
			}

		}
	}

	fmt.Fprintf(writer, "%d\n", ret)

	fmt.Fprintf(writer, "%d\n", len(burn))
	for _, u := range burn {
		fmt.Fprintf(writer, "%d ", u)
	}
	fmt.Fprintf(writer,"\n")

	fmt.Fprintf(writer, "%d\n", len(edge_from))
	for i := 0; i < len(edge_from); i++ {
		fmt.Fprintf(writer, "%d %d\n", edge_from[i], edge_to[i])
	}

}

package main

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

const N = 2020
const INF = int64(1e18)
var n int
var x[N] int
var y[N] int
var lab[N] int
var labx [N] int

var c[N] int
var k[N] int
var dt[N][] int

type Edge struct {
	u int
	v int
	dist int64
}

func uni(r1 int, r2 int)  {
	var x = lab[r1] + lab[r2]
	if lab[r1] < lab[r2] {
		lab[r1] = x
		lab[r2] = r1
	} else {
		lab[r2] = x
		lab[r1] = r2
	}
}

func unix(r1 int, r2 int)  {
	var x = labx[r1] + labx[r2]
	if labx[r1] < labx[r2] {
		labx[r1] = x
		labx[r2] = r1
	} else {
		labx[r2] = x
		labx[r1] = r2
	}
}

func getx(x int) int {
	if labx[x] < 0 {
		return x
	}
	labx[x] = getx(labx[x])
	return labx[x]
}

func getParent(x int) int {
	if lab[x] < 0 {
		return x
	}
	lab[x] = getParent(lab[x])
	return lab[x]
}

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

func getDist(i int, j int) int64 {
	var diff = int64(k[i] + k[j]) * int64(abs(x[i], x[j]) + abs(y[i], y[j]))
	return diff
}

func shouldConnect(i int, j int) bool {
	var diff = getDist(i, j)
	return int64(c[i]) > diff || int64(c[j]) > diff
}

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

	fmt.Fscanf(reader, "%d\n", &n)
	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d %d\n", &x[i], &y[i])
	}

	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d", &c[i])
	}
	fmt.Fscanf(reader, "\n")
	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d", &k[i])
	}

	for i := 1; i <= n; i++ {
		lab[i] = -1
	}

	for i := 1; i < n; i++ {
		for j := i + 1; j <= n; j++ {
			if shouldConnect(i, j) {
				var r1 = getParent(i)
				var r2 = getParent(j)
				if r1 != r2 {
					uni(r1, r2)
				}
			}
		}
	}

	for i := 1; i <= n; i++ {
		dt[i] = make([]int, 0)
	}

	for i := 1; i <= n; i++ {
		var parent = getParent(i)
		dt[parent] = append(dt[parent], i)
	}

	var burn = make([]int, 0)
	var edge_from = make([]int, 0)
	var edge_to = make([]int, 0)

	var ret = int64(0)

	for i := 1; i <= n; i++ {
		var sz = len(dt[i])
		if sz > 0 {

			var f = dt[i][0]
			for _, u := range dt[i] {
				if c[u] < c[f] {
					f = u
				}
			}
			burn = append(burn, f)
			ret += int64(c[f])

			for j := 1; j <= n; j++ {
				labx[j] = -1
			}

			var krusk = make([]Edge, 0)

			for u := 0; u < sz; u++ {
				var uu = dt[i][u]
				var pick = -1
				var mindiff = INF

				for v := 0; v < sz; v++ {
					if u != v {
						var vv= dt[i][v]
						var dist= getDist(uu, vv)

						if dist < mindiff {
							mindiff = dist
							pick = vv
						}
					}
				}
				if mindiff != INF {
					krusk = append(krusk, Edge{uu, pick, mindiff})
				}
			}

			sort.SliceStable(krusk, func(i, j int) bool {
				return krusk[i].dist < krusk[j].dist
			})

			for _, ed := range krusk {
				var r1 = getx(ed.u)
				var r2 = getx(ed.v)
				if r1 != r2 {
					ret += ed.dist
					unix(r1, r2)
					edge_from = append(edge_from, ed.u)
					edge_to = append(edge_to, ed.v)
				}
			}

		}
	}

	fmt.Fprintf(writer, "%d\n", ret)

	fmt.Fprintf(writer, "%d\n", len(burn))
	for _, u := range burn {
		fmt.Fprintf(writer, "%d ", u)
	}
	fmt.Fprintf(writer,"\n")

	fmt.Fprintf(writer, "%d\n", len(edge_from))
	for i := 0; i < len(edge_from); i++ {
		fmt.Fprintf(writer, "%d %d\n", edge_from[i], edge_to[i])
	}

}

package main

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

const N = 2020
const INF = int64(1e18)
var n int
var x[N] int
var y[N] int
var lab[N] int
var labx [N] int

var c[N] int
var k[N] int
var dt[N][] int

type Edge struct {
	u int
	v int
	dist int64
}

func uni(r1 int, r2 int)  {
	var x = lab[r1] + lab[r2]
	if lab[r1] < lab[r2] {
		lab[r1] = x
		lab[r2] = r1
	} else {
		lab[r2] = x
		lab[r1] = r2
	}
}

func unix(r1 int, r2 int)  {
	var x = labx[r1] + labx[r2]
	if labx[r1] < labx[r2] {
		labx[r1] = x
		labx[r2] = r1
	} else {
		labx[r2] = x
		labx[r1] = r2
	}
}

func getx(x int) int {
	if labx[x] < 0 {
		return x
	}
	labx[x] = getx(labx[x])
	return labx[x]
}

func getParent(x int) int {
	if lab[x] < 0 {
		return x
	}
	lab[x] = getParent(lab[x])
	return lab[x]
}

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

func getDist(i int, j int) int64 {
	var diff = int64(k[i] + k[j]) * int64(abs(x[i], x[j]) + abs(y[i], y[j]))
	return diff
}

func shouldConnect(i int, j int) bool {
	var diff = getDist(i, j)
	return int64(c[i]) > diff || int64(c[j]) > diff
}

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

	fmt.Fscanf(reader, "%d\n", &n)
	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d %d\n", &x[i], &y[i])
	}

	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d", &c[i])
	}
	fmt.Fscanf(reader, "\n")
	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d", &k[i])
	}

	for i := 1; i <= n; i++ {
		lab[i] = -1
	}

	for i := 1; i < n; i++ {
		for j := i + 1; j <= n; j++ {
			if shouldConnect(i, j) {
				var r1 = getParent(i)
				var r2 = getParent(j)
				if r1 != r2 {
					uni(r1, r2)
				}
			}
		}
	}

	for i := 1; i <= n; i++ {
		dt[i] = make([]int, 0)
	}

	for i := 1; i <= n; i++ {
		var parent = getParent(i)
		dt[parent] = append(dt[parent], i)
	}

	var burn = make([]int, 0)
	var edge_from = make([]int, 0)
	var edge_to = make([]int, 0)

	var ret = int64(0)

	for i := 1; i <= n; i++ {
		var sz = len(dt[i])
		if sz > 0 {

			var f = dt[i][0]
			for _, u := range dt[i] {
				if c[u] < c[f] {
					f = u
				}
			}
			burn = append(burn, f)
			ret += int64(c[f])

			for j := 1; j <= n; j++ {
				labx[j] = -1
			}

			var visited [N] bool

			var root = 0
			for j := 0; j < sz - 1; j++ {
				visited[root] = true
				var minDiff = INF
				var minNode = -1
				for z := 0; z < sz; z++ {
					if visited[z] == false {
						var canDiff = getDist(dt[i][z], dt[i][root])
						if canDiff < minDiff {
							minNode = z
							minDiff = canDiff
						}
					}
				}
				edge_to = append(edge_to, dt[i][root])
				edge_from = append(edge_from, dt[i][minNode])
				root = minNode
				ret += minDiff
			}
		}
	}

	fmt.Fprintf(writer, "%d\n", ret)

	fmt.Fprintf(writer, "%d\n", len(burn))
	for _, u := range burn {
		fmt.Fprintf(writer, "%d ", u)
	}
	fmt.Fprintf(writer,"\n")

	fmt.Fprintf(writer, "%d\n", len(edge_from))
	for i := 0; i < len(edge_from); i++ {
		fmt.Fprintf(writer, "%d %d\n", edge_from[i], edge_to[i])
	}

}

package main

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

const N = 2020
const INF = int64(1e18)
var n int
var x[N] int
var y[N] int
var lab[N] int
var labx [N] int

var c[N] int
var k[N] int
var dt[N][] int

type Edge struct {
	u int
	v int
	dist int64
}

func uni(r1 int, r2 int)  {
	var x = lab[r1] + lab[r2]
	if lab[r1] < lab[r2] {
		lab[r1] = x
		lab[r2] = r1
	} else {
		lab[r2] = x
		lab[r1] = r2
	}
}

func unix(r1 int, r2 int)  {
	var x = labx[r1] + labx[r2]
	if labx[r1] < labx[r2] {
		labx[r1] = x
		labx[r2] = r1
	} else {
		labx[r2] = x
		labx[r1] = r2
	}
}

func getx(x int) int {
	if labx[x] < 0 {
		return x
	}
	labx[x] = getx(labx[x])
	return labx[x]
}

func getParent(x int) int {
	if lab[x] < 0 {
		return x
	}
	lab[x] = getParent(lab[x])
	return lab[x]
}

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

func getDist(i int, j int) int64 {
	var diff = int64(k[i] + k[j]) * int64(abs(x[i], x[j]) + abs(y[i], y[j]))
	return diff
}

func shouldConnect(i int, j int) bool {
	var diff = getDist(i, j)
	return int64(c[i]) > diff || int64(c[j]) > diff
}

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

	fmt.Fscanf(reader, "%d\n", &n)
	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d %d\n", &x[i], &y[i])
	}

	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d", &c[i])
	}
	fmt.Fscanf(reader, "\n")
	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d", &k[i])
	}

	for i := 1; i <= n; i++ {
		lab[i] = -1
	}

	for i := 1; i < n; i++ {
		for j := i + 1; j <= n; j++ {
			if shouldConnect(i, j) {
				var r1 = getParent(i)
				var r2 = getParent(j)
				if r1 != r2 {
					uni(r1, r2)
				}
			}
		}
	}

	for i := 1; i <= n; i++ {
		dt[i] = make([]int, 0)
	}

	for i := 1; i <= n; i++ {
		var parent = getParent(i)
		dt[parent] = append(dt[parent], i)
	}

	var burn = make([]int, 0)
	var edge_from = make([]int, 0)
	var edge_to = make([]int, 0)

	var ret = int64(0)

	for i := 1; i <= n; i++ {
		var sz = len(dt[i])
		if sz > 0 {

			var f = dt[i][0]
			for _, u := range dt[i] {
				if c[u] < c[f] {
					f = u
				}
			}
			burn = append(burn, f)
			ret += int64(c[f])

			for j := 1; j <= n; j++ {
				labx[j] = -1
			}

			var visited [N] bool

			var root = 0
			for j := 0; j < sz - 1; j++ {
				visited[root] = true
				var minDiff = INF
				var minNode = -1
				for z := 0; z < sz; z++ {
					if visited[z] == false {
						var canDiff = getDist(dt[i][z], dt[i][root])
						if canDiff < minDiff {
							minNode = z
							minDiff = canDiff
						}
					}
				}
				edge_to = append(edge_to, root)
				edge_from = append(edge_from, minNode)
				root = minNode
				ret += minDiff
			}
		}
	}

	fmt.Fprintf(writer, "%d\n", ret)

	fmt.Fprintf(writer, "%d\n", len(burn))
	for _, u := range burn {
		fmt.Fprintf(writer, "%d ", u)
	}
	fmt.Fprintf(writer,"\n")

	fmt.Fprintf(writer, "%d\n", len(edge_from))
	for i := 0; i < len(edge_from); i++ {
		fmt.Fprintf(writer, "%d %d\n", edge_from[i], edge_to[i])
	}

}


package main

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

func main() {
	get := wrapStdin()
	n := get.Int()
	cnt := map[int]int{}
	for i := 0; i < n; i++ {
		a := get.Int()
		cnt[a]++
	}
	m := get.Int()
	b := get.Nint(m)
	var besta, bestb int
	ans := 1
	for i := 0; i < m; i++ {
		c := cnt[get.Int()]
		cur := cnt[b[i]]
		if besta > cur {
			continue
		}
		if besta < cur {
			besta = cur
			bestb = c
			ans = i + 1
			continue
		}
		if bestb >= c {
			continue
		}
		bestb = c
		ans = i + 1
	}
	fmt.Println(ans)
}

type Input struct {
	*bufio.Scanner
}

func wrapStdin() Input {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	return Input{Scanner: sc}
}
func (in Input) Int() int {
	in.Scan()
	x, _ := strconv.Atoi(in.Text())
	return x
}
func (in Input) IntOk() (int, bool) {
	ok := in.Scan()
	x, _ := strconv.Atoi(in.Text())
	return x, ok
}
func (in Input) Nint(n int) []int {
	v := make([]int, n)
	for i := 0; i < n; i++ {
		v[i] = in.Int()
	}
	return v
}
func (in Input) Vint(v []int) bool {
	var ok bool
	for i := 0; i < len(v); i++ {
		v[i], ok = in.IntOk()
	}
	return ok
}

package main

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

func main() {
	n := getInt()
	cnt := map[int]int{}
	for i := 0; i < n; i++ {
		a := getInt()
		cnt[a]++
	}
	m := getInt()
	b := getNint(m)
	var besta, bestb int
	ans := 1
	for i := 0; i < m; i++ {
		c := cnt[getInt()]
		cur := cnt[b[i]]
		if besta > cur {
			continue
		}
		if besta < cur {
			besta = cur
			bestb = c
			ans = i + 1
			continue
		}
		if bestb >= c {
			continue
		}
		bestb = c
		ans = i + 1
	}
	fmt.Println(ans)
}

type Input struct {
	*bufio.Scanner
}

var globalInput = wrapStdin()

func wrapStdin() Input {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	return Input{Scanner: sc}
}
func getInt() int {
	globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x
}
func getIntOk() (int, bool) {
	ok := globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x, ok
}
func getNint(n int) []int {
	v := make([]int, n)
	for i := 0; i < n; i++ {
		v[i] = getInt()
	}
	return v
}
func getVint(v []int) bool {
	var ok bool
	for i := 0; i < len(v); i++ {
		v[i], ok = getIntOk()
	}
	return ok
}

package main

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

func main() {
	n := getInt()
	cnt := map[int]int{}
	for i := 0; i < n; i++ {
		a := getInt()
		cnt[a]++
	}
	m := getInt()
	b := getNint(m)
	var besta, bestb int
	ans := 0
	for i := 0; i < m; i++ {
		c := cnt[getInt()]
		cur := cnt[b[i]]

		if remax(&besta, cur) {
			bestb = c
			ans = i
		} else if besta == cur {
			if remax(&bestb, c) {
				ans = i
			}
		}
	}
	fmt.Println(ans + 1)
}

type Input struct {
	*bufio.Scanner
}

var globalInput = wrapStdin()

func wrapStdin() Input {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	return Input{Scanner: sc}
}
func getInt() int {
	globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x
}
func getIntOk() (int, bool) {
	ok := globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x, ok
}
func getNint(n int) []int {
	v := make([]int, n)
	for i := 0; i < n; i++ {
		v[i] = getInt()
	}
	return v
}
func getVint(v []int) bool {
	var ok bool
	for i := 0; i < len(v); i++ {
		v[i], ok = getIntOk()
	}
	return ok
}
func getWord() string {
	globalInput.Scan()
	return globalInput.Text()
}
func getWordOk() (string, bool) {
	ok := globalInput.Scan()
	return globalInput.Text(), ok
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}
func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}
func max64(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}
func minStr(a, b string) string {
	if a < b {
		return a
	}
	return b
}
func maxStr(a, b string) string {
	if a < b {
		return b
	}
	return a
}
func remin(a *int, b int) bool {
	if b < *a {
		*a = b
		return true
	}
	return false
}
func remax(a *int, b int) bool {
	if b > *a {
		*a = b
		return true
	}
	return false
}
func remin64(a *int64, b int64) bool {
	if b < *a {
		*a = b
		return true
	}
	return false
}
func remax64(a *int64, b int64) bool {
	if b > *a {
		*a = b
		return true
	}
	return false
}
func reminStr(a *string, b string) bool {
	if b < *a {
		*a = b
		return true
	}
	return false
}
func remaxStr(a *string, b string) bool {
	if b > *a {
		*a = b
		return true
	}
	return false
}

func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}
func abs64(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}

type (
	FLess    func(i, j int) bool
	FSwap    func(i, j int)
	wrapSort struct {
		Length int
		FLess
		FSwap
	}
)

func (w *wrapSort) Len() int           { return w.Length }
func (w *wrapSort) Less(i, j int) bool { return w.FLess(i, j) }
func (w *wrapSort) Swap(i, j int)      { w.FSwap(i, j) }
func wsort(length int, fless func(i, j int) bool, fswap func(i, j int)) {
	sort.Sort(&wrapSort{length, fless, fswap})
}

package main

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

func main() {
	fsc := NewFastScanner()
	N := fsc.NextInt()
	A := make([]int, N)

	mapla := map[int]int{}
	for i := 0; i < N; i++ {
		A[i] = fsc.NextInt()
		mapla[A[i]]++
	}
	M := fsc.NextInt()

	B := make([]int, M) //audio
	C := make([]int, M) //subtitle

	for i := 0; i < M; i++ {
		B[i] = fsc.NextInt()
	}
	for i := 0; i < M; i++ {
		C[i] = fsc.NextInt()
	}
	vs := 0
	s := 0
	ans := 1
	for i := 0; i < M; i++ {
		if vs < mapla[B[i]] {
			vs = mapla[B[i]]
			s = mapla[C[i]]
			ans = i + 1
		} else if vs == mapla[B[i]] && s < mapla[C[i]] {
			vs = mapla[B[i]]
			s = mapla[C[i]]
			ans = i + 1
		}
	}
	fmt.Println(ans)
}

type FastScanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewFastScanner() *FastScanner {
	rdr := bufio.NewReaderSize(os.Stdin, 2028)
	return &FastScanner{r: rdr}
}

func (s *FastScanner) Next() string {
	s.Pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *FastScanner) NextInt() int {
	val, err := strconv.Atoi(s.Next())
	if err != nil {
		panic(err)
	}
	return val
}

func (s *FastScanner) Pre() {
	if s.p >= len(s.buf) {
		s.ReadLine()
		s.p = 0
	}
}

func (s *FastScanner) ReadLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, err := s.r.ReadLine()
		if err != nil {
			panic(err.Error())
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

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

func main() {
	fsc := NewFastScanner()
	N := fsc.NextInt()
	A := make([]int, N)

	mapla := map[int]int{}
	for i := 0; i < N; i++ {
		A[i] = fsc.NextInt()
		mapla[A[i]]++
	}
	M := fsc.NextInt()

	B := make([]int, M) //audio
	C := make([]int, M) //subtitle

	for i := 0; i < M; i++ {
		B[i] = fsc.NextInt()
	}
	for i := 0; i < M; i++ {
		C[i] = fsc.NextInt()
	}
	vs := 0
	s := 0
	ans := 0
	for i := 0; i < M; i++ {
		if vs < mapla[B[i]] {
			vs = mapla[B[i]]
			s = mapla[C[i]]
			ans = i + 1
		} else if vs == mapla[B[i]] && s < mapla[C[i]] {
			vs = mapla[B[i]]
			s = mapla[C[i]]
			ans = i + 1
		}
	}
	fmt.Println(ans)
}

type FastScanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewFastScanner() *FastScanner {
	rdr := bufio.NewReaderSize(os.Stdin, 2028)
	return &FastScanner{r: rdr}
}

func (s *FastScanner) Next() string {
	s.Pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *FastScanner) NextInt() int {
	val, err := strconv.Atoi(s.Next())
	if err != nil {
		panic(err)
	}
	return val
}

func (s *FastScanner) Pre() {
	if s.p >= len(s.buf) {
		s.ReadLine()
		s.p = 0
	}
}

func (s *FastScanner) ReadLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, err := s.r.ReadLine()
		if err != nil {
			panic(err.Error())
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

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

func main() {
	get := wrapStdin()
	n := get.Int()
	cnt := map[int]int{}
	for i := 0; i < n; i++ {
		a := get.Int()
		cnt[a]++
	}
	m := get.Int()
	b := get.Nint(m)
	var besta, bestb int
	ans := 1
	for i := 0; i < m; i++ {
		c := get.Int()
		cur := cnt[b[i]]
		if besta > cur {
			continue
		}
		if besta < cur {
			besta = cur
			bestb = c
			ans = i + 1
			continue
		}
		if bestb >= c {
			continue
		}
		bestb = c
		ans = i + 1
	}
	fmt.Println(ans)
}

type Input struct {
	*bufio.Scanner
}

func wrapStdin() Input {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	return Input{Scanner: sc}
}
func (in Input) Int() int {
	in.Scan()
	x, _ := strconv.Atoi(in.Text())
	return x
}
func (in Input) IntOk() (int, bool) {
	ok := in.Scan()
	x, _ := strconv.Atoi(in.Text())
	return x, ok
}
func (in Input) Nint(n int) []int {
	v := make([]int, n)
	for i := 0; i < n; i++ {
		v[i] = in.Int()
	}
	return v
}
func (in Input) Vint(v []int) bool {
	var ok bool
	for i := 0; i < len(v); i++ {
		v[i], ok = in.IntOk()
	}
	return ok
}


package main

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

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

	var T, n, a, b, da, db, dab, v, w, u, maxD int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &a, &b, &da, &db)
		g := make([][]int, n+1)
		for i := 1; i < n; i++ {
			Fscan(in, &v, &w)
			g[v] = append(g[v], w)
			g[w] = append(g[w], v)
		}
		var f func(v, fa, d int)
		f = func(v, fa, d int) {
			if v == b {
				dab = d
			}
			if d > maxD {
				maxD, u = d, v
			}
			for _, w := range g[v] {
				if w != fa {
					f(w, v, d+1)
				}
			}
		}
		maxD = -1
		f(a, 0, 0)
		if 2*da >= db || da >= dab {
			Fprintln(out, "Alice")
			continue
		}
		maxD = -1
		f(u, 0, 0)
		if 2*da >= maxD {
			Fprintln(out, "Alice")
		} else {
			Fprintln(out, "Bob")
		}
	}
}

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

package main

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

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 max2(a, b, c int64) (int64, int64) {
	tmp := []int64{a, b, c}
	sort.Slice(tmp, func(il, ir int) bool {
		return tmp[il] < tmp[ir]
	})
	return tmp[1], tmp[2]
}

func main() {
	defer writer.Flush()
	t := sint64()
	for ; t > 0; t-- {
		n := sint64()
		a := sint64() - 1
		b := sint64() - 1
		da := sint64()
		db := sint64()
		nodes := make([][]int64, n)
		for i := int64(0); i < n-1; i++ {
			u := sint64() - 1
			v := sint64() - 1
			nodes[u] = append(nodes[u], v)
			nodes[v] = append(nodes[v], u)
		}
		printf("%s\n", sol(n, a, b, da, db, nodes))
	}
}

func sol(n, a, b, da, db int64, nodes [][]int64) string {
	// check if bob is able to jump necessarily further than alice
	if db < da*2+1 {
		return "Alice"
	}

	// check if alice finds bob in the first move
	var findBob func(int64, int64) (int64, bool)
	findBob = func(from, to int64) (int64, bool) {
		if to == b {
			return 0, true
		}
		for _, nxt := range nodes[to] {
			if nxt == from {
				continue
			}
			dist, found := findBob(to, nxt)
			if found {
				return dist + 1, true
			}
		}
		return 0, false
	}
	dist, found := findBob(-1, a)
	if !found {
		panic("no path found from alice to bob")
	}
	if dist <= da {
		return "Alice"
	}

	// check if max path is long enough for bob to jump around
	var maxPathSum func(int64, int64) (int64, int64)
	maxPathSum = func(from, to int64) (int64, int64) {
		straight1 := int64(0)
		straight2 := int64(0)
		curve := int64(0)
		for _, node := range nodes[to] {
			if node == from {
				continue
			}
			ncurve, nstraight := maxPathSum(to, node)
			straight1, straight2 = max2(straight1, straight2, nstraight)
			curve = max(curve, ncurve)
		}
		return max(curve, 1+straight1+straight2), 1+max(straight1, straight2)
	}
	maxPath, _ := maxPathSum(-1, 0)
	if maxPath < da*2+2 {
		return "Alice"
	}

	return "Bob"
}

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()
		a := sint64() - 1
		b := sint64() - 1
		da := sint64()
		db := sint64()
		nodes := make([][]int64, n)
		for i := int64(0); i < n-1; i++ {
			u := sint64() - 1
			v := sint64() - 1
			nodes[u] = append(nodes[u], v)
			nodes[v] = append(nodes[v], u)
		}
		printf("%s\n", sol(n, a, b, da, db, nodes))
	}
}

func sol(n, a, b, da, db int64, nodes [][]int64) string {
	// bob needs to be able to jump far enough
	if db < da*2+1 {
		return "Alice"
	}

	// if alice finds bob in the first move
	var findBob func(int64, int64) (int64, bool)
	findBob = func(from, to int64) (int64, bool) {
		if to == b {
			return 0, true
		}
		for _, nxt := range nodes[to] {
			if nxt == from {
				continue
			}
			dist, found := findBob(to, nxt)
			if found {
				return dist+1, true
			}
		}
		return 0, false
	}
	dist, found := findBob(-1, a)
	if !found {
		panic("no path found from alice to bob")
	}
	if dist <= da {
		return "Alice"
	}

	// check if max path is long enough for bob to jump around
	var maxPathSum func(int64, int64) int64
	maxPathSum = func(from, to int64) int64 {
		maxPath := int64(0)
		for _, node := range nodes[to] {
			if node == from {
				continue
			}
			maxPath = max(maxPath, maxPathSum(to, node))
		}
		return maxPath+1
	}
	maxPath := maxPathSum(-1, 0)
	if maxPath < da*2+2 {
		return "Alice"
	}

	return "Bob"
}


package main

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

type pair struct {
	k, v int
}

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

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

	for ; t > 0; t-- {
		fmt.Fscan(in, &n)
		a := make([]pair, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i].v)
			a[i].k = i + 1
		}
		sort.Slice(a, func(i, j int) bool {
			return a[i].v < a[j].v
		})
		result := -1
		if n == 1 || a[1].v != a[0].v {
			result = a[0].k
		} else {
			for i := 1; i < n-1; i++ {
				if a[i].v != a[i+1].v && a[i].v != a[i-1].v {
					result = a[i].k
					break
				}
			}
			if result < 0 && a[n-1].v != a[n-2].v {
				result = a[n-1].k
			}
		}
		fmt.Println(result)
	}
}

package main

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

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

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

func solve() {
	var n int
	scanf("%d\n", &n)
	v := make([]int, n)
	d := make([]int, n)
	for i := 0; i < n; i++ {
		if i + 1 == n {
			scanf("%d\n", &v[i])
		} else {
			scanf("%d ", &v[i])
		}
		d[i] = v[i]
	}
	sort.Ints(v)
	ans := -1
	for i := 0; i < n; i++ {
		j := i
		for ; j < n && v[i] == v[j]; j++ {
		}
		if j - i == 1 {
			ans = v[i]
			break
		}
		i = j - 1
	}
	if ans == -1 {
		printf("-1\n")
		return
	}
	for i := 0; i < n; i++ {
		if d[i] == ans {
			printf("%d\n", i + 1)
			return
		}
	}
}

func main() {
	defer writer.Flush()
	var t int
	scanf("%d\n", &t)
	for ; t > 0; t-- {
		solve()
	}
}

package main

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

// 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 int
		Fscan(in, &n)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		cnt := map[int]int{}
		for _, v := range a {
			cnt[v]++
		}
		ans := -1
		for i, v := range a {
			if cnt[v] == 1 && (ans ==-1 || v < a[ans]) {
				ans = i
			}
		}
		if ans>-1 {
			ans++
		}
		Fprintln(out, ans)
	}

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

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

package main

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

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

func getInt64() int64 {
	scanner.Scan()
	x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
	return x
}

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

func getFloat() float64 {
	scanner.Scan()
	x, _ := strconv.ParseFloat(scanner.Text(), 64)
	return x
}

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

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	for i := getInt(); i > 0; i-- {
		sol()
	}
}

func sol() {
	var arr []int
	n := getInt()
	a := make([]int, n)

	for i := 0; i < n; i++ {
		temp := getInt()
		arr = append(arr, temp)
		a[temp-1]++
	}
	ans := -1
	for i := 0; i < n; i++ {
		if a[i] == 1 {
			ans = i + 1
			break
		}
	}

	if ans == -1 {
		writer.WriteString(fmt.Sprintf("%d\n", ans))
		return
	}

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

package main
import (
	"bufio"
	"fmt"
	"math"
	"os"
)
var rd = bufio.NewReader(os.Stdin)
func min(a, b int) int {
	if a <= b {
		return a
	} else {
		return b
	}
}
func main() {
	q := 0
	fmt.Fscan(rd, &q)
	for t := 0; t < q; t++ {
		n := 0
		fmt.Fscan(rd, &n)
		a := make([]int, n)
		for i := range a {
			fmt.Fscan(rd, &a[i])
		}
		cnt := make(map[int]int)
		for _, v := range a {
			cnt[v]++
		}
		mn := math.MaxInt32
		for k, v := range cnt {
			if v == 1 {
				mn = min(mn, k)
			}
		}
		res := -1
		for i := range a {
			if a[i] == mn {
				res = i + 1
				break
			}
		}
		fmt.Println(res)
	}
}

package main

import(

	"fmt"
	."fmt"
	"bufio"
	"os"
)

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

	Fscan(in,&TESTS)

	for ;TESTS>0;TESTS--{
		var n int
		Fscan(in,&n)
		a := make([]int,n)

		for i := range a{
			Fscan(in,&a[i])
		}

		m := make(map[int]int)

		for i := range a{
			m[a[i]]+=1
		}

		idx,val := -1,-1

		for i := range a{
			if m[a[i]]==1{
				if val==-1 || val>a[i]{
					idx,val = i+1,a[i]
				}
			}
		}

		fmt.Println(idx)
	}
}

package main

import(

	"fmt"
	."fmt"
	"bufio"
	"os"
)

func solve(n int){
	a := make([]int,n)
	in := bufio.NewReader(os.Stdin)

	for i := range a{
		Fscan(in,&a[i])
	}

	m := make(map[int]int)

	for i := range a{
		m[a[i]]+=1
	}

	idx,val := -1,-1

	for i := range a{
		if m[a[i]]==1{
			if val==-1 || val>a[i]{
				idx,val = i+1,a[i]
			}
		}
	}

	fmt.Println(idx)
	return

}

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

	Fscan(in,&TESTS)

	for ;TESTS>0;TESTS--{
		var n int
		Fscan(in,&n)
		solve(n)
	}
}


package main

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

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

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

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

var (
	n, k  int64
	convs []int64
	convM = make(map[int64]struct{})
)

func main() {
	defer writer.Flush()
	Scanf("%d %d\n", &n, &k)

	for i := int64(0); i < n; i++ {
		var ai int64
		Scanf("%d", &ai)

		// is ai in convs?
		if _, ok := convM[ai]; ok {
			continue
		}

		if int64(len(convs)) >= k {
			delete(convM, convs[0])
			convs = convs[1:]
		}
		convs = append(convs, ai)
		convM[ai] = struct{}{}
	}

	Printf("%d\n", len(convs))
	for i := len(convs) - 1; i >= 0; i-- {
		Printf("%d ", convs[i])
	}
}

package main

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

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

func f(in io.Reader, out io.Writer) {
	var n, k int
	fmt.Fscanf(in, "%d %d\n", &n, &k)

	ids := make(chan string, k)
	exists := make(map[string]bool)

	s := bufio.NewScanner(in)

	for _, id := range strings.Split(read(s), " ") {
		if !exists[id] {
			if len(ids) == cap(ids) {
				delete(exists, <-ids)
			}
			exists[id] = true
			ids <- id
 		}
	}

	idsCount := len(ids)
	r := make([]string, idsCount)
	for i := idsCount - 1; i >= 0; i -= 1 {
		r[i] = <-ids
	}

	fmt.Fprintln(out, len(r))
	fmt.Fprintln(out, strings.Join(r, " "))
}

func read(s *bufio.Scanner) string {
	s.Scan()
	return s.Text()
}

package main

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

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

	var n, k, t int
	fmt.Fscanf(reader, "%d %d\n", &n, &k)

	set := make(map[int]bool, k)
	slice := make([]int, 0, k)

	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &t)
		if _, ok := set[t]; ok {
			continue
		}

		if len(set) == k {
			delete(set, slice[0])
			slice = slice[1:]
		}

		set[t] = true
		slice = append(slice, t)
	}

	fmt.Printf("%d\n", len(slice))

	// for _, v := range slice {
	// 	fmt.Printf("%d ", v)
	// }

	for i := len(slice) - 1; i >= 0; i-- {
		fmt.Printf("%d ", slice[i])
	}
}

// Author: sighduck
// URL: https://codeforces.com/contest/1234/problem/B1

package main

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

func Solve(n int, k int, ids []int) []int {
	queue := []int{}
	idSet := make(map[int]bool)
	convos := 0
	for _, elem := range ids {
		if !idSet[elem] && convos < k {
			idSet[elem] = true
			queue = append([]int{elem}, queue...)
			convos += 1
		} else if !idSet[elem] && convos == k {
			idSet[queue[convos-1]] = false
			idSet[elem] = true
			queue = queue[:convos-1]
			queue = append([]int{elem}, queue...)
		}
	}
	return queue
}

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

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

	solution := Solve(n, k, ids)
	fmt.Fprintf(writer, "%d\n", len(solution))
	for _, elem := range solution {
		fmt.Fprintf(writer, "%d ", elem)
	}
	fmt.Fprintf(writer, "\n")
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	var n, k int
	var ID string
	fmt.Fscan(in, &n, &k)
	conversations := make([]string, 0, n)
	inConversations := make(map[string]bool, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &ID)
		if inConversations[ID] {
			continue
		}
		inConversations[ID] = true
		if len(conversations) < k {
			conversations = append(conversations, ID)
			continue
		}
		inConversations[conversations[0]] = false
		conversations = conversations[1:]
		conversations = append(conversations, ID)
	}
	fmt.Fprint(out, len(conversations), "\n")
	for i := len(conversations) - 1; i >= 0; i-- {
		fmt.Fprint(out, conversations[i], " ")
	}
	out.Flush()
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	var n, k, ID int
	fmt.Fscan(in, &n, &k)
	conversations := make([]int, 0, k)
	inConversations := make(map[int]bool, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &ID)
		if inConversations[ID] {
			continue
		}
		inConversations[ID] = true
		if len(conversations) < k {
			conversations = append(conversations, ID)
			continue
		}
		inConversations[conversations[0]] = false
		conversations = conversations[1:]
		conversations = append(conversations, ID)
	}
	fmt.Fprint(out, len(conversations), "\n")
	for i := len(conversations) - 1; i >= 0; i-- {
		fmt.Fprint(out, conversations[i], " ")
	}
	out.Flush()
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	var n, k, ID int
	fmt.Fscan(in, &n, &k)
	conversations := make([]int, 0)
	inConversations := make(map[int]bool, 0)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &ID)
		if inConversations[ID] {
			continue
		}
		inConversations[ID] = true
		if len(conversations) < k {
			conversations = append(conversations, ID)
			continue
		}
		inConversations[conversations[0]] = false
		conversations = conversations[1:]
		conversations = append(conversations, ID)
	}
	fmt.Fprint(out, len(conversations), "\n")
	for i := len(conversations) - 1; i >= 0; i-- {
		fmt.Fprint(out, conversations[i], " ")
	}
	out.Flush()
}

package main

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

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	t, _ := strconv.Atoi(sc.Text())
	return t
}

type queue struct {
	data []int
}

func (q *queue) queue(v int) {
	q.data = append(q.data, v)
}
func (q *queue) dequeue(v *int) (r bool) {
	if len(q.data) > 0 {
		*v = q.data[0]
		q.data = q.data[1:len(q.data)]
		r = true
	}
	return r
}

func main() {
	var n, k int
	fmt.Scan(&n, &k)
	id := make([]int, n)
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	for i := 0; i < len(id); i++ {
		id[i] = nextInt(sc)
	}
	current := make(map[int]bool, k)
	var q queue
	for i := 0; i < len(id); i++ {
		if !current[id[i]] { // not shown now
			current[id[i]] = true
			q.queue(id[i])
			if len(q.data) > k {
				var t int
				q.dequeue(&t)
				current[t] = false
			}
		}
	}
	fmt.Println(len(q.data))
	for i := len(q.data) - 1; i >= 0; i-- {
		fmt.Print(q.data[i], " ")
	}
	fmt.Println()
}

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])
		res = append(res, []rune(str)...)

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

	return string(res)
}

/********** 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 n, k int
var A []int
var Ids []int
var memo map[int]int

func main() {
	n, k = ReadInt2()
	A = ReadIntSlice(n)

	memo = make(map[int]int)
	for i := 0; i < n; i++ {
		memo[A[i]] = 0
	}

	Ids = []int{}
	for i := 0; i < n; i++ {
		a := A[i]

		if isExist(a) {
			continue
		}

		// 追加
		memo[a] = 1

		Ids = append(Ids, a)
		if len(Ids) <= k {
			continue
		} else {
			memo[Ids[0]] = 0
			Ids = Ids[1:]
		}
	}

	fmt.Println(len(Ids))
	reverseIds := rev()
	fmt.Println(PrintIntsLine(reverseIds...))
}

func isExist(id int) bool {
	if value, ok := memo[id]; ok {
		if value == 1 {
			return true
		} else {
			return false
		}
	} else {
		return false
	}
}

func rev() []int {
	res := []int{}

	for i := len(Ids) - 1; i >= 0; i-- {
		res = append(res, Ids[i])
	}

	return res
}

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

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

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])
		res = append(res, []rune(str)...)

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

	return string(res)
}

/********** 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 n, k int
var A []int
var Ids []int
var memo map[int]int

func main() {
	n, k = ReadInt2()
	A = ReadIntSlice(n)

	memo = make(map[int]int)
	for i := 0; i < n; i++ {
		memo[A[i]] = 0
	}

	Ids = []int{}
	for i := 0; i < n; i++ {
		a := A[i]

		if isExist(a) {
			continue
		}

		// 追加
		memo[a] = 1

		Ids = append(Ids, a)
		if len(Ids) <= k {
			continue
		} else {
			memo[Ids[0]] = 0
			Ids = Ids[1:]
		}
	}

	fmt.Println(len(Ids))
	reverseIds := rev()
	fmt.Println(PrintIntsLine(reverseIds...))
}

func isExist(id int) bool {
	// for i := 0; i < len(Ids); i++ {
	// 	if id == Ids[i] {
	// 		return true
	// 	}
	// }
	// return false

	if value, ok := memo[id]; ok {
		if value == 1 {
			return true
		} else {
			return false
		}
	} else {
		return false
	}
}

func rev() []int {
	res := []int{}

	for i := len(Ids) - 1; i >= 0; i-- {
		res = append(res, Ids[i])
	}

	return res
}

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

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

package main

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

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

func f(in io.Reader, out io.Writer) {
	var n, k int
	fmt.Fscanf(in, "%d %d\n", &n, &k)

	ids := make(chan string, k)
	exists := make(map[string]bool)

	s := bufio.NewScanner(in)
	s.Split(bufio.ScanWords)

	for i := 0; i < n; i += 1 {
		id := read(s)

		if !exists[id] {
			if len(ids) == cap(ids) {
				delete(exists, <-ids)
			}
			exists[id] = true
			ids <- id
		}
	}

	idsCount := len(ids)
	r := make([]string, idsCount)
	for i := idsCount - 1; i >= 0; i -= 1 {
		r[i] = <-ids
	}

	fmt.Fprintln(out, len(r))
	fmt.Fprintln(out, strings.Join(r, " "))
}

func read(s *bufio.Scanner) string {
	s.Scan()
	return s.Text()
}

package main

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

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

	var n, k, t int
	fmt.Fscanf(reader, "%d %d\n", &n, &k)

	set := make(map[int]bool, k)
	slice := make([]int, 0, k)
	del := 0

	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &t)
		if _, ok := set[t]; ok {
			continue
		}

		if len(set) == k {
			delete(set, slice[del])
			//slice = slice[1:]
			del++
		}

		set[t] = true
		slice = append(slice, t)
	}

	fmt.Printf("%d\n", len(slice)-del)

	// for _, v := range slice {
	// 	fmt.Printf("%d ", v)
	// }

	for i := len(slice) - 1; i >= del; i-- {
		fmt.Printf("%d ", slice[i])
	}
}

// Author: sighduck
// URL: https://codeforces.com/contest/1234/problem/B1, https://codeforces.com/contest/1234/problem/B2

package main

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

func reverseArray(size int, arr []int) []int {
	reversed := make([]int, size)
	for i := 0; i < size; i++ {
		reversed[i] = arr[size-i-1]
	}
	return reversed
}

func Solve(n int, k int, ids []int) []int {
	queue := []int{}
	idSet := make(map[int]bool)
	size := 0
	for _, elem := range ids {
		if !idSet[elem] && size < k {
			idSet[elem] = true
			queue = append(queue, elem)
			size += 1
		} else if !idSet[elem] && size == k {
			idSet[elem] = true
			queue = append(queue, elem)
			idSet[queue[0]] = false
			queue = queue[1:]
		}
	}
	return reverseArray(size, queue)
}

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

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

	solution := Solve(n, k, ids)
	fmt.Fprintf(writer, "%d\n", len(solution))
	for _, elem := range solution {
		fmt.Fprintf(writer, "%d ", elem)
	}
	fmt.Fprintf(writer, "\n")
}

package main
import (
  "fmt"
  "strconv"
  "strings"
)
type Screen int
var inQueue map[Screen]bool = map[Screen]bool{}
func (c Screen)Set(b bool) {
  if b {
    inQueue[c] = true
  } else {
    delete(inQueue, c)
  }
}
func (c Screen)Get() bool {
  _,x := inQueue[c]
  return x
}
type Queue struct {
  Screens []Screen
  Maxlen int
}
func (q*Queue)Len() int {
  return len(q.Screens)
}

func (q*Queue)Push(s Screen) *Screen {
  var x *Screen
  if q.Len() >= q.Maxlen {
    x = &q.Screens[0]
    q.Screens = q.Screens[1:]
  }
  q.Screens = append(q.Screens, s)
  return x
}

func(q*Queue)Pop() *Screen {
  if q.Len()==0 {
    return nil
  }
  x := &q.Screens[0]
  q.Screens = q.Screens[1:]
  return x
}

func rts(a []Screen) []string {
  k := []string{}
  for i:=len(a)-1;i>=0;i-- {
    k = append(k, strconv.Itoa(int(a[i])))
  }
  return k
}

func main() {
  var n,k int
  var a Screen
  fmt.Scanf("%d%d\n", &n, &k)
  q := Queue{Maxlen:k}
  for i:=0;i<n;i++ {
    fmt.Scanf("%d", &a)
    if !a.Get() {
      a.Set(true)
      b := q.Push(a)
      if b!=nil {
        if a == *b {
          panic("")
        }
        b.Set(false)
      }
    }
  }
  fmt.Println(q.Len())
  fmt.Println(strings.Join(rts(q.Screens), " "))
}

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])
		res = append(res, []rune(str)...)

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

	return string(res)
}

/********** 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 n, k int
var A []int
var Ids []int

func main() {
	n, k = ReadInt2()
	A = ReadIntSlice(n)

	Ids = []int{}
	for i := 0; i < n; i++ {
		a := A[i]

		if isExist(a) {
			continue
		}

		newIds := []int{}
		newIds = append(newIds, a)
		if len(Ids) < k {
			newIds = append(newIds, Ids...)
		} else {
			newIds = append(newIds, Ids[:len(Ids)-1]...)
		}
		Ids = newIds
	}

	fmt.Println(len(Ids))
	fmt.Println(PrintIntsLine(Ids...))
}

func isExist(id int) bool {
	for i := 0; i < len(Ids); i++ {
		if id == Ids[i] {
			return true
		}
	}
	return false
}

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

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

package main
import (
  "fmt"
  "strconv"
  "strings"
)
type Screen int
var inQueue map[Screen]bool = map[Screen]bool{}
func (c Screen)Set(b bool) {
  if b {
    inQueue[c] = true
  } else {
    delete(inQueue, c)
  }
}
func (c Screen)Get() bool {
  _,x := inQueue[c]
  return x
}
type Queue struct {
  Screens []Screen
  Maxlen int
}
func (q*Queue)Len() int {
  return len(q.Screens)
}

func (q*Queue)Push(s Screen) *Screen {
  var x *Screen
  if q.Len() >= q.Maxlen {
    x = &q.Screens[0]
    q.Screens = q.Screens[1:]
  }
  q.Screens = append(q.Screens, s)
  return x
}

func(q*Queue)Pop() *Screen {
  if q.Len()==0 {
    return nil
  }
  x := &q.Screens[0]
  q.Screens = q.Screens[1:]
  return x
}

func rts(a []Screen) []string {
  k := []string{}
  for i:=len(a)-1;i>=0;i-- {
    k = append(k, strconv.Itoa(int(a[i])))
  }
  return k
}

func main() {
  var n,k int
  var a Screen
  fmt.Scanf("%d%d", &n, &k)
  fmt.Println("n:", n)
  fmt.Println("k:", k)
  q := Queue{Maxlen:k}
  for i:=0;i<n;i++ {
    fmt.Scanf("%d", &a)
    fmt.Println("read:",a)
    if !a.Get() {
      a.Set(true)
      b := q.Push(a)
      if b!=nil {
        if a == *b {
          panic("")
        }
        b.Set(false)
      }
    }
  }
  fmt.Println(q.Len())
  fmt.Println(strings.Join(rts(q.Screens), " "))
}

package main
import (
  "fmt"
  "strconv"
  "strings"
)
type Screen int
var inQueue map[Screen]bool = map[Screen]bool{}
func (c Screen)Set(b bool) {
  if b {
    inQueue[c] = true
  } else {
    delete(inQueue, c)
  }
}
func (c Screen)Get() bool {
  _,x := inQueue[c]
  return x
}
type Queue struct {
  Screens []Screen
  Maxlen int
}
func (q*Queue)Len() int {
  return len(q.Screens)
}

func (q*Queue)Push(s Screen) *Screen {
  var x *Screen
  if q.Len() >= q.Maxlen {
    x = &q.Screens[0]
    q.Screens = q.Screens[1:]
  }
  q.Screens = append(q.Screens, s)
  return x
}

func(q*Queue)Pop() *Screen {
  if q.Len()==0 {
    return nil
  }
  x := &q.Screens[0]
  q.Screens = q.Screens[1:]
  return x
}

func rts(a []Screen) []string {
  k := []string{}
  for i:=len(a)-1;i>=0;i-- {
    k = append(k, strconv.Itoa(int(a[i])))
  }
  return k
}

func main() {
  var n,k int
  var a Screen
  fmt.Scanf("%d%d", &n, &k)
  q := Queue{Maxlen:k}
  for i:=0;i<n;i++ {
    fmt.Scanf("%d", &a)
    if !a.Get() {
      a.Set(true)
      b := q.Push(a)
      if b!=nil {
        if a == *b {
          panic("")
        }
        b.Set(false)
      }
    }
  }
  fmt.Println(q.Len())
  fmt.Println(strings.Join(rts(q.Screens), " "))
}