Ivan is collecting coins. There are only $$$N$$$ different collectible coins, Ivan has $$$K$$$ of them. He will be celebrating his birthday soon, so all his $$$M$$$ freinds decided to gift him coins. They all agreed to three terms:  Everyone must gift as many coins as others. All coins given to Ivan must be different. Not less than $$$L$$$ coins from gifts altogether, must be new in Ivan's collection.But his friends don't know which coins have Ivan already got in his collection. They don't want to spend money so they want to buy minimum quantity of coins, that satisfy all terms, irrespective of the Ivan's collection. Help them to find this minimum number of coins or define it's not possible to meet all the terms.

The only line of input contains 4 integers $$$N$$$, $$$M$$$, $$$K$$$, $$$L$$$ ($$$1 \le K \le N \le 10^{18}$$$; $$$1 \le M, \,\, L \le 10^{18}$$$) — quantity of different coins, number of Ivan's friends, size of Ivan's collection and quantity of coins, that must be new in Ivan's collection.

Print one number — minimal number of coins one friend can gift to satisfy all the conditions. If it is impossible to satisfy all three conditions print "-1" (without quotes).

NoteIn the first test, one coin from each friend is enough, as he will be presented with 15 different coins and 13 of them will definitely be new.In the second test, Ivan has 11 friends, but there are only 10 different coins. So all friends can't present him different coins.

package main

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

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

func main() {
  in = bufio.NewReader(os.Stdin)
  out = bufio.NewWriter(os.Stdout)
  var N,M,K,L int64
  fmt.Fscanln(in, &N,&M,&K,&L)
  L += K
  ans := L/M
  if ans*M != L {
    ans++
  }

  if ans*M <= N {
    fmt.Println(ans)
  } else {
    fmt.Println(-1)
  }

  return
}

package main

import (
	"fmt"
)

func main() {
	var N, M, K, L int64
	fmt.Scan(&N, &M, &K, &L)
	minCount := K + L
	if minCount > N {
		// Not enough coins to meet condition 3
		fmt.Println(-1)
	} else {
		// Cannot use math.Ceil here because float64 is not that acurate
		eachBuy := minCount / M
		if minCount%M > 0 {
			eachBuy++
		}
		// Not enough coins to meet condition 1
		if eachBuy*M > N {
			fmt.Println(-1)
		} else {
			fmt.Println(eachBuy)
		}
	}
}

package main

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

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

func main() {
  in = bufio.NewReader(os.Stdin)
  out = bufio.NewWriter(os.Stdout)
  var N,M,K,L int64
  fmt.Fscanln(in, &N,&M,&K,&L)
  //fmt.Println(N,M,K,L)
  if N < M {
    fmt.Println(-1)
    return
  }

  var ans int64
  if L%M == 0 {
    ans = L/M
  } else {
    ans = L/M + 1
  }
  fmt.Println(ans)
  return
}

package main

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

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

func main() {
  in = bufio.NewReader(os.Stdin)
  out = bufio.NewWriter(os.Stdout)
  var N,M,K,L int64
  fmt.Fscanln(in, &N,&M,&K,&L)
  //fmt.Println(N,M,K,L)
  if N < M || L > N {
    fmt.Println(-1)
    return
  }
  L += K
  var ans int64
  if L%M == 0 {
    fmt.Println(L/M)
  } else {
    ans = L/M +1
    if ans*M > N {
     fmt.Println(-1)
    } else {
     fmt.Println(ans)
    }
  }

  return
}

package main

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

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

func main() {
  in = bufio.NewReader(os.Stdin)
  out = bufio.NewWriter(os.Stdout)
  var N,M,K,L int64
  fmt.Fscanln(in, &N,&M,&K,&L)
  //fmt.Println(N,M,K,L)
  if N < M || L > N {
    fmt.Println(-1)
    return
  }

  var ans int64
  if L%M == 0 {
    fmt.Println(L/M)
  } else {
    ans = L/M +1
    if ans*M > N {
     fmt.Println(-1)
    } else {
     fmt.Println(ans)
    }
  }

  return
}

package main

import (
	"fmt"
	"math"
)

func main() {
	var N, M, K, L int64
	fmt.Scan(&N, &M, &K, &L)
	minCount := K + L
	if minCount > N {
		// Not enough coins to meet condition 3
		fmt.Println(-1)
	} else {
		eachBuy := int64(math.Ceil(float64(minCount) / float64(M)))
		// Not enough coins to meet condition 1
		if eachBuy*M > N {
			fmt.Println(-1)
		} else {
			fmt.Println(eachBuy)
		}
	}
}


The only difference between easy and hard versions is constraints.You are given a sequence $$$a$$$ consisting of $$$n$$$ positive integers.Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are $$$a$$$ and $$$b$$$, $$$a$$$ can be equal $$$b$$$) and is as follows: $$$[\underbrace{a, a, \dots, a}_{x}, \underbrace{b, b, \dots, b}_{y}, \underbrace{a, a, \dots, a}_{x}]$$$. There $$$x, y$$$ are integers greater than or equal to $$$0$$$. For example, sequences $$$[]$$$, $$$[2]$$$, $$$[1, 1]$$$, $$$[1, 2, 1]$$$, $$$[1, 2, 2, 1]$$$ and $$$[1, 1, 2, 1, 1]$$$ are three block palindromes but $$$[1, 2, 3, 2, 1]$$$, $$$[1, 2, 1, 2, 1]$$$ and $$$[1, 2]$$$ are not.Your task is to choose the maximum by length subsequence of $$$a$$$ that is a three blocks palindrome.You have to answer $$$t$$$ independent test cases.Recall that the sequence $$$t$$$ is a a subsequence of the sequence $$$s$$$ if $$$t$$$ can be derived from $$$s$$$ by removing zero or more elements without changing the order of the remaining elements. For example, if $$$s=[1, 2, 1, 3, 1, 2, 1]$$$, then possible subsequences are: $$$[1, 1, 1, 1]$$$, $$$[3]$$$ and $$$[1, 2, 1, 3, 1, 2, 1]$$$, but not $$$[3, 2, 3]$$$ and $$$[1, 1, 1, 1, 2]$$$.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2000$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 26$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. Note that the maximum value of $$$a_i$$$ can be up to $$$26$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2000$$$ ($$$\sum n \le 2000$$$).

For each test case, print the answer — the maximum possible length of some subsequence of $$$a$$$ that is a three blocks palindrome.

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }
// https://www.codementor.io/@tucnak/using-golang-for-competitive-programming-h8lhvxzt3

func main() {
	var T int
	scanf("%d\n", &T)
	for tt := 0; tt < T; tt++ {
		var N int
		scanf("%d\n", &N)
		//fmt.Printf("%d test case has N=%d\n", tt, N)

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

		ppsum := make([][]int, 27)

		for i := 1; i < 27; i++ {
			ppsum[i] = make([]int, N + 2)
			ppsum[i][0] = 0

			for j, e := range arr {
				if e == i {
					ppsum[i][j + 1] = ppsum[i][j] + 1
				} else {
					ppsum[i][j + 1] = ppsum[i][j]
				}
			}

			ppsum[i][N+1] = ppsum[i][N]
		}

		csum := func(l int, r int, e int) int {
			return ppsum[e][r] - ppsum[e][l - 1]
		}

		min := func(l int, r int) int {
			if l < r {
				return l
			} else {
				return r
			}
		}

		ans := 0

		for i, _ := range arr {
			for j, _ := range arr {
				if j < i {
					continue
				}

				m := 0

				for k := 1; k < 27; k++ {
					if tmp := csum(i+1, j+1, k); tmp > m {
						m = tmp
					}
				}

				r := 0

				for k := 1; k < 27; k++ {
					if tmp := min(csum(1, i, k), csum(j+2, N+1, k)); tmp > r {
						r = tmp
					}
				}

				if r * 2 + m > ans {
					ans = r * 2 + m
				}
			}
		}

		defer writer.Flush()
		printf("%d\n", ans)
	}
}

