package main

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

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

	var n, k int
	var s string
	// for Fscan(in, &T); T > 0; T-- {
	Fscan(in, &n, &k, &s)
	sz := 1
	for i := 0; i < n; i++ {
		if s[i] > s[i%sz] {
			break
		}
		if s[i] < s[i%sz] {
			sz = i + 1
		}
	}
	Fprintf(out, strings.Repeat(s[:sz], k/sz+1)[:k])
	// }
}

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

package main

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

var _rbuf = bufio.NewReader(os.Stdin)
var _wbuf = bufio.NewWriter(os.Stdout)

func scan(a ...interface{})             { _wbuf.Flush(); fmt.Fscan(_rbuf, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(_wbuf, f, a...) }

func main() {
	defer _wbuf.Flush()

	var n, k int
	var str string

	scan(&n, &k)
	scan(&str)

	for i := 1; i < n; i++ {
		if str[i:] > str || str[i:] == str[:n-i] {
			str = str[:i]
			break
		}
	}
	for len(str) < k {
		str += str
	}
	printf("%s\n", str[:k])
}

package main

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

const (
	MOD int = 1e9+7
	NL byte = byte('\n')
	SP byte = byte(' ')
)

var (
	in *bufio.Scanner
	out *bufio.Writer

	n, k int
	s string
	res int
)

func main() {
	initBuffer()
	var t int = 1
	// t = getInt()
	for i := 0; i < t; i++ {
		solve()
	}
	// test()
	out.Flush()
}

func solve() {
	initialize()
	input()
	count()
	output()
}

func initialize() {

}

func input() {
	n = getInt()
	k = getInt()
	s = getString()
}

func count() {
	var arr []int = make([]int, n)
	for i := 1; i < n; i++ {
		if arr[i] > arr[0] {
			res = i
			return
		}
		arr[i] = arr[i-1]
		for arr[i] > 0 && s[i] != s[arr[i]%(i-arr[i])] {
			if s[i] < s[arr[i]%(i-arr[i])] {
				arr[i] = arr[arr[i]-1]
			} else {
				break
			}
		}
		if s[i] == s[arr[i]%(i-arr[i])] {
			arr[i]++
		} else if s[i] > s[arr[i]%(i-arr[i])] {
			res = i - arr[i]
			return
		}
	}
	res = n - arr[n-1]
	for i := 0; i < n; i++ {
		if s[i%res] < s[(res+i)%n] {
			return
		}
	}
	res = n
}

func output() {
	for i := 0; i < k / res; i++ {
		out.WriteString(s[:res])
	}
	out.WriteString(s[:k%res])
}

func test() {
	fmt.Print()
}

func initBuffer() {
	in = bufio.NewScanner(os.Stdin)
	in.Buffer([]byte{}, 4 * 1024 * 1024)
	in.Split(scanWords)
	out = bufio.NewWriter(os.Stdout)
}

func scanWords(data []byte, atEOF bool) (advance int, token []byte, err error) {
	if atEOF && len(data) == 0 {
		return 0, nil, nil
	}
	i := bytes.IndexByte(data, '\n')
	j := bytes.IndexByte(data, ' ')
	ind := min(i, j)
	if ind < 0 {
		ind = max(i, j)
	}
	if ind >= 0 {
		return ind + 1, dropCR(data[0:ind]), nil
	}
	if atEOF {
		return len(data), dropCR(data), nil
	}
	return 0, nil, nil
}

func dropCR(data []byte) []byte {
	if len(data) > 0 && data[len(data)-1] == '\r' {
		return data[:len(data)-1]
	}
	return data
}

func getFloat() float64 {
	in.Scan()
	b := in.Bytes()
	var isNeg bool
	if b[0] == '-' {
		isNeg = true
		b = b[1:]
	}
	var res1 int64
	for i := 0; i < len(b); i++ {
		if b[i] == '.' {
			b = b[i+1:]
			break
		}
		res1 *= 10
		res1 += int64(b[i]) - 48
		if i == len(b) - 1 {
			b = b[:0]
		}
	}
	var res2 float64
	for i := len(b); i > 0; i-- {
		res2 += float64(b[i-1]) - 48
		res2 /= 10
	}
	if isNeg {
		res1 *= -1
		res2 *= -1
	}
	return float64(res1) + res2
}

func getInt() int {
	return int(getInt64())
}

func getInt64() int64 {
	in.Scan()
	b := in.Bytes()
	var isNeg bool
	if b[0] == '-' {
		isNeg = true
		b = b[1:]
	}
	var res int64
	for i := 0; i < len(b); i++ {
		res *= 10
		res += int64(b[i]) - 48
	}
	if isNeg {
		res *= -1
	}
	return res
}

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

func intToBytes(n int) []byte {
	return int64ToBytes(int64(n))
}

func int64ToBytes(n int64) []byte {
	var res [65]byte
	var isNeg bool
	if n < 0 {
		isNeg = true
		n = -n
	}
	var l int = 65
	for n > 0 {
		l--
		res[l] = byte(n % 10 + 48)
		n /= 10
	}
	if l == 65 {
		l--
		res[l] = byte(48)
	}
	if isNeg {
		l--
		res[l] = byte(45)
	}
	return res[l:]
}

func bsLower(arr []int, n int) int {
	var minN, midN, maxN int
	maxN = len(arr)
	for maxN - 1 > minN {
		midN = (minN + maxN) / 2
		if arr[midN] < n {
			minN = midN
		} else {
			maxN = midN
		}
	}
	if arr[minN] >= n {
		return minN
	}
	return maxN
}

func bsLower64(arr []int64, n int64) int {
	var minN, midN, maxN int
	maxN = len(arr)
	for maxN - 1 > minN {
		midN = (minN + maxN) / 2
		if arr[midN] < n {
			minN = midN
		} else {
			maxN = midN
		}
	}
	if arr[minN] >= n {
		return minN
	}
	return maxN
}

func bsUpper(arr []int, n int) int {
	var minN, midN, maxN int
	maxN = len(arr)
	for maxN - 1 > minN {
		midN = (minN + maxN) / 2
		if arr[midN] <= n {
			minN = midN
		} else {
			maxN = midN
		}
	}
	if arr[minN] > n {
		return minN
	}
	return maxN
}

func bsUpper64(arr []int64, n int64) int {
	var minN, midN, maxN int
	maxN = len(arr)
	for maxN - 1 > minN {
		midN = (minN + maxN) / 2
		if arr[midN] <= n {
			minN = midN
		} else {
			maxN = midN
		}
	}
	if arr[minN] > n {
		return minN
	}
	return maxN
}

func max(n1, n2 int) int {
	return int(max64(int64(n1), int64(n2)))
}

func max64(n1, n2 int64) int64 {
	if n1 > n2 {
		return n1
	}
	return n2
}

func min(n1, n2 int) int {
	return int(min64(int64(n1), int64(n2)))
}

func min64(n1, n2 int64) int64 {
	if n1 < n2 {
		return n1
	}
	return n2
}

func yes(res, isUpperCase bool) bool {
	var arr [4]string = [4]string{"No", "Yes", "NO", "YES"}
	var ind int
	if res == true {
		ind = 1
	}
	if isUpperCase == true {
		ind += 2
	}
	out.WriteString(arr[ind])
	out.WriteByte(NL)
	return res
}

package main

import (
	"fmt"
	"strings"
)

/*
https://codeforces.com/problemset/problem/1537/E1
https://codeforces.com/problemset/problem/1537/E2

本题分为简单版本和困难版本，在困难版本中，n 和 k 不超过 5e5。

输入 n (≤5000) 和 k (≤5000)，以及一个长为 n 的字符串 s，由小写字母组成。

有如下两种操作，每种操作你都可以执行任意多次：
1. 删除 s 的最后一个字符；
2. 将 s 复制一份拼在后面，即 s = s + s。

输出你能得到的所有长为 k 的字符串中，字典序最小的字符串。
 */
func samllstr(n int, str string, k int) string{
	start := 0
	end := 1
	for ; end < n ;{
		if str[end] > str[start] {
			break
		}else if str[end] < str[start] {
			end++
			continue
		}else {
			flag := 0
			i,j := start+1, end+1
			for  ;i < end && j < n; i, j = i+1, j+1 {
				if str[i] < str[j] {
					flag = 1
					break//end-1为止
				}
				if str[i] > str[j] {
					flag = 2
					break
				}
			}
			if flag == 1  {
				break
			}
			if flag == 2 {
				end = j+1
			}else {
				if i == end {
					end = j
				}else {
					break
				}
			}
		}
	}/*
	5 10
	dcdcd
	Output
	dcdcddcdcd
	Answer
	dcdcdcdcdc
	Checker Log
	*/
	length := end-start
	return strings.Repeat(str[start:end], k/length) + str[:k%length]
}
/*
输入
8 16
dbcadabc
输出
dbcadabcdbcadabc
解释 复制一次

输入
4 5
abcd
输出
aaaaa

 */
func main() {
	var n, k int
	var str string
	fmt.Scan(&n, &k, &str)
	fmt.Printf("%v\n", samllstr(n,str, k))
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := os.Stdout
	var n, k int
	fmt.Fscan(in,&n,&k)
	var s []byte
	fmt.Fscan(in,&s)
	delta := ""
	or := ""
	for i:=len(s)-1;i>=0;i--{
		now := string(s[:i+1])
		temp := now
		if delta==""{
			for len(now)<k{
				now = now+now
			}
			delta = now[:k]
			or = temp
		}else{
			for len(now)<len(delta){
				now = now + now
			}
			now = now[:len(delta)]
			flag := false
			for j:=0;j<len(delta);j++{
				if now[j]==delta[j]{
					continue
				}
				if now[j]>delta[j]{
					flag = false
					break
				}
				if now[j]<delta[j]{
					flag = true
					break
				}
			}
			if flag{
				delta = now
				or = temp
			}
		}
	}
	for len(or)<k{
		or = or + or
	}
	fmt.Fprintln(out,or[:k])
}

package main

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

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

	var n, k int
	fmt.Fscan(r, &n, &k)

	var s string
	fmt.Fscan(r, &s)

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

func solve(n, k int, s string) string {

	//cafsafhat

	//baaaaa
	//14

	//xkkhaxkkhaggkgkgxkkhaxkkha

	//xkkhaxkkhaggkgkgxkkhaxkkha
	//xkkhaxkkhaggkgkgxkkhaxkkhaggkgkg

	//8 + 8
	//20

	var i int
	var found bool

	for i = 1; i < n; i++ {
		if s[0] > s[i] {
			continue

		} else if s[0] == s[i] {
			s1 := s[:i]
			lenS1 := len(s1)

			rem := n - i

			var s2 string

			if lenS1 <= rem {
				s2 = s[i: i+lenS1]

				if s1 < s2 {
					found = true
					break

				} else {
					continue
				}

			} else {
				s1 := s[: rem]

				s2 := s[i: i+rem]

				if s1 <= s2 {
					found = true
					break
				} else {
					continue
				}
			}

		} else {
			found = true
			break
		}
	}

	var temp string

	if !found {
		temp = s
	} else {
		temp = s[0: i]
	}

	t := len(temp)

	d := k/t
	r := k % t

	var ans bytes.Buffer

	if r != 0 {
		for i := 0; i < d; i++ {
			ans.WriteString(temp)
		}

		for i := 0; i < r; i++ {
			ans.WriteByte(temp[i])
		}
	} else {
		for i := 0; i < d; i++ {
			ans.WriteString(temp)
		}
	}

	return ans.String()

}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1537E2(in io.Reader, out io.Writer) {
	var n, k int
	var s string
	Fscan(bufio.NewReader(in), &n, &k, &s)
	sz := 1
	for i := range s {
		if s[i] > s[i%sz] {
			break
		}
		if s[i] < s[i%sz] {
			sz = i + 1
		}
	}
	Fprint(out, strings.Repeat(s[:sz], k/sz+1)[:k])
}

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

package main

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

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

func cf(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var n int
	fmt.Fscan(in, &n)

	fmt.Fprintln(out, n)
}

func cf1537E1(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var n, k int
	var s string
	fmt.Fscan(in, &n, &k, &s)
	ss := []string{}
	for i := range s {
		ss = append(ss, strings.Repeat(s[:i+1], (k+i)/(i+1)))
	}
	sort.Strings(ss)

	fmt.Fprintln(out, ss[0][:k])
}

func cf691C(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var x string
	fmt.Fscan(in, &x)
	if !strings.Contains(x, ".") {
		x += "."
	}
	x = strings.Trim(x, "0")
	idx := strings.Index(x, ".")
	e := idx - strings.IndexAny(x, "123456789")
	if e > 0 {
		e--
	}
	x = strings.Trim(x[:idx]+x[idx+1:], "0")
	if len(x) > 1 {
		x = x[:1] + "." + x[1:]
	}
	fmt.Fprint(out, x)
	if e != 0 {
		fmt.Fprint(out, "E", e)
	}
}

func cf467C(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var n, m, k int
	fmt.Fscan(in, &n, &m, &k)
	sums := make([]int64, n+1)
	for i := 1; i <= n; i++ {
		fmt.Fscan(in, &sums[i])
		sums[i] += sums[i-1]
	}
	f := make([]int64, n+1)
	for j := 1; j <= k; j++ {
		ff := make([]int64, n+1)
		for i := j * m; i <= n; i++ {
			ff[i] = max64(ff[i-1], sums[i]-sums[i-m]+f[i-m])
		}
		f = ff
	}

	fmt.Fprintln(out, f[n])
}

func cf1469C(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
next:
	for ; t > 0; t-- {
		var n, k int
		fmt.Fscan(in, &n, &k)
		h := make([]int, n)
		for i := range h {
			fmt.Fscan(in, &h[i])
		}
		pre := []int{h[0], h[0]}
		for i := 1; i < n; i++ {
			cur := []int{max(pre[0]-k+1, h[i]), min(pre[1]+k-1, h[i]+k-1)}
			if cur[0] > cur[1] {
				fmt.Fprintln(out, "NO")
				continue next
			}
			pre = cur
		}
		if pre[0] == h[n-1] {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

func cf1060C(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var n, m, x int
	fmt.Fscan(in, &n, &m)
	nums1, nums2 := make([]int64, n+1), make([]int64, m+1)
	for i := 1; i <= n; i++ {
		fmt.Fscan(in, &nums1[i])
		nums1[i] += nums1[i-1]
	}
	for i := 1; i <= m; i++ {
		fmt.Fscan(in, &nums2[i])
		nums2[i] += nums2[i-1]
	}
	fmt.Fscan(in, &x)
	mins1, mins2 := make([]int64, n+1), make([]int64, m+1)
	for i := 1; i <= n; i++ {
		mins1[i] = nums1[i]
		for j := i + 1; j <= n; j++ {
			mins1[i] = min64(mins1[i], nums1[j]-nums1[j-i])
		}
	}
	for i := 1; i <= m; i++ {
		mins2[i] = nums2[i]
		for j := i + 1; j <= m; j++ {
			mins2[i] = min64(mins2[i], nums2[j]-nums2[j-i])
		}
	}
	ans := 0
	for i := 1; i <= n; i++ {
		for j := 1; j <= m; j++ {
			if mins1[i]*mins2[j] <= int64(x) && i*j > ans {
				ans = i * j
			}
		}
	}

	fmt.Fprintln(out, ans)
}

func cf1043D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var n, m int
	fmt.Fscan(in, &n, &m)
	arr, preArr := make([]int, n+1), make([]int, n+1)
	var pre, cur int
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &cur)
		preArr[cur] = pre
		arr[pre], pre = cur, cur
	}
	for ; m > 1; m-- {
		pre = 0
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &cur)
			if arr[pre] != cur {
				preArr[cur] = 0
				arr[pre] = 0
			}
			pre = cur
		}
		arr[cur] = 0
	}
	ans := int64(n)
	for i := 1; i <= n; i++ {
		if arr[i] > 0 {
			cnt, id := 0, i
			for preArr[id] != 0 {
				id = preArr[id]
			}
			for arr[id] != 0 {
				arr[id], id = 0, arr[id]
				cnt++
				ans += int64(cnt)
			}
		}
	}

	fmt.Fprintln(out, ans)
}

