package main

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

var _r = bufio.NewReader(os.Stdin)
var _w = bufio.NewWriter(os.Stdout)

type edge struct {
	first  int
	second int
}

var n, m, k int
var dis [1007][1007]int
var G [1007][]edge
var path []edge

/******** Here is Heap *********/
type PairHeap []edge

func (h PairHeap) Len() int {
	return len(h)
}

func (h PairHeap) Less(i, j int) bool {
	if h[i].first == h[j].first {
		return h[i].second < h[j].second
	}
	return h[i].first < h[j].first
}

func (h PairHeap) Swap(i, j int) {
	h[i], h[j] = h[j], h[i]
}
func (h *PairHeap) Push(x interface{}) {
	*h = append(*h, x.(edge))
}
func (h *PairHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

/******** End of Heap *********/

func dijkstra(st int) {
	for i := 0; i <= n; i++ {
		dis[st][i] = 1e9 + 7
	}
	dis[st][st] = 0
	vis := make([]bool, n+2)
	h := &PairHeap{}
	heap.Init(h)
	heap.Push(h, edge{0, st})
	for h.Len() > 0 {
		cur := (*h)[0]
		heap.Pop(h)
		if vis[cur.second] {
			continue
		}
		vis[cur.second] = true
		for _, e := range G[cur.second] {
			if dis[st][e.first] > dis[st][cur.second]+e.second {
				dis[st][e.first] = dis[st][cur.second] + e.second
				heap.Push(h, edge{dis[st][e.first], e.first})
			}
		}
	}
}

func min(x int, y int) int {
	if x < y {
		return x
	} else {
		return y
	}
}
func main() {
	_, _ = fmt.Fscan(_r, &n, &m, &k)
	for i := 1; i <= m; i++ {
		var x, y, w int
		_, _ = fmt.Fscan(_r, &x, &y, &w)
		G[x] = append(G[x], edge{y, w})
		G[y] = append(G[y], edge{x, w})
	}
	for i := 1; i <= k; i++ {
		var x, y int
		_, _ = fmt.Fscan(_r, &x, &y)
		path = append(path, edge{x, y})
	}
	for i := 1; i <= n; i++ {
		dijkstra(i)
	}
	ans := int(2e9 + 7)

	for v := 1; v <= n; v++ {
		for _, e := range G[v] {
			//enumerate edges
			res := 0
			for _, tmp := range path {
				res += min(dis[tmp.first][tmp.second], min(dis[tmp.first][v]+dis[e.first][tmp.second], dis[tmp.first][e.first]+dis[tmp.second][v]))
			}
			ans = min(ans, res)
		}
	}

	fmt.Println(ans)

}

package main

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

// github.com/EndlessCheng/codeforces-go
type vdPair struct {
	v   int
	dis int
}
type vdHeap []vdPair

func (h vdHeap) Len() int              { return len(h) }
func (h vdHeap) Less(i, j int) bool    { return h[i].dis < h[j].dis }
func (h vdHeap) Swap(i, j int)         { h[i], h[j] = h[j], h[i] }
func (h *vdHeap) Push(v interface{})   { *h = append(*h, v.(vdPair)) }
func (h *vdHeap) Pop() (v interface{}) { a := *h; *h, v = a[:len(a)-1], a[len(a)-1]; return }
func (h *vdHeap) push(v vdPair)        { heap.Push(h, v) }
func (h *vdHeap) pop() vdPair          { return heap.Pop(h).(vdPair) }

func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	type edge struct{ v, w, wt int }

	var n, m, k int
	Fscan(in, &n, &m, &k)
	type nb struct {
		to int
		wt int
	}
	g := make([][]nb, n)
	es := make([]edge, m)
	for i := 0; i < m; i++ {
		var v, w, wt int
		Fscan(in, &v, &w, &wt)
		v--
		w--
		es[i] = edge{v, w, wt}
		g[v] = append(g[v], nb{w, wt})
		g[w] = append(g[w], nb{v, wt})
	}

	dij := func(st int) []int {
		const inf int = 1e9 + 1
		dist := make([]int, n)
		for i := range dist {
			dist[i] = inf
		}
		dist[st] = 0
		q := vdHeap{{st, 0}}
		for len(q) > 0 {
			p := q.pop()
			v := p.v
			if dist[v] < p.dis {
				continue
			}
			for _, e := range g[v] {
				w := e.to
				if newD := dist[v] + e.wt; newD < dist[w] {
					dist[w] = newD
					q.push(vdPair{w, newD})
				}
			}
		}
		return dist
	}
	dis := make([][]int, n)
	for i := 0; i < n; i++ {
		dis[i] = dij(i)
	}

	ans := 0
	rs := make([]edge, k)
	for i := range rs {
		var v, w int
		Fscan(in, &v, &w)
		v--
		w--
		ans += dis[v][w]
		rs[i] = edge{v, w, dis[v][w]}
	}

	maxDelta := 0
	for _, e := range es {
		v, w := e.v, e.w
		delta := 0
		for _, r := range rs {
			d := min(dis[r.v][v]+dis[r.w][w], dis[r.v][w]+dis[r.w][v])
			if d < r.wt {
				delta += r.wt - d
			}
		}
		maxDelta = max(maxDelta, delta)
	}
	Fprint(out, ans-maxDelta)
}

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

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

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


package main

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

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

	var n, v, w, q int
	Fscan(in, &n)
	type edge struct{ to, id int }
	g := make([][]edge, n)
	for i := 0; i < n-1; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], edge{w, i})
		g[w] = append(g[w], edge{v, i})
	}
	pa := make([]int, n)
	for i := range pa {
		pa[i] = i
	}
	var find func(int) int
	find = func(x int) int {
		if pa[x] != x {
			pa[x] = find(pa[x])
		}
		return pa[x]
	}

	d := make([]int, n)
	Fscan(in, &q)
	qs := make([][]int, n)
	vis := make([]int8, n)
	var _f func(int)
	_f = func(v int) {
		vis[v] = 1
		for _, e := range g[v] {
			if w := e.to; vis[w] == 0 {
				_f(w)
				pa[w] = v
			}
		}
		for _, w := range qs[v] {
			if vis[w] == 2 {
				d[v]++
				d[w]++
				d[find(w)] -= 2
			}
		}
		vis[v] = 2
	}
	for ; q > 0; q-- {
		Fscan(in, &v, &w)
		if v != w {
			v--
			w--
			qs[v] = append(qs[v], w)
			qs[w] = append(qs[w], v)
		}
	}
	_f(0)

	ans := make([]interface{}, n-1)
	var f func(v, fa int) int
	f = func(v, fa int) int {
		sum := d[v]
		for _, e := range g[v] {
			if w := e.to; w != fa {
				s := f(w, v)
				ans[e.id] = s
				sum += s
			}
		}
		return sum
	}
	f(0, -1)
	Fprint(out, ans...)
}

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

package main

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

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

var n, k, tyme uint
var log uint
var uu, vv, tin, tout []uint
var count []int
var used []bool
var g, q, p [][]uint

func DFS(v uint, par uint) {
    tyme++
    tin[v] = tyme
    p[v][0] = par
    for i := uint(1); i <= log; i++ {
        p[v][i] = p[p[v][i-1]][i-1]
    }
    for i := 0; i < len(g[v]); i++ {
        if g[v][i] != par {
            DFS(g[v][i], v)
        }
    }
    tyme++
    tout[v] = tyme
}