package main

import (
	"fmt"
	"os"
)

func solve() {

	var n int
	fmt.Scan(&n)

	input := make([]int, n)

	for i := 0; i < n; i++{
		fmt.Scan(&input[i])
	}

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

	for i := 0; i < 26; i++ {
		for j := 0; j < n; j++ {
			if input[j] - 1 == i {
				freq[i][j]++
			}
			if j - 1 > -1 {
				freq[i][j] += freq[i][j - 1]
			}
		}
	}

	ans := 0
	for i := 0; i < 26; i++ {
		ans = max(ans, freq[i][n - 1])
	}

	for l := 1; l < n - 1; l++ {
		for r := l; r < n - 1 ; r++ {
			edge, mid := 0, 0

			for x := 0; x < 26; x++ {
				edge = max(edge, min(freq[x][l - 1], freq[x][n - 1] - freq[x][r]))
				mid = max(mid, freq[x][r] - freq[x][l - 1])
			}
			ans = max(ans, 2 * edge + mid)
		}
	}
	fmt.Println(ans)

}

func main() {

	in, err := os.Open("input.txt")
	if err == nil {
		os.Stdin = in
	}

	testcase := 1
	fmt.Scan(&testcase)

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

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

package main

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

func solve() {

	n := fs()
	input := make([]int, n)

	for i := 0; i < n; i++{
		input[i] = fs()
	}

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

	for i := 0; i < 26; i++ {
		for j := 0; j < n; j++ {
			if input[j] - 1 == i {
				freq[i][j]++
			}
			if j - 1 > -1 {
				freq[i][j] += freq[i][j - 1]
			}
		}
	}

	ans := 0
	for i := 0; i < 26; i++ {
		ans = max(ans, freq[i][n - 1])
	}

	for l := 1; l < n - 1; l++ {
		for r := l; r < n - 1 ; r++ {
			edge, mid := 0, 0

			for x := 0; x < 26; x++ {
				edge = max(edge, min(freq[x][l - 1], freq[x][n - 1] - freq[x][r]))
				mid = max(mid, freq[x][r] - freq[x][l - 1])
			}
			ans = max(ans, 2 * edge + mid)
		}
	}
	fmt.Println(ans)

}

func main() {

	in, err := os.Open("input.txt")
	if err == nil {
		os.Stdin = in
	}

	bytes, _ = ioutil.ReadAll(bufio.NewReader(os.Stdin))
	maxV = len(bytes) - 1

	testcase := 1
	testcase = fs();

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

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

var bytes []byte
var l, maxV int

func fs() int {
	b := bytes[l]

	for b < 48 || b > 57 {
		l++
		b = bytes[l]
	}

	result := 0
	for 47 < b && b < 58 {
		result = (result << 3) + (result << 1) + int(b-48)

		l++
		if l > maxV {
			return result
		}
		b = bytes[l]
	}
	return result
}

package main

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

type BufferedWriter interface {
    Printf(format string, a ...interface{})
    Flush()
}

type writerImpl struct {
    *bufio.Writer
}

func NewBufferedWriter(writer io.Writer) BufferedWriter {
    return &writerImpl{Writer: bufio.NewWriter(writer)}
}

func (impl *writerImpl) Printf(f string, a ...interface{}) {
    fmt.Fprintf(impl.Writer, f, a...)
}

func (impl *writerImpl) Flush() {
    impl.Writer.Flush()
}

type WordScanner interface {
    NextInt() int
    NextInt64() int64
    NextString() string
}

type wordScannerImpl struct {
    *bufio.Scanner
}

func NewWordScanner(reader io.Reader) WordScanner {
    s := bufio.NewScanner(reader)
    s.Split(bufio.ScanWords)
    // adjust the following size as needed
    // 2 << 17 is good enough to scan strings of len 2*10^5
    // it could go as high as 2 << 26 before cf judge breaks
    size := 2 << 20
    buf := make([]byte, size)
    s.Buffer(buf, size)
    return &wordScannerImpl{Scanner: s}
}

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

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

func (impl *wordScannerImpl) NextString() string {
    impl.Scan()
    return impl.Text()
}

var reader = NewWordScanner(os.Stdin)
var writer = NewBufferedWriter(os.Stdout)

func main() {
    defer writer.Flush()
    t := reader.NextInt64()
    for ; t > 0; t -- {
        n := reader.NextInt64()
        aa := []int64{}
        for ; n > 0; n-- {
            aa = append(aa, reader.NextInt64())
        }
        writer.Printf("%d\n", sol(aa))
    }
}

func sol(aa []int64) int64 {
    maxSoFar := int64(0)
    for i := int64(1); i <= 200; i++ {
        for j := int64(1); j <= 200; j++ {
            newArr := []int64{}
            count0 := int64(0)
            count1 := int64(0)
            for _, a := range aa {
                if a == i {
                    newArr = append(newArr, 0)
                    count0++
                } else if a == j {
                    newArr = append(newArr, 1)
                    count1++
                }
            }
            if len(newArr) > 0 {
                maxSoFar = max(maxSoFar, solve(newArr, count0, count1))
            }
        }
    }
    if maxSoFar == 0 {
        return 1
    }
    return maxSoFar
}

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

func solve(arr []int64, count0, count1 int64) int64 {
    lp := -1
    rp := len(arr)
    sideCount := int64(0)
    midCount := count1
    maxSoFar := midCount
    for {
        lp++
        for lp < len(arr) && arr[lp] != 0 {
            midCount--
            lp++
        }
        rp--
        for rp > -1 && arr[rp] != 0 {
            midCount--
            rp--
        }
        if lp >= rp {
            break
        }
        sideCount++
        maxSoFar = max(maxSoFar, sideCount*2 + midCount)
    }
    return maxSoFar
}

package main

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

func solve() {

	n := fs()

	input := make([]int, n+1)
	pos := make([][]int, 201)
	freq := make([][]int, 201)

	for i := 1; i < 201; i++ {
		pos[i] = make([]int, 0)
		freq[i] = make([]int, n+1)
	}
	for i := 1; i <= n; i++ {
		input[i] = fs()
		pos[input[i]] = append(pos[input[i]], i)
	}

	for i := 1; i < 201; i++ {
		for j := 1; j <= n; j++ {
			if input[j] == i {
				freq[i][j]++
			}
			freq[i][j] += freq[i][j-1]
		}
	}

	ans := 0
	for i := 1; i < 201; i++ {
		ans = max(ans, freq[i][n])
	}
	var left, right, l, r int

	for i := 1; i < 201; i++ {

		m := len(pos[i])
		left, right = 0, m-1

		for left < right {
			l = pos[i][left]
			r = pos[i][right]
			mid := 0
			for j := 1; j < 201; j++ {
				if i == j {
					continue
				}
				mid = max(mid, freq[j][r-1]-freq[j][l])
			}
			ans = max(ans, mid+2*(left+1))
			left++
			right--
		}
	}
	fmt.Println(ans)
}

func main() {

	in, err := os.Open("input.txt")
	if err == nil {
		os.Stdin = in
	}
	bytes, _ = ioutil.ReadAll(bufio.NewReader(os.Stdin))
	maxV = len(bytes) - 1

	testcase := fs()

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

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

var bytes []byte
var l, maxV int

func fs() int {
	b := bytes[l]

	for b < 48 || b > 57 {
		l++
		b = bytes[l]
	}

	result := 0
	for 47 < b && b < 58 {
		result = (result << 3) + (result << 1) + int(b-48)

		l++
		if l > maxV {
			return result
		}
		b = bytes[l]
	}
	return result
}

package main

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

const M = 200
const N = 200010
var nt int
var n int
var a[N] int

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

func main()  {
	var reader = bufio.NewReader(os.Stdin)
	var writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	fmt.Fscanf(reader, "%d\n", &nt)
	for kk := 0; kk < nt; kk++ {
		fmt.Fscanf(reader, "%d\n", &n)
		for i := 1; i <= n; i++ {
			fmt.Fscanf(reader, "%d", &a[i])
			a[i]--
		}
		fmt.Fscanf(reader, "\n")

		var at = make([][]int, M)
		for i := 0; i < M; i++ {
			at[i] = make([]int, 0)
		}

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

		for i := 1; i <= n; i++ {
			for j := 0; j < M; j++ {
				prefix[j][i] = prefix[j][i - 1]
				if j == a[i] {
					prefix[j][i]++
				}
			}
			at[a[i]] = append(at[a[i]], i)
		}

		var ret = 0
		for i := 0; i < M; i++ {
			var z = prefix[i][n]
			ret = max(ret, z)
			for c := 1; c * 2 <= z; c++ {
				var from = at[i][c - 1] + 1
				var to = at[i][z - c] - 1

				for j := 0; j < M; j++ {
					ret = max(ret, 2 * c + prefix[j][to] - prefix[j][from - 1])
				}
			}
		}
		fmt.Fprintf(writer, "%d\n", ret)
	}

}

// Author: sighduck
// URL: https://codeforces.com/contest/1335/problem/E2

package main

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

const MaxElem = 200

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

func Solve(n int, a []int) int {
	count := make([][]int, MaxElem+1)
	for i := 0; i <= MaxElem; i++ {
		count[i] = make([]int, n)
	}
	indexes := make([][]int, MaxElem+1)
	for i, elem := range a {
		count[elem][i] += 1
		indexes[elem] = append(indexes[elem], i)
	}
	for i := 1; i <= MaxElem; i++ {
		for j := 1; j < n; j++ {
			count[i][j] += count[i][j-1]
		}
	}
	ans := 0
	for i := 1; i <= MaxElem; i++ {
		ans = max(ans, len(indexes[i]))
		for j := 0; j < len(indexes[i])/2; j++ {
			l := indexes[i][j] + 1
			r := indexes[i][len(indexes[i])-1-j] - 1
			for k := 1; k <= MaxElem; k++ {
				s := (j+1)*2 + count[k][r] - count[k][l-1]
				ans = max(ans, s)
			}
		}
	}
	return ans
}

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

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

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }
// https://www.codementor.io/@tucnak/using-golang-for-competitive-programming-h8lhvxzt3

func main() {
	var T int
	scanf("%d\n", &T)
	for tt := 0; tt < T; tt++ {
		var N int
		scanf("%d\n", &N)
		//fmt.Printf("%d test case has N=%d\n", tt, N)

		arr := make([]int, N)
		var a int
		for i := 0; i < N; i++ {
			if i != N - 1 {
				scanf("%d ", &a)
			} else {
				scanf("%d ", &a)
			}
			arr[i] = a
		}

		ppsum := make([][]int, 27)

		for i := 1; i < 27; i++ {
			ppsum[i] = make([]int, N + 2)
			ppsum[i][0] = 0

			for j, e := range arr {
				if e == i {
					ppsum[i][j + 1] = ppsum[i][j] + 1
				} else {
					ppsum[i][j + 1] = ppsum[i][j]
				}
			}

			ppsum[i][N+1] = ppsum[i][N]
		}

		csum := func(l int, r int, e int) int {
			return ppsum[e][r] - ppsum[e][l - 1]
		}

		min := func(l int, r int) int {
			if l < r {
				return l
			} else {
				return r
			}
		}

		ans := 0

		for i, _ := range arr {
			for j, _ := range arr {
				if j < i {
					continue
				}

				m := 0

				for k := 1; k < 27; k++ {
					if tmp := csum(i+1, j+1, k); tmp > m {
						m = tmp
					}
				}

				r := 0

				for k := 1; k < 27; k++ {
					if tmp := min(csum(1, i, k), csum(j+2, N+1, k)); tmp > r {
						r = tmp
					}
				}

				if r * 2 + m > ans {
					ans = r * 2 + m
				}
			}
		}

		defer writer.Flush()
		printf("%d\n", ans)
	}
}

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }
// https://www.codementor.io/@tucnak/using-golang-for-competitive-programming-h8lhvxzt3

func main() {
	var T int
	scanf("%d\n", &T)
	for tt := 0; tt < T; tt++ {
		var N int
		scanf("%d\n", &N)

		arr := make([]int, N)
		var a int
		for i := 0; i < N; i++ {
			scanf("%d", &a)
			arr[i] = a
		}

		ppsum := make([][]int, 27)

		for i := 1; i < 27; i++ {
			ppsum[i] = make([]int, N + 2)
			ppsum[i][0] = 0

			for j, e := range arr {
				if e == i {
					ppsum[i][j + 1] = ppsum[i][j] + 1
				} else {
					ppsum[i][j + 1] = ppsum[i][j]
				}
			}

			ppsum[i][N+1] = ppsum[i][N]
		}

		csum := func(l int, r int, e int) int {
			return ppsum[e][r] - ppsum[e][l - 1]
		}

		min := func(l int, r int) int {
			if l < r {
				return l
			} else {
				return r
			}
		}

		ans := 0

		for i, _ := range arr {
			for j, _ := range arr {
				if j < i {
					continue
				}

				m := 0

				for k := 1; k < 27; k++ {
					if tmp := csum(i+1, j+1, k); tmp > m {
						m = tmp
					}
				}

				r := 0

				for k := 1; k < 27; k++ {
					if tmp := min(csum(1, i, k), csum(j+2, N+1, k)); tmp > r {
						r = tmp
					}
				}

				if r * 2 + m > ans {
					ans = r * 2 + m
				}
			}
		}

		defer writer.Flush()
		printf("%d\n", ans)
	}
}

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }
// https://www.codementor.io/@tucnak/using-golang-for-competitive-programming-h8lhvxzt3