func cf1519D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var n int
	fmt.Fscan(in, &n)
	nums1, nums2 := make([]int64, n), make([]int64, n)
	for i := range nums1 {
		fmt.Fscan(in, &nums1[i])
	}
	for i := range nums2 {
		fmt.Fscan(in, &nums2[i])
	}
	f := make([]int64, n+1)
	for i := range nums1 {
		f[i+1] = f[i] + nums1[i]*nums2[i]
	}
	ans := f[n]
	for i := range nums1 {
		for j, d, sum := i, 1, nums1[i]*nums2[i]; j-d >= 0 && j+d < n; d++ {
			sum += nums1[j-d]*nums2[j+d] + nums1[j+d]*nums2[j-d]
			ans = max64(ans, sum+f[n]-f[j+d+1]+f[j-d])
		}
		for j1, j2, d, sum := i, i+1, 0, int64(0); j1-d >= 0 && j2+d < n; d++ {
			sum += nums1[j2+d]*nums2[j1-d] + nums1[j1-d]*nums2[j2+d]
			ans = max64(ans, sum+f[n]-f[j2+d+1]+f[j1-d])
		}
	}

	fmt.Fprintln(out, ans)
}

func cf175C(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var n int
	fmt.Fscan(in, &n)
	nums := make([][2]int64, n)
	for i := range nums {
		fmt.Fscan(in, &nums[i][0], &nums[i][1])
	}
	var t int
	fmt.Fscan(in, &t)
	p := make([]int64, t+2)
	p[t+1] = 1e12
	pre := int64(0)
	for i := 1; i <= t; i++ {
		fmt.Fscan(in, &p[i])
		pre, p[i] = p[i], p[i]-pre
	}
	sort.Slice(nums, func(i, j int) bool {
		return nums[i][1] < nums[j][1]
	})
	sum := int64(0)
	for i, j := 0, 1; i < len(nums); {
		sum += min64(nums[i][0], p[j]) * nums[i][1] * int64(j)
		if nums[i][0] > p[j] {
			nums[i][0] -= p[j]
			j++
		} else {
			p[j] -= nums[i][0]
			i++
		}
	}

	fmt.Fprintln(out, sum)
}

func cf494A(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var s string
	fmt.Fscan(in, &s)
	sum, minSum, cnt := 0, len(s), 0
	for i := range s {
		if s[i] == '(' {
			sum++
		} else if s[i] == ')' {
			sum--
		} else {
			sum--
			minSum = sum
			cnt++
		}
		if sum < 0 {
			fmt.Fprintln(out, -1)
			return
		}
		minSum = min(minSum, sum)
	}
	if minSum < sum {
		fmt.Fprintln(out, -1)
	} else {
		fmt.Fprintln(out, strings.Repeat("1\n", cnt-1), sum+1)
	}
}

func cf1281B(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var n int
	fmt.Fscan(in, &n)
	for ; n > 0; n-- {
		var s, c string
		fmt.Fscan(in, &s, &c)
		cs := []byte(s)
		j, a, b := len(s)-1, 0, 0
		for i := len(s) - 1; i >= 0; i-- {
			if cs[i] > cs[j] {
				a, b = i, j
			} else if cs[i] < cs[j] {
				j = i
			}
		}
		cs[a], cs[b] = cs[b], cs[a]
		if s = string(cs); s < c {
			fmt.Fprintln(out, s)
		} else {
			fmt.Fprintln(out, "---")
		}
	}
}

func cf605A(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var n int
	fmt.Fscan(in, &n)
	nums := make([][3]int, n) // nums[0]原数字  nums[1]原下标	nums[2]排序后的下标
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &nums[i][0])
		nums[i][1] = i
	}
	sort.SliceStable(nums, func(i, j int) bool {
		return nums[i][0] < nums[j][0]
	})
	for i := range nums {
		nums[i][2] = i
	}
	sort.Slice(nums, func(i, j int) bool {
		return nums[i][1] < nums[j][1]
	})
	f := make([]int, n+1)
	for _, num := range nums {
		f[num[2]+1] = f[num[2]] + 1
	}

	fmt.Fprintln(out, n-max(f...))
}

func cf297B(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var n, m, k int
	fmt.Fscan(in, &n, &m, &k)
	if n > m {
		fmt.Fprintln(out, "YES")
		return
	}
	a, b := make([]int, n), make([]int, m)
	for i := range a {
		fmt.Fscan(in, &a[i])
	}
	for i := range b {
		fmt.Fscan(in, &b[i])
	}
	sort.Sort(sort.Reverse(sort.IntSlice(a)))
	sort.Sort(sort.Reverse(sort.IntSlice(b)))
	for i := range a {
		if a[i] > b[i] {
			fmt.Fprintln(out, "YES")
			return
		}
	}

	fmt.Fprintln(out, "NO")
}

func helan(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var n int
	fmt.Fscan(in, &n)
	nums := make([]int, n)
	for i := range nums {
		fmt.Fscan(in, &nums[i])
	}

	less, more := -1, n
	for i := 0; i < more; i++ {
		if nums[i] == 0 {
			less++
			nums[i], nums[less] = nums[less], nums[i]
		} else if nums[i] == 2 {
			more--
			nums[i], nums[more] = nums[more], nums[i]
			i--
		}
	}

	fmt.Fprintln(out, nums)
}

func cf1203D2(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var s, t string
	fmt.Fscan(in, &s, &t)
	ans := 0
	left, right := make([]int, len(t)), make([]int, len(t))
	i, j := 0, 0
	for ; i < len(s) && j < len(t); i++ {
		if s[i] == t[j] {
			left[j] = i
			j++
		}
	}
	ans = max(ans, len(s)-i)
	i, j = len(s)-1, len(t)-1
	for ; i >= 0 && j >= 0; i-- {
		if s[i] == t[j] {
			right[j] = i
			j--
		}
	}
	ans = max(ans, i+1)
	for k := 0; k < len(t)-1; k++ {
		ans = max(ans, right[k+1]-left[k]-1)
	}

	fmt.Fprintln(out, ans)

}

func cf1196D2(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var t, n, k int
	var s string
	fmt.Fscan(in, &t)
	pat := "RGB"
	for ; t > 0; t-- {
		fmt.Fscan(in, &n, &k, &s)
		ans := k
		for i := 0; i < 3; i++ {
			diff := 0
			for j := range s[:k] {
				if s[j] != pat[(i+j)%3] {
					diff++
				}
			}
			ans = min(ans, diff)
			for j := k; j < n; j++ {
				if s[j] != pat[(i+j)%3] {
					diff++
				}
				if s[j-k] != pat[(i+j-k%3)%3] {
					diff--
				}
				ans = min(ans, diff)
			}
		}

		fmt.Fprintln(out, ans)
	}
}

func cf44H(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var s string
	fmt.Fscan(in, &s)
	f := make([][10]int64, len(s))
	for i := range f[0] {
		f[0][i] = 1
	}
	for i := 1; i < len(s); i++ {
		for j := range f[i] {
			f[i][(j+int(s[i])-'0')/2] += f[i-1][j]
			if (j+int(s[i])-'0')&1 == 1 {
				f[i][(j+int(s[i])-'0'+1)/2] += f[i-1][j]
			}
		}
	}
	flag := true
	for i := range s[:len(s)-1] {
		if s[i]-s[i+1] <= 1 || s[i+1]-s[i] <= 1 {
			continue
		} else {
			flag = false
			break
		}
	}
	ans := int64(0)
	for i := range f[0] {
		ans += f[len(s)-1][i]
	}
	if flag {
		ans--
	}
	fmt.Fprintln(out, ans)
}

func cf1051D(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, k int
	fmt.Fscan(in, &n, &k)
	if k == 1 {
		fmt.Fprintln(out, 2)
		return
	}
	mod := int64(998244353)
	f := make([][2]int64, k+1)
	f[1][1], f[2][0] = 2, 2
	for i := 2; i <= n; i++ {
		for j := min(k, 2*i); j >= 2; j-- {
			f[j][1] = (f[j][0]*2 + f[j][1] + f[j-1][1]) % mod
			f[j][0] = (f[j][0] + f[j-2][0] + f[j-1][1]*2) % mod
		}
	}

	fmt.Fprintln(out, (f[k][0]+f[k][1])%mod)
}

func cf1051D3(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, k int
	fmt.Fscan(in, &n, &k)
	if k == 1 {
		fmt.Fprintln(out, 2)
		return
	}
	mod := int64(998244353)
	f := make([][][2]int64, n+1)
	for i := range f {
		f[i] = make([][2]int64, k+1)
	}
	f[1][1][1], f[1][2][0] = 2, 2
	for i := 2; i <= n; i++ {
		f[i][1][1] = 2
		for j := 2; j <= k && j <= 2*i; j++ {
			f[i][j][0] = (f[i-1][j][0] + f[i-1][j-2][0] + f[i-1][j-1][1]*2) % mod
			f[i][j][1] = (f[i-1][j][0]*2 + f[i-1][j][1] + f[i-1][j-1][1]) % mod
		}
	}

	fmt.Fprintln(out, (f[n][k][0]+f[n][k][1])%mod)
}

func cf468A(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n int
	fmt.Fscan(in, &n)
	if n < 4 {
		fmt.Fprintln(out, "NO")
		return
	}
	fmt.Fprintln(out, "YES")
	if (n-4)&1 == 0 {
		fmt.Fprintln(out, "1 * 2 = 2")
		fmt.Fprintln(out, "2 * 3 = 6")
		fmt.Fprintln(out, "6 * 4 = 24")
	} else {
		fmt.Fprintln(out, "2 * 4 = 8")
		fmt.Fprintln(out, "3 * 5 = 15")
		fmt.Fprintln(out, "8 + 15 = 23")
		fmt.Fprintln(out, "23 + 1 = 24")
	}
	for i := 5 + n&1; i < n; i += 2 {
		fmt.Fprintln(out, i+1, "-", i, "= 1")
		fmt.Fprintln(out, "1 * 24 = 24")
	}
}

func cf573B(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, ans int
	fmt.Fscan(in, &n)
	arr := make([]int, n+2)
	for i := 1; i <= n; i++ {
		fmt.Fscan(in, &arr[i])
		arr[i] = min(arr[i], arr[i-1]+1)
	}
	for i := n; i >= 1; i-- {
		arr[i] = min(arr[i], arr[i+1]+1)
		ans = max(ans, arr[i])
	}

	fmt.Fprintln(out, ans)
}

func min(nums ...int) int {
	ans := nums[0]
	for _, num := range nums {
		if ans > num {
			ans = num
		}
	}
	return ans
}

func min64(nums ...int64) int64 {
	ans := nums[0]
	for _, num := range nums {
		if ans > num {
			ans = num
		}
	}
	return ans
}

func max(nums ...int) int {
	ans := nums[0]
	for _, num := range nums {
		if ans < num {
			ans = num
		}
	}
	return ans
}

func max64(nums ...int64) int64 {
	ans := nums[0]
	for _, num := range nums {
		if ans < num {
			ans = num
		}
	}
	return ans
}

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

package main

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

var (
	in   *bufio.Reader = bufio.NewReader(os.Stdin)
	n, k int
	s    string
)

func main() {
	fmt.Fscan(in, &n, &k, &s)
	sz := 1
	for i := range s {
		if s[i] > s[i%sz] {
			break
		}
		if s[i] < s[i%sz] {
			sz = i + 1
		}
	}
	ans := strings.Repeat(s[:sz], k/sz+1)[:k]
	fmt.Println(ans)
}

package main

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

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

	var n, k int
	var s string
	Fscan(in, &n, &k, &s)
	ans := ""
	for i := 1; i <= n; i++ {
		time := (k - 1) / i + 1
		t := strings.Repeat(s[:i], time)
		if len(t) > k {
			t = t[:k]
		}
		if ans == "" || t < ans {
			ans = t
		}
	}
	Fprintln(out, ans)
}

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

package main

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

type IO struct {
	in  io.Reader
	out io.Writer
}