func IsP(v1 uint, v2 uint) bool {
    return tin[v1] <= tin[v2] && tout[v1] >= tout[v2]
}

func LCA(v1 uint, v2 uint) uint {
    if IsP(v1, v2) {
        return v1
    }
    if IsP(v2, v1) {
        return v2
    }
    for i := int(log); i >= 0; i-- {
        if !IsP(p[v1][i], v2) {
            v1 = p[v1][i]
        }
    }
    return p[v1][0]
}

func GetAns(v uint) int {
    if used[v] {
        return count[v]
    } else {
        for i := 0; i < len(g[v]); i++ {
            if !IsP(g[v][i], v) {
                count[v] += GetAns(g[v][i])
            }
        }
        used[v] = true
        return count[v]
    }
}

func Max(a uint, b uint) uint {
    if a < b {
        return b
    }
    return a
}

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

    scanner.Scan()
    tmp, _ := strconv.ParseUint(scanner.Text(), 10, 32)
    n = uint(tmp)
    for log = 1; (1 << log) <= n; log++ {
    }
    uu, vv, tin, tout, count = make([]uint, n),
        make([]uint, n),
        make([]uint, n),
        make([]uint, n),
        make([]int, n)
    g, q, p = make([][]uint, n), make([][]uint, n), make([][]uint, n)
    for i := uint(0); i < n; i++ {
        p[i] = make([]uint, log+1)
    }

    used = make([]bool, n)
    var u, v uint
    for i := uint(0); i < n-1; i++ {
        scanner.Scan()
        tmp, _ = strconv.ParseUint(scanner.Text(), 10, 32)
        u = uint(tmp)
        scanner.Scan()
        tmp, _ = strconv.ParseUint(scanner.Text(), 10, 32)
        v = uint(tmp)
        u--
        v--
        uu[i] = u
        vv[i] = v
        g[u] = append(g[u], v)
        g[v] = append(g[v], u)
    }
    DFS(0, 0)
    scanner.Scan()
    tmp, _ = strconv.ParseUint(scanner.Text(), 10, 32)
    k = uint(tmp)
    for i := uint(0); i < k; i++ {
        scanner.Scan()
        tmp, _ = strconv.ParseUint(scanner.Text(), 10, 32)
        u = uint(tmp)
        scanner.Scan()
        tmp, _ = strconv.ParseUint(scanner.Text(), 10, 32)
        v = uint(tmp)
        u--
        v--
        count[u]++
        count[v]++
        count[LCA(u, v)] -= 2
    }
    for i := 0; i < int(n); i++ {
        used[i] = false
    }
    for i := 0; i < int(n-1); i++ {
        if IsP(uu[i], vv[i]) {
            w.WriteString(strconv.Itoa(GetAns(vv[i])))
        } else {
            w.WriteString(strconv.Itoa(GetAns(uu[i])))
        }
        w.WriteByte(' ')
    }
    w.WriteByte('\n')
    w.Flush()
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF191C(_r io.Reader, _w io.Writer) {
	out := bufio.NewWriter(_w)
	defer out.Flush()
	buf := make([]byte, 4096)
	_i := len(buf)
	rc := func() byte {
		if _i == len(buf) {
			_r.Read(buf)
			_i = 0
		}
		b := buf[_i]
		_i++
		return b
	}
	r := func() (x int) {
		b := rc()
		for ; '0' > b || b > '9'; b = rc() {
		}
		for ; '0' <= b && b <= '9'; b = rc() {
			x = x*10 + int(b&15)
		}
		return
	}

	n := r()
	type edge struct{ to, id int }
	g := make([][]edge, n)
	for i := 0; i < n-1; i++ {
		v, w := r()-1, r()-1
		g[v] = append(g[v], edge{w, i})
		g[w] = append(g[w], edge{v, i})
	}
	pa := make([]int, n)
	for i := range pa {
		pa[i] = i
	}
	var find func(int) int
	find = func(x int) int {
		if pa[x] != x {
			pa[x] = find(pa[x])
		}
		return pa[x]
	}

	d := make([]int, n)
	qs := make([][]int, n)
	vis := make([]int8, n)
	var _f func(int)
	_f = func(v int) {
		vis[v] = 1
		for _, e := range g[v] {
			if w := e.to; vis[w] == 0 {
				_f(w)
				pa[w] = v
			}
		}
		for _, w := range qs[v] {
			if vis[w] == 2 {
				d[v]++
				d[w]++
				d[find(w)] -= 2
			}
		}
		vis[v] = 2
	}
	for q := r(); q > 0; q-- {
		if v, w := r()-1, r()-1; v != w {
			qs[v] = append(qs[v], w)
			qs[w] = append(qs[w], v)
		}
	}
	_f(0)

	ans := make([]interface{}, n-1)
	var f func(v, fa int) int
	f = func(v, fa int) int {
		sum := d[v]
		for _, e := range g[v] {
			if w := e.to; w != fa {
				s := f(w, v)
				ans[e.id] = s
				sum += s
			}
		}
		return sum
	}
	f(0, -1)
	Fprint(out, ans...)
}

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

package main

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

type edge struct {
	id, to int
}

type rmq struct {
	r      [][]int
	values []int
}

func makeRMQ(indices, values []int) rmq {
	r := [][]int{indices}
	for i := 1; 1<<i <= len(indices); i++ {
		l := 1 << i
		r = append(r, make([]int, len(indices)-l+1))
		for j := range r[i] {
			a := r[i-1][j]
			b := r[i-1][j+l/2]
			if values[a] < values[b] {
				r[i][j] = a
			} else {
				r[i][j] = b
			}
		}
	}
	return rmq{r, values}
}

func (rmq rmq) min(fr, to int) int {
	if fr > to {
		fr, to = to, fr
	}
	i := bits.Len(uint(to-fr+1)) - 1
	a := rmq.r[i][fr]
	b := rmq.r[i][to-(1<<i)+1]
	if rmq.values[a] < rmq.values[b] {
		return a
	}
	return b
}

func main() {
	in := bufio.NewReaderSize(os.Stdin, 2<<16)
	var n int
	fmt.Fscan(in, &n)
	edges := make([][]edge, n+1)
	for i := 0; i < n-1; i++ {
		var u, v int
		fmt.Fscan(in, &u, &v)
		edges[u] = append(edges[u], edge{i, v})
		edges[v] = append(edges[v], edge{i, u})
	}

	var traversal []int
	traversalIndex := make([]int, n+1)
	depth := make([]int, n+1)
	var buildTraversal func(v, p int)
	buildTraversal = func(v, p int) {
		depth[v] = depth[p] + 1
		traversalIndex[v] = len(traversal)
		traversal = append(traversal, v)
		for _, e := range edges[v] {
			if e.to == p {
				continue
			}
			buildTraversal(e.to, v)
			traversal = append(traversal, v)
		}
	}
	buildTraversal(1, 1)

	rmq := makeRMQ(traversal, depth)

	numLCAs := make([]int, n+1)
	numStart := make([]int, n+1)

	var k int
	fmt.Fscan(in, &k)
	for i := 0; i < k; i++ {
		var a, b int
		fmt.Fscan(in, &a, &b)
		numStart[a]++
		numStart[b]++
		numLCAs[rmq.min(traversalIndex[a], traversalIndex[b])]++
	}

	ans := make([]int, n-1)
	var fools func(v, p int) int
	fools = func(v, p int) int {
		f := numStart[v] - 2*numLCAs[v]
		for _, e := range edges[v] {
			if e.to == p {
				continue
			}
			cur := fools(e.to, v)
			ans[e.id] = cur
			f += cur
		}
		return f
	}
	fools(1, 1)

	out := bufio.NewWriterSize(os.Stdout, 2<<16)
	defer out.Flush()
	for _, ans := range ans {
		fmt.Fprintln(out, ans)
	}
}