func main() {
	var T int
	scanf("%d\n", &T)
	for tt := 0; tt < T; tt++ {
		var N int
		scanf("%d\n", &N)

		arr := make([]int, N)
		var a int
		for i := 0; i < N; i++ {
			scanf("%d ", &a)
			arr[i] = a
		}

		ppsum := make([][]int, 27)

		for i := 1; i < 27; i++ {
			ppsum[i] = make([]int, N + 2)
			ppsum[i][0] = 0

			for j, e := range arr {
				if e == i {
					ppsum[i][j + 1] = ppsum[i][j] + 1
				} else {
					ppsum[i][j + 1] = ppsum[i][j]
				}
			}

			ppsum[i][N+1] = ppsum[i][N]
		}

		csum := func(l int, r int, e int) int {
			return ppsum[e][r] - ppsum[e][l - 1]
		}

		min := func(l int, r int) int {
			if l < r {
				return l
			} else {
				return r
			}
		}

		ans := 0

		for i, _ := range arr {
			for j, _ := range arr {
				if j < i {
					continue
				}

				m := 0

				for k := 1; k < 27; k++ {
					if tmp := csum(i+1, j+1, k); tmp > m {
						m = tmp
					}
				}

				r := 0

				for k := 1; k < 27; k++ {
					if tmp := min(csum(1, i, k), csum(j+2, N+1, k)); tmp > r {
						r = tmp
					}
				}

				if r * 2 + m > ans {
					ans = r * 2 + m
				}
			}
		}

		defer writer.Flush()
		printf("%d\n", ans)
	}
}

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }
// https://www.codementor.io/@tucnak/using-golang-for-competitive-programming-h8lhvxzt3