func NewIO(in io.Reader, out io.Writer) *IO {
	return &IO{in: bufio.NewReader(in), out: out}
}

func (io *IO) ReadInt() int {
	var res int
	fmt.Fscanf(io.in, "%d\n", &res)
	return res
}

func (io *IO) ReadPairInt() (int, int) {
	var a, b int
	fmt.Fscanf(io.in, "%d %d\n", &a, &b)
	return a, b
}

func (io *IO) ReadString() string {
	var res string
	fmt.Fscanf(io.in, "%s\n", &res)
	return res
}

func (io *IO) ReadIntSlice(n int) sort.IntSlice {
	var t int
	res := make(sort.IntSlice, 0, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(io.in, "%d", &t)
		res = append(res, t)
	}
	fmt.Fscanf(io.in, "\n")
	return res
}

func (io *IO) ReadByteSlice(n int) []byte {
	res := make([]byte, 0, n)
	fmt.Fscanf(io.in, "%s\n", &res)
	return res
}

func (io *IO) Println(v ...interface{}) {
	fmt.Fprintln(io.out, v...)
}

func (io *IO) Print(v ...interface{}) {
	fmt.Fprint(io.out, v...)
}

func (io *IO) Printf(format string, v ...interface{}) {
	fmt.Fprintf(io.out, format, v...)
}

func (io *IO) PrintIntSlice(s sort.IntSlice) {
	for _, v := range s {
		io.Printf("%d ", v)
	}
}

func (io *IO) PrintlnIntSlice(s sort.IntSlice) {
	io.PrintIntSlice(s)
	io.Printf("\n")
}

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
}

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

func zFunction(a []byte) []int {
	res := make([]int, len(a))
	l, r := 0, 0

	for i := 1; i < len(a); i++ {
		if i <= r {
			res[i] = min(r-i+1, res[i-l])
		}
		for i+res[i] < len(a) && a[res[i]] == a[i+res[i]] {
			res[i]++
		}
		if i+res[i]-1 > r {
			l = i
			r = i + res[i] - 1
		}
	}

	return res
}

func concatBytes (a ...[]byte) []byte {
	size := 0
	for _, v := range a {
		size += len(v)
	}

	res := make([]byte, size)

	size = 0
	for _, v := range a {
		size += copy(res[size:], v)
	}

	return res
}

//func findMinString(a, b []byte, mn int) []byte {
//	uh := concatBytes(b, []byte{'|'}, a, b)
//	z := zFunction(uh)
//	size := len(a)
//
//	for i := mn; i < size; i++ {
//		if a[i] > b[0] {
//			return concatBytes(a[:i], b[:i])
//		} else if a[i] < b[0] {
//			continue
//		} else {
//			k := z[size + 1 + i]
//			if k >= i - 2 {
//				return concatBytes(a[:i], b[:i])
//			} else if uh[size + 1 + i + k] < uh[k] {
//				return concatBytes(a, b)
//			} else if uh[size + 1 + i + k] > uh[k] {
//				return concatBytes(a[:i], b[:i])
//			}
//		}
//	}
//
//	return concatBytes(a, b)
//}

func getAns(a []byte, k int) []byte {
	mp := k / len(a)
	if k % len(a) != 0 {
		mp++
	}
	res := make([]byte, len(a) * mp)
	for i := 0; i < mp; i++ {
		copy(res[i * len(a):], a)
	}
	return res[:k]
}

func main() {
	io := NewIO(os.Stdin, os.Stdout)

	n, k := io.ReadPairInt()
	a := io.ReadByteSlice(n)

	uh := concatBytes(a, []byte{'|'}, a, a)
	z := zFunction(uh)

	for i := 1; i < len(a); i++ {
		//fmt.Println(i)
		if a[i] > a[0] {
			a = a[:i]
			break
		} else if a[i] < a[0] {
			continue
		} else {
			zz := z[len(a) + 1 + i]
			if zz == len(a) {
				break
			}
			if a[zz] < uh[len(a) + 1 + i + zz] {
				a = a[:i]
				break
			} else {
				continue
			}
		}
	}
	a = getAns(a, k)

	io.Println(string(a))
}

package main

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

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

func CF1537E(in io.Reader, out io.Writer) {
	r := bufio.NewReader(in)
	w := bufio.NewWriter(out)
	defer w.Flush()

	var n, k int
	var s string
	fmt.Fscan(r, &n, &k, &s)
	sz := 1
	for i := range s {
		if s[i] > s[i%sz] {
			break
		}
		if s[i] < s[i%sz] {
			sz = i + 1
		}
	}
	fmt.Fprint(w, strings.Repeat(s[:sz], k/sz+1)[:k])
}

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

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

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

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

package main

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

type IO struct {
	in  io.Reader
	out io.Writer
}

func NewIO(in io.Reader, out io.Writer) *IO {
	return &IO{in: bufio.NewReader(in), out: out}
}

func (io *IO) ReadInt() int {
	var res int
	fmt.Fscanf(io.in, "%d\n", &res)
	return res
}

func (io *IO) ReadPairInt() (int, int) {
	var a, b int
	fmt.Fscanf(io.in, "%d %d\n", &a, &b)
	return a, b
}

func (io *IO) ReadString() string {
	var res string
	fmt.Fscanf(io.in, "%s\n", &res)
	return res
}

func (io *IO) ReadIntSlice(n int) sort.IntSlice {
	var t int
	res := make(sort.IntSlice, 0, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(io.in, "%d", &t)
		res = append(res, t)
	}
	fmt.Fscanf(io.in, "\n")
	return res
}

func (io *IO) ReadByteSlice(n int) []byte {
	res := make([]byte, 0, n)
	fmt.Fscanf(io.in, "%s\n", &res)
	return res
}

func (io *IO) Println(v ...interface{}) {
	fmt.Fprintln(io.out, v...)
}

func (io *IO) Print(v ...interface{}) {
	fmt.Fprint(io.out, v...)
}

func (io *IO) Printf(format string, v ...interface{}) {
	fmt.Fprintf(io.out, format, v...)
}

func (io *IO) PrintIntSlice(s sort.IntSlice) {
	for _, v := range s {
		io.Printf("%d ", v)
	}
}

func (io *IO) PrintlnIntSlice(s sort.IntSlice) {
	io.PrintIntSlice(s)
	io.Printf("\n")
}

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
}

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

func zFunction(a []byte) []int {
	res := make([]int, len(a))
	l, r := 0, 0

	for i := 1; i < len(a); i++ {
		if i <= r {
			res[i] = min(r-i+1, res[i-l])
		}
		for i+res[i] < len(a) && a[res[i]] == a[i+res[i]] {
			res[i]++
		}
		if i+res[i]-1 > r {
			l = i
			r = i + res[i] - 1
		}
	}

	return res
}

func concatBytes (a ...[]byte) []byte {
	size := 0
	for _, v := range a {
		size += len(v)
	}

	res := make([]byte, size)

	size = 0
	for _, v := range a {
		size += copy(res[size:], v)
	}

	return res
}

//func findMinString(a, b []byte, mn int) []byte {
//	uh := concatBytes(b, []byte{'|'}, a, b)
//	z := zFunction(uh)
//	size := len(a)
//
//	for i := mn; i < size; i++ {
//		if a[i] > b[0] {
//			return concatBytes(a[:i], b[:i])
//		} else if a[i] < b[0] {
//			continue
//		} else {
//			k := z[size + 1 + i]
//			if k >= i - 2 {
//				return concatBytes(a[:i], b[:i])
//			} else if uh[size + 1 + i + k] < uh[k] {
//				return concatBytes(a, b)
//			} else if uh[size + 1 + i + k] > uh[k] {
//				return concatBytes(a[:i], b[:i])
//			}
//		}
//	}
//
//	return concatBytes(a, b)
//}

func getAns(a []byte, k int) []byte {
	mp := k / len(a)
	if k % len(a) != 0 {
		mp++
	}
	res := make([]byte, len(a) * mp)
	for i := 0; i < mp; i++ {
		copy(res[i * len(a):], a)
	}
	return res[:k]
}

func main() {
	io := NewIO(os.Stdin, os.Stdout)

	n, k := io.ReadPairInt()
	a := io.ReadByteSlice(n)

	uh := concatBytes(a, []byte{'|'}, a, a)
	z := zFunction(uh)

	for i := 1; i < len(a); i++ {
		//fmt.Println(i)
		if a[i] > a[0] {
			a = a[:i]
			break
		} else if a[i] < a[0] {
			continue
		} else {
			zz := z[len(a) + 1 + i]
			i = max(i, zz - 1)
			continue
		}
	}

	a = getAns(a, k)

	io.Println(string(a))
}

package main

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

type IO struct {
	in  io.Reader
	out io.Writer
}

func NewIO(in io.Reader, out io.Writer) *IO {
	return &IO{in: bufio.NewReader(in), out: out}
}

func (io *IO) ReadInt() int {
	var res int
	fmt.Fscanf(io.in, "%d\n", &res)
	return res
}

func (io *IO) ReadPairInt() (int, int) {
	var a, b int
	fmt.Fscanf(io.in, "%d %d\n", &a, &b)
	return a, b
}

func (io *IO) ReadString() string {
	var res string
	fmt.Fscanf(io.in, "%s\n", &res)
	return res
}

func (io *IO) ReadIntSlice(n int) sort.IntSlice {
	var t int
	res := make(sort.IntSlice, 0, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(io.in, "%d", &t)
		res = append(res, t)
	}
	fmt.Fscanf(io.in, "\n")
	return res
}

func (io *IO) ReadByteSlice(n int) []byte {
	res := make([]byte, 0, n)
	fmt.Fscanf(io.in, "%s\n", &res)
	return res
}

func (io *IO) Println(v ...interface{}) {
	fmt.Fprintln(io.out, v...)
}

func (io *IO) Print(v ...interface{}) {
	fmt.Fprint(io.out, v...)
}

func (io *IO) Printf(format string, v ...interface{}) {
	fmt.Fprintf(io.out, format, v...)
}

func (io *IO) PrintIntSlice(s sort.IntSlice) {
	for _, v := range s {
		io.Printf("%d ", v)
	}
}

func (io *IO) PrintlnIntSlice(s sort.IntSlice) {
	io.PrintIntSlice(s)
	io.Printf("\n")
}

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
}

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

func zFunction(a []byte) []int {
	res := make([]int, len(a))
	l, r := 0, 0

	for i := 1; i < len(a); i++ {
		if i <= r {
			res[i] = min(r-i+1, res[i-l])
		}
		for i+res[i] < len(a) && a[res[i]] == a[i+res[i]] {
			res[i]++
		}
		if i+res[i]-1 > r {
			l = i
			r = i + res[i] - 1
		}
	}

	return res
}

func concatBytes (a ...[]byte) []byte {
	size := 0
	for _, v := range a {
		size += len(v)
	}

	res := make([]byte, size)

	size = 0
	for _, v := range a {
		size += copy(res[size:], v)
	}

	return res
}

//func findMinString(a, b []byte, mn int) []byte {
//	uh := concatBytes(b, []byte{'|'}, a, b)
//	z := zFunction(uh)
//	size := len(a)
//
//	for i := mn; i < size; i++ {
//		if a[i] > b[0] {
//			return concatBytes(a[:i], b[:i])
//		} else if a[i] < b[0] {
//			continue
//		} else {
//			k := z[size + 1 + i]
//			if k >= i - 2 {
//				return concatBytes(a[:i], b[:i])
//			} else if uh[size + 1 + i + k] < uh[k] {
//				return concatBytes(a, b)
//			} else if uh[size + 1 + i + k] > uh[k] {
//				return concatBytes(a[:i], b[:i])
//			}
//		}
//	}
//
//	return concatBytes(a, b)
//}

func getAns(a []byte, k int) []byte {
	mp := k / len(a)
	if k % len(a) != 0 {
		mp++
	}
	res := make([]byte, len(a) * mp)
	for i := 0; i < mp; i++ {
		copy(res[i * len(a):], a)
	}
	return res[:k]
}

func main() {
	io := NewIO(os.Stdin, os.Stdout)

	n, k := io.ReadPairInt()
	a := io.ReadByteSlice(n)

	uh := concatBytes(a, []byte{'|'}, a, a)
	z := zFunction(uh)

	for i := 1; i < len(a); i++ {
		//fmt.Println(i)
		if a[i] > a[0] {
			a = a[:i]
			break
		} else if a[i] < a[0] {
			continue
		} else {
			zz := z[len(a) + 1 + i]
			i = max(i, zz)
			continue
		}
	}

	a = getAns(a, k)

	io.Println(string(a))
}

package main

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

type IO struct {
	in  io.Reader
	out io.Writer
}

func NewIO(in io.Reader, out io.Writer) *IO {
	return &IO{in: bufio.NewReader(in), out: out}
}

func (io *IO) ReadInt() int {
	var res int
	fmt.Fscanf(io.in, "%d\n", &res)
	return res
}

func (io *IO) ReadPairInt() (int, int) {
	var a, b int
	fmt.Fscanf(io.in, "%d %d\n", &a, &b)
	return a, b
}

func (io *IO) ReadString() string {
	var res string
	fmt.Fscanf(io.in, "%s\n", &res)
	return res
}

func (io *IO) ReadIntSlice(n int) sort.IntSlice {
	var t int
	res := make(sort.IntSlice, 0, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(io.in, "%d", &t)
		res = append(res, t)
	}
	fmt.Fscanf(io.in, "\n")
	return res
}

func (io *IO) ReadByteSlice(n int) []byte {
	res := make([]byte, 0, n)
	fmt.Fscanf(io.in, "%s\n", &res)
	return res
}

func (io *IO) Println(v ...interface{}) {
	fmt.Fprintln(io.out, v...)
}

func (io *IO) Print(v ...interface{}) {
	fmt.Fprint(io.out, v...)
}

func (io *IO) Printf(format string, v ...interface{}) {
	fmt.Fprintf(io.out, format, v...)
}

func (io *IO) PrintIntSlice(s sort.IntSlice) {
	for _, v := range s {
		io.Printf("%d ", v)
	}
}