package main

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

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

	var T, n int
o:
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		cnt := map[int]int{}
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
			cnt[a[i]]++
		}
		for _, c := range cnt {
			if c&1 > 0 {
				Fprintln(out, -1)
				continue o
			}
		}

		op := [][2]int{}
		sz := []int{}
		for len(a) > 0 {
			pos := map[int]int{}
			for i, v := range a {
				p := pos[v]
				if p == 0 {
					pos[v] = i + 1
					continue
				}
				p--

				for j := p - 1; j >= 0; j-- {
					op = append(op, [2]int{i + (len(op)+len(sz))*2 - (p - 1 - j), a[j]})
				}
				for j := p + 1; j < i; j++ {
					op = append(op, [2]int{i + 1 + (len(op)+len(sz))*2 - (j - 1 - p), a[j]})
				}
				for j := p - 1; j >= 0; j-- {
					op = append(op, [2]int{i + 1 + (len(op)+len(sz))*2 - (i - 2 - j), a[j]})
				}
				sz = append(sz, (i+p)*2)

				for i, j := p+1, i-1; i < j; i++ {
					a[i], a[j] = a[j], a[i]
					j--
				}
				copy(a[p:], a[p+1:i])
				copy(a[i-1:], a[i+1:])
				a = a[:len(a)-2]
				break
			}
		}

		Fprintln(out, len(op))
		for _, p := range op {
			Fprintln(out, p[0], p[1])
		}
		Fprintln(out, len(sz))
		for _, v := range sz {
			Fprint(out, v, " ")
		}
		Fprintln(out)
	}
}

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

package main

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

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

	var T, n int
o:
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		cnt := map[int]int{}
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
			cnt[a[i]]++
		}
		for _, c := range cnt {
			if c&1 > 0 {
				Fprintln(out, -1)
				continue o
			}
		}

		op := [][2]int{}
		sz := []int{}
		for len(a) > 0 {
			pos := map[int]int{}
			for i, v := range a {
				p := pos[v]
				if p == 0 {
					pos[v] = i + 1
					continue
				}
				p--

				for j := p - 1; j >= 0; j-- {
					op = append(op, [2]int{i + (len(op)+len(sz))*2 - (p - 1 - j), a[j]})
				}
				for j := p + 1; j < i; j++ {
					op = append(op, [2]int{i + 1 + (len(op)+len(sz))*2 - (j - 1 - p), a[j]})
				}
				for j := p - 1; j >= 0; j-- {
					op = append(op, [2]int{i + 1 + (len(op)+len(sz))*2 - (i - p - 1 + p - 1 - j), a[j]})
				}
				sz = append(sz, (i+p)*2)
				b := append([]int{}, a[p+1:i]...)
				for i, n := 0, len(b); i < n/2; i++ {
					b[i], b[n-1-i] = b[n-1-i], b[i]
				}

				a = append(append(append([]int{}, a[:p]...), b...), a[i+1:]...)
				break
			}
		}

		Fprintln(out, len(op))
		for _, p := range op {
			Fprintln(out, p[0], p[1])
		}
		Fprintln(out, len(sz))
		for _, v := range sz {
			Fprint(out, v, " ")
		}
		Fprintln(out)
	}
}

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

package main

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

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

	var T, n int
o:
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		cnt := map[int]int{}
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
			cnt[a[i]]++
		}
		for _, c := range cnt {
			if c&1 > 0 {
				Fprintln(out, -1)
				continue o
			}
		}
		op := [][2]int{}
		sz := []int{}
		for len(a) > 2 {
			pos := map[int]int{}
			for i, v := range a {
				p := pos[v]
				if p == 0 {
					pos[v] = i + 1
					continue
				}
				p--

				for j := p - 1; j >= 0; j-- {
					op = append(op, [2]int{i + (len(op)+len(sz))*2 - (p - 1 - j), a[j]})
				}
				for j, k := i-1, 0; j >= 0; j-- {
					if j != p {
						op = append(op, [2]int{i + 1 + (len(op)+len(sz))*2 - k, a[j]})
						k++
					}
				}
				sz = append(sz, (i+p)*2)

				a = append(append(append([]int{}, a[:p]...), a[p+1:i]...), a[i+1:]...)
				break
			}
		}
		sz = append(sz, 2)

		Fprintln(out, len(op))
		for _, p := range op {
			Fprintln(out, p[0], p[1])
		}
		Fprintln(out, len(sz))
		for _, v := range sz {
			Fprint(out, v, " ")
		}
		Fprintln(out)
	}
}

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


package main

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

const MOD int64 = 1000000007

var n int

var a[]int

// ByAge implements sort.Interface based on the Age field.
type ByV []*pair

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

func main() {

	read := bufio.NewReader(os.Stdin)

	var t int
	fmt.Fscan(read, &t)

	for i := 0; i < t; i++ {

		fmt.Fscan(read, &n)

		a = make([]int, n)

		for j := 0; j < n; j++ {
			fmt.Fscan(read, &a[j])
		}

		fmt.Println(solve())
	}
}

type pair struct {
	v int
	idx []int
}

func solve() int64 {

	var sum int64

	for i := 0; i < n; i++ {
		sum += int64(a[i])
	}

	if sum % int64(n) == 0 {
		return 0
	}

	return 1
}

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

package main

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

func CF1615A(rawIn io.Reader, rawOut io.Writer) {
	in := bufio.NewReader(rawIn)
	out := bufio.NewWriter(rawOut)
	defer out.Flush()

	var cases int
	fmt.Fscan(in, &cases)
	for c := 0; c < cases; c++ {
		var t int64
		fmt.Fscan(in, &t)

		var sum int64
		sum = 0
		for i := int64(0); i < t; i++ {
			var th int64
			fmt.Fscan(in, &th)
			sum += th
		}

		if sum%t == 0 {
			fmt.Fprint(out, "0\n")
		} else {
			fmt.Fprint(out, "1\n")
		}
	}
}

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

package main

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

func main() {

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

	t := 0
	fmt.Fscan(f, &t)

	for t > 0 {

		n := 0
		fmt.Fscan(f, &n)
		size := n

		sum := 0

		for n > 0 {
			temp := 0
			fmt.Fscan(f, &temp)
			sum += temp
			n--
		}

		if sum%size == 0 {
			fmt.Fprintln(w, 0)
		} else {
			fmt.Fprintln(w, 1)
		}

		w.Flush()

		t--
	}

	// a := []int{1, 2, 3, 1, 5}
	// fmt.Println(solve(a))
}

// func solve(a []int) int {

// 	sum := 0
// 	size := len(a)

// 	for i := range a {
// 		sum += a[i]
// 	}

// 	if sum%size == 0 {
// 		return 0
// 	} else {
// 		return 1
// 	}

// }

package main

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

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

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

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

			sum += v
		}

		if sum%n == 0 {
			fmt.Fprintln(out, 0)
		} else {
			fmt.Fprintln(out, 1)
		}
	}
}

package main

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