func main() {
	var T int
	scanf("%d\n", &T)
	for tt := 0; tt < T; tt++ {
		var N int
		scanf("%d\n", &N)
		//fmt.Printf("%d test case has N=%d\n", tt, N)

		arr := make([]int, N)
		var a int
		for i := 0; i < N; i++ {
			if i != N - 1 {
				scanf("%d ", &a)
			} else {
				scanf("%d", &a)
			}
			arr[i] = a
		}

		ppsum := make([][]int, 27)

		for i := 1; i < 27; i++ {
			ppsum[i] = make([]int, N + 2)
			ppsum[i][0] = 0

			for j, e := range arr {
				if e == i {
					ppsum[i][j + 1] = ppsum[i][j] + 1
				} else {
					ppsum[i][j + 1] = ppsum[i][j]
				}
			}

			ppsum[i][N+1] = ppsum[i][N]
		}

		csum := func(l int, r int, e int) int {
			return ppsum[e][r] - ppsum[e][l - 1]
		}

		min := func(l int, r int) int {
			if l < r {
				return l
			} else {
				return r
			}
		}

		ans := 0

		for i, _ := range arr {
			for j, _ := range arr {
				if j < i {
					continue
				}

				m := 0

				for k := 1; k < 27; k++ {
					if tmp := csum(i+1, j+1, k); tmp > m {
						m = tmp
					}
				}

				r := 0

				for k := 1; k < 27; k++ {
					if tmp := min(csum(1, i, k), csum(j+2, N+1, k)); tmp > r {
						r = tmp
					}
				}

				if r * 2 + m > ans {
					ans = r * 2 + m
				}
			}
		}

		defer writer.Flush()
		printf("%d\n", ans)
	}
}

package main

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

type BufferedWriter interface {
    Printf(format string, a ...interface{})
    Flush()
}

type writerImpl struct {
    *bufio.Writer
}

func NewBufferedWriter(writer io.Writer) BufferedWriter {
    return &writerImpl{Writer: bufio.NewWriter(writer)}
}

func (impl *writerImpl) Printf(f string, a ...interface{}) {
    fmt.Fprintf(impl.Writer, f, a...)
}

func (impl *writerImpl) Flush() {
    impl.Writer.Flush()
}

type WordScanner interface {
    NextInt() int
    NextInt64() int64
    NextString() string
}

type wordScannerImpl struct {
    *bufio.Scanner
}

func NewWordScanner(reader io.Reader) WordScanner {
    s := bufio.NewScanner(reader)
    s.Split(bufio.ScanWords)
    // adjust the following size as needed
    // 2 << 17 is good enough to scan strings of len 2*10^5
    // it could go as high as 2 << 26 before cf judge breaks
    size := 2 << 20
    buf := make([]byte, size)
    s.Buffer(buf, size)
    return &wordScannerImpl{Scanner: s}
}

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

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

func (impl *wordScannerImpl) NextString() string {
    impl.Scan()
    return impl.Text()
}

var reader = NewWordScanner(os.Stdin)
var writer = NewBufferedWriter(os.Stdout)

func main() {
    defer writer.Flush()
    t := reader.NextInt64()
    for ; t > 0; t -- {
        n := reader.NextInt64()
        aa := []int64{}
        for ; n > 0; n-- {
            aa = append(aa, reader.NextInt64())
        }
        writer.Printf("%d\n", sol(aa))
    }
}

func sol(aa []int64) int64 {
    prefixes := make([]map[int64]int64, len(aa)+1)
    currMap := map[int64]int64{}
    prefixes[0] = currMap
    for i, a := range aa {
        tmp := copyMap(currMap)
        tmp[a]++
        prefixes[i+1] = tmp
        currMap = tmp
    }
    suffixes := make([]map[int64]int64, len(aa)+1)
    currMap = map[int64]int64{}
    suffixes[len(suffixes)-1] = currMap
    for i := len(aa)-1; i > -1; i-- {
        tmp := copyMap(currMap)
        tmp[aa[i]]++
        suffixes[i] = tmp
        currMap = tmp
    }
    //writer.Printf("prefixes: %v\n", prefixes)
    //writer.Printf("suffixes: %v\n", suffixes)
    maxSoFarEle := int64(0)
    maxSoFarCount := int64(0)
    for i := 0; i < len(aa)+1; i++ {
        maxEle, maxCount := tally(prefixes[i], suffixes[i])
        if maxCount > maxSoFarCount {
            maxSoFarEle = maxEle
            maxSoFarCount = maxCount
        }
    }
    countLeft := int64(0)
    ptrLeft := 0
    for {
        if aa[ptrLeft] == maxSoFarEle {
            countLeft++
        }
        ptrLeft++
        if countLeft == maxSoFarCount {
            break
        }
    }
    countRight := int64(0)
    ptrRight := len(aa)-1
    for {
        if aa[ptrRight] == maxSoFarEle {
            countRight++
        }
        ptrRight--
        if countRight == maxSoFarCount {
            break
        }
    }
    //writer.Printf("%d %d\n", ptrLeft, ptrRight)
    midMap := map[int64]int64{}
    for i := ptrLeft; i <= ptrRight; i++ {
        midMap[aa[i]]++
    }
    maxSoFar := int64(0)
    for _, n := range midMap {
        if n > maxSoFar {
            maxSoFar = n
        }
    }
    ret := (maxSoFarCount * 2) + maxSoFar
    if ret == 0 {
        return 1
    }
    return ret
}

func copyMap(m map[int64]int64) map[int64]int64 {
    ret := map[int64]int64{}
    for k, v := range m {
        ret[k] = v
    }
    return ret
}

// returns element, count
func tally(p, s map[int64]int64) (int64, int64) {
    retCount := int64(0)
    retElement := int64(0)
    for k, v := range p {
        if v2, ok := s[k]; ok {
            if v2 > retCount {
                retElement = k
                retCount = min(v, v2)
            }
        }
    }
    return retElement, retCount
}

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

package main

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

type BufferedWriter interface {
    Printf(format string, a ...interface{})
    Flush()
}

type writerImpl struct {
    *bufio.Writer
}

func NewBufferedWriter(writer io.Writer) BufferedWriter {
    return &writerImpl{Writer: bufio.NewWriter(writer)}
}

func (impl *writerImpl) Printf(f string, a ...interface{}) {
    fmt.Fprintf(impl.Writer, f, a...)
}