func (io *IO) PrintlnIntSlice(s sort.IntSlice) {
	io.PrintIntSlice(s)
	io.Printf("\n")
}

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
}

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

func zFunction(a []byte) []int {
	res := make([]int, len(a))
	l, r := 0, 0

	for i := 1; i < len(a); i++ {
		if i <= r {
			res[i] = min(r-i+1, res[i-l])
		}
		for i+res[i] < len(a) && a[res[i]] == a[i+res[i]] {
			res[i]++
		}
		if i+res[i]-1 > r {
			l = i
			r = i + res[i] - 1
		}
	}

	return res
}

func concatBytes (a ...[]byte) []byte {
	size := 0
	for _, v := range a {
		size += len(v)
	}

	res := make([]byte, size)

	size = 0
	for _, v := range a {
		size += copy(res[size:], v)
	}

	return res
}

func findMinString(a, b []byte, mn int) []byte {
	uh := concatBytes(b, []byte{'|'}, a, b)
	z := zFunction(uh)
	size := len(a)

	for i := mn; i < size; i++ {
		if a[i] > b[0] {
			return concatBytes(a[:i], b[:i])
		} else if a[i] < b[0] {
			continue
		} else {
			k := z[size + 1 + i]
			if k >= i - 2 {
				return concatBytes(a[:i], b[:i])
			} else if uh[size + 1 + i + k] < uh[k] {
				return concatBytes(a, b)
			} else if uh[size + 1 + i + k] > uh[k] {
				return concatBytes(a[:i], b[:i])
			}
		}
	}

	return concatBytes(a, b)
}

func main() {
	io := NewIO(os.Stdin, os.Stdout)

	n, k := io.ReadPairInt()
	a := io.ReadByteSlice(n)
	a = findMinString(a, a, 1)

	for {
		//io.Println(string(a))
		a = findMinString(a, a, len(a) / 2 + 1)
		if len(a) >= k {
			a = a[:k]
			break
		}
	}

	io.Println(string(a))
}

package main

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

type IO struct {
	in  io.Reader
	out io.Writer
}

func NewIO(in io.Reader, out io.Writer) *IO {
	return &IO{in: bufio.NewReader(in), out: out}
}

func (io *IO) ReadInt() int {
	var res int
	fmt.Fscanf(io.in, "%d\n", &res)
	return res
}

func (io *IO) ReadPairInt() (int, int) {
	var a, b int
	fmt.Fscanf(io.in, "%d %d\n", &a, &b)
	return a, b
}

func (io *IO) ReadString() string {
	var res string
	fmt.Fscanf(io.in, "%s\n", &res)
	return res
}

func (io *IO) ReadIntSlice(n int) sort.IntSlice {
	var t int
	res := make(sort.IntSlice, 0, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(io.in, "%d", &t)
		res = append(res, t)
	}
	fmt.Fscanf(io.in, "\n")
	return res
}

func (io *IO) ReadByteSlice(n int) []byte {
	res := make([]byte, 0, n)
	fmt.Fscanf(io.in, "%s\n", &res)
	return res
}

func (io *IO) Println(v ...interface{}) {
	fmt.Fprintln(io.out, v...)
}

func (io *IO) Print(v ...interface{}) {
	fmt.Fprint(io.out, v...)
}

func (io *IO) Printf(format string, v ...interface{}) {
	fmt.Fprintf(io.out, format, v...)
}

func (io *IO) PrintIntSlice(s sort.IntSlice) {
	for _, v := range s {
		io.Printf("%d ", v)
	}
}

func (io *IO) PrintlnIntSlice(s sort.IntSlice) {
	io.PrintIntSlice(s)
	io.Printf("\n")
}

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
}

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

func multiplyByte(b byte, k int) []byte {
	a := make([]byte, 0, k)
	for i := 0; i < k; i++ {
		a = append(a, b)
	}
	return a
}

func main() {
	io := NewIO(os.Stdin, os.Stdout)

	n, k := io.ReadPairInt()
	a := io.ReadByteSlice(n)

	for {
		//io.Println(string(a))

		size := len(a)
		if size == 1 {
			a = multiplyByte(a[0], k)
			break
		}

		if a[size-1] > a[0] {
			a = a[:size-1]
			continue
		}

		if a[size-1] < a[0] {
			if size >= k {
				a = a[:k]
				break
			}
			a = append(a, a...)
			continue
		}

		indexOfDiff := 1
		for ; indexOfDiff < size; indexOfDiff++ {
			if a[indexOfDiff] != a[0] {
				break
			}
		}

		if indexOfDiff == size {
			a = multiplyByte(a[0], k)
			break
		}

		if a[indexOfDiff] < a[0] {
			a = a[:size-1]
			continue
		}

		if a[indexOfDiff] > a[0] {
			if size >= k {
				a = a[:k]
				break
			}
			a = append(a, a...)
			continue
		}
	}

	io.Println(string(a))
}

package main

import (
	"fmt"
	"strings"
)

/*
https://codeforces.com/problemset/problem/1537/E1
https://codeforces.com/problemset/problem/1537/E2

本题分为简单版本和困难版本，在困难版本中，n 和 k 不超过 5e5。

输入 n (≤5000) 和 k (≤5000)，以及一个长为 n 的字符串 s，由小写字母组成。

有如下两种操作，每种操作你都可以执行任意多次：
1. 删除 s 的最后一个字符；
2. 将 s 复制一份拼在后面，即 s = s + s。

输出你能得到的所有长为 k 的字符串中，字典序最小的字符串。
 */
func samllstr(n int, str string, k int) string{
	start := 0
	end := 1
	for ; end < n ;{
		if str[end] > str[start] {
			break
		}else if str[end] < str[start] {
			end++
			continue
		}else {
			flag := 0
			i,j := start+1, end+1
			for  ;i < end && j < n; i, j = i+1, j+1 {
				if str[i] < str[j] {
					flag = 1
					break//end-1为止
				}
				if str[i] > str[j] {
					flag = 2
					break
				}
			}
			if flag == 1  {
				break
			}
			if flag == 2 {
				end = j+1
			}else {
				end= j
			}
		}
	}
	length := end-start
	return strings.Repeat(str[start:end], k/length) + str[:k%length]
}
/*
输入
8 16
dbcadabc
输出
dbcadabcdbcadabc
解释 复制一次

输入
4 5
abcd
输出
aaaaa

 */
func main() {
	var n, k int
	var str string
	fmt.Scan(&n, &k, &str)
	fmt.Printf("%v\n", samllstr(n,str, k))
}

package main

import (
	"fmt"
	"strings"
)

/*
https://codeforces.com/problemset/problem/1537/E1
https://codeforces.com/problemset/problem/1537/E2

本题分为简单版本和困难版本，在困难版本中，n 和 k 不超过 5e5。

输入 n (≤5000) 和 k (≤5000)，以及一个长为 n 的字符串 s，由小写字母组成。

有如下两种操作，每种操作你都可以执行任意多次：
1. 删除 s 的最后一个字符；
2. 将 s 复制一份拼在后面，即 s = s + s。

输出你能得到的所有长为 k 的字符串中，字典序最小的字符串。
 */
func samllstr(n int, str string, k int) string{
	start := 0
	end := 1
	for ; end < n ;{
		if str[end] > str[start] {
			break
		}else if str[end] < str[start] {
			end++
			continue
		}else {
			flag := 0
			i,j := start+1, end+1
			for  ;i < end && j < n; i, j = i+1, j+1 {
				if str[i] < str[j] {
					flag = 1
					break//end-1为止
				}
				if str[i] > str[j] {
					flag = 2
					break
				}
			}
			if flag == 1  {
				break
			}
			if flag == 2 {
				end = j+1
			}else {
				end= j
				break
			}
		}
	}
	length := end-start
	return strings.Repeat(str[start:end], k/length) + str[:k%length]
}
/*
输入
8 16
dbcadabc
输出
dbcadabcdbcadabc
解释 复制一次

输入
4 5
abcd
输出
aaaaa

 */
func main() {
	var n, k int
	var str string
	fmt.Scan(&n, &k, &str)
	fmt.Printf("%v\n", samllstr(n,str, k))
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := os.Stdout
	var n, k int
	fmt.Fscan(in,&n,&k)
	var s []byte
	fmt.Fscan(in,&s)
	delta := ""
	or := ""
	for i:=len(s)-1;i>=0;i--{
		now := string(s[:i+1])
		temp := now
		if delta==""{
			delta = now
			or = temp
		}else{
			for len(now)<len(delta){
				now = now + now
			}
			now = now[:len(delta)]
			flag := false
			for j:=0;j<len(delta);j++{
				if now[j]==delta[j]{
					continue
				}
				if now[j]>delta[j]{
					flag = false
					break
				}
				if now[j]<delta[j]{
					flag = true
					break
				}
			}
			if flag{
				delta = now
				or = temp
			}
		}
	}
	for len(or)<k{
		or = or + or
	}
	fmt.Fprintln(out,or[:k])
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := os.Stdout
	var n, k int
	fmt.Fscan(in,&n,&k)
	var s []byte
	fmt.Fscan(in,&s)
	delta := ""
	for i:=len(s)-1;i>=0;i--{
		now := string(s[:i+1])
		if delta==""{
			delta = now
		}else{
			for len(now)<len(delta){
				now = now + now
			}
			now = now[:len(delta)]
			flag := false
			for j:=0;j<len(delta);j++{
				if now[j]==delta[j]{
					continue
				}
				if now[j]>delta[j]{
					flag = false
					break
				}
				if now[j]<delta[j]{
					flag = true
					break
				}
			}
			if flag{
				delta = now
			}
		}
	}
	for len(delta)<k{
		delta = delta + delta
	}
	fmt.Fprintln(out,delta[:k])
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := os.Stdout
	var n, k int
	fmt.Fscan(in,&n,&k)
	var s []byte
	fmt.Fscan(in,&s)
	delta := ""
	for i:=len(s)-1;i>=0;i--{
		now := string(s[:i+1])
		if delta==""{
			delta = now
		}else{
			for len(now)<len(delta){
				now = now + now
			}
			now = now[:len(delta)]
			if now<delta{
				delta = now
			}
		}
	}
	for len(delta)<k{
		delta = delta + delta
	}
	fmt.Fprintln(out,delta[:k])
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := os.Stdout
	var n, k int
	fmt.Fscan(in,&n,&k)
	var s []byte
	fmt.Fscan(in,&s)
	index := -1
	for i:=0;i<len(s);i++{
		if s[i]>s[0]&&i+1<=k{
			index = i
			break
		}
	}
	if index!=-1{
		cool := s[:index]
		ret := string(cool)
		for len(ret)<k{
			ret = ret + ret
		}
		fmt.Fprintln(out,ret[:k])
	}else{
		ret := string(s)
		for len(ret)<k{
			ret = ret + ret
		}
		fmt.Fprintln(out,ret[:k])
	}
}

package main

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

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

	var n, k int
	fmt.Fscan(r, &n, &k)

	var s string
	fmt.Fscan(r, &s)

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

func solve(n, k int, s string) string {

	//cafsafhat

	//baaaaa
	//14

	//xkkhaxkkhaggkgkgxkkhaxkkha

	//xkkhaxkkhaggkgkgxkkhaxkkha
	//xkkhaxkkhaggkgkgxkkhaxkkhaggkgkg

	//8 + 8
	//20

	var i int
	var found bool

	for i = 1; i < n; i++ {
		if s[0] > s[i] {
			continue

		} else if s[0] == s[i] {
			s1 := s[:i]
			lenS1 := len(s1)

			rem := n - i

			var s2 string

			if lenS1 <= rem {
				s2 = s[i: i+lenS1]

				if s1 < s2 {
					found = true
					break

				} else {
					i += lenS1 - 1
					continue
				}

			} else {
				s1 := s[: rem]

				s2 := s[i: i+rem]

				if s1 <= s2 {
					found = true
					break
				} else {
					break
				}
			}

		} else {
			found = true
			break
		}
	}

	var temp string

	if !found {
		temp = s
	} else {
		temp = s[0: i]
	}

	t := len(temp)

	d := k/t
	r := k % t

	var ans bytes.Buffer

	if r != 0 {
		for i := 0; i < d; i++ {
			ans.WriteString(temp)
		}

		for i := 0; i < r; i++ {
			ans.WriteByte(temp[i])
		}
	} else {
		for i := 0; i < d; i++ {
			ans.WriteString(temp)
		}
	}

	return ans.String()

}

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

package main

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

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

	var n, k int
	fmt.Fscan(r, &n, &k)

	var s string
	fmt.Fscan(r, &s)

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

func solve(n, k int, s string) string {

	//cafsafhat

	//baaaaa
	//14

	//baaaaabaaaaa

	//cbavasdagafasac

	//cbacba

	//40
	//cbbcbacbbcbaadad cbbcbacbbcbaadad cbbcbac

	//cbbcbacbbcba

	//8 + 8
	//20

	var i int
	var found bool

	for i = 1; i < n; i++{
		if s[0] > s[i] {
			continue

		} else if s[0] == s[i] {
			s1 := s[:i]
			lenS1 := len(s1)
			t := min(lenS1, n - i)

			s2 := s[i : i + t]

			if s1 < s2 {
				found = true
				break

			} else {
				i += t - 1
				continue
			}

		} else {
			found = true
			break
		}
	}

	var temp string

	if !found {
		temp = s
	} else {
		temp = s[0: i]
	}

	t := len(temp)

	d := k/t
	r := k % t

	var ans bytes.Buffer

	if r != 0 {
		for i := 0; i < d + 1; i++ {
			ans.WriteString(temp)
		}

		for i := 0; i < (d + 1) * t - k; i++ {
			ans.WriteByte(temp[i])
		}
	} else {
		for i := 0; i < d; i++ {
			ans.WriteString(temp)
		}
	}

	return ans.String()

}

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

package main

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

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

	var n, k int
	fmt.Fscan(r, &n, &k)

	var s string
	fmt.Fscan(r, &s)

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