type CF1615A struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1615A) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1615A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1615A) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1615A) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1615A) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
TO SEND
Run solve the problem CF1615A
Date: 8/01/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1615/A
Problem: CF1615A
**/
func (in *CF1615A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		sum := 0
		for i := 0; i < n; i++ {
			num := in.NextInt()
			sum += num
		}

		ans := 0
		if sum%n != 0 {
			ans = 1
		}
		fmt.Println(ans)
	}
}

func NewCF1615A(r *bufio.Reader) *CF1615A {
	return &CF1615A{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1615A(bufio.NewReader(os.Stdin)).Run()
}

package main

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

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

func solve(t int) {
	n := readInt()
	sum := 0
	for i := 0; i < n; i++ {
		sum += readInt()
	}
	if sum%n == 0 {
		println(0)
	} else {
		println(1)
	}
}

func main() {
	var t int
	fmt.Fscan(in, &t)

	for i := 0; i < t; i++ {
		solve(i)
	}
}

// READ
func readInt() int {
	var res int
	fmt.Fscan(in, &res)
	return res
}

func readInt64() int64 {
	var res int64
	fmt.Fscan(in, &res)
	return res
}

func readString() string {
	var res string
	fmt.Fscan(in, &res)
	return res
}

// PRINT
func print(args ...interface{}) {
	fmt.Fprint(out, args...)
}

func println(args ...interface{}) {
	fmt.Fprintln(out, args...)
	out.Flush()
}

package main

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

var (
	rr = bufio.NewReader(os.Stdin)
	ww = bufio.NewWriter(os.Stdout)
)

func main() {

	var t int64
	fmt.Fscan(rr, &t)

	for i := int64(0); i < t; i++ {

		var n int64
		fmt.Fscan(rr, &n)

		var sum int64
		for j := int64(0); j < n; j++ {
			var tmp int64
			fmt.Fscan(rr, &tmp)

			sum += tmp

		}

		var bruh int64
		bruh = sum / n
		bruh = bruh * n

		ans := 0

		if bruh != sum {
			ans = 1
		}

		fmt.Fprintln(ww, ans)
		ww.Flush()

	}
	ww.Flush()
}

package main

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

func main() {
	reader := bReader(os.Stdin)
	writer := bWriter(os.Stdout)

	t := readInt(reader)

	for t > 0 {
		n := readInt(reader)
		sum := 0

		a := readArrayInt(reader)
		for i := 0; i < n; i++ {
			sum += a[i]
		}

		if sum % n == 0 {
			fmt.Fprintln(writer, 0)
		} else {
			fmt.Fprintln(writer, 1)
		}

		t--
	}

	writer.Flush()
}

func bReader(stdin *os.File) *bufio.Reader {
	return bufio.NewReaderSize(stdin, 16*1024*1024)
}

func bWriter(stdout *os.File) *bufio.Writer {
	return bufio.NewWriterSize(stdout, 16*1024*1024)
}

func readLine(reader *bufio.Reader) string {
	str, _, err := reader.ReadLine()
	if err == io.EOF {
		return ""
	}

	return strings.TrimRight(string(str), "\r\n")
}

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}

func readInt(reader *bufio.Reader) int {
	return int(readInt64(reader))
}

func readInt64(reader *bufio.Reader) int64 {
	value, err := strconv.ParseInt(readLine(reader), 10, 64)
	checkError(err)

	return value
}

func readString(reader *bufio.Reader) string {
	return readLine(reader)
}

func readArrayString(reader *bufio.Reader) []string {
	return strings.Split(strings.TrimSpace(readLine(reader)), " ")
}

func readArrayInt(reader *bufio.Reader) []int {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64)
		checkError(err)
		arItem := int(arItemTemp)
		ar = append(ar, arItem)
	}

	return ar
}

func readArrayInt64(reader *bufio.Reader) []int64 {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int64

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64)
		checkError(err)
		arItem := arItemTemp
		ar = append(ar, arItem)
	}

	return ar
}

func readTwoInt(reader *bufio.Reader) (int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1]
}

func readTwoInt64(reader *bufio.Reader) (int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1]
}

func readThreeInt(reader *bufio.Reader) (int, int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1], ar[2]
}

func readThreeInt64(reader *bufio.Reader) (int64, int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1], ar[2]
}

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

	return x
}

func lcm(x int64, y int64) int64 {
	return x*y/gcd(x,y)
}

package main

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

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

func main() {
	var ntc int
	fmt.Fscan(in, &ntc)
	for t := 0; t < ntc; t++ {
		var n = 0
		fmt.Fscan(in, &n)
		var ai = 0
		var sum = 0
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &ai)
			sum += ai
		}
		if sum%n == 0 {
			fmt.Fprintf(out, "0\n")
		} else {
			fmt.Fprintf(out, "1\n")
		}
	}
	out.Flush()
}

package main
// github.com/jamesgrimard/codeforces
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var n,sum int
		fmt.Fscan(in,&n)
		list:= make([]int,n)
		for j:=0;j<n;j++ {
			fmt.Fscan(in,&list[j])
			sum+=list[j]
		}
		if sum%n == 0 {
			fmt.Fprintln(out,0)
		} else {
			fmt.Fprintln(out,1)
		}
	}
}

package main

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

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, n, p := 0, 0, 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &n)
		sum := 0
		for j := 0; j < n; j++ {
			fmt.Fscan(f, &p)
			sum += p
		}
		if sum%n == 0 {
			fmt.Fprintln(w, 0)
		} else {
			fmt.Fprintln(w, 1)
		}
	}
	w.Flush()
}

func main() {
	solve()
}

package main
// github.com/jamesgrimard/codeforces
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var n int
		fmt.Fscan(in,&n)
		list:= make([]int,n)
		for j:=0;j<n;j++ {fmt.Fscan(in,&list[j])}
		for j:=1;j<n;j++{
			if list[j] < list[j-1] {
				list[j],list[j-1]=list[j-1],list[j]
				j-=2
				if j<0 {
					j=0
				}
			}
		}
		for j:=0;j<=n/2;j++{
			list[j],list[n-1-j] = (list[j]+(list[n-1-j]-list[j])/2),(list[n-j-1]-(list[n-1-j]-list[j])/2)
		}
		for j:=1;j<n;j++{
			if list[j] < list [j-1] {
				list[j],list[j-1] = list[j-1],list[j]
				j-=2
				if j < 0 {
					j=0
				}
			}
		}
		fmt.Fprintln(out,list[n-1]-list[0])
	}
}

package main
// github.com/jamesgrimard/codeforces
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var n int
		fmt.Fscan(in,&n)
		list:= make([]int,n)
		for j:=0;j<n;j++ {fmt.Fscan(in,&list[j])}
		for j:=1;j<n;j++{
			if list[j] < list[j-1] {
				list[j],list[j-1]=list[j-1],list[j]
				j-=2
				if j<0 {
					j=0
				}
			}
		}
		for j:=0;j<=n/2;j++{
			list[j],list[n-1-j] = (list[j]+(list[n-1-j]-list[j])/2),(list[n-j-1]-(list[n-1-j]-list[j])/2)
		}
		fmt.Fprintln(out,list[n-1]-list[1])
	}
}

package main
// github.com/jamesgrimard/codeforces
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var n,xor,sum int
		fmt.Fscan(in,&n)
		list:= make([]int,n)
		for j:=0;j<n;j++{
			fmt.Fscan(in,&list[j])
			sum+=list[j]
			xor^=list[j]
		}
		if sum%n != 0 {
			fmt.Fprintln(out,xor-(sum/n+1))
			continue
		}
		fmt.Fprintln(out,xor-sum/n)
	}
}