func (impl *writerImpl) Flush() {
    impl.Writer.Flush()
}

type WordScanner interface {
    NextInt() int
    NextInt64() int64
    NextString() string
}

type wordScannerImpl struct {
    *bufio.Scanner
}

func NewWordScanner(reader io.Reader) WordScanner {
    s := bufio.NewScanner(reader)
    s.Split(bufio.ScanWords)
    // adjust the following size as needed
    // 2 << 17 is good enough to scan strings of len 2*10^5
    // it could go as high as 2 << 26 before cf judge breaks
    size := 2 << 20
    buf := make([]byte, size)
    s.Buffer(buf, size)
    return &wordScannerImpl{Scanner: s}
}

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

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

func (impl *wordScannerImpl) NextString() string {
    impl.Scan()
    return impl.Text()
}

var reader = NewWordScanner(os.Stdin)
var writer = NewBufferedWriter(os.Stdout)

func main() {
    defer writer.Flush()
    t := reader.NextInt64()
    for ; t > 0; t -- {
        n := reader.NextInt64()
        aa := []int64{}
        for ; n > 0; n-- {
            aa = append(aa, reader.NextInt64())
        }
        writer.Printf("%d\n", sol(aa))
    }
}

func sol(aa []int64) int64 {
    total := map[int64]int64{}
    track := map[int64]int64{}
    for _, a := range aa {
        total[a]++
        track[a]++
    }
    maxSoFar := int64(0)
    maxSoFarEle := int64(0)
    for _, a := range aa {
        track[a]--
        totalLeft := total[a] - track[a]
        totalRight := track[a]
        bal := min(totalLeft, totalRight)
        if bal > maxSoFar {
            maxSoFar = bal
            maxSoFarEle = a
        }
    }
    ptrLeft := findLeftPtr(aa, maxSoFar, maxSoFarEle)
    ptrRight := findRightPtr(aa, maxSoFar, maxSoFarEle)
    writer.Printf("%d %d\n", ptrLeft, ptrRight)
    midMap := map[int64]int64{}
    for i := ptrLeft; i <= ptrRight; i++ {
        midMap[aa[i]]++
    }
    midMaxSoFar := int64(0)
    for _, n := range midMap {
        if n > midMaxSoFar {
            midMaxSoFar = n
        }
    }
    ret := (maxSoFar * 2) + midMaxSoFar
    if ret == 0 {
        return 1
    }
    return ret
}

func findLeftPtr(aa []int64, maxSoFar int64, maxSoFarEle int64) int64 {
    p := int64(0)
    for {
        if aa[p] == maxSoFarEle {
            maxSoFar--
        }
        p++
        if maxSoFar == 0 {
            break
        }
    }
    return p
}

func findRightPtr(aa []int64, maxSoFar int64, maxSoFarEle int64) int64 {
    p := int64(len(aa))-1
    for {
        if aa[p] == maxSoFarEle {
            maxSoFar--
        }
        p--
        if maxSoFar == 0 {
            break
        }
    }
    return p
}

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

package main

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

type BufferedWriter interface {
    Printf(format string, a ...interface{})
    Flush()
}

type writerImpl struct {
    *bufio.Writer
}

func NewBufferedWriter(writer io.Writer) BufferedWriter {
    return &writerImpl{Writer: bufio.NewWriter(writer)}
}

func (impl *writerImpl) Printf(f string, a ...interface{}) {
    fmt.Fprintf(impl.Writer, f, a...)
}

func (impl *writerImpl) Flush() {
    impl.Writer.Flush()
}

type WordScanner interface {
    NextInt() int
    NextInt64() int64
    NextString() string
}

type wordScannerImpl struct {
    *bufio.Scanner
}

func NewWordScanner(reader io.Reader) WordScanner {
    s := bufio.NewScanner(reader)
    s.Split(bufio.ScanWords)
    // adjust the following size as needed
    // 2 << 17 is good enough to scan strings of len 2*10^5
    // it could go as high as 2 << 26 before cf judge breaks
    size := 2 << 20
    buf := make([]byte, size)
    s.Buffer(buf, size)
    return &wordScannerImpl{Scanner: s}
}

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

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

func (impl *wordScannerImpl) NextString() string {
    impl.Scan()
    return impl.Text()
}

var reader = NewWordScanner(os.Stdin)
var writer = NewBufferedWriter(os.Stdout)

func main() {
    defer writer.Flush()
    t := reader.NextInt64()
    for ; t > 0; t -- {
        n := reader.NextInt64()
        aa := []int64{}
        for ; n > 0; n-- {
            aa = append(aa, reader.NextInt64())
        }
        writer.Printf("%d\n", sol(aa))
    }
}

func sol(aa []int64) int64 {
    total := map[int64]int64{}
    track := map[int64]int64{}
    for _, a := range aa {
        total[a]++
        track[a]++
    }
    maxSoFar := int64(0)
    maxSoFarEle := int64(0)
    for _, a := range aa {
        track[a]--
        totalLeft := total[a] - track[a]
        totalRight := track[a]
        bal := min(totalLeft, totalRight)
        if bal > maxSoFar {
            maxSoFar = bal
            maxSoFarEle = a
        }
    }
    ptrLeft := findLeftPtr(aa, maxSoFar, maxSoFarEle)
    ptrRight := findRightPtr(aa, maxSoFar, maxSoFarEle)
    midMap := map[int64]int64{}
    for i := ptrLeft; i <= ptrRight; i++ {
        midMap[aa[i]]++
    }
    midMaxSoFar := int64(0)
    for _, n := range midMap {
        if n > midMaxSoFar {
            midMaxSoFar = n
        }
    }
    ret := (maxSoFar * 2) + midMaxSoFar
    if ret == 0 {
        return 1
    }
    return ret
}

func findLeftPtr(aa []int64, maxSoFar int64, maxSoFarEle int64) int64 {
    p := int64(0)
    for {
        if aa[p] == maxSoFarEle {
            maxSoFar--
        }
        p++
        if maxSoFar == 0 {
            break
        }
    }
    return p
}

func findRightPtr(aa []int64, maxSoFar int64, maxSoFarEle int64) int64 {
    p := int64(len(aa))-1
    for {
        if aa[p] == maxSoFarEle {
            maxSoFar--
        }
        p--
        if maxSoFar == 0 {
            break
        }
    }
    return p
}

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


There is a robot on a coordinate plane. Initially, the robot is located at the point $$$(0, 0)$$$. Its path is described as a string $$$s$$$ of length $$$n$$$ consisting of characters 'L', 'R', 'U', 'D'.Each of these characters corresponds to some move:   'L' (left): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x - 1, y)$$$;  'R' (right): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x + 1, y)$$$;  'U' (up): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y + 1)$$$;  'D' (down): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y - 1)$$$. The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn't want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point $$$(x_e, y_e)$$$, then after optimization (i.e. removing some single substring from $$$s$$$) the robot also ends its path at the point $$$(x_e, y_e)$$$.This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot's path such that the endpoint of his path doesn't change. It is possible that you can't optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string $$$s$$$).Recall that the substring of $$$s$$$ is such string that can be obtained from $$$s$$$ by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of "LURLLR" are "LU", "LR", "LURLLR", "URL", but not "RR" and "UL".You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The next $$$2t$$$ lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the robot's path. The second line of the test case contains one string $$$s$$$ consisting of $$$n$$$ characters 'L', 'R', 'U', 'D' — the robot's path. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$).