func solve(n, k int, s string) string {

	//cafsafhat

	//baaaaa
	//14

	//baaaaabaaaaa

	//30
	//cbbcbacbvcbadad

	//cbcacbca

	//8 + 8
	//20

	var i int
	var found bool

	for i = 1; i < n; i++{
		if s[0] > s[i] {
			continue

		} else if s[0] == s[i] {
			s1 := s[:i]
			lenS1 := len(s1)
			t := min(lenS1, n - i)

			s2 := s[i : i + t]

			if s1 <= s2 {
				found = true
				break

			} else {
				i += t - 1
				continue
			}

		} else {
			found = true
			break
		}
	}

	var temp string

	if !found {
		temp = s
	} else {
		temp = s[0: i]
	}

	t := len(temp)

	d := k/t
	r := k % t

	var ans bytes.Buffer

	if r != 0 {
		for i := 0; i < d + 1; i++ {
			ans.WriteString(temp)
		}

		for i := 0; i < (d + 1) * t - k; i++ {
			ans.WriteByte(temp[i])
		}
	} else {
		for i := 0; i < d; i++ {
			ans.WriteString(temp)
		}
	}

	return ans.String()

}

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

package main

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

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

	var n, k int
	fmt.Fscan(r, &n, &k)

	var s string
	fmt.Fscan(r, &s)

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

func solve(n, k int, s string) string {

	//cafsafhat

	//

	//8 + 8
	//20

	var i int
	var found bool

	for i = 1; i < n; i++{
		if s[0] >= s[i] {
			continue
		}
		found = true
		break
	}

	var temp string

	if !found {
		temp = s
	} else {
		temp = s[0: i]
	}

	t := len(temp)

	d := k/t
	r := k % t

	var ans bytes.Buffer

	if r != 0 {
		for i := 0; i < d + 1; i++ {
			ans.WriteString(temp)
		}

		for i := 0; i < (d + 1) * t - k; i++ {
			ans.WriteByte(temp[i])
		}
	} else {
		for i := 0; i < d; i++ {
			ans.WriteString(temp)
		}
	}

	return ans.String()

}

package main

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

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

func cf(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var n int
	fmt.Fscan(in, &n)

	fmt.Fprintln(out, n)
}

func cf1537E1(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var n, k int
	var s string
	fmt.Fscan(in, &n, &k, &s)
	ss := []string{}
	for i := range s {
		ss = append(ss, s[:i+1]+s[:i+1])
	}
	sort.Strings(ss)
	s = ss[0]
	for len(s) < k {
		s += s
	}

	fmt.Fprintln(out, s[:k])
}

func cf691C(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var x string
	fmt.Fscan(in, &x)
	if !strings.Contains(x, ".") {
		x += "."
	}
	x = strings.Trim(x, "0")
	idx := strings.Index(x, ".")
	e := idx - strings.IndexAny(x, "123456789")
	if e > 0 {
		e--
	}
	x = strings.Trim(x[:idx]+x[idx+1:], "0")
	if len(x) > 1 {
		x = x[:1] + "." + x[1:]
	}
	fmt.Fprint(out, x)
	if e != 0 {
		fmt.Fprint(out, "E", e)
	}
}

func cf467C(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var n, m, k int
	fmt.Fscan(in, &n, &m, &k)
	sums := make([]int64, n+1)
	for i := 1; i <= n; i++ {
		fmt.Fscan(in, &sums[i])
		sums[i] += sums[i-1]
	}
	f := make([]int64, n+1)
	for j := 1; j <= k; j++ {
		ff := make([]int64, n+1)
		for i := j * m; i <= n; i++ {
			ff[i] = max64(ff[i-1], sums[i]-sums[i-m]+f[i-m])
		}
		f = ff
	}

	fmt.Fprintln(out, f[n])
}

func cf1469C(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
next:
	for ; t > 0; t-- {
		var n, k int
		fmt.Fscan(in, &n, &k)
		h := make([]int, n)
		for i := range h {
			fmt.Fscan(in, &h[i])
		}
		pre := []int{h[0], h[0]}
		for i := 1; i < n; i++ {
			cur := []int{max(pre[0]-k+1, h[i]), min(pre[1]+k-1, h[i]+k-1)}
			if cur[0] > cur[1] {
				fmt.Fprintln(out, "NO")
				continue next
			}
			pre = cur
		}
		if pre[0] == h[n-1] {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

func cf1060C(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var n, m, x int
	fmt.Fscan(in, &n, &m)
	nums1, nums2 := make([]int64, n+1), make([]int64, m+1)
	for i := 1; i <= n; i++ {
		fmt.Fscan(in, &nums1[i])
		nums1[i] += nums1[i-1]
	}
	for i := 1; i <= m; i++ {
		fmt.Fscan(in, &nums2[i])
		nums2[i] += nums2[i-1]
	}
	fmt.Fscan(in, &x)
	mins1, mins2 := make([]int64, n+1), make([]int64, m+1)
	for i := 1; i <= n; i++ {
		mins1[i] = nums1[i]
		for j := i + 1; j <= n; j++ {
			mins1[i] = min64(mins1[i], nums1[j]-nums1[j-i])
		}
	}
	for i := 1; i <= m; i++ {
		mins2[i] = nums2[i]
		for j := i + 1; j <= m; j++ {
			mins2[i] = min64(mins2[i], nums2[j]-nums2[j-i])
		}
	}
	ans := 0
	for i := 1; i <= n; i++ {
		for j := 1; j <= m; j++ {
			if mins1[i]*mins2[j] <= int64(x) && i*j > ans {
				ans = i * j
			}
		}
	}

	fmt.Fprintln(out, ans)
}

func cf1043D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var n, m int
	fmt.Fscan(in, &n, &m)
	arr, preArr := make([]int, n+1), make([]int, n+1)
	var pre, cur int
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &cur)
		preArr[cur] = pre
		arr[pre], pre = cur, cur
	}
	for ; m > 1; m-- {
		pre = 0
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &cur)
			if arr[pre] != cur {
				preArr[cur] = 0
				arr[pre] = 0
			}
			pre = cur
		}
		arr[cur] = 0
	}
	ans := int64(n)
	for i := 1; i <= n; i++ {
		if arr[i] > 0 {
			cnt, id := 0, i
			for preArr[id] != 0 {
				id = preArr[id]
			}
			for arr[id] != 0 {
				arr[id], id = 0, arr[id]
				cnt++
				ans += int64(cnt)
			}
		}
	}

	fmt.Fprintln(out, ans)
}

func cf1519D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var n int
	fmt.Fscan(in, &n)
	nums1, nums2 := make([]int64, n), make([]int64, n)
	for i := range nums1 {
		fmt.Fscan(in, &nums1[i])
	}
	for i := range nums2 {
		fmt.Fscan(in, &nums2[i])
	}
	f := make([]int64, n+1)
	for i := range nums1 {
		f[i+1] = f[i] + nums1[i]*nums2[i]
	}
	ans := f[n]
	for i := range nums1 {
		for j, d, sum := i, 1, nums1[i]*nums2[i]; j-d >= 0 && j+d < n; d++ {
			sum += nums1[j-d]*nums2[j+d] + nums1[j+d]*nums2[j-d]
			ans = max64(ans, sum+f[n]-f[j+d+1]+f[j-d])
		}
		for j1, j2, d, sum := i, i+1, 0, int64(0); j1-d >= 0 && j2+d < n; d++ {
			sum += nums1[j2+d]*nums2[j1-d] + nums1[j1-d]*nums2[j2+d]
			ans = max64(ans, sum+f[n]-f[j2+d+1]+f[j1-d])
		}
	}

	fmt.Fprintln(out, ans)
}

func cf175C(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var n int
	fmt.Fscan(in, &n)
	nums := make([][2]int64, n)
	for i := range nums {
		fmt.Fscan(in, &nums[i][0], &nums[i][1])
	}
	var t int
	fmt.Fscan(in, &t)
	p := make([]int64, t+2)
	p[t+1] = 1e12
	pre := int64(0)
	for i := 1; i <= t; i++ {
		fmt.Fscan(in, &p[i])
		pre, p[i] = p[i], p[i]-pre
	}
	sort.Slice(nums, func(i, j int) bool {
		return nums[i][1] < nums[j][1]
	})
	sum := int64(0)
	for i, j := 0, 1; i < len(nums); {
		sum += min64(nums[i][0], p[j]) * nums[i][1] * int64(j)
		if nums[i][0] > p[j] {
			nums[i][0] -= p[j]
			j++
		} else {
			p[j] -= nums[i][0]
			i++
		}
	}

	fmt.Fprintln(out, sum)
}

func cf494A(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var s string
	fmt.Fscan(in, &s)
	sum, minSum, cnt := 0, len(s), 0
	for i := range s {
		if s[i] == '(' {
			sum++
		} else if s[i] == ')' {
			sum--
		} else {
			sum--
			minSum = sum
			cnt++
		}
		if sum < 0 {
			fmt.Fprintln(out, -1)
			return
		}
		minSum = min(minSum, sum)
	}
	if minSum < sum {
		fmt.Fprintln(out, -1)
	} else {
		fmt.Fprintln(out, strings.Repeat("1\n", cnt-1), sum+1)
	}
}

func cf1281B(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var n int
	fmt.Fscan(in, &n)
	for ; n > 0; n-- {
		var s, c string
		fmt.Fscan(in, &s, &c)
		cs := []byte(s)
		j, a, b := len(s)-1, 0, 0
		for i := len(s) - 1; i >= 0; i-- {
			if cs[i] > cs[j] {
				a, b = i, j
			} else if cs[i] < cs[j] {
				j = i
			}
		}
		cs[a], cs[b] = cs[b], cs[a]
		if s = string(cs); s < c {
			fmt.Fprintln(out, s)
		} else {
			fmt.Fprintln(out, "---")
		}
	}
}

func cf605A(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var n int
	fmt.Fscan(in, &n)
	nums := make([][3]int, n) // nums[0]原数字  nums[1]原下标	nums[2]排序后的下标
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &nums[i][0])
		nums[i][1] = i
	}
	sort.SliceStable(nums, func(i, j int) bool {
		return nums[i][0] < nums[j][0]
	})
	for i := range nums {
		nums[i][2] = i
	}
	sort.Slice(nums, func(i, j int) bool {
		return nums[i][1] < nums[j][1]
	})
	f := make([]int, n+1)
	for _, num := range nums {
		f[num[2]+1] = f[num[2]] + 1
	}

	fmt.Fprintln(out, n-max(f...))
}

func cf297B(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var n, m, k int
	fmt.Fscan(in, &n, &m, &k)
	if n > m {
		fmt.Fprintln(out, "YES")
		return
	}
	a, b := make([]int, n), make([]int, m)
	for i := range a {
		fmt.Fscan(in, &a[i])
	}
	for i := range b {
		fmt.Fscan(in, &b[i])
	}
	sort.Sort(sort.Reverse(sort.IntSlice(a)))
	sort.Sort(sort.Reverse(sort.IntSlice(b)))
	for i := range a {
		if a[i] > b[i] {
			fmt.Fprintln(out, "YES")
			return
		}
	}

	fmt.Fprintln(out, "NO")
}

func helan(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var n int
	fmt.Fscan(in, &n)
	nums := make([]int, n)
	for i := range nums {
		fmt.Fscan(in, &nums[i])
	}

	less, more := -1, n
	for i := 0; i < more; i++ {
		if nums[i] == 0 {
			less++
			nums[i], nums[less] = nums[less], nums[i]
		} else if nums[i] == 2 {
			more--
			nums[i], nums[more] = nums[more], nums[i]
			i--
		}
	}

	fmt.Fprintln(out, nums)
}

func cf1203D2(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var s, t string
	fmt.Fscan(in, &s, &t)
	ans := 0
	left, right := make([]int, len(t)), make([]int, len(t))
	i, j := 0, 0
	for ; i < len(s) && j < len(t); i++ {
		if s[i] == t[j] {
			left[j] = i
			j++
		}
	}
	ans = max(ans, len(s)-i)
	i, j = len(s)-1, len(t)-1
	for ; i >= 0 && j >= 0; i-- {
		if s[i] == t[j] {
			right[j] = i
			j--
		}
	}
	ans = max(ans, i+1)
	for k := 0; k < len(t)-1; k++ {
		ans = max(ans, right[k+1]-left[k]-1)
	}

	fmt.Fprintln(out, ans)

}

func cf1196D2(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var t, n, k int
	var s string
	fmt.Fscan(in, &t)
	pat := "RGB"
	for ; t > 0; t-- {
		fmt.Fscan(in, &n, &k, &s)
		ans := k
		for i := 0; i < 3; i++ {
			diff := 0
			for j := range s[:k] {
				if s[j] != pat[(i+j)%3] {
					diff++
				}
			}
			ans = min(ans, diff)
			for j := k; j < n; j++ {
				if s[j] != pat[(i+j)%3] {
					diff++
				}
				if s[j-k] != pat[(i+j-k%3)%3] {
					diff--
				}
				ans = min(ans, diff)
			}
		}

		fmt.Fprintln(out, ans)
	}
}

func cf44H(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var s string
	fmt.Fscan(in, &s)
	f := make([][10]int64, len(s))
	for i := range f[0] {
		f[0][i] = 1
	}
	for i := 1; i < len(s); i++ {
		for j := range f[i] {
			f[i][(j+int(s[i])-'0')/2] += f[i-1][j]
			if (j+int(s[i])-'0')&1 == 1 {
				f[i][(j+int(s[i])-'0'+1)/2] += f[i-1][j]
			}
		}
	}
	flag := true
	for i := range s[:len(s)-1] {
		if s[i]-s[i+1] <= 1 || s[i+1]-s[i] <= 1 {
			continue
		} else {
			flag = false
			break
		}
	}
	ans := int64(0)
	for i := range f[0] {
		ans += f[len(s)-1][i]
	}
	if flag {
		ans--
	}
	fmt.Fprintln(out, ans)
}