package main

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

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, n, p := 0, 0, 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &n)
		min, max, sum, o := math.MaxInt, math.MinInt, 0, map[int]int{}
		for j := 0; j < n; j++ {
			fmt.Fscan(f, &p)
			sum += p
			if p > max {
				max = p
			}
			if p < min {
				min = p
			}
			o[p]++
		}
		avg := sum / n
		fmt.Fprintln(w, ((o[max] * (max - avg)) - (o[min] * (avg - min))))
	}
	w.Flush()
}

func main() {
	solve()
}

package main

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

func CF1615A(in io.Reader, out io.Writer) {
	var cases int
	fmt.Fscan(in, &cases)
	for c := 0; c < cases; c++ {
		var t int64
		fmt.Fscan(in, &t)

		var sum int64
		sum = 0
		for i := int64(0); i < t; i++ {
			var th int64
			fmt.Fscan(in, &th)
			sum += th
		}

		if sum%t == 0 {
			fmt.Fprint(out, "0\n")
		} else {
			fmt.Fprint(out, "1\n")
		}
	}
}

func main() { CF1615A(bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)) }


package main

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

func main() {
    writer := bufio.NewWriter(os.Stdout)
    var a,b,n int
    fmt.Scan(&a,&b,&n)
    ok := false
    for i := 0; i < 10; i++ {
        if ((a*10)+i)%b == 0 {
            ok, a = true, (a*10)+i
            n--
            break
        }
    }
    if ok {
        fmt.Print(a)
        for ; n > 0; n-- { writer.WriteRune('0') }
        writer.WriteRune('\n')
        writer.Flush()
    } else {
        fmt.Println(-1)
    }
}

package main
import (
	"fmt"
)
func main() {
	var a,b,n,j int
	fmt.Scan(&a,&b,&n)
	flag:=false
	for i:=0;i<10;i++ {
		if (a*10+i)%b==0 {
			fmt.Printf("%d%d",a,i)
		for j=0;j<n-1;j++ {
			fmt.Printf("0")
		}
		flag=true
		break
		}
	}
	if flag==false {
		fmt.Println("-1")
	}
}

// 260A-mic
package main

import (
    "fmt"
)

func main() {
    var a, b, n int
    fmt.Scan(&a, &b, &n)
    a *= 10
    x := a % b
    if x != 0 {
        x = b - x
    }
    if x/10 == 0 {
        a += x
    } else {
        a = -1
    }
    fmt.Print(a)

    if a != -1 {
        for i := 1; i < n; i++ {
            fmt.Print("0")
        }
        fmt.Println()
    }
}

package main
import (
	"fmt"
)
func main() {
	var a,b,n,j int
	fmt.Scan(&a,&b,&n)
	flag:=false
	for i:=0;i<10;i++ {
		if (a*10+i)%b==0 {
			fmt.Printf("%d%d",a,i)
		for j=0;j<n-1;j++ {
			fmt.Printf("0")
			flag=true
		}
		break
		}
	}
	if flag==false {
		fmt.Println("-1")
	}
}


package main
import (
    "fmt"
    "unicode/utf8"
)

func main() {
    var k int
    flag:=false
    var x string
    n:=make([]int,27)
    fmt.Scan(&k,&x)
    len:= utf8.RuneCountInString(x)
    for i:=0; i<len;i++ {
        n[int(x[i]-'a'+1)]++
    }
    for i:=1; i<=26;i++ {
            if n[i]%k!=0{
                fmt.Println("-1")
                flag=true
                break
            }
    }
    if flag==false {
        for q:=1;q<=k;q++ {
            for i:=1;i<=26;i++ {
                for j:=1;j<=n[i]/k;j++ {
                    fmt.Print(string(i+'a'-1))
                }
            }
        }
    }
    fmt.Println()
}

package main

import (
	"fmt"
)

var ps = fmt.Printf
var ss = fmt.Scanf

func main() {

	var s string
	var k int
	ss("%d\n%s\n", &k, &s)
	count := [27]int{0}
	for i := 0; i < len(s); i++ {
		count[int(s[i])-97]++
	}
	if len(s)%k != 0 {
		ps("-1")
		return
	}
	s = ""
	for i := 0; i <= 25; i++ {
		c := count[i] / k
		if count[i]%k == 0 {
			for j := 0; j < c; j++ {
				s = s + string(i+97)
			}
		} else {
			ps("-1")
			return
		}
	}
	for k > 0 {
		ps(s)
		k--
	}
}

package main

import (
	"fmt"
	"strings"
)

func kstring(str string, k int, table []int) {
	res := ""
	for i := 0; i < len(str); i++ {
		table[str[i]-97]++
	}
	for i := 0; i < 26; i++ {
		if table[i] != 0 {
			if table[i]%k == 0 {
				res += strings.Repeat(string(i+97), table[i]/k)
			} else {
				fmt.Println(-1)
				return
			}
		}
	}
	fmt.Println(strings.Repeat(res, k))
}

func main() {
	var k int
	var str string
	table := make([]int, 26)
	fmt.Scanln(&k)
	fmt.Scanln(&str)
	kstring(str, k, table)
}

package main

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

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

	reader := bufio.NewReader(os.Stdin)
	re := regexp.MustCompile(`\r?\n`)
	if s, err := reader.ReadString('\n'); err != nil {
		if err != io.EOF {
			return
		}
	} else {
		s = re.ReplaceAllString(s, "")
		if k == 1 {
			fmt.Println(s)
			return
		}

		dict := make(map[byte]int)
		for i := 0; i < len(s); i++ {
			dict[s[i]] = dict[s[i]] + 1
		}

		var sub_string []byte

		for l, v := range dict {
			if v%k != 0 {
				fmt.Println(-1)
				return
			} else {
				for i := 0; i < v/k; i++ {
					sub_string = append(sub_string, l)
				}
			}
		}

		for i := 0; i < k; i++ {
			fmt.Printf("%s", string(sub_string))
		}
		fmt.Println()
	}
}

package main

import "fmt"

func main() {
    var k int
    var s string
    var char [26]int
    fmt.Scan(&k)
    fmt.Scan(&s)
    for _, v := range s { char[v-'a']++ }
    if len(s)%k == 0 {
        ans := true
        for i := 0; i < 26; i++ {
            if char[i] % k != 0 {
                ans = false
                break
            }
        }
        if !ans {
            fmt.Println(-1)
        } else {
            ns := ""
            for i := 0; i < 26; i++ {
                for j := 0; j < char[i]/k; j++ {
                    ns += string(i+'a')
                }
            }
            for ; k > 0; k-- { fmt.Print(ns) }
            fmt.Println()
        }
    } else {
        fmt.Println(-1)
    }
}

package main

import (
    "fmt"
)

func main() {
    var k int
    var str string
    fmt.Scanf("%d\n", &k)
    fmt.Scanf("%s\n", &str)
    D := make(map[int]int)
    for _, v := range str {
        D[int(v)]++
    }
    for _, v := range D {
        if v%k != 0 {
            fmt.Printf("-1\n")
            return
        }
    }
    for j := 0; j < k; j++ {
        for A := 0x61; A <= 0x7A; A++ {
            v := D[A]
            for i := 0; i < v/k; i++ {
                fmt.Printf("%c", A)
            }
        }
    }
    fmt.Print("\n")

}

package main

import (
	"fmt"
)