For each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot's path doesn't change, print -1. Otherwise, print two integers $$$l$$$ and $$$r$$$ such that $$$1 \le l \le r \le n$$$ — endpoints of the substring you remove. The value $$$r-l+1$$$ should be minimum possible. If there are several answers, print any of them.

package main

import "fmt"

const maxn int = 2e5 + 10

type pair struct {
	fir, sec int
}

var t int

func main() {
	fmt.Scan(&t)
	for ; t > 0; t-- {
		var n int
		var s string
		fmt.Scan(&n, &s)
		s = " " + s
		var ans, l, r, x, y = n + 1, 0, 0, 0, 0
		m := make(map[pair]int)
		m[pair{fir: 0, sec: 0}] = 1
		for i := 1; i < len(s); i++ {
			switch s[i] {
			case 'L':
				x--
			case 'R':
				x++
			case 'U':
				y++
			case 'D':
				y--
			}
			v := m[pair{fir: x, sec: y}]
			if v != 0 {
				if ans > (i - v + 1) {
					ans = i - v + 1
					l = v
					r = i
				}
			}
			m[pair{fir: x, sec: y}] = i + 1
		}
		if ans == (n + 1) {
			fmt.Println("-1")
		} else {
			fmt.Println(l, r)
		}
	}
}

package main

import "fmt"

const maxn int = 2e5 + 10

type pair struct {
	fir, sec int
}

var t int

func main() {
	fmt.Scan(&t)
	for cnt := 1; cnt <= t; cnt++ {
		var n int
		var s string
		fmt.Scan(&n, &s)
		s = " " + s
		var ans, l, r, x, y = n + 1, 0, 0, 0, 0
		m := make(map[pair]int)
		m[pair{fir: 0, sec: 0}] = 1
		for i := 1; i < len(s); i++ {
			switch s[i] {
			case 'L':
				x--
			case 'R':
				x++
			case 'U':
				y++
			case 'D':
				y--
			}
			v := m[pair{fir: x, sec: y}]
			if v != 0 {
				if ans > (i - v + 1) {
					ans = i - v + 1
					l = v
					r = i
				}
			}
			m[pair{fir: x, sec: y}] = i + 1
		}
		if ans == (n + 1) {
			fmt.Println("-1")
		} else {
			fmt.Println(l, r)
		}
	}
}

package main

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

var sc, wr = bufio.NewScanner(os.Stdin), bufio.NewWriter(os.Stdout)

func scanInt() int {
	sc.Scan(); a,_ := strconv.Atoi(sc.Text()); return a
}

func scanInt64() int64 {
	sc.Scan(); a,_ := strconv.ParseInt(sc.Text(),10,64); return a
}

func scanFloat64() float64 {
	sc.Scan(); a,_ := strconv.ParseFloat(sc.Text(),64); return a
}

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

func scanString() string {
	sc.Scan(); return sc.Text()
}

func printInts(a ...int) {
	for i, e := range a {
		fmt.Fprint(wr, e); if i != len(a)-1 { fmt.Fprint(wr, " ") }
	}
	fmt.Fprintln(wr); wr.Flush()
}

func abs(a int) int { if a<0 { return -a }; return a }
func min(a,b int) int { if a<b { return a }; return b }
func max(a,b int) int { if a>b { return a }; return b }

//•*¨*•.¸¸♪Main•*¨*•.¸¸♪(　-ω-)ノ　(　・ω・)

func main() {
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 10000), 1000000)

	in := scanInt()
	for t := 0; t < in; t++ {
		_ = scanInt()
		s := scanString()
		c := map[point]int{}

		p := point{0,0}
		c[p] = 0

		ans := p
		f := false
		mi := 1 << 31 -1

		for i, e := range s {
			switch e {
			case 'L':
				p.x++
			case 'R':
				p.x--
			case 'U':
				p.y++
			case 'D':
				p.y--
			}

			if val, ok := c[p]; ok {
				if mi > i-val+1 {
					ans = point{val+1,i+1}
					mi = i-val+1
					f = true
				}
			}

			c[p] = i+1
		}

		if !f {
			fmt.Println(-1)
		} else {
			fmt.Println(ans.x,ans.y)
		}
	}
}

type point struct {
	x int
	y int
}

package main

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

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

	var t int
	fmt.Fscanf(reader, "%d\n", &t)
	for i := 0; i < t; i++ {
		var n int
		var s string
		fmt.Fscanf(reader, "%d\n%s\n", &n, &s)
		l, r := optimize(s)
		if l == -1 {
			fmt.Fprintf(writer, "-1\n")
		} else {
			fmt.Fprintf(writer, "%d %d\n", l, r)
		}
	}
}

func optimize(s string) (int, int) {
	n := len(s)

	mp := make(map[int64]int)
	mp[hash(0, 0)] = -1
	l, r := -2, n+1
	x, y := 0, 0
	for i, c := range s {
		switch c {
		case 'L':
			x--
		case 'R':
			x++
		case 'U':
			y++
		case 'D':
			y--
		}

		cord := hash(x, y)
		j, ok := mp[cord]
		if ok && (r-l+1 > i-j) {
			l, r = j+1, i
		}

		mp[cord] = i
	}

	return l + 1, r + 1
}

func hash(x, y int) int64 {
	return int64(x)*int64(1000000) + int64(y)
}

package main

import (
	"fmt"
)

type Point struct {
	x int
	y int
}

func main() {
	t := 0
	fmt.Scanln(&t)
	for i := 0; i < t; i++ {
		n := 0
		fmt.Scanln(&n)
		s := ""
		fmt.Scanln(&s)
		l := 0
		r := -1
		x := 0
		y := 0
		m := make(map[Point]int)
		m[Point{x: 0, y: 0}] = 0
		for ii, v := range s {
			ii++
			switch v {
			case 'U':
				y++
			case 'D':
				y--
			case 'L':
				x--
			case 'R':
				x++
			}
			p := Point{x: x, y: y}
			oldP, exists := m[p]
			if exists {
				if r == -1 || ii - oldP < r - l {
					r = ii
					l = oldP
				}
			}
			m[p] = ii
		}
		if r == -1 {
			fmt.Println(-1)
		} else {
			fmt.Println(l + 1, r)
		}
	}
}

package main

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

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

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

type point struct {
	x int
	y int
}

func solve() {
	var n int
	scanf("%d\n", &n)
	var s string
	scanf("%s\n", &s)
	idx, len, x, y := -1, n+1, 0, 0
	m := make(map[point]int)
	m[point{x: 0, y: 0}] = 0

	for i := 0; i < n; i++ {
		switch s[i] {
		case 'L':
			x--
		case 'R':
			x++
		case 'U':
			y++
		case 'D':
			y--
		default:
			x++
		}
		p := point{x: x, y: y}
		if _, ok := m[p]; ok {
			l := i + 1 - m[p]
			if l < len {
				len = l
				idx = i + 1
			}
		}
		m[p] = i + 1
	}
	if len <= n {
		printf("%d %d\n", idx-len+1, idx)
	} else {
		printf("-1\n")
	}
}

func main() {
	t := 1
	scanf("%d\n", &t)
	for t > 0 {
		solve()
		t--
	}
	writer.Flush()
}

package main

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