func cf1051D(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, k int
	fmt.Fscan(in, &n, &k)
	if k == 1 {
		fmt.Fprintln(out, 2)
		return
	}
	mod := int64(998244353)
	f := make([][2]int64, k+1)
	f[1][1], f[2][0] = 2, 2
	for i := 2; i <= n; i++ {
		for j := min(k, 2*i); j >= 2; j-- {
			f[j][1] = (f[j][0]*2 + f[j][1] + f[j-1][1]) % mod
			f[j][0] = (f[j][0] + f[j-2][0] + f[j-1][1]*2) % mod
		}
	}

	fmt.Fprintln(out, (f[k][0]+f[k][1])%mod)
}

func cf1051D3(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, k int
	fmt.Fscan(in, &n, &k)
	if k == 1 {
		fmt.Fprintln(out, 2)
		return
	}
	mod := int64(998244353)
	f := make([][][2]int64, n+1)
	for i := range f {
		f[i] = make([][2]int64, k+1)
	}
	f[1][1][1], f[1][2][0] = 2, 2
	for i := 2; i <= n; i++ {
		f[i][1][1] = 2
		for j := 2; j <= k && j <= 2*i; j++ {
			f[i][j][0] = (f[i-1][j][0] + f[i-1][j-2][0] + f[i-1][j-1][1]*2) % mod
			f[i][j][1] = (f[i-1][j][0]*2 + f[i-1][j][1] + f[i-1][j-1][1]) % mod
		}
	}

	fmt.Fprintln(out, (f[n][k][0]+f[n][k][1])%mod)
}

func cf468A(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n int
	fmt.Fscan(in, &n)
	if n < 4 {
		fmt.Fprintln(out, "NO")
		return
	}
	fmt.Fprintln(out, "YES")
	if (n-4)&1 == 0 {
		fmt.Fprintln(out, "1 * 2 = 2")
		fmt.Fprintln(out, "2 * 3 = 6")
		fmt.Fprintln(out, "6 * 4 = 24")
	} else {
		fmt.Fprintln(out, "2 * 4 = 8")
		fmt.Fprintln(out, "3 * 5 = 15")
		fmt.Fprintln(out, "8 + 15 = 23")
		fmt.Fprintln(out, "23 + 1 = 24")
	}
	for i := 5 + n&1; i < n; i += 2 {
		fmt.Fprintln(out, i+1, "-", i, "= 1")
		fmt.Fprintln(out, "1 * 24 = 24")
	}
}

func cf573B(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, ans int
	fmt.Fscan(in, &n)
	arr := make([]int, n+2)
	for i := 1; i <= n; i++ {
		fmt.Fscan(in, &arr[i])
		arr[i] = min(arr[i], arr[i-1]+1)
	}
	for i := n; i >= 1; i-- {
		arr[i] = min(arr[i], arr[i+1]+1)
		ans = max(ans, arr[i])
	}

	fmt.Fprintln(out, ans)
}

func min(nums ...int) int {
	ans := nums[0]
	for _, num := range nums {
		if ans > num {
			ans = num
		}
	}
	return ans
}

func min64(nums ...int64) int64 {
	ans := nums[0]
	for _, num := range nums {
		if ans > num {
			ans = num
		}
	}
	return ans
}

func max(nums ...int) int {
	ans := nums[0]
	for _, num := range nums {
		if ans < num {
			ans = num
		}
	}
	return ans
}

func max64(nums ...int64) int64 {
	ans := nums[0]
	for _, num := range nums {
		if ans < num {
			ans = num
		}
	}
	return ans
}

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

package main

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

var (
	in   *bufio.Reader = bufio.NewReader(os.Stdin)
	n, k int
	s    string
)

func main() {
	fmt.Fscan(in, &n, &k, &s)
	// fmt.Printf("n=%d, k=%d, s=%s\n", n, k, s)
	pre := s
	for i := n - 1; i > 0; i-- {
		pre = min(pre, s[:i])
	}
	ans := ""
	for len(ans) < k {
		ans = ans + pre
	}
	fmt.Println(ans[:k])
}
func min(s1, s2 string) string {
	if s1+s1 < s2+s2 {
		return s1
	}
	return s2
}

package main

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

var _rbuf = bufio.NewReader(os.Stdin)
var _wbuf = bufio.NewWriter(os.Stdout)

func scan(a ...interface{})             { _wbuf.Flush(); fmt.Fscan(_rbuf, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(_wbuf, f, a...) }

func main() {
	defer _wbuf.Flush()

	var n, k int
	var str string

	scan(&n, &k)
	scan(&str)

	for i := 1; i < n; i++ {
		if str[i:] > str {
			str = str[:i]
			break
		}
	}
	for len(str) < k {
		str += str
	}
	printf("%s\n", str[:k])
}


package main

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

func main() {
	var n int
	fmt.Scan(&n)
	p := make([]float64, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&p[i])
	}
	sort.Sort(sort.Reverse(sort.Float64Slice(p)))
	var res float64 = 0.0
	var P, S float64 = 1.0, 0.0
	if p[0] == 1 {
		fmt.Println(1.0)
	} else {
		for i := 0; i < n; i++ {
			P *= (1.0 - p[i])
			S += p[i] / (1 - p[i])
			res = math.Max(res, P*S)
		}
		fmt.Printf("%0.10f\n", res)
	}
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var n int
	fmt.Scan(&n)
	p := make([]float64, n)
	for i := range p {
		fmt.Scan(&p[i])
	}
	sort.Float64s(p)
	if p[len(p)-1] >= 0.5 {
		fmt.Printf("%.11f", p[len(p)-1])
		return
	}
	var a, q float64
	q = 1
	for i := len(p) - 1; i >= 0; i-- {
		if x := a * (1 - p[i]) + p[i] * q; x > a {
			a = x
			q *= 1 - p[i]
		}
	}
	fmt.Printf("%.11f", a)
}


package main

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

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

	points := []int{500, 1000, 1500, 2000, 2500}

	m := make([]int, 5)
	w := make([]int, 5)
	var hS, hU int

	fmt.Fscanf(stdin, "%d%d%d%d%d\n", &m[0], &m[1], &m[2], &m[3], &m[4])
	fmt.Fscanf(stdin, "%d%d%d%d%d\n", &w[0], &w[1], &w[2], &w[3], &w[4])
	fmt.Fscanf(stdin, "%d%d\n", &hS, &hU)

	res := 0
	for i := 0; i < 5; i++ {
		v := max(points[i]*3/10, (points[i]-m[i]*points[i]/250)-50*w[i])
		// fmt.Println(v)
		res = res + v

	}
	res += hS * 100
	res -= hU * 50
	fmt.Println(res)
}

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

	return b
}

package main

import (
	"fmt"
)

func main() {
	x := [5]int{500, 1000, 1500, 2000, 2500}

	var m [5]int
	for i := 0; i < 4; i++ {
		if _, err := fmt.Scanf("%d", &m[i]); err != nil {
			return
		}
	}
	if _, err := fmt.Scanf("%d\n", &m[4]); err != nil {
		return
	}
	var w [5]int
	for i := 0; i < 4; i++ {
		if _, err := fmt.Scanf("%d", &w[i]); err != nil {
			return
		}
	}
	if _, err := fmt.Scanf("%d\n", &w[4]); err != nil {
		return
	}
	var h_s, h_u int
	if _, err := fmt.Scanf("%d %d\n", &h_s, &h_u); err != nil {
		return
	}

	score := 0
	for i := 0; i < 5; i++ {
		s1 := x[i] * 3 / 10
		s2 := x[i] - m[i]*(x[i]/250) - 50*w[i]
		if s1 > s2 {
			score += s1
		} else {
			score += s2
		}
	}
	score += 100 * h_s
	score -= 50 * h_u

	fmt.Println(score)
}

package main

import (
	"fmt"
)

func main() {
	x := [5]int{500, 1000, 1500, 2000, 2500}

	var m [5]int
	for i := 0; i < 5; i++ {
		if _, err := fmt.Scanf("%d", &m[i]); err != nil {
			return
		}
	}
	var w [5]int
	for i := 0; i < 5; i++ {
		if _, err := fmt.Scanf("%d", &w[i]); err != nil {
			return
		}
	}
	var h_s, h_u int
	if _, err := fmt.Scanf("%d %d\n", &h_s, &h_u); err != nil {
		return
	}

	score := 0
	for i := 0; i < 5; i++ {
		s1 := int(0.3 * float32(x[i]))
		s2 := int((1.0-float32(m[i])/250.0)*float32(x[i]) - float32(50*w[i]))
		if s1 > s2 {
			score += s1
		} else {
			score += s2
		}
	}
	score += 100 * h_s
	score -= 50 * h_u

	fmt.Println(score)
}

package main

import (
	"fmt"
)

func main() {
	x := [5]int{500, 1000, 1500, 2000, 2500}

	var m [5]int
	for i := 0; i < 4; i++ {
		if _, err := fmt.Scanf("%d", &m[i]); err != nil {
			return
		}
	}
	if _, err := fmt.Scanf("%d\n", &m[4]); err != nil {
		return
	}
	var w [5]int
	for i := 0; i < 4; i++ {
		if _, err := fmt.Scanf("%d", &w[i]); err != nil {
			return
		}
	}
	if _, err := fmt.Scanf("%d\n", &w[4]); err != nil {
		return
	}
	var h_s, h_u int
	if _, err := fmt.Scanf("%d %d\n", &h_s, &h_u); err != nil {
		return
	}

	score := 0
	for i := 0; i < 5; i++ {
		s1 := int(0.3 * float32(x[i]))
		s2 := int((1.0-float32(m[i])/250.0)*float32(x[i]) - float32(50*w[i]))
		if s1 > s2 {
			score += s1
		} else {
			score += s2
		}
	}
	score += 100 * h_s
	score -= 50 * h_u

	fmt.Println(score)
}


package main

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

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

func ReadInt() int{
	var i int
	fmt.Fscan(in, &i)
	return i
}

func main(){
	defer out.Flush()
	t := ReadInt()
	for ; t > 0; t--{
		n := ReadInt()
		a := make([]int, n)
		who := make([]int, n+1)
		for i:=0;i<n;i++{
			a[i] = ReadInt()
		}
		found:=false
		for i:=n-1;i>=0;i--{
			if who[a[i]]==1{
				found=true
				fmt.Fprintln(out, i+1)
				break
			}
			who[a[i]] = 1
		}
		if !found{
			fmt.Fprintln(out, 0)
		}
	}
}

package main
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])
		}
		mapper:= make(map[int]bool) // create a map
		for j:=n-1;j>=0;j--{ // loop through each element, starting from end
			if mapper[list[j]] { // if map coords exist, print j (position in the loop)
				fmt.Fprintln(out,j+1)
				goto out
			}
			mapper[list[j]]= true // if first time, add map coords to map
		}
		fmt.Fprintln(out,0)
		out:
	}
}

package main

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

func solve() {
	n := get()
	A := geta(n)
	occur := make([]bool, n)
	for i := n - 1; i >= 0; i-- {
		if occur[A[i]-1] {
			putln(i + 1)
			return
		}
		occur[A[i]-1] = true
	}
	putln(0)
}

const (
	once = false
)

var (
	in  *bufio.Scanner
	out *bufio.Writer
)

func main() {
	in = bufio.NewScanner(bufio.NewReader(os.Stdin))
	in.Split(bufio.ScanWords)

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

	if once {
		solve()
		return
	}

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

func geta(n int) []int {
	a := make([]int, n)
	for i := range a {
		a[i] = get()
	}
	return a
}

func get() int {
	x, _ := strconv.Atoi(gets())
	return x
}

func gets() string {
	scan()
	return in.Text()
}

func scan() {
	in.Scan()
}

func put(x int) {
	out.WriteString(strconv.Itoa(x))
}

func putln(x int) {
	put(x)
	putc('\n')
}

func putc(c rune) {
	out.WriteRune(c)
}

package main

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

func solve() {
	A := geta(get())
	occur := make([]bool, len(A))
	for N := len(A) - 1; N >= 0; N-- {
		if occur[A[N]-1] {
			fmt.Println(N + 1)
			return
		}
		occur[A[N]-1] = true
	}
	fmt.Println(0)
}

const (
	once = false
)

var (
	in  *bufio.Scanner
	out *bufio.Writer
)

func main() {
	in = bufio.NewScanner(bufio.NewReader(os.Stdin))
	in.Split(bufio.ScanWords)

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

	if once {
		solve()
		return
	}

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

func geta(n int) []int {
	a := make([]int, n)
	for i := range a {
		a[i] = get()
	}
	return a
}

func get() int {
	x, _ := strconv.Atoi(gets())
	return x
}

func gets() string {
	scan()
	return in.Text()
}

func scan() {
	in.Scan()
}

func put(x int) {
	out.WriteString(strconv.Itoa(x))
}

func putln(x int) {
	put(x)
	putc('\n')
}

func putc(c rune) {
	out.WriteRune(c)
}

package main

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

func solve() {
	A := geta(get())
	occur := make(map[int]bool)
	for N := len(A) - 1; N >= 0; N-- {
		if occur[A[N]] {
			fmt.Println(N + 1)
			return
		}
		occur[A[N]] = true
	}
	fmt.Println(0)
}

const (
	once = false
)

var (
	in  *bufio.Scanner
	out *bufio.Writer
)

func main() {
	in = bufio.NewScanner(bufio.NewReader(os.Stdin))
	in.Split(bufio.ScanWords)

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

	if once {
		solve()
		return
	}

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

func geta(n int) []int {
	a := make([]int, n)
	for i := range a {
		a[i] = get()
	}
	return a
}

func get() int {
	x, _ := strconv.Atoi(gets())
	return x
}

func gets() string {
	scan()
	return in.Text()
}

func scan() {
	in.Scan()
}

package main

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

var IN = bufio.NewReader(os.Stdin)

func main() {
	var t int
	fmt.Fscan(IN, &t)
	for ; t > 0; t-- {
		testCase()
	}
}

func testCase() {
	var N int
	fmt.Fscan(IN, &N)
	A := make([]int, N)
	for i := range A {
		fmt.Fscan(IN, &A[i])
	}
	occur := make(map[int]bool)
	for N = N - 1; N >= 0; N-- {
		if occur[A[N]] {
			fmt.Println(N + 1)
			return
		}
		occur[A[N]] = true
	}
	fmt.Println(0)
}

package main

import (

	"bufio"
	"os"
	"fmt"
)

var (

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

func max(a, b int) int {

	if a > b {

		return a
	}
	return b
}

func main() {

	defer out.Flush()

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

		var n int
		fmt.Fscan(in, &n)
		var ans = -1
		var mp map[int]int = map[int]int{}
		for j := 0; j < n; j++ {

			var x int
			fmt.Fscan(in, &x)
			if val, ok := mp[x]; ok {

				ans = max(val, ans)
			}
			mp[x] = j
		}
		fmt.Fprintln(out, ans + 1)
	}
}

package main

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

var IN *bufio.Reader

func cp() {
	N := readInt(IN)
	A := readArrInt(IN)
	unique := 0
	numberExists := make(map[int]bool)
	for i := N - 1; i >= 0; i-- {
		if _, exists := numberExists[A[i]]; exists {
			break
		}
		unique++
		numberExists[A[i]] = true
	}

	fmt.Println(N - unique)
}

func main() {
	t := 1
	t = readInt(IN)
	for ; t > 0; t-- {
		cp()
	}
}

func init() {
	IN = bufio.NewReader(os.Stdin)
}

func Max(x, y int) int {
	if x < y {
		return y
	}
	return x
}
func Min(x, y int) int {
	if x > y {
		return y
	}
	return x
}

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

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

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

func readArrInt64(in *bufio.Reader) []int64 {
	numbs := readLineNumbs(in)
	arr := make([]int64, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.ParseInt(n, 10, 64)
		arr[i] = val
	}
	return arr
}

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)

		aa := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &aa[i])
		}
		var d int
		var f bool
		bb := make([]bool, n+1)
		for i := n - 1; i >= 0; i-- {
			if !bb[aa[i]] {
				bb[aa[i]] = true
				continue
			}
			d = i
			f = true
			break
		}
		if !f {
			fmt.Fprintln(out, 0)
		} else {
			fmt.Fprintln(out, d+1)
		}
	}
}