func solve(k int, s string) string {
	length := len(s)
	if length%k != 0 {
		return "-1"
	}

	counts := make([]int, 26, 26)
	for i := 0; i < length; i++ {
		counts[s[i]-'a']++
	}

	tmp := ""
	for i := 0; i < 26; i++ {
		if counts[i] == 0 {
			continue
		}
		if counts[i]%k != 0 {
			return "-1"
		}
		for j := 0; j < counts[i]/k; j++ {
			tmp += string(i + 'a')
		}
	}

	ans := ""
	for i := 0; i < length/len(tmp); i++ {
		ans += tmp
	}

	return ans
}

func main() {
	var k int
	fmt.Scan(&k)

	var s string
	fmt.Scan(&s)

	fmt.Println(solve(k, s))
}

package main

import (
	"fmt"
	"strings"
)

func main() {
	var (
		count int
		str   string
	)

	fmt.Scanf("%d\n", &count)
	charMap := make(map[rune]int)

	fmt.Scan(&str)
	for i := 0; i < len(str); i++ {
		charMap[rune(str[i])]++
	}

	fragment := make([]rune, 0)
	for char, amount := range charMap {
		if amount % count != 0 {
			fmt.Println(-1)
			return
		}
		for i := amount / count; i >= 1; i-- {
			fragment = append(fragment, char)
		}
	}

	result := strings.Repeat(string(fragment), count)

	fmt.Println(result)
}

package main

import "fmt"

func main() {
	var k int
	var s string
	fmt.Scanf("%d\n%s", &k, &s)
	cnt := [26]int{}
	for _, c := range s {
		cnt[c - 'a']++
	}
	for _, v := range cnt {
		if v % k != 0 {
			fmt.Println(-1)
			return
		}
	}
	for i := 0; i < k; i++ {
		for j := 0; j < 26; j++ {
			for z := 0; z < cnt[j] / k; z++ {
				fmt.Print(string('a' + j));
			}
		}
	}
}

package main

import (
	"fmt"
	"strings"
)

func kstring(str string, k int, table []byte) {
	res := ""
	for i := 0; i < len(str); i++ {
		table[str[i]-97]++
	}
	for i := 0; i < 26; i++ {
		if table[i] != 0 {
			if int(table[i])%k == 0 {
				res += strings.Repeat(string(i+97), int(table[i])/k)
			} else {
				fmt.Println(-1)
				return
			}
		}
	}
	fmt.Println(strings.Repeat(res, k))
}

func main() {
	var k int
	var str string
	table := make([]byte, 26)
	fmt.Scanln(&k)
	fmt.Scanln(&str)
	kstring(str, k, table)
}

package main

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

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

	reader := bufio.NewReader(os.Stdin)
	re := regexp.MustCompile(`\r?\n`)
	if s, err := reader.ReadString('\n'); err != nil {
		if err != io.EOF {
			return
		}
	} else {
		s = re.ReplaceAllString(s, "")
		if k == 1 {
			fmt.Println(s)
			return
		}

		if len(s)%2 != 0 {
			fmt.Println(-1)
		} else {
			dict := make(map[byte]int)
			for i := 0; i < len(s); i++ {
				dict[s[i]] = dict[s[i]] + 1
			}

			var sub_string []byte

			for l, v := range dict {
				if v%k != 0 {
					fmt.Println(-1)
					return
				} else {
					for i := 0; i < v/k; i++ {
						sub_string = append(sub_string, l)
					}
				}
			}

			for i := 0; i < k; i++ {
				fmt.Printf("%s", string(sub_string))
			}
			fmt.Println()
		}
	}
}

package main

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

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

	reader := bufio.NewReader(os.Stdin)
	re := regexp.MustCompile(`\r?\n`)
	if s, err := reader.ReadString('\n'); err != nil {
		if err != io.EOF {
			return
		}
	} else {
		s = re.ReplaceAllString(s, "")

		if len(s)%2 != 0 {
			fmt.Println(-1)
		} else {
			dict := make(map[byte]int)
			for i := 0; i < len(s); i++ {
				dict[s[i]] = dict[s[i]] + 1
			}

			var sub_string []byte

			for l, v := range dict {
				if v%k != 0 {
					fmt.Println(-1)
					return
				} else {
					for i := 0; i < v/k; i++ {
						sub_string = append(sub_string, l)
					}
				}
			}

			for i := 0; i < k; i++ {
				fmt.Printf("%s", string(sub_string))
			}
			fmt.Println()
		}
	}
}

package main

import (
    "fmt"
)

func main() {
    var k int
    var str string
    fmt.Scanf("%d\n", &k)
    fmt.Scanf("%s\n", &str)
    D := make(map[int]int)
    for _, v := range str {
        D[int(v)]++
    }
    for _, v := range D {
        if v%k != 0 {
            fmt.Printf("-1\n")
            return
        }
    }
    for j := 0; j < k; j++ {
        for K, v := range D {
            for i := 0; i < v/k; i++ {
                fmt.Printf("%c", K)
            }
        }
    }
    fmt.Print("\n")

}

// 219A
package main

import (
	"fmt"
)

func main() {
	var k int
	fmt.Scan(&k)
	var input, output string
	fmt.Scan(&input)
	var letters [26]int
	for _, u := range input {
		letters[u-'a']++
	}
	for u := 0; u < 26; u++ {
		if letters[u] == 0 {
			continue
		} else if letters[u]%k != 0 {
			for v := 0; v < letters[u]/k; v++ {
				output += string('a') + string(u)
			}
		} else {
			output = "-1"
			break
		}
	}
	if output == "-1" {
		fmt.Println(output)
	} else {
		for u := 0; u < k; u++ {
			fmt.Print(output)
		}
		fmt.Println()
	}
}

package main

import (
	"fmt"
)

func solve(k int, s string) string {
	length := len(s)
	if length%k != 0 {
		return "-1"
	}

	counts := make([]int, 26, 26)
	for i := 0; i < length; i++ {
		counts[s[i]-'a']++
	}

	n := length / k
	tmp := ""
	for i := 0; i < 26; i++ {
		if counts[i] == 0 {
			continue
		}
		if counts[i]%k != 0 {
			return "-1"
		}
		for j := 0; j < counts[i]/n; j++ {
			tmp += string(i + 'a')
		}
	}

	ans := ""
	for i := 0; i < n; i++ {
		ans += tmp
	}

	return ans
}

func main() {
	var k int
	fmt.Scan(&k)

	var s string
	fmt.Scan(&s)

	fmt.Println(solve(k, s))
}

package main

import (
	"fmt"
)

func solve(k int, s string) string {
	length := len(s)
	if length%k != 0 {
		return "-1"
	}

	counts := make([]int, 26, 26)
	for i := 0; i < length; i++ {
		counts[s[i]-'a']++
	}

	n := length / k
	tmp := ""
	for i := 0; i < 26; i++ {
		if counts[i] == 0 {
			continue
		}
		if counts[i] != n {
			return "-1"
		}
		tmp += string(i + 'a')
	}

	ans := ""
	for i := 0; i < n; i++ {
		ans += tmp
	}

	return ans
}

func main() {
	var k int
	fmt.Scan(&k)

	var s string
	fmt.Scan(&s)

	fmt.Println(solve(k, s))
}

package main

import (
	"fmt"
)