type point struct {
	x int
	y int
}
const MAXINT = 2147483647;
func main() {
	in := bufio.NewReader(os.Stdin);
	out := bufio.NewWriter(os.Stdout);
	var t, n int;
	fmt.Fscanln(in, &t);
	for i := 0; i < t; i++ {
		fmt.Fscanln(in, &n);
		cur := point{0, 0};
		var commands string;
		positions := map[point]int{};
		positions[cur] = 0;
		ansLeft := -1;
		ansRight := -1;
		minAns := MAXINT;
		fmt.Fscanln(in, &commands);
		for j := 1; j <= n; j++ {
			command := commands[j - 1];
			switch command {
				case 'L':
					cur.x--
				case 'R':
					cur.x++
				case 'U':
					cur.y++
				case 'D':
					cur.y--
			}
			if val, ok := positions[cur]; ok {
				ans := j - val;
				if (ans < minAns) {
					minAns = ans;
					ansLeft = val + 1;
					ansRight = j;
				}
			}
			positions[cur] = j;
		}
		if (ansLeft != -1) {
			fmt.Fprintln(out, ansLeft, ansRight);
		} else {
			fmt.Fprintln(out, "-1");
		}
	}
	out.Flush();
}

package main

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

var sc, wr = bufio.NewScanner(os.Stdin), bufio.NewWriter(os.Stdout)

func scanInt() int {
	sc.Scan(); a,_ := strconv.Atoi(sc.Text()); return a
}

func scanInt64() int64 {
	sc.Scan(); a,_ := strconv.ParseInt(sc.Text(),10,64); return a
}

func scanFloat64() float64 {
	sc.Scan(); a,_ := strconv.ParseFloat(sc.Text(),64); return a
}

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

func scanString() string {
	sc.Scan(); return sc.Text()
}

func printInts(a ...int) {
	for i, e := range a {
		fmt.Fprint(wr, e); if i != len(a)-1 { fmt.Fprint(wr, " ") }
	}
	fmt.Fprintln(wr); wr.Flush()
}

func abs(a int) int { if a<0 { return -a }; return a }
func min(a,b int) int { if a<b { return a }; return b }
func max(a,b int) int { if a>b { return a }; return b }

//•*¨*•.¸¸♪Main•*¨*•.¸¸♪(　-ω-)ノ　(　・ω・)

func main() {
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 10000), 1000000)

	in := scanInt()
	T: for t := 0; t < in; t++ {
		_ = scanInt()
		s := scanString()
		c := map[point]int{}

		p := point{0,0}
		c[p] = 0

		for i, e := range s {
			switch e {
			case 'L':
				p.x++
			case 'R':
				p.x--
			case 'U':
				p.y++
			case 'D':
				p.y--
			}

			if val, ok := c[p]; ok {
				fmt.Println(val+1, i+1)
				continue T
			}

			c[p] = i+1
		}

		fmt.Println(-1)

	}
}

type point struct {
	x int
	y int
}

package main

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

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

	var t int
	fmt.Fscanf(reader, "%d\n", &t)
	for i := 0; i < t; i++ {
		var n int
		var s string
		fmt.Fscanf(reader, "%d\n%s\n", &n, &s)
		l, r := optimize(s)
		if l == -1 {
			fmt.Fprintf(writer, "-1\n")
		} else {
			fmt.Fprintf(writer, "%d %d\n", l, r)
		}
	}
}

func optimize(s string) (int, int) {
	n := len(s)

	mp := make(map[int64]int)
	mp[hash(0, 0)] = -1
	l, r := -2, n+1
	x, y := 0, 0
	for i, c := range s {
		switch c {
		case 'L':
			x--
		case 'R':
			x++
		case 'U':
			y++
		case 'D':
			y--
		}

		cord := hash(x, y)
		j, ok := mp[cord]
		if ok {
			fmt.Println(j)
		}
		if ok && (r-l+1 > i-j) {
			l, r = j+1, i
		}

		mp[cord] = i
	}

	return l + 1, r + 1
}

func hash(x, y int) int64 {
	return int64(x)*int64(1000000) + int64(y)
}


Anatoly lives in the university dorm as many other students do. As you know, cockroaches are also living there together with students. Cockroaches might be of two colors: black and red. There are n cockroaches living in Anatoly's room.Anatoly just made all his cockroaches to form a single line. As he is a perfectionist, he would like the colors of cockroaches in the line to alternate. He has a can of black paint and a can of red paint. In one turn he can either swap any two cockroaches, or take any single cockroach and change it's color.Help Anatoly find out the minimum number of turns he needs to make the colors of cockroaches in the line alternate.

The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of cockroaches. The second line contains a string of length n, consisting of characters 'b' and 'r' that denote black cockroach and red cockroach respectively.

Print one integer — the minimum number of moves Anatoly has to perform in order to make the colors of cockroaches in the line to alternate.

NoteIn the first sample, Anatoly has to swap third and fourth cockroaches. He needs 1 turn to do this.In the second sample, the optimum answer is to paint the second and the fourth cockroaches red. This requires 2 turns.In the third sample, the colors of cockroaches in the line are alternating already, thus the answer is 0.

package main

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

func I(t interface{}, delim string) {
	fmt.Fscanf(r, "%v"+delim, t)
}

func O(o interface{}, delim string) {
	fmt.Fprintf(w, "%v"+delim, o)
}

var r *bufio.Reader
var w *bufio.Writer

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

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

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

	var a, b, ra, ba, rb, bb, n int

	I(&n, "\n")
	var s string
	I(&s, "\n")
	for i := 0; i < n; i++ {
		if (s[i] != 'r' && i%2 == 0) || (s[i] != 'b' && i%2 == 1) {
			if s[i] == 'r' {
				ra++
			} else {
				ba++
			}
		}
		if (s[i] != 'b' && i%2 == 0) || (s[i] != 'r' && i%2 == 1) {
			if s[i] == 'r' {
				rb++
			} else {
				bb++
			}
		}
	}
	a = abs(ra-ba) + min(ra, ba)
	b = abs(rb-bb) + min(rb, bb)
	O(min(a, b), "\n")
}

package main

import "fmt"
import "bufio"
import "os"

var bufin *bufio.Reader
var bufout *bufio.Writer

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

func cost(c []int, n int, color int) int {
	diff := make([]int, 2)
	for i := 0; i < n; i++ {
		if c[i] != color {
			diff[c[i]]++
		}
		color = 1 - color
	}

	ret := min(diff[0], diff[1])
	return ret + diff[0] - ret + diff[1] - ret
}

func main() {
	bufin = bufio.NewReader(os.Stdin)
	bufout = bufio.NewWriter(os.Stdout)
	defer bufout.Flush()

	var n int
	fmt.Fscanf(bufin, "%d\n", &n)

	a := make([]byte, n)
	fmt.Fscanf(bufin, "%s\n", &a)

	c := make([]int, n)
	for i := 0; i < n; i++ {
		if a[i] == 'r' {
			c[i] = 0
		} else {
			c[i] = 1
		}
	}

	res := n
	for color := 0; color < 2; color++ {
		res = min(res, cost(c, n, color))
	}

	fmt.Fprintf(bufout, "%d\n", res)
}