// B. Удали префикс
// https://codeforces.com/contest/1714/problem/B
package main

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

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

	var CountData int
	fmt.Fscan(in, &CountData)

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

		MapElem := make(map[int]int)
		EndInd := -1

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

			if Ind, ok := MapElem[Elem]; ok {
				if EndInd < Ind {
					EndInd = Ind
				}
			}
			MapElem[Elem] = n
		}

		fmt.Fprintln(out, 1+EndInd)
	}
}

package main

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

const MAXN = 2e5

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	a := make([]int, MAXN+7)
	rcd := make([]int, MAXN+7)
	var t, n, i, ans int
	fmt.Fscan(in, &t)
	for t != 0 {
		fmt.Fscan(in, &n)
		for i = 1; i <= n; i++ {
			fmt.Fscan(in, &a[i])
		}
		ans = 0
		for i = n; i != 0; i-- {
			if rcd[a[i]] == t {
				break
			}
			ans++
			rcd[a[i]] = t
		}
		fmt.Fprintln(out, n-ans)
		t--
	}
}

package main

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

var (
  in = bufio.NewReader(os.Stdin)
  out = bufio.NewWriter(os.Stdout)
)
func scan(a ...any) {
  fmt.Fscan(in, a...)
}
func printf(f string, a ...any) {
  fmt.Fprintf(out, f, a...)
}

func solve() {
  var n int
  scan(&n)
  a := make([]int, n)
  for i := 0; i < n; i++ { scan(&a[i]) }

  cnt := make(map[int]int)
  for i := n - 1; i >= 0; i-- {
    if _, ok := cnt[a[i]]; ok {
      printf("%d\n", i + 1)
      return
    }
    cnt[a[i]] = 1
  }
  printf("%d\n", 0)
}

func main() {
  defer out.Flush()
  var t int
  scan(&t)
  for i := 0; i < t; i++ {
    solve()
  }
}

package main

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

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

func scan(a ...interface{}) {
	fmt.Fscan(in, a...)
}
func printf(f string, a ...interface{}) {
	fmt.Fprintf(out, f, a...)
}
func solve() {
	var n int
	scan(&n)
	a := make([]int, n)
	f := make([]int, n+1)
	for i := 0; i < n; i++ {
		scan(&a[i])
	}
	id := n - 1
	for ; id >= 0; id-- {
		if f[a[id]] == 0 {
			f[a[id]]++
		} else {
			break
		}
	}
	printf("%d\n", id+1)
}

func main() {
	defer out.Flush()
	n := 0
	scan(&n)
	for i := 0; i < n; i++ {
		solve()
	}
}

package main

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

func solve(in io.Reader, out io.Writer) {
	var n int
	fmt.Fscan(in, &n)
	arr := []int{}
	for i := 0; i < n; i++ {
		var num int
		fmt.Fscan(in, &num)
		arr = append(arr, num)
	}
	mp := make(map[int]bool)
	for i := n - 1; i >= 0; i-- {
		if !mp[arr[i]] {
			mp[arr[i]] = true
		} else {
			fmt.Fprintln(out, i+1)
			return
		}
	}
	fmt.Fprintln(out, 0)
}

func main() {
	in := bufio.NewReader(os.Stdin)
	// in, _ := os.Open("in.txt")
	t := 1
	fmt.Fscan(in, &t)
	for t > 0 {
		solve(in, os.Stdout)
		t--
	}
}

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
}

package main

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

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

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

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

func do(n int, a []int) int {
	m := make(map[int]bool)
	for i := len(a) - 1; i >= 0; i-- {
		if m[a[i]] {
			return i + 1
		}
		m[a[i]] = true
	}
	return 0
}

package main

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

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

	n := 0
	fmt.Fscan(reader, &n)
	for i := 0; i < n; i++ {
		var m int
		fmt.Fscan(reader, &m)
		am := make(map[int]bool)
		a := make([]int, m, m)
		for j := 0; j < m; j++ {
			fmt.Fscan(reader, &a[j])
		}
		max := 0
		for j := m - 1; j >= 0; j-- {
			if _, ok := am[a[j]]; ok {
				break
			} else {
				am[a[j]] = false
				max++
			}

		}

		fmt.Fprintf(writer, "%d\n", m-max)
	}
}

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	t, n   int
	a      []int
)

func solve(n int, a []int) int {
	var b, c = make([]int, n), make(map[int]int)
	for i, x := range a {
		lastRepeated, ok := c[x]
		if !ok {
			b[i] = -1
		} else {
			b[i] = lastRepeated
		}
		c[x] = i
	}
	var maxb = -1
	for i := range b {
		if b[i] > maxb {
			maxb = b[i]
		}
	}
	return maxb + 1
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n)
		a = make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a[i])
		}
		fmt.Println(solve(n, a))
	}
}

package main

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

func Min(x, y int) int {
    if x < y {
        return x
    } else {
        return y
    }
}

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

func solve(n int, a []int) int {
    ans := n
    mp := make(map[int]int)
    for i := n-1; i >= 0; i-- {
        if _, ok := mp[a[i]]; ok {
            break
        }
        mp[a[i]]++
        ans--
    }
    return ans
}

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)
        a := make([]int, n)
        for j := 0; j < n; j++ {
            fmt.Fscan(in, &a[j])
        }

        ans := solve(n, a)
        fmt.Fprintln(out, ans)
    }
}

package main

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

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

func (in *CF1714B) 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 *CF1714B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1714B
Date: 3/08/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1714/B
Problem: CF1714B
**/
func (in *CF1714B) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		arr := make([]int, n)

		for i := 0; i < n; i++ {
			arr[i] = in.NextInt()
		}
		mapa := make(map[int]bool)
		ans := 0
		for i := n - 1; i >= 0; i-- {
			_, e := mapa[arr[i]]
			if !e {
				mapa[arr[i]] = true
			} else {
				ans = i + 1
				break
			}
		}

		fmt.Println(ans)
	}
}

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

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

package main

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

func main() {
	// START WRITING YOUR SOLUTION HERE:
	// writeLine(typeOf(readInt()))
	n := readInt()
	/*for i := 0; i < n; i++ {
		tmp := readInts()
		bedT := tmp[1]*60 + tmp[2]
		var times []int
		f := false
		for j := 0; j < tmp[0]; j++ {
			tt := readInts()
			t := tt[0]*60 + tt[1]
			times = append(times, t)
			if t == bedT {
				f = true
				break
			}
		}
		if f || len(times) == 0 {
			writeLine("0 0")
		} else {
			sort.Ints(times)
			//writeLine(times)
			if times[len(times)-1] < bedT {
				d := 1440 - bedT
				//writeLine(bedT, times[0])
				writeLine(fmt.Sprintf("%d %d", (times[0]+d)/60, (times[0]+d)%60))
			} else {
				for i := 0; i < len(times); i++ {
					if times[i] > bedT {
						writeLine(fmt.Sprintf("%d %d", (times[i]-bedT)/60, (times[i]-bedT)%60))
						break
					}
				}
			}
		}
	}*/
	for i := 0; i < n; i++ {
		m := readInt()
		seq := readInts()
		mm := make(map[int]int)
		mmm := make(map[int]struct{})
		for j := 0; j < m; j++ {
			mm[seq[j]]++
			if _, ok := mmm[seq[j]]; !ok && mm[seq[j]] > 1 {
				mmm[seq[j]] = struct{}{}
			}
		}
		if len(mmm) == 0 {
			writeLine("0")
		} else {
			for j := 0; j < m; j++ {
				mm[seq[j]]--
				if _, ok := mmm[seq[j]]; ok && mm[seq[j]] == 1 {
					delete(mmm, seq[j])
				}
				if len(mmm) == 0 {
					writeLine(fmt.Sprintf("%d", j+1))
					break
				}
			}
		}
	}
}

/* ~~~~~~~~~~ */
/* ~~~~~~~~~~ */
/* ~~~~~~~~~~ */

var in = bufio.NewReader(os.Stdin)

type float = float32
type double = float64
type long = int64
type ulong = uint64

func typeOf(arg interface{}) string {
	switch reflect.TypeOf(arg).Kind() {
	case reflect.Int:
		return "int"
	case reflect.Int8:
		return "int8"
	case reflect.Int16:
		return "int16"
	case reflect.Int32:
		return "int32"
	case reflect.Int64:
		return "int64"
	case reflect.Uint:
		return "uint"
	case reflect.Uint8:
		return "uint8"
	case reflect.Uint16:
		return "uint16"
	case reflect.Uint32:
		return "uint32"
	case reflect.Uint64:
		return "uint64"
	case reflect.Float32:
		return "float32"
	case reflect.Float64:
		return "float64"
	case reflect.Complex64:
		return "complex64"
	case reflect.Complex128:
		return "complex128"
	case reflect.Bool:
		return "bool"
	case reflect.String:
		return "string"
	case reflect.Chan:
		return "chan"
	case reflect.Ptr:
		return "ptr"
	case reflect.Uintptr:
		return "uintptr"
	case reflect.UnsafePointer:
		return "unsafeptr"
	case reflect.Interface:
		return "interface"
	case reflect.Array:
		return "array"
	case reflect.Slice:
		return "slice"
	case reflect.Map:
		return "map"
	case reflect.Struct:
		return "struct"
	case reflect.Func:
		return "func"
	case reflect.Invalid:
		return "invalid"
	default:
		return "nil"
	}
}

/* ~~~~~~~~~~ */

func _scln() string {
	ln, _ := in.ReadString('\n')
	return strings.Trim(ln, " \r\n")
}

func _sc(s []string) string {
	if len(s) == 0 {
		return _scln()
	}
	return s[0]
}

func readLine() string              { return _scln() }
func readString() string            { return _scln() }
func readStrings() []string         { return strings.Split(_scln(), " ") }
func readBool(s ...string) bool     { t, _ := strconv.ParseBool(_sc(s)); return t }
func readByte(s ...string) byte     { t, _ := strconv.ParseUint(_sc(s), 10, 8); return byte(t) }
func readDouble(s ...string) double { t, _ := strconv.ParseFloat(_sc(s), 64); return t }
func readFloat(s ...string) float   { t, _ := strconv.ParseFloat(_sc(s), 32); return float32(t) }
func readInt(s ...string) int       { t, _ := strconv.Atoi(_sc(s)); return t }
func readLong(s ...string) long     { t, _ := strconv.ParseInt(_sc(s), 10, 64); return t }
func readULong(s ...string) ulong   { t, _ := strconv.ParseUint(_sc(s), 10, 64); return t }

/* ~~~~~~~~~~ */

// Too much overhead with universal method using interface{}, wait for the generics,
// but for now — just don't look at the redundancy of the code after that commentary :)

func readBools() []bool {
	strs := readStrings()
	arr := make([]bool, len(strs))
	for i, s := range strs {
		arr[i] = readBool(s)
	}
	return arr
}
func readBytes() []byte {
	strs := readStrings()
	arr := make([]byte, len(strs))
	for i, s := range strs {
		arr[i] = readByte(s)
	}
	return arr
}
func readDoubles() []double {
	strs := readStrings()
	arr := make([]double, len(strs))
	for i, s := range strs {
		arr[i] = readDouble(s)
	}
	return arr
}
func readFloats() []float {
	strs := readStrings()
	arr := make([]float, len(strs))
	for i, s := range strs {
		arr[i] = readFloat(s)
	}
	return arr
}
func readInts() []int {
	strs := readStrings()
	arr := make([]int, len(strs))
	for i, s := range strs {
		arr[i] = readInt(s)
	}
	return arr
}
func readLongs() []long {
	strs := readStrings()
	arr := make([]long, len(strs))
	for i, s := range strs {
		arr[i] = readLong(s)
	}
	return arr
}
func readULongs() []ulong {
	strs := readStrings()
	arr := make([]ulong, len(strs))
	for i, s := range strs {
		arr[i] = readULong(s)
	}
	return arr
}

/* ~~~~~~~~~~ */

func write(arg ...interface{})                 { fmt.Print(arg...) }
func writeLine(arg ...interface{})             { fmt.Println(arg...) }
func writeFormat(f string, arg ...interface{}) { fmt.Printf(f, arg...) }

package main

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