func solve(k int, s string) string {
	length := len(s)
	if length%k != 0 {
		return "-1"
	}

	counts := make([]int, 26, 26)
	for i := 0; i < length; i++ {
		counts[s[i]-'a']++
	}

	n := length / k
	tmp := ""
	for i := 0; i < 26; i++ {
		if counts[i] == 0 {
			continue
		}
		if counts[i]%n != 0 {
			return "-1"
		}
		for j := 0; j < counts[i]/n; j++ {
			tmp += string(i + 'a')
		}
	}

	ans := ""
	for i := 0; i < n; i++ {
		ans += tmp
	}

	return ans
}

func main() {
	var k int
	fmt.Scan(&k)

	var s string
	fmt.Scan(&s)

	fmt.Println(solve(k, s))
}


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

func main(){
    var n int
    stdin := bufio.NewReader(os.Stdin)
    fmt.Fscanf(stdin, "%d\n", &n)
    cnt := 0
    for i := 0; i < n; i++{
        var m int
        fmt.Fscanf(stdin, "%d", &m)
        if m % 2 != 0{
            cnt += 1
        }
    }
    if cnt == 0{
        fmt.Println("Second")
    } else{
        fmt.Println("First")
    }
}

package main

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

func main() {
	var n, temp int

	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &n)

	for i := 0; i < n ; i++ {
		fmt.Fscan(reader, &temp)
		if temp % 2 == 1 {
			fmt.Println("First")
			return
		}
	}

	fmt.Println("Second")
}

package main

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

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

	n := readInt(reader)
	b := readA(reader, n)
	a := make([]int, n)
	total_parity := 0
	for i,v := range *b {
		if v%2==0 {
			a[i] = 0
		} else {
			a[i] = 1
			if total_parity==0 {
				total_parity = 1
			} else {
				total_parity = 0
			}
		}
	}

	ok := true

	if total_parity!=1 {
		ok = false
		for _,v := range a {
			if v==1 {
				ok = true
				break
			}
		}
	}

	if ok {
		fmt.Println("First")
	} else {
		fmt.Println("Second")
	}

}

func readLine(nr *bufio.Reader) *string {
	line, _, err := nr.ReadLine()
	if err!=nil {
		panic(err)
	}
	s := string(line)
	return &s
}

func skipLine(nr *bufio.Reader) {
	_, _, err := nr.ReadLine()
	if err!=nil {
		panic(err)
	}
}

func readInt(nr *bufio.Reader) int {
	var a int
	fmt.Fscan(nr, &a)
	return a
}

func readA(nr *bufio.Reader, n int) *[]int {
	a := make([]int, n)

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

	return &a
}

package main

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

func main(){
    var n, sum int
    stdin := bufio.NewReader(os.Stdin)
    fmt.Fscanf(stdin, "%d\n", &n)

    for i := 0; i < n; i++{
        var cr int
        fmt.Fscanf(stdin, "%d", &cr)
        sum += cr % 2
    }

    if sum != 0 {
        fmt.Println("First")
    } else {
        fmt.Println("Second")
    }
}

package main

/*
    To solve this problem, notice that beside odd & even sum, there is also
    player turns starting by the first player. So even though the total sum of
    number is even, the first player still able to win as long as there is
    odd number on the array.

    Thus if there is odd number on the array, then first player surely would
    be the winner.
*/

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

func main(){
    var n, sum int
    stdin := bufio.NewReader(os.Stdin)
    fmt.Fscanf(stdin, "%d\n", &n)

    for i := 0; i < n; i++{
        var cr int
        fmt.Fscanf(stdin, "%d", &cr)
        sum += cr % 2
    }

    // if there is odd number on the sequence
    // the sum != 0
    if sum != 0 {
        fmt.Println("First")
    } else {
        fmt.Println("Second")
    }
}

package main

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

const maxInputSize = 512 * 1024 //bytes

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

	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	// a := make([]int, n)

	for i := 0; i < n; i++ {
		scanner.Scan()
		x, _ := strconv.Atoi(scanner.Text())
		if x%2 == 1 {
			fmt.Println("First")
			return
		}
	}
	fmt.Println("Second")
}

package main

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

func solve() {
	n := readInt()
	a := make([]int, n)
	total := int64(0)
	for i := 0; i < n; i++ {
		a[i] = readInt()
		total += int64(a[i])
	}
	if total%2 != 0 {
		fmt.Println("First")
		return
	}
	total = int64(0)
	for i := 0; i < n; i++ {
		total += int64(a[i])
		if total%2 != 0 {
			fmt.Println("First")
			return
		}
	}
	total = int64(0)
	for i := n - 1; i >= 0; i-- {
		total += int64(a[i])
		if total%2 != 0 {
			fmt.Println("First")
			return
		}
	}
	fmt.Println("Second")
}

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

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

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

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

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

package main

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

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

	fmt.Fscan(reader, &qr)
	for qr > 0 {
		var qrs int
		fmt.Fscan(reader, &qrs)

		if qrs%2 != 0 {
			fmt.Println("First")
			return
		}

		qr--
	}

	fmt.Println("Second")

}

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

func main(){
    var n int
    stdin := bufio.NewReader(os.Stdin)
    fmt.Fscanf(stdin, "%d", &n)
    cnt := 0
    for i := 0; i < n; i++{
        var m int
        fmt.Fscanf(stdin, "%d", &m)
        if m % 2 != 0{
            cnt += 1
        }
    }
    if cnt == 0{
        fmt.Println("Second")
    } else{
        fmt.Println("First")
    }
}

package main

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

func main(){
    var n, sum int
    stdin := bufio.NewReader(os.Stdin)
    fmt.Fscanf(stdin, "%d\n", &n)

    for i := 0; i < n; i++{
        var cr int
        fmt.Fscanf(stdin, "%d", &cr)
        sum += cr
    }

    if sum % 2 != 0 {
        fmt.Println("First")
    } else {
        fmt.Println("Second")
    }
}

package main

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

func solve() {
	n := readInt()
	a := make([]int, n)
	total := int64(0)
	for i := 0; i < n; i++ {
		a[i] = readInt()
		total += int64(a[i])
	}
	if total%2 != 0 {
		fmt.Println("First")
	} else {
		fmt.Println("Second")
	}
}

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

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

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

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

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


package main

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

func MinInt(a int, b int) int {
	if a < b {
		return a
	}
	return b
}
func main() {
	r:= bufio.NewReader(os.Stdin)
	var t, n int

	for fmt.Fscan(r,&t); t > 0; t-- {
		fmt.Fscan(r,&n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(r, &a[i])
		}
		if (n <= 1) {
			fmt.Println(-1)
		} else {
			m := make(map[int]int)
			mi := n + 1
			for i := 0; i < n; i++ {
				if v, ok := m[a[i]]; ok {
					mi = MinInt(mi, i-v+1)
				}
				m[a[i]] = i
			}
			if mi == n+1 {
				mi = -1
			}
			fmt.Println(mi)
		}
	}
}

package main

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

func main() {
	T := ReadInt()

	for T > 0 {
		n := ReadInt()

		arr := ReadIntSlice(n)

		fmt.Println(solve(arr))

		T--
	}
}

func solve(arr []int) int {
	if len(arr) <= 1 {
		return -1
	}

	ans := -1

	m := make(map[int]int)

	for i, v := range arr {
		p, ok := m[v]
		if !ok {
			m[v] = i
		} else {
			m[v] = i
			if ans == -1 {
				ans = i - p + 1
			} else {
				ans = minInt(ans, i-p+1)
			}
		}
	}

	return ans
}

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

	return b
}

package main

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