// http://codeforces.com/problemset/problem/719/B
package main

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

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 main() {
	var n int

	input := bufio.NewReader(os.Stdin)
	fmt.Fscanln(input, &n)

	if n == 1 {
		fmt.Println(0)
		return
	}

	even := make(map[rune]int)
	odd := make(map[rune]int)

	for i := 0; i < n; i++ {
		var t rune
		fmt.Fscanf(input, "%c", &t)
		if i % 2 == 0 {
			even[t]++
		} else {
			odd[t]++
		}
	}

	// Solve
	// b - odd, r - even
	swaps := min(even['b'], odd['r'])
	y := max(even['b'], odd['r'])
	p1 := swaps + (y - swaps)

	// b - even, r - odd
	swaps = min(even['r'], odd['b'])
	y = max(even['r'], odd['b'])
	p2 := swaps + (y - swaps)

	fmt.Println(min(p1, p2))
}

package main

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

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

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

		moves := [2]int{}
		l := [2][]int{make([]int, n), make([]int, n)}

		for i, c := range s {
			if c == 'r' {
				l[0][i] = 1
				l[1][i] = 1
			} else if c == 'b' {
				l[0][i] = 0
				l[1][i] = 0
			}
		}

		disorder_start := [2][2]int{}
		for i := 0; i < n; i++ {
			for j := 0; j < 2; j++ {
				if l[j][i] != (i+j)%2 {
					moves[j] += 1
					swap := false
					if disorder_start[j][(i+1)%2] < n {
						for k := max(disorder_start[j][(i+1)%2], i+1); k < n; k += 2 { // Swap with k
							if l[j][k] != (k+j)%2 {
								l[j][i], l[j][k] = l[j][k], l[j][i]
								swap = true
								for f := k + 2; f < n; f += 2 {
									if l[j][f] != (f+j)%2 {
										disorder_start[j][(i+1)%2] = f
										break
									}
								}
								break
							}
						}
					}
					if !swap { // Change color
						disorder_start[j][(i+1)%2] = n
						l[j][i] = (i + j) % 2
					}
				}
			}
		}

		if moves[0] < moves[1] {
			fmt.Println(moves[0])
		} else {
			fmt.Println(moves[1])
		}
	}
}

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

package main

import "fmt"
import "bufio"
import "os"

var bufin *bufio.Reader
var bufout *bufio.Writer

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

func cost(from int, to int) int {
	if from == to {
		return 0
	}
	return 1
}

func main() {
	bufin = bufio.NewReader(os.Stdin)
	bufout = bufio.NewWriter(os.Stdout)
	defer bufout.Flush()

	var n int
	fmt.Fscanf(bufin, "%d\n", &n)

	a := make([]byte, n)
	fmt.Fscanf(bufin, "%s\n", &a)

	c := make([]int, n+1)
	for i := 0; i < n; i++ {
		if a[i] == 'r' {
			c[i+1] = 0
		} else {
			c[i+1] = 1
		}
	}

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

	for i := 1; i <= n; i++ {
		for j := 0; j < 2; j++ {
			f[i][j] = min(f[i][j], f[i-1][1-j]+cost(c[i], j))
			if i >= 2 {
				f[i][j] = min(f[i][j], f[i-2][j]+cost(c[i-1], j)+cost(c[i], 1-j)+1)
				f[i][j] = min(f[i][j], f[i-1][j]+cost(c[i], 1-j)+1)
			}
		}
	}

	res := min(f[n][0], f[n][1])
	fmt.Fprintf(bufout, "%d\n", res)
}

package main

import "fmt"
import "bufio"
import "os"

var bufin *bufio.Reader
var bufout *bufio.Writer

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

func cost(from int, to int) int {
	if from == to {
		return 0
	}
	return 1
}

func main() {
	bufin = bufio.NewReader(os.Stdin)
	bufout = bufio.NewWriter(os.Stdout)
	defer bufout.Flush()

	var n int
	fmt.Fscanf(bufin, "%d\n", &n)

	a := make([]byte, n)
	fmt.Fscanf(bufin, "%s\n", &a)

	c := make([]int, n+1)
	for i := 0; i < n; i++ {
		if a[i] == 'r' {
			c[i+1] = 0
		} else {
			c[i+1] = 1
		}
	}

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

	for i := 1; i <= n; i++ {
		for j := 0; j < 2; j++ {
			f[i][j] = min(f[i][j], f[i-1][1-j]+cost(c[i], j))
			if (i >= 2) && (c[i-1] == j) && (c[i] == 1-j) {
				f[i][j] = min(f[i][j], f[i-2][j]+1)
			}
		}
	}

	res := min(f[n][0], f[n][1])
	fmt.Fprintf(bufout, "%d\n", res)
}

package main

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

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

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 main() {
	var n int

	input := bufio.NewReader(os.Stdin)
	fmt.Fscanln(input, &n)

	if n == 1 {
		fmt.Println(0)
		return
	}

	even := make(map[rune]int)
	odd := make(map[rune]int)

	for i := 0; i < n; i++ {
		var t rune
		fmt.Fscanf(input, "%c", &t)
		if i % 2 == 0 {
			even[t]++
		} else {
			odd[t]++
		}

	}

	x1, x2 := even['b'] - odd['r'], odd['b'] - even['r']
	y1, y2 := even['r'] - odd['b'], odd['r'] - even['b']

	ans, ansy := 0, 0
	if (x1 <= 0 && x2 <= 0) || (x1 >= 0 && x2 >= 0) {
		ans = (x1 + x2) / 2
	} else {
		ans = min(abs(x1), abs(x2)) + (abs(x1 + x2) / 2)

	}

	if (y1 <= 0 && y2 <= 0) || (y1 >= 0 && y2 >= 0) {
		ansy = (y1 + y2) / 2
	} else {
		ansy = min(abs(y1), abs(y2)) + (abs(y1 + y2) / 2)

	}

	if ans < ansy {
		fmt.Println(ans)
	} else {
		fmt.Println(ansy)
	}

}

package main

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

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

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

		moves := [2]int{}
		l := [2][]int{make([]int, n), make([]int, n)}

		for i, c := range s {
			if c == 'r' {
				l[0][i] = 1
				l[1][i] = 1
			} else if c == 'b' {
				l[0][i] = 0
				l[1][i] = 0
			}
		}

		for j := 0; j < 2; j++ {
			for i := 0; i < n; i++ {
				if l[j][i] != (i+j)%2 {
					moves[j] += 1
					swap := false
					for k := i + 1; k < n; k++ {
						if l[j][k] != (k+j)%2 {
							l[j][i], l[j][k] = l[j][k], l[j][i]
							swap = true
							break
						}
					}
					if !swap { // Change color
						l[j][i] = (i + j) % 2
					}
				}
			}
		}

		if moves[0] < moves[1] {
			fmt.Println(moves[0])
		} else {
			fmt.Println(moves[1])
		}
	}
}

package main

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

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

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

		moves := [2]int{}
		l := [2][]int{make([]int, n), make([]int, n)}

		for i, c := range s {
			if c == 'r' {
				l[0][i] = 1
				l[1][i] = 1
			} else if c == 'b' {
				l[0][i] = 0
				l[1][i] = 0
			}
		}

		for i := 0; i < n; i++ {
			for j := 0; j < 2; j++ {
				if l[j][i] != (i+j)%2 {
					moves[j] += 1
					if i < n-1 && l[j][i+1] != (i+j+1)%2 { // Swap
						l[j][i], l[j][i+1] = l[j][i+1], l[j][i]
					} else { // Change color
						l[j][i] = (i + j) % 2
					}
				}
			}
		}

		if moves[0] < moves[1] {
			fmt.Println(moves[0])
		} else {
			fmt.Println(moves[1])
		}
	}
}