var in = bufio.NewReader(os.Stdin)

func main() {
	var T int
	fmt.Fscan(in, &T)
	for i := 0; i < T; i++ {
		var n int
		fmt.Fscan(in, &n)
		a := make([]int, n)
		for k := 0; k < n; k++ {
			fmt.Fscan(in, &a[k])
		}
		mp := make(map[int]bool)
		res := n
		for k := n - 1; k >= 0; k-- {
			if !mp[a[k]] {
				res--
				mp[a[k]] = true
			} else {
				break
			}
		}
		fmt.Println(res)
	}
}

package main

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

func solve() {
	n := get()
	a := geta1(n)
	c := ints(n, 0)
	for _, v := range a {
		c[v]++
	}
	count := 0
	for _, v := range c {
		if v < 2 {
			continue
		}
		count++
	}
	i := 0
	for count > 0 {
		x := a[i]
		c[x]--
		if c[x] == 1 {
			count--
		}
		i++
	}
	putln(i)
}

const (
	once       = false
	maxBufSize = 0 //10_000_000
	YES        = "YES"
	NO         = "NO"
)

var (
	splitFunc = bufio.ScanWords //bufio.SplitFunc(nil)
)

func main() {
	in = bufio.NewScanner(bufio.NewReader(os.Stdin))
	if splitFunc != nil {
		in.Split(splitFunc)
	}
	if maxBufSize > 0 {
		in.Buffer(make([]byte, maxBufSize), maxBufSize)
	}

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

	if once {
		solve()
		return
	}

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

var (
	in  *bufio.Scanner
	out *bufio.Writer
)

func get() int {
	x, _ := strconv.Atoi(gets())
	return x
}

func geta(n int) []int {
	a := make([]int, n)
	for i := range a {
		a[i] = get()
	}
	return a
}

func geta1(n int) []int {
	a := make([]int, n)
	for i := range a {
		a[i] = get() - 1
	}
	return a
}

func get64() int64 {
	x, _ := strconv.ParseInt(gets(), 10, 64)
	return x
}

func get64a(n int) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = get64()
	}
	return a
}

func get64a1(n int) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = get64() - 1
	}
	return a
}

func getf() float64 {
	x, _ := strconv.ParseFloat(gets(), 64)
	return x
}

func getfa(n int) []float64 {
	a := make([]float64, n)
	for i := range a {
		a[i] = getf()
	}
	return a
}

func gets() string {
	scan()
	return in.Text()
}

func scanN(n int) {
	for n > 0 {
		scan()
		n--
	}
}

func scan() {
	in.Scan()
}

func putc(c rune) {
	out.WriteRune(c)
}

func line() {
	putc('\n')
}

func space() {
	putc(' ')
}

func put(x int) {
	out.WriteString(strconv.Itoa(x))
}

func putln(i int) {
	put(i)
	line()
}

func puta(a []int) {
	for _, v := range a {
		put(v)
		space()
	}
	line()
}

func put64(x int64) {
	out.WriteString(strconv.FormatInt(x, 10))
}

func put64ln(x int64) {
	put64(x)
	line()
}

func put64a(a []int64) {
	for _, v := range a {
		put64(v)
		space()
	}
	line()
}

func puts(s string) {
	out.WriteString(s)
}

func putsln(s string) {
	puts(s)
	line()
}

func yn(y bool) {
	if y {
		yes()
		return
	}
	no()
}

func yes() {
	puts(YES)
	line()
}

func no() {
	puts(NO)
	line()
}

func putf(x float64) {
	out.WriteString(strconv.FormatFloat(x, 'f', -1, 64))
}

func putfln(x float64) {
	putf(x)
	line()
}

func putfa(a []float64) {
	for _, v := range a {
		putf(v)
		space()
	}
	line()
}

type IntTp interface {
	int | int64
}

type Tp interface {
	IntTp | float64
}

func abs[T IntTp](x T) T {
	if x < 0 {
		return -x
	}
	return x
}

func fabs(x float64) float64 {
	return math.Abs(x)
}

func max1[T Tp](x T, y T) T {
	if x > y {
		return x
	}
	return y
}

func max[T Tp](xs ...T) T {
	r := xs[0]
	for _, x := range xs[1:] {
		r = max1(r, x)
	}
	return r
}

func maxa[T Tp](a []T) (i int, val T) {
	i, val = 0, a[0]
	for j := 1; j < len(a); j++ {
		if a[j] <= val {
			continue
		}
		i = j
		val = a[j]
	}
	return
}

func min1[T Tp](x T, y T) T {
	if x < y {
		return x
	}
	return y
}

func min[T Tp](xs ...T) T {
	r := xs[0]
	for _, x := range xs[1:] {
		r = min1(r, x)
	}
	return r
}

func mina[T Tp](a []T) (i int, val T) {
	i, val = 0, a[0]
	for j := 1; j < len(a); j++ {
		if a[j] >= val {
			continue
		}
		i = j
		val = a[j]
	}
	return
}

func L(i int) int64 {
	return int64(i)
}

func F(i int) float64 {
	return float64(i)
}

func ints(n int, val int) (a []int) {
	a = make([]int, n)
	if val == 0 {
		return
	}
	for i := range a {
		a[i] = val
	}
	return
}

func int64s(n int, val int64) (a []int64) {
	a = make([]int64, n)
	if val == 0 {
		return
	}
	for i := range a {
		a[i] = val
	}
	return
}

func floats(n int, val float64) (a []float64) {
	a = make([]float64, n)
	if val == 0 {
		return
	}
	for i := range a {
		a[i] = val
	}
	return
}

const heapBase = 4

func children(i int) (int, int) {
	start := heapBase * i
	return start + 1, start + (1 + heapBase)
}

func parent(i int) int {
	return (i - 1) / heapBase
}

package main

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

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

func scan(args ...*int) {

	for _, arg := range args {
		scanner.Scan()
		*arg, _ = strconv.Atoi(scanner.Text())
	}
}

func print(args ...interface{}) {
	writter.WriteString(fmt.Sprintln(args...))
}

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

func main() {
	scanner.Split(bufio.ScanWords)
	defer writter.Flush()
	var t int

	scan(&t)

	var n int

	for i := 0; i < t; i++ {
		scan(&n)
		a := make([]int, n, n)
		for j := 0; j < n; j++ {
			scan(&a[j])
		}

		aa := make(map[int]bool)
		for j := len(a) - 1; j >= 0; j-- {
			if aa[a[j]] == true {
				print(j + 1)
				break
			}
			aa[a[j]] = true
			if j == 0 {
				print(0)
			}
		}
	}
}

// 1714B. Remove Prefix
package main

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

func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n int
		fmt.Fscan(reader, &n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a[i])
		}
		m := make(map[int]bool)
		ans := 0
		for i := n - 1; i >= 0; i-- {
			if m[a[i]] {
				ans = i + 1
				break
			} else {
				m[a[i]] = true
			}
		}
		fmt.Println(ans)
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var t,N,a,anc int

	fmt.Fscan(in, &t)
	for i := 0;i < t;i++{
		fmt.Fscan(in, &N)
		m := make(map[int]int)
		anc = -1
		for j := 0;j < N;j++{
			fmt.Fscan(in, &a)
			if _,ok := m[a];ok{
				if anc < m[a]{
					anc = m[a]
				}
				m[a] = j
			}else{
				m[a] = j
			}

		}
		if anc == -1{
			fmt.Println(0)
		}else{
			fmt.Println(anc+1)
		}

	}

}

package main

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

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

func solve() string {
	var n int
	fmt.Fscan(in, &n)
	nums := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &nums[i])
	}

	cnt := 0
	set := make(map[int]bool)
	for i := n - 1; i >= 0; i-- {
		if exists := set[nums[i]]; exists {
			break
		}
		set[nums[i]] = true
		cnt++
	}

	return strconv.FormatInt(int64(n-cnt), 10)
}

func main() {
	defer out.Flush()
	var t int
	for fmt.Fscanln(in, &t); t > 0; t-- {
		fmt.Fprintln(out, solve())
	}
}

// util functions
func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

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

func makeMatrix(n, m int) [][]int {
	x := make([][]int, n)
	for i := range x {
		x[i] = make([]int, m)
	}
	return x
}

package main

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

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

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

func gcd(a, b int64) int64 {
	if a < b {
		a, b = b, a
	}
	if a%b == 0 {
		return b
	} else {
		return gcd(b, a%b)
	}
}

func main() {
	scanner := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var niter int
	fmt.Fscan(scanner, &niter)
	for iiter := 0; iiter < niter; iiter++ {
		var n int
		fmt.Fscan(scanner, &n)
		slc := make([]int, n)
		m := make(map[int]bool)
		l := 0
		for i := 0; i < n; i++ {
			fmt.Fscan(scanner, &slc[i])
		}
		for i := n - 1; i >= 0; i-- {
			if _, ok := m[slc[i]]; ok {
				break
			} else {
				m[slc[i]] = true
				l++
			}
		}
		fmt.Fprintln(writer, n-l)
	}
}

package main

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

func readLine() string {
	s, err := f.ReadString('\n')
	if err != nil {
		panic(err)
	}
	s = strings.ReplaceAll(s, "\n", "")
	s = strings.ReplaceAll(s, "\r", "")
	return s
}

func readInts(n int) []int {
	s := readLine()
	as := strings.SplitN(s, " ", n)
	if len(as) != n {
		panic("invalid len")
	}
	arr := make([]int, n)
	for k, a := range as {
		x, _ := strconv.ParseInt(a, 10, 64)
		arr[k] = int(x)
	}
	return arr
}

var f = bufio.NewReader(os.Stdin)

func main() {
	var t, n int

	l1 := readInts(1)
	t = l1[0]

outer:
	for i := 0; i < t; i++ {
		l1 = readInts(1)
		n = l1[0]

		arr := readInts(n)

		mp := make(map[int]bool, n)
		for j := len(arr) - 1; j >= 0; j-- {
			if mp[arr[j]] {
				fmt.Println(j + 1)
				continue outer
			}
			mp[arr[j]] = true
		}
		fmt.Println(0)
	}
}

package main
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:=n-1;j>n-len(list);j--{
			for k:=j-1;k>=0;k--{
				if list[j] == list[k] {
					list = list[k+1:]
					j-=k
					break
				}
			}
		}
		fmt.Fprintln(out,n-len(list))
	}
}

package main
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:=n-1;j>0;j--{
			for k:=j-1;k>=0;k--{
				if list[j] == list[k] {
					list=list[k+1:]
					goto out
				}
			}
		}
		out:
		fmt.Fprintln(out,n-len(list))
	}
}

package main

import (

	"bufio"
	"os"
	"fmt"
)

var (

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

func main() {

	defer out.Flush()

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

		var n int64
		fmt.Fscan(in, &n)
		var ans = int64(-1)
		var mp map[int64]int64 = map[int64]int64{}
		for j := 0; int64(j) < n; j++ {

			var x int64
			fmt.Fscan(in, &x)
			if val, ok := mp[x]; ok {

				ans = val
			}
			mp[x] = int64(j)
		}
		fmt.Fprintln(out, ans + 1)
	}
}

package main

import (

	"bufio"
	"os"
	"fmt"
)

var (

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

func main() {

	defer out.Flush()

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

		var n int
		fmt.Fscan(in, &n)
		var ans = -1
		var mp map[int]int = map[int]int{}
		for j := 0; j < n; j++ {

			var x int
			fmt.Fscan(in, &x)
			if val, ok := mp[x]; ok {

				ans = val
			}
			mp[x] = j
		}
		fmt.Fprintln(out, ans + 1)
	}
}

package main

import (

	"bufio"
	"os"
	"fmt"
)

var (

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

func main() {

	defer out.Flush()

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

		fmt.Fscan(in, &n)
		var ans = -1
		var mp map[int]int = map[int]int{}
		for j := 0; j < n; j++ {

			var x int
			fmt.Fscan(in, &x)
			if val, ok := mp[x]; ok {

				ans = val
			}
			mp[x] = j
		}
		fmt.Fprintln(out, ans + 1)
	}
}

package main

import (
	"fmt"
)

func Max(x, y int) int {
	if x < y {
		return y
	}
	return x
}
func Min(x, y int) int {
	if x > y {
		return y
	}
	return x
}

func cp() {
	var N int
	fmt.Scanf("%d\n", &N)
	var A []int = make([]int, N)
	for i := range A {
		fmt.Scanf("%d", &A[i])
	}
	unique := 0
	numberExists := make(map[int]bool)
	for i := N - 1; i >= 0; i-- {
		if _, exists := numberExists[A[i]]; exists {
			break
		}
		unique++
		numberExists[A[i]] = true
	}

	fmt.Println(N - unique)
}

func main() {
	t := 1
	fmt.Scanf("%d\n", &t)
	for ; t > 0; t-- {
		cp()
	}
}

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)

		aa := make([]int, n+1)
		for i := 0; i < n+1; i++ {
			aa[i] = -1
		}
		var d int
		var f bool
		for i := 0; i < n; i++ {
			var v int
			fmt.Fscan(in, &v)
			if aa[v] >= 0 {
				d = aa[v]
				f = true
			}
			aa[v] = i
		}
		if !f {
			fmt.Fprintln(out, 0)
		} else {
			fmt.Fprintln(out, d+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)

		aa := make([]int, n+1)
		for i := 0; i < n+1; i++ {
			aa[i] = -1
		}
		d := -1
		for i := 0; i < n; i++ {
			var v int
			fmt.Fscan(in, &v)
			if aa[v] >= 0 {
				d = aa[v]
			}
			aa[v] = i
		}
		if d < 0 {
			d = 0
		} else {
			d++
		}
		fmt.Fprintln(out, d)
	}
}

// B. Удали префикс
// https://codeforces.com/contest/1714/problem/B
package main

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

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

	var CountData int
	fmt.Fscan(in, &CountData)

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

		MapElem := make(map[int]int)
		EndInd := -1

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

			if Ind, ok := MapElem[Elem]; ok {
				EndInd = Ind
			}
			MapElem[Elem] = n
		}

		fmt.Fprintln(out, 1+EndInd)
	}
}