// Educational Codeforces Round 76
// C. Dominated Subarray
// https://codeforces.com/contest/1257/problem/C
func min(a, b int) int {
	if a > b {
		return b
	}
	return a
}

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

const inf = int(1e9) + 1

func main() {
	var T int
	fmt.Scan(&T)
	ans := make([]int, 0)
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	for i := 0; i < T; i++ {
		N := nextInt(sc)
		set := make(map[int]int, 0)    // 整数xの最後の出現位置を覚えておくハッシュマップ
		lenset := make(map[int]int, 0) // 整数x同士の距離の最小値
		minlen := inf
		for j := 0; j < N; j++ {
			v := nextInt(sc)
			if set[v] == 0 {
				set[v] = j + 1
			} else {
				tlen := j + 1 - set[v] + 1
				if lenset[v] == 0 {
					lenset[v] = tlen
				} else {
					lenset[v] = min(tlen, lenset[v])
				}
				minlen = min(lenset[v], minlen)
				set[v] = j + 1
			}
		}
		ans = append(ans, minlen)
	}
	for i := range ans {
		if ans[i] == inf {
			fmt.Println(-1)
		} else {
			fmt.Println(ans[i])
		}
	}
}

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
}

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

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

	return s
}

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

	if e == 0 {
		return 1
	}

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

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

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

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

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

	return a
}

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

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

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

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

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

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

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

	return string(res)
}

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

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

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

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

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

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

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

var t int
var n int
var A []int
var cnt []int

func main() {
	t = ReadInt()

	for tc := 0; tc < t; tc++ {
		n = ReadInt()
		A = ReadIntSlice(n)
		solve()
	}
}

func solve() {
	if n == 1 {
		fmt.Println(-1)
		return
	}

	cnt = make([]int, n+1)
	flag := true
	for i := 0; i < n; i++ {
		a := A[i]
		if cnt[a] > 0 {
			// 2回登場するものがあれば答えはある
			flag = false
			break
		}
		cnt[a]++
	}

	if flag {
		// すべて異なる数ならNO
		fmt.Println(-1)
		return
	}

	// m は中央を意味する何らかの値
	isOK := func(m int) bool {
		// リセット
		cnt = make([]int, n+1)
		// 初期化
		for i := 0; i < m; i++ {
			a := A[i]
			if cnt[a] > 0 {
				return true
			}
			cnt[a]++
		}
		// スライド
		for i := m; i < n; i++ {
			j := i - m
			cnt[A[j]]--
			a := A[i]
			if cnt[a] > 0 {
				return true
			}
			cnt[a]++
		}

		return false
	}

	ng, ok := 1, n
	for int(math.Abs(float64(ok-ng))) > 1 {
		mid := (ok + ng) / 2
		if isOK(mid) {
			ok = mid
		} else {
			ng = mid
		}
	}
	// boundaryIdx := ok
	fmt.Println(ok)
}

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

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

package main

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

var in = bufio.NewScanner(os.Stdin)

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

func solve() {
	n := readInt()
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = readInt()
	}
	p := map[int]int{}
	ans := -1
	for i := 0; i < n; i++ {
		if _, ok := p[a[i]]; ok {
			l := i - p[a[i]] + 1
			if ans == -1 || l < ans {
				ans = l
			}
		}
		p[a[i]] = i
	}
	fmt.Println(ans)
}

func main() {
	in.Split(bufio.ScanWords)
	t := readInt()
	for i := 0; i < t; i++ {
		solve()
	}
}

package main

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

/* solution */

func solve() {
	t := readInt()
	for i := 0; i < t; i++ {
		printInt(solveOne())
	}
}

func solveOne() int {
	n := readInt()
	// a := make([]int, n)
	prev := make([]int, n+1)
	ans := -1
	for i := 1; i <= n; i++ {
		v := readInt()
		if prev[v] != 0 {
			cand := i - prev[v] + 1
			if ans == -1 || cand < ans {
				ans = cand
			}
		}
		prev[v] = i
	}

	return ans

}

/* 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("\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
}

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

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

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

	return s
}

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

	if e == 0 {
		return 1
	}

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

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

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

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

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

	return a
}

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

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

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

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

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

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

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

	return string(res)
}

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

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

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

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

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

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

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

var t int
var n int
var A []int
var cnt []int

func main() {
	for t:= ReadInt(); t > 0; t-- {
		n:= ReadInt()
		a := ReadIntSlice(n)

		if (n <= 1) {
			fmt.Println(-1)
		} else {
			m := make(map[int]int)
			mi := n + 1
			for i := 0; i < n; i++ {
				if v, ok := m[a[i]]; ok {
					mi = Min(mi, i-v+1)
				}
				m[a[i]] = i
			}
			if mi == n+1 {
				mi = -1
			}
			fmt.Println(mi)
		}
	}
}

package main

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

var reader = bufio.NewReader(os.Stdin)

func main() {
	text, _ := reader.ReadString('\n')
	t, _ := strconv.ParseInt(strings.TrimSpace(text), 10, 64)
	lst := [200001]int{}
	ret := 0
	for i := int64(0); i < t; i++ {
		text, _ = reader.ReadString('\n')
		text = strings.TrimSpace(text)
		n, _ := strconv.ParseInt(text, 10, 64)
		for j := int64(0); j <= n; j++ {
			lst[j] = 0
		}
		text, _ = reader.ReadString('\n')
		text = strings.TrimSpace(text)
		parts := strings.Split(text, " ")
		ret = 0
		for k, p := range parts {
			v, _ := strconv.ParseInt(p, 10, 64)
			if lst[v] != 0 && (k-lst[v]+2 < ret || ret == 0) {
				ret = k - lst[v] + 2
			}
			lst[v] = k + 1
		}
		if ret == 0 {
			fmt.Println("-1")
		} else {
			fmt.Println(ret)
		}
	}
}

package main

import (
	"fmt"
)

func MinInt(a int, b int) int {
	if a < b {
		return a
	}
	return b
}
func main() {
	var t, n int

	for fmt.Scan(&t); t > 0; t-- {
		fmt.Scan(&n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Scanf("%d", &a[i])
		}
		if (n <= 1) {
			fmt.Println(-1)
		} else {
			m := make(map[int]int)
			mi := n + 1
			for i := 0; i < n; i++ {
				if v, ok := m[a[i]]; ok {
					mi = MinInt(mi, i-v+1)
				}
				m[a[i]] = i
			}
			if mi == n+1 {
				mi = -1
			}
			fmt.Println(mi)
		}
	}
}

package main

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

// Educational Codeforces Round 76
// C. Dominated Subarray
// https://codeforces.com/contest/1257/problem/C

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

func main() {
	var T int
	fmt.Scan(&T)
	ans := make([]int, 0)
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	for i := 0; i < T; i++ {
		var N int
		fmt.Scan(&N)
		if N == 1 {
			_ = nextInt(sc)
			ans = append(ans, -1)
			continue
		}
		set := make(map[int][]int, 0)
		for j := 0; j < N; j++ {
			v := nextInt(sc)
			if set[v] == nil {
				set[v] = make([]int, 0)
			}
			set[v] = append(set[v], j)
		}
		minlen := int(1e9) + 1
		for _, v := range set {
			tlen := 0
			for j := 0; j+1 < len(v); j++ {
				tlen = v[j+1] - v[j] + 1
				minlen = min(minlen, tlen)
			}
		}
		ans = append(ans, minlen)
	}
	for i := range ans {
		fmt.Println(ans[i])
	}
}

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