Baby Ehab is known for his love for a certain operation. He has an array $$$a$$$ of length $$$n$$$, and he decided to keep doing the following operation on it:   he picks $$$2$$$ adjacent elements; he then removes them and places a single integer in their place: their bitwise XOR. Note that the length of the array decreases by one. Now he asks you if he can make all elements of the array equal. Since babies like to make your life harder, he requires that you leave at least $$$2$$$ elements remaining.

The first line contains an integer $$$t$$$ ($$$1 \le t \le 15$$$) — the number of test cases you need to solve. The first line of each test case contains an integers $$$n$$$ ($$$2 \le n \le 2000$$$) — the number of elements in the array $$$a$$$. The second line contains $$$n$$$ space-separated integers $$$a_1$$$, $$$a_2$$$, $$$\ldots$$$, $$$a_{n}$$$ ($$$0 \le a_i < 2^{30}$$$) — the elements of the array $$$a$$$.

If Baby Ehab can make all elements equal while leaving at least $$$2$$$ elements standing, print "YES". Otherwise, print "NO".

NoteIn the first sample, he can remove the first $$$2$$$ elements, $$$0$$$ and $$$2$$$, and replace them by $$$0 \oplus 2=2$$$. The array will be $$$[2,2]$$$, so all the elements are equal.In the second sample, there's no way to make all the elements equal.

package main

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

func main() {
	//f, _ := os.Open("in.txt")
	//in := bufio.NewReader(f)
	in := bufio.NewReader(os.Stdin)
	var t, n int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &n)
		ans := 0
		m := make(map[int]bool, 0)
		tm := make([]int, 0)
		a := make([]int, n)
		m[0] = true
		max := 0

		for j := 0; j < n; j++ {
			fmt.Fscan(in, &a[j])
			ans ^= a[j]
			m[a[j]] = true
			if ans == 0 {
				for _, v := range tm {
					m[v] = true
				}
				tm = make([]int, 0)
			}
			tm = append(tm, ans)
			if a[j] > max {
				max = a[j]
			}

		}
		if ok := m[ans]; ok && validate(ans, max, a) {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

func validate(ans, max int, m []int) bool {
	if ans != max {
		return true
	}
	count := 0
	for _, v := range m {
		tmp := v ^ max
		if tmp <= max/2 {
			count++
		}
	}

	if count > 1 {
		return true
	}

	return false
}

package main

import (
	"fmt"
)

func solve() {
	var n int
	fmt.Scan(&n)
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
	}
	for i := 1; i < n; i++ {
		a[i] ^= a[i-1]
	}
	var ok bool = false
	if a[n-1] == 0 {
		ok = true

	}
	for i := 0; i < n; i++ {
		for j := i + 1; j < n-1; j++ {
			if (a[i] == a[j]^a[i]) && (a[i] == (a[n-1] ^ a[j])) {
				ok = true
			}
		}
	}
	if ok == true {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}
}

func main() {
	var t int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		solve()
	}
}

package main
import ."fmt"

// github.com/EndlessCheng/codeforces-go
func main() {
	var T, n int
	a := [3e3]int{}
	for Scan(&T); T > 0; T-- {
		var x, y, c int
		for Scan(&n); n > 0; n-- {
			Scan(&a[n])
			x ^= a[n]
		}
		for _, v := range a {
			if y ^= v; y == x {
				c++
				y = 0
			}
		}
		if c > 1 {
			Println("YES")
		} else {
			Println("NO")
		}
	}
}

package main
import ."fmt"

// github.com/EndlessCheng/codeforces-go
func main() {
	var T, n int
	for Scan(&T); T > 0; T-- {
		Scan(&n)
		x := 0
		a := make([]int, n)
		for i := range a {
			Scan(&a[i])
			x ^= a[i]
		}
		if x == 0 {
			Println("YES")
			continue
		}
		c := 0
		for i := 0; i < n; i++ {
			for s := 0; i < n && s^a[i] != x; i++ {
				s ^= a[i]
			}
			c++
		}
		if c > 2 {
			Println("YES")
		} else {
			Println("NO")
		}
	}
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"io/ioutil"
	"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()

	solve := func(Case int) bool {
		var n, xor int
		Fscan(in, &n)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
			xor ^= a[i]
		}
		for i := 1; i < n; i++ {
			if a[i] != a[i-1] {
				goto next
			}
		}
		return true
	next:
		if xor == 0 {
			return true
		}
		c := 0
		for i := 0; i < n; {
			s := 0
			for ; i < n; i++ {
				if s^a[i] == xor {
					i++
					break
				}
				s ^= a[i]
			}
			c++
		}
		return c >= 3
	}

	var T int
	Fscan(in, &T)
	for Case := 1; Case <= T; Case++ {
		Fprintln(out, map[bool]string{true: "YES", false: "NO"}[solve(Case)])
	}

	_leftData, _ := ioutil.ReadAll(in)
	if _s := strings.TrimSpace(string(_leftData)); _s != "" {
		panic("有未读入的数据：\n" + _s)
	}
}

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

package main

import (
	"fmt"
)

func main() {
	var t int

	fmt.Scan(&t)
	for ; t > 0; t-- {
		var n int
		fmt.Scan(&n)

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

		if curr == 0 {
			fmt.Println("YES")
			continue
		}

		prefix := precalc(&a)

		flag := false
		for i := 0; i < n; i++ {
			for j := i + 1; j < n; j++ {
				if (*prefix)[i] == (*prefix)[n-1]^(*prefix)[j-1] && (*prefix)[i] == (*prefix)[j-1]^(*prefix)[i] {
					flag = true
				}
			}
		}

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

func precalc(a *[]int) *[]int {
	prefix := make([]int, len(*a))
	prefix[0] = (*a)[0]
	for i := 1; i < len(*a); i++ {
		prefix[i] = prefix[i-1] ^ (*a)[i]
	}

	return &prefix
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var buf bytes.Buffer
	tc := readNum(reader)

	for tc > 0 {
		tc--
		n := readNum(reader)
		A := readNNums(reader, n)
		res := solve(n, A)
		if res {
			buf.WriteString("YES\n")
		} else {
			buf.WriteString("NO\n")
		}
	}
	fmt.Print(buf.String())
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}
func solve(n int, A []int) bool {
	var xor int
	for i := 0; i < n; i++ {
		xor ^= A[i]
	}
	if xor == 0 {
		return true
	}
	right := xor
	xor = 0
	mem := make(map[int]bool)
	for i := 0; i < n-1; i++ {
		xor ^= A[i]
		if xor == 0 && mem[right] {
			return true
		}
		mem[xor] = true
	}

	return false
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var buf bytes.Buffer
	tc := readNum(reader)

	for tc > 0 {
		tc--
		n := readNum(reader)
		A := readNNums(reader, n)
		res := solve(n, A)
		if res {
			buf.WriteString("YES\n")
		} else {
			buf.WriteString("NO\n")
		}
	}
	fmt.Print(buf.String())
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}
func solve(n int, A []int) bool {
	var xor int
	sum := make([]int, n)
	for i := 0; i < n; i++ {
		xor ^= A[i]
		sum[i] = xor
	}
	if xor == 0 {
		return true
	}
	xor = 0
	for i := n - 1; i > 0; i-- {
		xor ^= A[i]
		for j := i - 1; j >= 0; j-- {
			a := sum[j]
			if a == xor && sum[i-1] == 0 {
				return true
			}
		}
	}

	return false
}

package main

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

func run(or io.Reader, ow io.Writer) {
	in := bufio.NewReader(or)
	out := bufio.NewWriter(ow)
	defer out.Flush()

	solve := func() {
		var n int
		fmt.Fscan(in, &n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
		}
		b := a
		for i := 1; i < n; i++ {
			b[i] = b[i-1] ^ a[i]
		}
		if b[n-1] == 0 {
			fmt.Fprint(out, "YES")
		} else {
			var ok = false
			for i := 0; i < n-2; i++ {
				if b[i] == b[n-1] {
					for j := i + 1; j < n-1; j++ {
						if b[j] == 0 {
							ok = true
							break
						}
					}
				}
			}
			if ok {
				fmt.Fprint(out, "YES")
			} else {
				fmt.Fprint(out, "NO")
			}

		}

		fmt.Fprintln(out)
	}

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

}

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

package main

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

const REPEAT bool = true
const CASE   bool = false

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

  if REPEAT {
    var t int
    Fscan(in, &t);
    for i := 1; i <= t; i++ {
      if CASE {
        Fprintf(out, "Case #%d: ", i)
      }
      solve(in, out)
    }
  } else {
    solve(in, out)
  }
}

func solve(in io.Reader, out io.Writer) {
  var n int
  Fscan(in, &n)
  arr := make([]int, n)
  pref := make([]int, n)
  xor := 0
  for i := range arr {
    Fscan(in, &arr[i])
    xor ^= arr[i]
    pref[i] = xor
  }
  if xor == 0 {
    Fprintln(out, "YES")
  } else {
    for i := 0; i < n - 1; i++ {
      for j := i + 1; j < n; j++ {
        part1 := pref[i]
        part2 := pref[j] ^ pref[i]
        part3 := xor ^ pref[j]
        if part1 == part2 && part2 == part3 {
          Fprintln(out, "YES")
          return
        }
      }
    }
    Fprintln(out, "NO")
  }
}

package main

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

var in *bufio.Reader

func solve() {
	var n int
	fmt.Fscan(in, &n)

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

	total := 0
	for _, ai := range a {
		total ^= ai
	}

	if total == 0 {
		fmt.Println("YES")
		return
	}

	cur := 0
	cnt := 0
	for _, ai := range a {
		cur ^= ai
		if cur == total {
			cnt++
			cur = 0
		}
	}
	if cnt > 1 {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}
}

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

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

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

package main

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

func main() {
	//f, _ := os.Open("in.txt")
	//in := bufio.NewReader(f)
	in := bufio.NewReader(os.Stdin)
	var t, n int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &n)
		ans := 0
		m := make(map[int]bool, 0)
		tm := make([]int, 0)
		a := make([]int, n)
		m[0] = true
		max := 0

		for j := 0; j < n; j++ {
			fmt.Fscan(in, &a[j])
			ans ^= a[j]
			m[a[j]] = true
			if ans == 0 {
				for _, v := range tm {
					m[v] = true
				}
				tm = make([]int, 0)
			}
			tm = append(tm, ans)
			if a[j] > max {
				max = a[j]
			}

		}
		if ok := m[ans]; ok && validate(ans, max, a) {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

func validate(ans, max int, m []int) bool {
	if ans != max {
		return true
	}
	count := 0
	for _, v := range m {
		tmp := v ^ max
		if tmp < max/2 {
			count++
		}
	}

	if count > 1 {
		return true
	}

	return false
}

package main

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

func main() {
	f, _ := os.Open("in.txt")
	in := bufio.NewReader(f)
	//in := bufio.NewReader(os.Stdin)
	var t, n int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &n)
		ans := 0
		m := make(map[int]bool, 0)
		tm := make([]int, 0)
		a := make([]int, n)
		m[0] = true
		max := 0

		for j := 0; j < n; j++ {
			fmt.Fscan(in, &a[j])
			ans ^= a[j]
			m[a[j]] = true
			if ans == 0 {
				for _, v := range tm {
					m[v] = true
				}
				tm = make([]int, 0)
			}
			tm = append(tm, ans)
			if a[j] > max {
				max = a[j]
			}

		}
		if ok := m[ans]; ok && validate(ans, max, a) {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

func validate(ans, max int, m []int) bool {
	if ans != max {
		return true
	}
	count := 0
	for _, v := range m {
		tmp := v ^ max
		if tmp < max/2 {
			count++
		}
	}

	if count > 1 {
		return true
	}

	return false
}

package main

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

func main() {
	//f, _ := os.Open("in.txt")
	//in := bufio.NewReader(f)
	in := bufio.NewReader(os.Stdin)
	var t, n int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &n)
		ans := 0
		m := make(map[int]bool, 0)
		tm := make([]int, 0)
		a := make([]int, n)
		m[0] = true
		max := 0

		for j := 0; j < n; j++ {
			fmt.Fscan(in, &a[j])
			ans ^= a[j]
			m[a[j]] = true
			if ans == 0 {
				for _, v := range tm {
					m[v] = true
				}
				tm = make([]int, 0)
			}
			tm = append(tm, ans)
			if a[j] > max {
				max = a[j]
			}

		}
		if ok := m[ans]; ok && validate(ans, max, a) {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

func validate(ans, max int, m []int) bool {
	if ans != max {
		return true
	}
	count := 0
	for _, v := range m {
		if v >= max/2 || v == max {
			count++
		}
	}

	if count > 1 {
		return true
	}

	return false
}

package main

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

func main() {
	//f, _ := os.Open("in.txt")
	//in := bufio.NewReader(f)
	in := bufio.NewReader(os.Stdin)
	var t, n int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &n)
		ans := 0
		m := make(map[int]bool, 0)
		a := make([]int, n)
		m[0] = true
		max := 0
		la := 0
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &a[j])
			ans ^= a[j]
			m[a[j]] = true
			m[la] = true
			if a[j] > max {
				max = a[j]
			}
			la = ans
		}
		if ok := m[ans]; ok && validate(ans, max, a) {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

func validate(ans, max int, m []int) bool {
	if ans != max {
		return true
	}
	count := 0
	for _, v := range m {
		if v >= max/2 || v == max {
			count++
		}
	}

	if count > 1 {
		return true
	}

	return false
}

package main

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

func main() {
	//f, _ := os.Open("in.txt")
	//in := bufio.NewReader(f)
	in := bufio.NewReader(os.Stdin)
	var t, n int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &n)
		ans := 0
		m := make(map[int]bool, 0)
		a := make([]int, n)
		m[0] = true
		max := 0
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &a[j])
			ans ^= a[j]
			m[a[j]] = true
			m[ans] = true
			if a[j] > max {
				max = a[j]
			}
		}
		if ok := m[ans]; ok && validate(ans, max, a) {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

func validate(ans, max int, m []int) bool {
	if ans != max {
		return true
	}
	count := 0
	for _, v := range m {
		if v >= max/2 || v == max {
			count++
		}
	}

	if count > 1 {
		return true
	}

	return false
}

package main

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

func main() {
	//f, _ := os.Open("in.txt")
	//in := bufio.NewReader(f)
	in := bufio.NewReader(os.Stdin)
	var t, n int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &n)
		ans := 0
		m := make(map[int]bool, 0)
		a := make([]int, n)
		m[0] = true
		max := 0
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &a[j])
			ans ^= a[j]
			m[a[j]] = true
			if a[j] > max {
				max = a[j]
			}
		}
		if ok := m[ans]; ok && validate(ans, max, a) {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

func validate(ans, max int, m []int) bool {
	if ans != max {
		return true
	}
	count := 0
	for _, v := range m {
		if v >= max/2 || v == max {
			count++
		}
	}

	if count > 1 {
		return true
	}

	return false
}

package main

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

func main() {
	//f, _ := os.Open("in.txt")
	//in := bufio.NewReader(f)
	in := bufio.NewReader(os.Stdin)
	var t, n, a int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &n)
		ans := 0
		m := make(map[int]bool, 0)
		m[0] = true
		max := 0
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &a)
			ans ^= a
			m[a] = true
			if a > max {
				max = a
			}
		}
		if ok := m[ans]; ok && validate(ans, max, m) {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

func validate(ans, max int, m map[int]bool) bool {
	if ans != max {
		return true
	}

	for k, _ := range m {
		if k >= max/2 || k == max {
			return true
		}
	}

	return false
}

package main

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

func main() {
	f, _ := os.Open("in.txt")
	in := bufio.NewReader(f)
	//in := bufio.NewReader(os.Stdin)
	var t, n, a int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &n)
		ans := 0
		m := make(map[int]bool, 0)
		m[0] = true
		max := 0
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &a)
			ans ^= a
			m[a] = true
			if a > max {
				max = a
			}
		}
		if ok := m[ans]; ok && validate(ans, max, m) {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

func validate(ans, max int, m map[int]bool) bool {
	if ans != max {
		return true
	}

	for k, _ := range m {
		if k >= max/2 || k == max {
			return true
		}
	}

	return false
}

package main

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

func main() {
	//f, _ := os.Open("in.txt")
	//in := bufio.NewReader(f)
	in := bufio.NewReader(os.Stdin)
	var t, n, a int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &n)
		ans := 0
		m := make(map[int]bool, 0)
		m[0] = true
		max := 0
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &a)
			ans ^= a
			m[a] = true
			if a > max {
				max = a
			}
		}
		if ok := m[ans]; ok && ans != max {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

func validate(ans, max int, m map[int]bool) bool {
	if ans != max {
		return true
	}

	for k, _ := range m {
		if k > max/2 {
			return true
		}
	}

	return false
}

package main

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

func main() {
	//f, _ := os.Open("in.txt")
	//in := bufio.NewReader(f)
	in := bufio.NewReader(os.Stdin)
	var t, n, a int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &n)
		ans := 0
		m := make(map[int]bool, 0)
		m[0] = true
		max := 0
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &a)
			ans ^= a
			m[a] = true
			if a == max {
				max++
			}
			if a > max {
				max = a
			}
		}
		if ok := m[ans]; ok && ans != max {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

func main() {
	//f, _ := os.Open("in.txt")
	//in := bufio.NewReader(f)
	in := bufio.NewReader(os.Stdin)
	var t, n, a int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &n)
		ans := 0
		m := make(map[int]bool, 0)
		m[0] = true
		max := 0
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &a)
			ans ^= a
			m[a] = true
			if a > max {
				max = a
			}
			if a == max {
				max++
			}
		}
		if ok := m[ans]; ok && ans != max {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

func main() {
	f, _ := os.Open("in.txt")
	in := bufio.NewReader(f)
	//in := bufio.NewReader(os.Stdin)
	var t, n, a int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &n)
		ans := 0
		m := make(map[int]bool, 0)
		m[0] = true
		max := 0
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &a)
			ans ^= a
			m[a] = true
			if a > max {
				max = a
			}
		}
		if ok := m[ans]; ok && ans != max {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

func main() {
	//f, _ := os.Open("in.txt")
	//in := bufio.NewReader(f)
	in := bufio.NewReader(os.Stdin)
	var t, n, a int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &n)
		ans := 0
		m := make(map[int]bool, 0)
		m[0] = true
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &a)
			ans ^= a
			m[a] = true
		}
		if ok := m[ans]; ok {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

func main() {
	//f, _ := os.Open("in.txt")
	//in := bufio.NewReader(f)
	in := bufio.NewReader(os.Stdin)
	var t, n, a int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &n)
		ans := 0
		isAllSame := true
		la := 0
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &a)
			ans ^= a
			if j > 0 && a != la {
				isAllSame = false
			}
			la = a
		}
		if ans == 0 || isAllSame {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

func main() {
	//f, _ := os.Open("in.txt")
	//in := bufio.NewReader(f)
	in := bufio.NewReader(os.Stdin)
	var t, n, a int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &n)
		ans := 0
		isAllSame := true
		la := 0
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &a)
			ans ^= a
			la = a
			if j > 0 && a != la {
				isAllSame = false
			}
		}
		if ans == 0 || isAllSame {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

func main() {
	//f, _ := os.Open("in.txt")
	//in := bufio.NewReader(f)
	in := bufio.NewReader(os.Stdin)
	var t, n, a int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &n)
		ans := 0
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &a)
			ans ^= a
		}
		if ans == 0 {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import (
	"fmt"
)

func solve() {
	var n int
	fmt.Scan(&n)
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
	}
	for i := 1; i < n; i++ {
		a[i] ^= a[i-1]
	}
	var ok bool = false
	if a[n-1] == 0 {
		ok = true

	}
	for i := 0; i < n; i++ {
		for j := i + 1; j < n-1; j++ {
			if (a[i] == a[j]) && (a[i] == (a[n-1] ^ a[j])) {
				ok = true
			}
		}
	}
	if ok == true {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}
}

func main() {
	var t int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		solve()
	}
}

package main

import (
	"fmt"
)

func solve() {
	var n int
	fmt.Scan(&n)
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
	}
	for i := 1; i < n; i++ {
		a[i] ^= a[i-1]
	}
	var ok bool = false
	if a[n-1] == 0 {
		ok = true

	}
	for i := 0; i < n; i++ {
		for j := i + 1; j < n; j++ {
			if (a[i] == a[j]) && (a[i] == (a[n-1] ^ a[j])) {
				ok = true
			}
		}
	}
	if ok == true {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}
}

func main() {
	var t int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		solve()
	}
}

package main

import (
	"fmt"
)

func solve() {
	var n int
	fmt.Scan(&n)
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
	}
	for i := 1; i < n; i++ {
		a[i] ^= a[i-1]
	}
	for i := 1; i < n-1; i++ {
		if a[i-1] == (a[n-1] ^ a[i-1]) {
			fmt.Println("YES")
			return
		}
	}
	fmt.Println("NO")
}

func main() {
	var t int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		solve()
	}
}

package main

import (
	"fmt"
)

func solve() {
	var n int
	fmt.Scan(&n)
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
	}
	for i := 1; i < n; i++ {
		a[i] ^= a[i-1]
	}
	for i := 1; i < n; i++ {
		if a[i-1] == (a[n-1] ^ a[i-1]) {
			fmt.Println("YES")
			return
		}
	}
	fmt.Println("NO")
}

func main() {
	var t int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		solve()
	}
}

package main

import (
	"fmt"
)

func solve() {
	var n int
	fmt.Scan(&n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
	}
	for i := 1; i < n; i++ {
		a[i] ^= a[i-1]
	}
	for i := 1; i < n; i++ {
		if a[i-1] == (a[n-1] ^ a[i-1]) {
			fmt.Println("YES")
			return
		}
	}
	fmt.Println("NO")
}

func main() {
	var t int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		solve()
	}
}

package main
import ."fmt"

// github.com/EndlessCheng/codeforces-go
func main() {
	var T, n int
	for Scan(&T); T > 0; T-- {
		Scan(&n)
		var x, y, c int
		a := make([]int, n)
		for i := range a {
			Scan(&a[i])
			x ^= a[i]
		}
		for _, v := range a {
			if y ^= v; y == x {
				c++
				y = 0
			}
		}
		if c > 1 {
			Println("YES")
		} else {
			Println("NO")
		}
	}
}

package main
import ."fmt"

// github.com/EndlessCheng/codeforces-go
func main() {
	var T, n, x int
	for Scan(&T); T > 0; T-- {
		Scan(&n)
		a := make([]int, n)
		for i := range a {
			Scan(&a[i])
			x ^= a[i]
		}
		if x == 0 {
			Println("YES")
			continue
		}
		c := 0
		for i := 0; i < n; i++ {
			for s := 0; i < n && s^a[i] != x; i++ {
				s ^= a[i]
			}
			c++
		}
		if c > 2 {
			Println("YES")
		} else {
			Println("NO")
		}
	}
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"io/ioutil"
	"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()

	solve := func(Case int) bool {
		var n, xor int
		Fscan(in, &n)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
			xor ^= a[i]
		}
		for i := 1; i < n; i++ {
			if a[i] != a[i-1] {
				goto next
			}
		}
		return true
	next:
		if xor == 0 {
			return true
		}
		c := 0
		for i := 0; i < n; {
			s := a[i]
			for i++; i < n && s^a[i] != xor; i++ {
				s ^= a[i]
			}
			c++
		}
		return c >= 3
	}

	var T int
	Fscan(in, &T)
	for Case := 1; Case <= T; Case++ {
		Fprintln(out, map[bool]string{true: "YES", false: "NO"}[solve(Case)])
	}

	_leftData, _ := ioutil.ReadAll(in)
	if _s := strings.TrimSpace(string(_leftData)); _s != "" {
		panic("有未读入的数据：\n" + _s)
	}
}

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

package main

import (
	"fmt"
)

func main() {
	var t int

	fmt.Scan(&t)
	for ; t > 0; t-- {
		var n int
		fmt.Scan(&n)

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

		if curr == 0 {
			fmt.Println("YES")
			continue
		}

		prefix := precalc(&a)

		flag := false
		for i := 0; i < n; i++ {
			for j := i + 1; j < n; j++ {
				if (*prefix)[i] == (*prefix)[n-1]^(*prefix)[j] && (*prefix)[i] == (*prefix)[j-1]^(*prefix)[i] {
					flag = true
				}
			}
		}

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

func precalc(a *[]int) *[]int {
	prefix := make([]int, len(*a))
	prefix[0] = (*a)[0]
	for i := 1; i < len(*a); i++ {
		prefix[i] = prefix[i-1] ^ (*a)[i]
	}

	return &prefix
}

package main

import (
	"fmt"
)

func main() {
	var t int

	fmt.Scan(&t)
	for ; t > 0; t-- {
		var n int
		fmt.Scan(&n)

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

		if curr == 0 {
			fmt.Println("YES")
			continue
		}

		left := a[0]
		right := 0
		for i := 1; i < n; i++ {
			right ^= a[i]
		}

		flag := false
		for i := 1; i < n-1; i++ {
			right ^= a[i]
			if left == a[i] && right == a[i] {
				flag = true
				break
			}
			left ^= a[i]
		}

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

package main

import (
	"fmt"
)

func main() {
	var t int

	fmt.Scan(&t)
	for ; t > 0; t-- {
		var n int
		fmt.Scan(&n)

		curr := 0
		var a int
		for i := 0; i < n; i++ {
			fmt.Scan(&a)
			curr = curr ^ a
		}

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

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var buf bytes.Buffer
	tc := readNum(reader)

	for tc > 0 {
		tc--
		n := readNum(reader)
		A := readNNums(reader, n)
		res := solve(n, A)
		if res {
			buf.WriteString("YES\n")
		} else {
			buf.WriteString("NO\n")
		}
	}
	fmt.Print(buf.String())
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}
func solve(n int, A []int) bool {
	var res int
	for i := 0; i < n; i++ {
		res ^= A[i]
	}

	return res == 0
}

package main

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

func run(or io.Reader, ow io.Writer) {
	in := bufio.NewReader(or)
	out := bufio.NewWriter(ow)
	defer out.Flush()

	solve := func() {
		var n int
		fmt.Fscan(in, &n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
		}
		b := a
		for i := 1; i < n; i++ {
			b[i] = b[i-1] ^ a[i]
		}
		if b[n-1] == 0 {
			fmt.Fprint(out, "YES")
		} else {
			var ok = false
			for i := 0; i < n-2; i++ {
				if b[i] == b[n-1] {
					ok = true
				}
			}
			if ok {
				fmt.Fprint(out, "YES")
			} else {
				fmt.Fprint(out, "NO")
			}

		}

		fmt.Fprintln(out)
	}

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

}

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

package main

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

func run(or io.Reader, ow io.Writer) {
	in := bufio.NewReader(or)
	out := bufio.NewWriter(ow)
	defer out.Flush()

	solve := func() {
		var n int
		fmt.Fscan(in, &n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
		}
		b := a
		for i := 1; i < n; i++ {
			b[i] = b[i-1] ^ a[i]
		}
		if b[n-1] == 0 {
			fmt.Print(out, "YES")
		} else {
			var ok = false
			for i := 0; i < n-2; i++ {
				if b[i] == b[n-1] {
					ok = true
				}
			}
			if ok {
				fmt.Print(out, "YES")
			} else {
				fmt.Print(out, "NO")
			}

		}

		fmt.Fprintln(out)
	}

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

}

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

package main

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

const REPEAT bool = true
const CASE   bool = false

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

  if REPEAT {
    var t int
    Fscan(in, &t);
    for i := 1; i <= t; i++ {
      if CASE {
        Fprintf(out, "Case #%d: ", i)
      }
      solve(in, out)
    }
  } else {
    solve(in, out)
  }
}

func solve(in io.Reader, out io.Writer) {
  var n, e int
  Fscan(in, &n)
  cntr := make(map[int]int)
  for i := 0; i < n; i++ {
    Fscan(in, &e)
    if val, ok := cntr[e]; !ok {
      cntr[e] = 1
    } else {
      cntr[e] = val + 1
    }
  }
  for num := range cntr {
    cntr[num] -= 1
    val := mapXOR(cntr)
    if val == num {
      Fprintln(out, "YES")
      return
    }
    if cntr[num] > 0 {
      cntr[num] -= 1
      val := mapXOR(cntr)
      if val == num {
        Fprintln(out, "YES")
        return
      }
      cntr[num] += 1
    }
    cntr[num] += 1
  }
  Fprintln(out, "NO")
}

func mapXOR(cnts map[int]int) int {
  ans := 0
  for num := range cnts {
    cnt := cnts[num]
    if cnt % 2 != 0 {
      ans ^= num
    }
  }
  return ans
}


Woken up by the alarm clock Igor the financial analyst hurried up to the work. He ate his breakfast and sat in his car. Sadly, when he opened his GPS navigator, he found that some of the roads in Bankopolis, the city where he lives, are closed due to road works. Moreover, Igor has some problems with the steering wheel, so he can make no more than two turns on his way to his office in bank.Bankopolis looks like a grid of n rows and m columns. Igor should find a way from his home to the bank that has no more than two turns and doesn't contain cells with road works, or determine that it is impossible and he should work from home. A turn is a change in movement direction. Igor's car can only move to the left, to the right, upwards and downwards. Initially Igor can choose any direction. Igor is still sleepy, so you should help him.

The first line contains two integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the grid. Each of the next n lines contains m characters denoting the corresponding row of the grid. The following characters can occur:    "." — an empty cell;  "*" — a cell with road works;  "S" — the cell where Igor's home is located;  "T" — the cell where Igor's office is located.  It is guaranteed that "S" and "T" appear exactly once each.

In the only line print "YES" if there is a path between Igor's home and Igor's office with no more than two turns, and "NO" otherwise.

NoteThe first sample is shown on the following picture:  In the second sample it is impossible to reach Igor's office using less that 4 turns, thus there exists no path using no more than 2 turns. The path using exactly 4 turns is shown on this picture:

package main

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

type node struct {
	r, c, dir int
}

var (
	dr = [4]int{-1, 0, 1, 0}
	dc = [4]int{0, -1, 0, 1}
)

var dist [1000][1000][4]int
var used [1000][1000][4]bool

func getDist(v node) (int, bool) {
	return dist[v.r][v.c][v.dir], used[v.r][v.c][v.dir]
}

func setDist(v node, d int) {
	dist[v.r][v.c][v.dir] = d
	used[v.r][v.c][v.dir] = true
}

func solve(s [][]byte) bool {
	n := len(s)
	m := len(s[0])
	var q []node
findS:
	for i := range s {
		for j := range s[i] {
			if s[i][j] == 'S' {
				for dir := 0; dir < 4; dir++ {
					v := node{i, j, dir}
					q = append(q, v)
					setDist(v, 0)
				}
				break findS
			}
		}
	}

	for len(q) > 0 {
		v := q[0]
		q = q[1:]
		if s[v.r][v.c] == 'T' {
			return true
		}

		consider := func(u node, d int) {
			if u.r >= 0 && u.r < n && u.c >= 0 && u.c < m && s[u.r][u.c] != '*' {
				if _, ok := getDist(u); !ok {
					setDist(u, d)
					q = append(q, u)
				}
			}
		}

		d, _ := getDist(v)
		consider(node{
			r:   v.r + dr[v.dir],
			c:   v.c + dc[v.dir],
			dir: v.dir,
		}, d)
		if d == 2 {
			continue
		}
		for _, dir := range []int{(v.dir + 1) % 4, (v.dir - 1 + 4) % 4} {
			consider(node{
				r:   v.r,
				c:   v.c,
				dir: dir,
			}, d+1)
		}
	}
	return false
}

func main() {
	in := bufio.NewReaderSize(os.Stdin, 2<<16)
	var n, m int
	fmt.Fscanf(in, "%d %d\n", &n, &m)
	s := make([][]byte, n)
	for i := range s {
		fmt.Fscanf(in, "%s\n", &s[i])
	}

	if solve(s) {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}
}

package main

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

func I(i ...interface{}) error {
	_, err := fmt.Fscan(r, i...)
	return err
}

func O(o ...interface{}) {
	fmt.Fprint(w, o...)
}

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

func init() {
	if f, err := os.Open("in.goC"); err == nil {
		r = bufio.NewReader(f)
	} else {
		r = bufio.NewReader(os.Stdin)
	}
	w = bufio.NewWriter(os.Stdout)
}

var n, m int

func main() {
	defer w.Flush()
	for I(&n, &m) == nil {
		solve(n, m)
	}
}

var in []string
var ii, iii, jj, jjj int
var visited [][][5][5]bool

func solve(n, m int) {
	in = make([]string, n)
	visited = make([][][5][5]bool, n)
	for i := range in {
		I(&in[i])
		visited[i] = make([][5][5]bool, m)
		for j, ch := range in[i] {
			if ch == 'S' {
				ii = i
				jj = j
			} else if ch == 'T' {
				iii = i
				jjj = j
			}
		}
	}
	if dfs(ii, jj, 0, 0) < 3 {
		O("YES\n")
	} else {
		O("NO\n")
	}
}

func dfs(i, j, q, dir int) int {
	if i < 0 || j < 0 || i > n-1 || j > m-1 || in[i][j] == '*' || q > 2 || visited[i][j][q][dir] {
		return 1e9
	}
	visited[i][j][q][dir] = true
	if i == iii && j == jjj {
		return q
	}
	ans := int(1e9)
	var a, b int
	switch dir {
	case 1:
		a++
	case 2:
		b++
	}
	min(dfs(i+1, j, q+b, 1), &ans)
	min(dfs(i, j+1, q+a, 2), &ans)
	min(dfs(i-1, j, q+b, 1), &ans)
	min(dfs(i, j-1, q+a, 2), &ans)
	return ans
}

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

package main

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

func I(i ...interface{}) error {
	_, err := fmt.Fscan(r, i...)
	return err
}

func O(o ...interface{}) {
	fmt.Fprint(w, o...)
}

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

func init() {
	if f, err := os.Open("in.goC"); err == nil {
		r = bufio.NewReader(f)
	} else {
		r = bufio.NewReader(os.Stdin)
	}
	w = bufio.NewWriter(os.Stdout)
}

var n, m int

func main() {
	defer w.Flush()
	for I(&n, &m) == nil {
		solve(n, m)
	}
}

var in []string
var ii, iii, jj, jjj int
var visited [][][5][5]bool
var dp [][]int

func solve(n, m int) {
	in = make([]string, n)
	visited = make([][][5][5]bool, n)
	dp = make([][]int, n)
	for i := range in {
		I(&in[i])
		visited[i] = make([][5][5]bool, m)
		dp[i] = make([]int, m)
		for j, ch := range in[i] {
			if ch == 'S' {
				ii = i
				jj = j
			} else if ch == 'T' {
				iii = i
				jjj = j
			}
			dp[i][j] = 1e9 * 2
		}
	}
	dfs(ii, jj, 0, 0)
	if dp[iii][jjj] < 3 {
		O("YES\n")
	} else {
		O("NO\n")
	}
}

func dfs(i, j, q, dir int) {
	if i < 0 || j < 0 || i > n-1 || j > m-1 || q > 2 || in[i][j] == '*' {
		return
	}
	min(q, &dp[i][j])
	if visited[i][j][dir][q] {
		return
	}
	visited[i][j][dir][q] = true
	var a, b int
	switch dir {
	case 1:
		a++
	case 2:
		b++
	}
	dfs(i+1, j, q+b, 1)
	dfs(i, j+1, q+a, 2)
	dfs(i-1, j, q+b, 1)
	dfs(i, j-1, q+a, 2)
}

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

package main

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

func solve() {
	n := readInt()
	m := readInt()
	mm := make([][]int, n)
	startX := 0
	startY := 0
	for i := 0; i < n; i++ {
		mm[i] = make([]int, m)
		for k, v := range readString() {
			switch v {
			case 'S':
				mm[i][k] = 3
				startY = i
				startX = k
			case 'T':
				mm[i][k] = 2
			case '*':
				mm[i][k] = 1
			}
		}
	}
	yx := [][]int{
		{1, 0},
		{-1, 0},
		{0, 1},
		{0, -1},
	}
	for _, v := range yx {

		vis := make([][]int, n)
		for i := 0; i < n; i++ {
			vis[i] = make([]int, m)
			for j := 0; j < m; j++ {
				vis[i][j] = 1000000
			}
		}

		q := make([][5]int, 0)
		q = append(q, [5]int{startY, startX, 0, v[0], v[1]})
		vis[startY][startX] = 0
		for len(q) > 0 {
			cur := q[0]
			q = q[1:]
			for _, curdir := range yx {
				cnt := cur[2]
				if curdir[0] != cur[3] || curdir[1] != cur[4] {
					cnt++
					if cnt > 2 {
						continue
					}
				}
				ny := cur[0] + curdir[0]
				nx := cur[1] + curdir[1]
				if ny >= 0 && ny < n && nx >= 0 && nx < m &&
					mm[ny][nx] != 1 && vis[ny][nx] > cnt {
					if mm[ny][nx] == 2 {
						fmt.Println("YES")
						return
					}
					q = append(q, [5]int{ny, nx, cnt, curdir[0], curdir[1]})
					vis[ny][nx] = cnt
				}
			}
		}
	}
	fmt.Println("NO")
}

var scanner *bufio.Scanner

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

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

// IO

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

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

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

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

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

package main

import (
	"bufio"
	"container/list"
	"fmt"
	"os"
	"strconv"
)

const MAX_N = 1001

type State struct {
	y    int
	x    int
	dir  int
	turn int
}

var visited [3][MAX_N][MAX_N]bool

func main() {
	fsc := NewFastScanner()
	N, M := fsc.NextInt(), fsc.NextInt()
	board := make([]string, 0)
	for i := 0; i < N; i++ {
		board = append(board, fsc.Next())
	}
	sy, sx := -1, -1
	gy, gx := -1, -1
	DX := []int{1, 0, -1, 0}
	DY := []int{0, -1, 0, 1}
	for i := 0; i < N; i++ {
		for j := 0; j < M; j++ {
			if board[i][j] == 'S' {
				sy = i
				sx = j
			}
			if board[i][j] == 'T' {
				gy = i
				gx = j
			}
		}
	}
	que := list.New()
	que.PushBack(State{sy, sx, -1, 0})

	for que.Len() > 0 {
		pos := que.Front()
		val := pos.Value.(State)
		py := val.y
		px := val.x
		turn := val.turn
		pDir := val.dir
		que.Remove(pos)
		if py == gy && px == gx {
			fmt.Println("YES")
			return
		}
		if visited[turn][py][px] {
			continue
		}
		visited[turn][py][px] = true
		//nexpt
		// const dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

		for dir := 0; dir < 4; dir++ {
			ny := py + DY[dir]
			nx := px + DX[dir]
			nTurn := turn
			if pDir != -1 && pDir != dir {
				nTurn++
			}
			if nTurn > 2 {
				continue
			}
			if ny < 0 || ny >= N || nx < 0 || nx >= M {
				continue
			}
			if board[ny][nx] == '*' {
				continue
			}
			que.PushBack(State{ny, nx, dir, nTurn})
		}
	}
	fmt.Println("NO")
}

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

func NewFastScanner() *FastScanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1024)
	return &FastScanner{r: rdr}
}
func (s *FastScanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *FastScanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *FastScanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *FastScanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

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

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

func Int64Max(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}
func Float64Max(a, b float64) float64 {
	if a < b {
		return b
	}
	return a
}

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

func Int64Min(a, b int64) int64 {
	if a > b {
		return b
	}
	return a
}
func Float64Min(a, b float64) float64 {
	if a > b {
		return b
	}
	return a
}

package main

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

var in = bufio.NewScanner(os.Stdin)

func readInt() int {
	if !in.Scan() {
		panic(in.Err())
	}
	res := 0
	for _, b := range in.Bytes() {
		res = res*10 + int(b-'0')
	}
	return res
}

func readBytes() []byte {
	if !in.Scan() {
		panic(in.Err())
	}
	return in.Bytes()
}

func solve(s [][]byte) bool {
	n := len(s)
	m := len(s[0])
	var q [][2]int
findS:
	for i := range s {
		for j := range s[i] {
			if s[i][j] == 'S' {
				q = append(q, [2]int{i, j})
				s[i][j] = 0
				break findS
			}
		}
	}
	for len(q) > 0 {
		p := q[0]
		q = q[1:]
		next := s[p[0]][p[1]] + 1
		check := func(i, j int) int {
			switch s[i][j] {
			case 'T':
				return 1 // return true
			case '*':
				return -1 // break
			case '.':
				if next < 3 {
					s[i][j] = next
					q = append(q, [2]int{i, j})
				}
			}
			return 0
		}
		for i := p[0] - 1; i >= 0; i-- {
			if c := check(i, p[1]); c == -1 {
				break
			} else if c == 1 {
				return true
			}
		}
		for i := p[0] + 1; i < n; i++ {
			if c := check(i, p[1]); c == -1 {
				break
			} else if c == 1 {
				return true
			}
		}
		for i := p[1] - 1; i >= 0; i-- {
			if c := check(p[0], i); c == -1 {
				break
			} else if c == 1 {
				return true
			}
		}
		for i := p[1] + 1; i < m; i++ {
			if c := check(p[0], i); c == -1 {
				break
			} else if c == 1 {
				return true
			}
		}
	}
	return false
}

func main() {
	in.Split(bufio.ScanWords)
	s := make([][]byte, readInt())
	readInt()
	for i := range s {
		s[i] = readBytes()
	}

	if solve(s) {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}
}

package main

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

func I(i ...interface{}) error {
	_, err := fmt.Fscan(r, i...)
	return err
}

func O(o ...interface{}) {
	fmt.Fprint(w, o...)
}

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

func init() {
	if f, err := os.Open("in.goC"); err == nil {
		r = bufio.NewReader(f)
	} else {
		r = bufio.NewReader(os.Stdin)
	}
	w = bufio.NewWriter(os.Stdout)
}

var n, m int

func main() {
	defer w.Flush()
	for I(&n, &m) == nil {
		solve(n, m)
	}
}

var in []string
var ii, iii, jj, jjj int
var visited [][][5][5]bool

func solve(n, m int) {
	in = make([]string, n)
	visited = make([][][5][5]bool, n)
	for i := range in {
		I(&in[i])
		visited[i] = make([][5][5]bool, m)
		for j, ch := range in[i] {
			if ch == 'S' {
				ii = i
				jj = j
			} else if ch == 'T' {
				iii = i
				jjj = j
			}
		}
	}
	if dfs(ii, jj, 0, 0) < 3 {
		O("YES\n")
	} else {
		O("NO\n")
	}
}

func dfs(i, j, q, dir int) int {
	if i < 0 || j < 0 || i > n-1 || j > m-1 || in[i][j] == '*' || q > 2 || visited[i][j][q][dir] {
		return 1e9
	}
	visited[i][j][q][dir] = true
	if i == iii && j == jjj {
		return q
	}
	ans := int(1e9)
	var a, b int
	switch dir % 2 {
	case 1:
		a++
	case 0:
		b++
	}
	min(dfs(i+1, j, q+b, 1), &ans)
	min(dfs(i, j+1, q+a, 2), &ans)
	min(dfs(i-1, j, q+b, 3), &ans)
	min(dfs(i, j-1, q+a, 4), &ans)
	return ans
}

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

package main

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

func I(i ...interface{}) error {
	_, err := fmt.Fscan(r, i...)
	return err
}

func O(o ...interface{}) {
	fmt.Fprint(w, o...)
}

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

func init() {
	if f, err := os.Open("in.goC"); err == nil {
		r = bufio.NewReader(f)
	} else {
		r = bufio.NewReader(os.Stdin)
	}
	w = bufio.NewWriter(os.Stdout)
}

var n, m int

func main() {
	defer w.Flush()
	for I(&n, &m) == nil {
		solve(n, m)
	}
}

var in []string
var ii, iii, jj, jjj int
var visited [][]bool
var dp [][]int

func solve(n, m int) {
	in = make([]string, n)
	visited = make([][]bool, n)
	dp = make([][]int, n)
	for i := range in {
		I(&in[i])
		visited[i] = make([]bool, m)
		dp[i] = make([]int, m)
		for j, ch := range in[i] {
			if ch == 'S' {
				ii = i
				jj = j
			} else if ch == 'T' {
				iii = i
				jjj = j
			}
			dp[i][j] = 1e9 * 2
		}
	}
	dfs(ii, jj, 0, 0)
	if dp[iii][jjj] < 3 {
		O("YES\n")
	} else {
		O("NO\n")
	}
}

func dfs(i, j, q, dir int) {
	if i < 0 || j < 0 || i > n-1 || j > m-1 || in[i][j] == '*' {
		return
	}
	min(q, &dp[i][j])
	if visited[i][j] {
		return
	}
	visited[i][j] = true
	var a, b int
	switch dir {
	case 1:
		a++
	case 2:
		b++
	}
	dfs(i, j-1, q+a, 2)
	dfs(i+1, j, q+b, 1)
	dfs(i, j+1, q+a, 2)
	dfs(i-1, j, q+b, 1)
}

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

package main

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

func I(i ...interface{}) error {
	_, err := fmt.Fscan(r, i...)
	return err
}

func O(o ...interface{}) {
	fmt.Fprint(w, o...)
}

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

func init() {
	if f, err := os.Open("in.goC"); err == nil {
		r = bufio.NewReader(f)
	} else {
		r = bufio.NewReader(os.Stdin)
	}
	w = bufio.NewWriter(os.Stdout)
}

var n, m int

func main() {
	defer w.Flush()
	for I(&n, &m) == nil {
		solve(n, m)
	}
}

var in []string
var ii, iii, jj, jjj int
var visited [][]bool
var dp [][]int

func solve(n, m int) {
	in = make([]string, n)
	visited = make([][]bool, n)
	dp = make([][]int, n)
	for i := range in {
		I(&in[i])
		visited[i] = make([]bool, m)
		dp[i] = make([]int, m)
		for j, ch := range in[i] {
			if ch == 'S' {
				ii = i
				jj = j
			} else if ch == 'T' {
				iii = i
				jjj = j
			}
			dp[i][j] = 1e9 * 2
		}
	}
	dfs(ii, jj, 0, 0)
	if dp[iii][jjj] < 3 {
		O("YES\n")
	} else {
		O("NO\n")
	}
}

func dfs(i, j, q, dir int) {
	if i < 0 || j < 0 || i > n-1 || j > m-1 || in[i][j] == '*' {
		return
	}
	min(q, &dp[i][j])
	if visited[i][j] {
		return
	}
	visited[i][j] = true
	var a, b int
	switch dir {
	case 1:
		a++
	case 2:
		b++
	}
	dfs(i, j-1, q+a, 2)
	dfs(i+1, j, q+b, 1)
	dfs(i, j+1, q+a, 2)
	dfs(i-1, j, q+b, 1)
	visited[i][j] = true
}

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

package main

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

func I(i ...interface{}) error {
	_, err := fmt.Fscan(r, i...)
	return err
}

func O(o ...interface{}) {
	fmt.Fprint(w, o...)
}

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

func init() {
	if f, err := os.Open("in.goC"); err == nil {
		r = bufio.NewReader(f)
	} else {
		r = bufio.NewReader(os.Stdin)
	}
	w = bufio.NewWriter(os.Stdout)
}

var n, m int

func main() {
	defer w.Flush()
	for I(&n, &m) == nil {
		solve(n, m)
	}
}

var in []string
var ii, iii, jj, jjj int
var visited [][]bool

func solve(n, m int) {
	in = make([]string, n)
	visited = make([][]bool, n)
	for i := range in {
		I(&in[i])
		visited[i] = make([]bool, m)
		for j, ch := range in[i] {
			if ch == 'S' {
				ii = i
				jj = j
			} else if ch == 'T' {
				iii = i
				jjj = j
			}
		}
	}
	if dfs(ii, jj, 0, 0) < 3 {
		O("YES\n")
	} else {
		O("NO\n")
	}
}

func dfs(i, j, q, dir int) int {
	if i < 0 || j < 0 || i > n-1 || j > m-1 || in[i][j] == '*' || q > 2 || visited[i][j] {
		return 1e9
	}
	visited[i][j] = true
	if i == iii && j == jjj {
		return q
	}
	ans := int(1e9)
	var a, b int
	switch dir {
	case 1:
		a++
	case 2:
		b++
	}
	min(dfs(i+1, j, q+b, 1), &ans)
	min(dfs(i, j+1, q+a, 2), &ans)
	min(dfs(i-1, j, q+b, 1), &ans)
	min(dfs(i, j-1, q+a, 2), &ans)
	return ans
}

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

package main

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

func solve() {
	n := readInt()
	m := readInt()
	mm := make([][]int, n)
	startX := 0
	startY := 0
	for i := 0; i < n; i++ {
		mm[i] = make([]int, m)
		for k, v := range readString() {
			switch v {
			case 'S':
				mm[i][k] = 3
				startX = k
				startY = i
			case 'T':
				mm[i][k] = 2
			case '*':
				mm[i][k] = 1
			}
		}
	}
	yx := [][]int{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}
	for _, v := range yx {

		vis := make([][]bool, n)
		for i := 0; i < n; i++ {
			vis[i] = make([]bool, m)
		}

		q := make([][5]int, 0)
		q = append(q, [5]int{startY, startX, 0, v[0], v[1]})
		vis[startY][startX] = true
		for len(q) > 0 {
			cur := q[0]
			q = q[1:]
			for _, curdir := range yx {
				cnt := cur[2]
				ny := cur[0] + curdir[0]
				nx := cur[1] + curdir[1]
				if curdir[0] != cur[3] || curdir[1] != cur[4] {
					cnt++
				}
				if cnt <= 2 && ny >= 0 && ny < n && nx >= 0 && nx < m &&
					mm[ny][nx] != 1 && !vis[ny][nx] {
					if mm[ny][nx] == 2 {
						fmt.Println("YES")
						return
					}
					q = append(q, [5]int{ny, nx, cnt, curdir[0], curdir[1]})
					vis[ny][nx] = true
				}
			}
		}
	}
	fmt.Println("NO")
}

var scanner *bufio.Scanner

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

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

// IO

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

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

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

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

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


Santa Claus has Robot which lives on the infinite grid and can move along its lines. He can also, having a sequence of m points p1, p2, ..., pm with integer coordinates, do the following: denote its initial location by p0. First, the robot will move from p0 to p1 along one of the shortest paths between them (please notice that since the robot moves only along the grid lines, there can be several shortest paths). Then, after it reaches p1, it'll move to p2, again, choosing one of the shortest ways, then to p3, and so on, until he has visited all points in the given order. Some of the points in the sequence may coincide, in that case Robot will visit that point several times according to the sequence order.While Santa was away, someone gave a sequence of points to Robot. This sequence is now lost, but Robot saved the protocol of its unit movements. Please, find the minimum possible length of the sequence.

The first line of input contains the only positive integer n (1 ≤ n ≤ 2·105) which equals the number of unit segments the robot traveled. The second line contains the movements protocol, which consists of n letters, each being equal either L, or R, or U, or D. k-th letter stands for the direction which Robot traveled the k-th unit segment in: L means that it moved to the left, R — to the right, U — to the top and D — to the bottom. Have a look at the illustrations for better explanation.

The only line of input should contain the minimum possible length of the sequence.

NoteThe illustrations to the first three tests are given below.  The last example illustrates that each point in the sequence should be counted as many times as it is presented in the sequence.

package main

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

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

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

	var n int
	fmt.Fscanf(bufin, "%d\n", &n)
	var s string
	fmt.Fscanf(bufin, "%s\n", &s)
	res := 1
	hor := byte('*')
	ver := byte('*')
	for i := 0; i < n; i++ {
		dir := byte(s[i])
		// fmt.Fprintf(bufout, "dir = %c, ver = %c, hor = %c\n", dir, ver, hor)
		if (dir == 'U') || (dir == 'D') {
			if (ver != '*') && (ver != dir) {
				hor = byte('*')
				ver = byte('*')
				res++
			}
			ver = dir
		} else {
			if (hor != '*') && (hor != dir) {
				hor = byte('*')
				ver = byte('*')
				res++
			}
			hor = dir
		}
	}
	fmt.Fprintf(bufout, "%d\n", res)
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scanln(&n)

	var s string
	fmt.Scanln(&s)

	var x, y = 0, 0
	var ans = 1

	for _, c := range s {
		switch c {
		case 'L':
			if abs(x-1) > abs(x) {
				x--
			} else {
				x, y, ans = -1, 0, ans+1
			}
		case 'R':
			if abs(x+1) > abs(x) {
				x++
			} else {
				x, y, ans = 1, 0, ans+1
			}
		case 'U':
			if abs(y+1) > abs(y) {
				y++
			} else {
				x, y, ans = 0, 1, ans+1
			}
		case 'D':
			if abs(y-1) > abs(y) {
				y--
			} else {
				x, y, ans = 0, -1, ans+1
			}
		}

		// fmt.Println(string(c), ans, x, y)
	}

	fmt.Println(ans)
}

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

package main

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

func solve() {
	readInt()
	path := readString()
	prevdir := map[rune]bool{}
	opdir := map[rune]rune{'R': 'L', 'L': 'R', 'D': 'U', 'U': 'D'}
	res := 1
	for _, v := range path {
		if _, ok := prevdir[opdir[v]]; ok {
			res++
			for k, _ := range prevdir {
				delete(prevdir, k)
			}
		}
		prevdir[v] = true
	}
	fmt.Println(res)
}

var scanner *bufio.Scanner

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

// IO

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

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

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

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

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

package main

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

func solve() {
	readInt()
	path := readString()
	prevdir := map[rune]bool{}
	opdir := map[rune]rune{'R': 'L', 'L': 'R', 'D': 'U', 'U': 'D'}
	res := 1
	for _, v := range path {
		if _, ok := prevdir[opdir[v]]; ok {
			res++
			prevdir = map[rune]bool{}
		}
		prevdir[v] = true
	}
	fmt.Println(res)
}

var scanner *bufio.Scanner

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

// IO

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

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

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

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

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

package main

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

func solve() {
	readInt()
	path := readString()
	prevdir := 0
	res := 1
	for _, v := range path {
		value := 0
		switch v {
		case 'R':
			value = 1
		case 'L':
			value = 2
		case 'U':
			value = 4
		case 'D':
			value = 8
		}
		prevdir |= value
		if ((prevdir&1) != 0 && (prevdir&2) != 0) ||
			((prevdir&4) != 0 && (prevdir&8) != 0) {
			res++
			prevdir = 0
		}
		prevdir |= value
	}
	fmt.Println(res)
}

var scanner *bufio.Scanner

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

// IO

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

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

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

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

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


User ainta decided to paint a wall. The wall consists of n2 tiles, that are arranged in an n × n table. Some tiles are painted, and the others are not. As he wants to paint it beautifully, he will follow the rules below.  Firstly user ainta looks at the wall. If there is at least one painted cell on each row and at least one painted cell on each column, he stops coloring. Otherwise, he goes to step 2.  User ainta choose any tile on the wall with uniform probability.  If the tile he has chosen is not painted, he paints the tile. Otherwise, he ignores it.  Then he takes a rest for one minute even if he doesn't paint the tile. And then ainta goes to step 1.   However ainta is worried if it would take too much time to finish this work. So he wants to calculate the expected time needed to paint the wall by the method above. Help him find the expected time. You can assume that choosing and painting any tile consumes no time at all.

The first line contains two integers n and m (1 ≤ n ≤ 2·103; 0 ≤ m ≤ min(n2, 2·104)) — the size of the wall and the number of painted cells. Next m lines goes, each contains two integers ri and ci (1 ≤ ri, ci ≤ n) — the position of the painted cell. It is guaranteed that the positions are all distinct. Consider the rows of the table are numbered from 1 to n. Consider the columns of the table are numbered from 1 to n.

In a single line print the expected time to paint the wall in minutes. Your answer will be considered correct if it has at most 10 - 4 absolute or relative error.

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	n, m   int
	br, bc int
	f      [][]float64
)

func foo(x, y int) (ret float64) {
	if f[x][y] > -1.0 {
		return f[x][y]
	}

	defer func() {
		f[x][y] = ret
	}()

	if x == 0 {
		for i := y; i >= 1; i-- {
			ret += float64(n) / float64(i)
		}
		return
	}

	if y == 0 {
		for i := x; i >= 1; i-- {
			ret += float64(n) / float64(i)
		}
		return
	}

	nn := float64(n * n)

	p1 := (float64(x*y) / nn)
	e1 := foo(x-1, y-1) * p1

	p2 := float64((n-y)*x) / nn
	e2 := foo(x-1, y) * p2

	p3 := float64((n-x)*y) / nn
	e3 := foo(x, y-1) * p3

	ret = (e1 + e2 + e3 + 1) / (p1 + p2 + p3)
	return
}

func main() {
	fmt.Fscan(reader, &n, &m)

	f = make([][]float64, n+1)
	for i := 0; i < len(f); i++ {
		f[i] = make([]float64, n+1)
		for j := 0; j < len(f[i]); j++ {
			f[i][j] = -10.0
		}
	}

	rmap := map[int]bool{}
	cmap := map[int]bool{}

	for i := 0; i < m; i++ {
		var r, c int
		fmt.Fscan(reader, &r, &c)
		rmap[r] = true
		cmap[c] = true
	}

	ans := foo(n-len(rmap), n-len(cmap))
	fmt.Printf("%.10f\n", ans)
}

package main

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

var expect, prob [2002][2002]float64

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)
	var n, m int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	row := make([]bool, n+1)
	col := make([]bool, n+1)
	var rowCount, colCount int
	for i := 0; i < m; i++ {
		var r, c int
		fmt.Fscanf(reader, "%d %d\n", &r, &c)
		if !row[r] {
			rowCount++
			row[r] = true
		}
		if !col[c] {
			colCount++
			col[c] = true
		}
	}
	prob[rowCount][colCount] = 1.0
	for i := rowCount; i <= n; i++ {
		for j := colCount; j <= n; j++ {
			p := float64(i*j) / float64(n*n)
			pCol := float64((n-j)*i) / float64(n*n)
			pRow := float64((n-i)*j) / float64(n*n)
			pRowCol := float64((n-j)*(n-i)) / float64(n*n)

			expect[i][j+1] += (expect[i][j]*pCol/(1-p) + pCol/(1-p)/(1-p)*prob[i][j])
			expect[i+1][j] += (expect[i][j]*pRow/(1-p) + pRow/(1-p)/(1-p)*prob[i][j])
			expect[i+1][j+1] += (expect[i][j]*pRowCol/(1-p) + pRowCol/(1-p)/(1-p)*prob[i][j])

			prob[i][j+1] += pCol / (1 - p) * prob[i][j]
			prob[i+1][j] += pRow / (1 - p) * prob[i][j]
			prob[i+1][j+1] += pRowCol / (1 - p) * prob[i][j]
		}
	}
	fmt.Printf("%f", expect[n][n])
}


A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. The parent of a vertex $$$v$$$ (different from root) is the previous to $$$v$$$ vertex on the shortest path from the root to the vertex $$$v$$$. Children of the vertex $$$v$$$ are all vertices for which $$$v$$$ is the parent.You are given a rooted tree with $$$n$$$ vertices. The vertex $$$1$$$ is the root. Initially, all vertices are healthy.Each second you do two operations, the spreading operation and, after that, the injection operation:   Spreading: for each vertex $$$v$$$, if at least one child of $$$v$$$ is infected, you can spread the disease by infecting at most one other child of $$$v$$$ of your choice.  Injection: you can choose any healthy vertex and infect it. This process repeats each second until the whole tree is infected. You need to find the minimal number of seconds needed to infect the whole tree.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the number of the vertices in the given tree. The second line of each test case contains $$$n - 1$$$ integers $$$p_2, p_3, \ldots, p_n$$$ ($$$1 \le p_i \le n$$$), where $$$p_i$$$ is the ancestor of the $$$i$$$-th vertex in the tree. It is guaranteed that the given graph is a tree. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.

For each test case you should output a single integer — the minimal number of seconds needed to infect the whole tree.

NoteThe image depicts the tree from the first test case during each second.A vertex is black if it is not infected. A vertex is blue if it is infected by injection during the previous second. A vertex is green if it is infected by spreading during the previous second. A vertex is red if it is infected earlier than the previous second.  Note that you are able to choose which vertices are infected by spreading and by injections.

package main

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

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

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

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

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

		m := make(map[int]int)
		for i := 0; i < n-1; i++ {
			var v int
			fmt.Fscan(in, &v)

			m[v]++
		}

		type vx struct {
			id, c int
		}

		a := []vx{}
		for k, v := range m {
			a = append(a, vx{k, v - 1})
		}

		sort.Slice(a, func(i, j int) bool {
			return a[i].c > a[j].c
		})

		k := len(a)
		var rem int
		for i, vx := range a {
			rem += vx.c - min(vx.c, k)
			a[i].c -= k
			k--
		}

		if rem <= 0 {
			fmt.Fprintln(out, len(a)+1)
		} else {
			tot := len(a)
			b := make([]int, len(a))
			for i := range a {
				b[i] = a[i].c
			}
			for rem > 0 {
				var maxIdx int
				for i := range b {
					if b[i] > 0 {
						b[i]--
						rem--

						if b[i] > b[maxIdx] {
							maxIdx = i
						}
					}
				}

				b[maxIdx]--
				rem--
				tot++
			}
			fmt.Fprintln(out, tot+1)
		}
	}
}

package main

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

const mod = 998244353

var ans int

func solve(in *bufio.Reader, out *bufio.Writer) {
	defer out.Flush()
	var n int
	fmt.Fscan(in,&n)
	degree := make([]int,n + 1)
	for i := 0;i < n - 1;i++{
		var item int
		fmt.Fscan(in,&item)
		degree[item - 1]++
	}
	degree[n] = 1
	sort.Slice(degree,func(i,j int)bool{
		return degree[i] > degree[j]
	})
	for len(degree) != 0 && degree[len(degree) - 1] == 0{
		degree = degree[:len(degree) - 1]
	}
	ans = len(degree)
	for i,v := range degree{
		if degree[i] != 0{
			degree[i] = v - (len(degree) - i)
		}else{
			break
		}
	}
	sort.Slice(degree,func(i,j int)bool{
		return degree[i] > degree[j]
	})
	for len(degree) != 0 && degree[len(degree) - 1] <= 0{
		degree = degree[:len(degree) - 1]
	}

	proc(degree)
	fmt.Fprintln(out,ans)
}
func proc(arr []int){
	if len(arr) == 0{
		return
	}
	last := 0
	for i,v := range arr{
		if v == arr[0]{
			last = i
		}else{
			break
		}
	}
	arr[last]--
	ans++
	for i := range arr{
		if arr[i] != 0{
			arr[i]--
		}else{
			break
		}
	}
	for len(arr) != 0 && arr[len(arr) - 1] == 0{
		arr = arr[:len(arr) - 1]
	}
	proc(arr)
}

func abs(a,b int)int{
	if a > b{
		return a - b
	}
	return b - a
}
func sum(a,b int64)int64{
	return (b + a) * (b - a + 1) / 2
}

func max(a,b int)int{
	if a > b{
		return a
	}
	return b
}
func min(a,b int)int{
	if a > b{
		return b
	}
	return a
}
func gcd(a,b int)int{
	if a % b == 0{
		return b
	}
	return gcd(b,a%b)
}

func main() {
	stdin := os.Stdin
	stdout := os.Stdout
	in := bufio.NewReader(stdin)
	out := bufio.NewWriter(stdout)
	n := 1
	fmt.Fscan(in, &n)
	for i := 0; i < n; i++ {
		solve(in, out)
	}
}

package main

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

const mod = 998244353

func solve(in *bufio.Reader, out *bufio.Writer) {
	defer out.Flush()
	var n int
	fmt.Fscan(in,&n)
	degree := make([]int,n + 1)
	for i := 0;i < n - 1;i++{
		var item int
		fmt.Fscan(in,&item)
		degree[item - 1]++
	}
	degree[n] = 1
	sort.Slice(degree,func(i,j int)bool{
		return degree[i] > degree[j]
	})
	l := 1
	r := n
	for l <= r{
		mid := (l + r) / 2
		inj := mid
		rest := 0
		for i,v := range degree{
			if v == 0{
				break
			}
			inj--
			if i + v > mid{
				rest += (i + v) - mid
			}
		}

		if inj - rest >= 0{
			r = mid - 1
		}else{
			l = mid + 1
		}
	}
	fmt.Fprintln(out,l)
}

func abs(a,b int)int{
	if a > b{
		return a - b
	}
	return b - a
}
func sum(a,b int64)int64{
	return (b + a) * (b - a + 1) / 2
}

func max(a,b int)int{
	if a > b{
		return a
	}
	return b
}
func min(a,b int)int{
	if a > b{
		return b
	}
	return a
}
func gcd(a,b int)int{
	if a % b == 0{
		return b
	}
	return gcd(b,a%b)
}

func main() {
	stdin := os.Stdin
	stdout := os.Stdout
	in := bufio.NewReader(stdin)
	out := bufio.NewWriter(stdout)
	n := 1
	fmt.Fscan(in, &n)
	for i := 0; i < n; i++ {
		solve(in, out)
	}
}

package main

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

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

func A(_r io.Reader, _w io.Writer) {
	in, out := bufio.NewReader(_r), bufio.NewWriter(_w)
	defer out.Flush()
	var c int
	var n int
	Fscan(in, &c)
	for ; c > 0; c-- {
		Fscan(in, &n)
		arr := []int{}
		mp := make(map[int]int)
		for i := 2; i <= n; i++ {
			v := 0
			Fscan(in, &v)
			mp[v]++
		}
		for _, v := range mp {
			arr = append(arr, v)
		}
		arr = append(arr, 1)
		sort.Slice(arr, func(i, j int) bool {
			return arr[i] > arr[j]
		})

		ans := len(arr)
		for i := range arr {
			arr[i]--
			arr[i] -= len(arr) - i - 1
		}
		for len(arr) > 0 && arr[len(arr)-1] <= 0 {
			arr = arr[:len(arr)-1]
		}
		for len(arr) > 0 {
			//扩散
			for i := range arr {
				arr[i]--
			}
			for len(arr) > 0 && arr[len(arr)-1] <= 0 {
				arr = arr[:len(arr)-1]
			}
			//注射
			if len(arr) > 0 {
				arr[0]--
			}
			sort.Slice(arr, func(i, j int) bool {
				return arr[i] > arr[j]
			})
			for len(arr) > 0 && arr[len(arr)-1] <= 0 {
				arr = arr[:len(arr)-1]
			}
			ans++
		}
		Fprintln(out, ans)
	}
}

package main

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

func main_div2_781_C() {
	_in, _out := os.Stdin, os.Stdout
	in, out := bufio.NewReader(_in), bufio.NewWriter(_out)
	defer out.Flush()
	var cas int = 1
	Fscan(in, &cas)
	var sol func()
	/* Begin Coding */
	sol = func() {
		var n int
		Fscan(in, &n)
		children := make([][]int, n+1)
		var pa int
		parent := map[int]struct{}{}
		for i := 2; i <= n; i++ {
			Fscan(in, &pa)
			children[pa] = append(children[pa], i)
			parent[pa] = struct{}{}
		}
		sort.Slice(children, func(i, j int) bool {
			return len(children[i]) > len(children[j])
		})
		arr := []int{}
		Inject := len(parent) + 1
		for i := 0; i < len(children); i++ {
			if len(children[i]) == 0 {
				break
			}
			spreadTime := Inject - i - 1
			if len(children[i])-1 > spreadTime {
				// Inject += (len(children[i]) - 1 - spreadTime + 1) / 2
				arr = append(arr, len(children[i])-1-spreadTime)
			}
		}
		sort.Slice(arr, func(i, j int) bool {
			return arr[i] > arr[j]
		})
		for len(arr) > 0 {
			arr[0] -= 1
			Inject++
			for i := 0; i < len(arr); i++ {
				arr[i]--
			}
			sort.Slice(arr, func(i, j int) bool {
				return arr[i] > arr[j]
			})
			for arr[len(arr)-1] <= 0 {
				arr = arr[:len(arr)-1]
				if len(arr) == 0 {
					break
				}
			}
		}

		// Inject := len(parent) + 1
		// for i := 0; i < len(children); i++ {
		// 	if len(children[i]) == 0 {
		// 		break
		// 	}
		// 	spreadTime := Inject - i - 1
		// 	if len(children[i])-1 > spreadTime {
		// 		Inject += (len(children[i]) - 1 - spreadTime + 1) / 2
		// 	}
		// }
		Fprintln(out, Inject)
	}
	/* Code Ending */
	for i := 0; i < cas; i++ {
		sol()
	}

}

func main() {
	main_div2_781_C()
}

package main

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

func main() {
	in, out := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var n,k int
	fmt.Fscan(in, &n)
	for i := 0;i < n;i++ {
		fmt.Fscan(in,&k)
		tree := make(map[int]int)
		arr := []int{}
		for j := 2;j <= k;j++ {
			var t int
			fmt.Fscan(in,&t)
			tree[t]++
		}
		for _,v := range tree {
			arr = append(arr,v)
		}
		arr = append(arr,1)
		sort.Slice(arr, func(i, j int) bool {
			return arr[i] > arr[j]
		})
		res := len(arr)
		for i := range arr {
			arr[i] -= len(arr) - i
		}
		for len(arr) > 0 && arr[len(arr)-1] <= 0 {
			arr = arr[:len(arr)-1]
		}
		for len(arr) > 0 {
			for i := range arr {
				arr[i] --
			}
			for len(arr) > 0 && arr[len(arr)-1] <= 0{
				arr = arr[:len(arr)-1]
			}
			if len(arr) > 0 {
				arr[0]--
			}
			sort.Slice(arr, func(i, j int) bool {
				return arr[i] > arr[j]
			})
			for len(arr) > 0 && arr[len(arr)-1] <= 0{
				arr = arr[:len(arr)-1]
			}
			res++
		}
		fmt.Fprintln(out,res)
	}
}

package main

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

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)

		m := make(map[int]int)
		for i := 0; i < n-1; i++ {
			var v int
			fmt.Fscan(in, &v)

			m[v]++
		}

		type vx struct {
			id, c int
		}

		a := []vx{}
		for k, v := range m {
			a = append(a, vx{k, v - 1})
		}

		sort.Slice(a, func(i, j int) bool {
			return a[i].c > a[j].c
		})

		z := len(a)
		var tot int
		for _, v := range a {
			// fmt.Println(v.c - z)
			if v.c-z > 0 {
				tot += (v.c - z) / 2
				if (v.c-z)%2 != 0 {
					tot++
				}
			}
			tot++
			z--
		}

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

package main

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

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)

		m := make(map[int]int)
		for i := 0; i < n-1; i++ {
			var v int
			fmt.Fscan(in, &v)

			m[v]++
		}

		type vx struct {
			id, c int
		}

		a := []vx{}
		for k, v := range m {
			a = append(a, vx{k, v - 1})
		}

		sort.Slice(a, func(i, j int) bool {
			return a[i].c > a[j].c
		})

		z := len(a) + 1
		var tot int
		for _, v := range a {
			if v.c-z > 0 {
				tot += (v.c - z) / 2
				if (v.c-z)%2 != 0 {
					tot++
				}
			}
			tot++
			z--
		}

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

package main

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

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)

		m := make(map[int]int)
		for i := 0; i < n-1; i++ {
			var v int
			fmt.Fscan(in, &v)

			m[v]++
		}

		type vx struct {
			id, c int
		}

		a := []vx{}
		for k, v := range m {
			a = append(a, vx{k, v})
		}

		sort.Slice(a, func(i, j int) bool {
			return a[i].c > a[j].c
		})

		z := len(a) + 1
		var tot int
		for _, v := range a {
			if v.c-z > 0 {
				tot += (v.c - z) / 2
				if (v.c-z)%2 != 0 {
					tot++
				}
			}
			tot++
			z--
		}

		fmt.Fprintln(out, tot+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)

		m := make(map[int]int)
		u := 1
		var sibs int
		for i := 0; i < n-1; i++ {
			var v int
			fmt.Fscan(in, &v)

			if m[v] == 0 {
				u++
			} else {
				sibs++
			}
			m[v]++
		}

		if sibs-u <= 0 {
			fmt.Fprintln(out, u)
		} else {
			tot := u + (sibs / 2)
			// if sibs%2 != 0 {
			// 	tot++
			// }
			fmt.Fprintln(out, tot)
		}
	}
}

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)

		m := make(map[int]int)
		u := 1
		var sibs int
		for i := 0; i < n-1; i++ {
			var v int
			fmt.Fscan(in, &v)

			if m[v] == 0 {
				u++
			} else {
				sibs++
			}
			m[v]++
		}

		if sibs-u < 0 {
			fmt.Fprintln(out, u)
		} else {
			tot := u + (sibs / 2)
			// if sibs%2 != 0 {
			// 	tot++
			// }
			fmt.Fprintln(out, tot)
		}
	}
}

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)

		m := make(map[int]int)
		u := 1
		var sibs int
		for i := 0; i < n-1; i++ {
			var v int
			fmt.Fscan(in, &v)

			if m[v] == 0 {
				u++
			} else {
				sibs++
			}
			m[v]++
		}

		if sibs-u < 0 {
			fmt.Fprintln(out, u)
		} else {
			tot := u + sibs/2
			if sibs%2 != 0 {
				tot++
			}
			fmt.Fprintln(out, tot)
		}
	}
}

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)

		m := make(map[int]int)
		u := 1
		var sibs int
		for i := 0; i < n-1; i++ {
			var v int
			fmt.Fscan(in, &v)

			if m[v] == 0 {
				u++
			} else {
				sibs++
			}
			m[v]++
		}

		if sibs-u < 0 {
			fmt.Fprintln(out, u)
		} else {
			tot := u + (sibs-u)/2
			if sibs-u%2 != 0 {
				tot++
			}
			fmt.Fprintln(out, tot)
		}
	}
}

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)

		m := make(map[int]int)
		u := 1
		for i := 0; i < n-1; i++ {
			var v int
			fmt.Fscan(in, &v)

			_, ok := m[v]
			if !ok {
				u++
			}
			m[v]++
		}

		tot := u
		for _, v := range m {
			if v > tot {
				tot += (v - u) / 2
				if (v-u)%2 != 0 {
					tot++
				}
			}
		}
		fmt.Fprintln(out, tot)
	}
}

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)

		m := make(map[int]int)
		var u int
		for i := 0; i < n-1; i++ {
			var v int
			fmt.Fscan(in, &v)

			_, ok := m[v]
			if !ok {
				u++
			}
			m[v]++
		}

		tot := u
		for _, v := range m {
			if v > tot {
				tot += (v - u) / 2
				if (v-u)%2 != 0 {
					tot++
				}
			}
		}
		fmt.Fprintln(out, tot+1)
	}
}

package main

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

const mod = 998244353

func solve(in *bufio.Reader, out *bufio.Writer) {
	defer out.Flush()
	var n int
	fmt.Fscan(in,&n)
	degree := make([]int,n)
	for i := 0;i < n - 1;i++{
		var item int
		fmt.Fscan(in,&item)
		degree[item - 1]++
	}
	sort.Slice(degree,func(i,j int)bool{
		return degree[i] > degree[j]
	})
	l := 1
	r := int(1e5) + 100
	ans := -1
	for l <= r{
		mid := (l + r) / 2
		inj := mid
		for i,v := range degree{
			if v == 0{
				break
			}
			inj--
			if i + v > mid{
				inj -= i + v - mid
			}
		}
		if inj < 1{
			l = mid + 1
		}else{
			ans = mid
			r = r - 1
		}
	}
	fmt.Fprintln(out,ans)
}

func abs(a,b int)int{
	if a > b{
		return a - b
	}
	return b - a
}
func sum(a,b int64)int64{
	return (b + a) * (b - a + 1) / 2
}

func max(a,b int)int{
	if a > b{
		return a
	}
	return b
}
func min(a,b int)int{
	if a > b{
		return b
	}
	return a
}
func gcd(a,b int)int{
	if a % b == 0{
		return b
	}
	return gcd(b,a%b)
}

func main() {
	stdin := os.Stdin
	stdout := os.Stdout
	in := bufio.NewReader(stdin)
	out := bufio.NewWriter(stdout)
	n := 1
	fmt.Fscan(in, &n)
	for i := 0; i < n; i++ {
		solve(in, out)
	}
}

package main

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

const mod = 998244353

func solve(in *bufio.Reader, out *bufio.Writer) {
	defer out.Flush()
	var n int
	fmt.Fscan(in,&n)
	degree := make([]int,n)
	for i := 0;i < n - 1;i++{
		var item int
		fmt.Fscan(in,&item)
		degree[item - 1]++
	}
	sort.Slice(degree,func(i,j int)bool{
		return degree[i] > degree[j]
	})
	l := 1
	r := int(1e5) + 1
	for l <= r{
		mid := (l + r) / 2
		inj := mid
		for i,v := range degree{
			if v == 0{
				break
			}
			inj--
			if i + v > mid{
				inj -= i + v - mid
			}
		}
		if inj < 1{
			l = mid + 1
		}else{
			r = r - 1
		}
	}
	fmt.Println(l)
}

func abs(a,b int)int{
	if a > b{
		return a - b
	}
	return b - a
}
func sum(a,b int64)int64{
	return (b + a) * (b - a + 1) / 2
}

func max(a,b int)int{
	if a > b{
		return a
	}
	return b
}
func min(a,b int)int{
	if a > b{
		return b
	}
	return a
}
func gcd(a,b int)int{
	if a % b == 0{
		return b
	}
	return gcd(b,a%b)
}

func main() {
	stdin := os.Stdin
	stdout := os.Stdout
	in := bufio.NewReader(stdin)
	out := bufio.NewWriter(stdout)
	n := 1
	fmt.Fscan(in, &n)
	for i := 0; i < n; i++ {
		solve(in, out)
	}
}

package main

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

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

func A(_r io.Reader, _w io.Writer) {
	in, out := bufio.NewReader(_r), bufio.NewWriter(_w)
	defer out.Flush()
	var c int
	var n int
	Fscan(in, &c)
	for ; c > 0; c-- {
		Fscan(in, &n)
		arr := []int{}
		mp := make(map[int]int)
		for i := 2; i <= n; i++ {
			v := 0
			Fscan(in, &v)
			mp[v]++
		}
		for _, v := range mp {
			arr = append(arr, v)
		}
		arr = append(arr, 1)
		sort.Slice(arr, func(i, j int) bool {
			return arr[i] > arr[j]
		})
		p := 0
		x := 0
		ans := 0
		for n > 0 {
			ans++
			//注入
			for p < len(arr) && arr[p] <= 0 {
				p++
			}
			if p == len(arr) {
				p = 0
			}
			for p < len(arr) && arr[p] <= 0 {
				p++
			}
			arr[p]--
			n--
			p++
			if p == len(arr) {
				p = 0
			}
			//扩散
			for i := 0; i < min(x, len(arr)); i++ {
				if arr[i] > 0 {
					n--
					arr[i]--
				}
			}
			x++
		}
		Fprintf(out, "%d\n", ans)
	}
}
func min(i, j int) int {
	if i < j {
		return i
	}
	return j
}

package main

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

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

func A(_r io.Reader, _w io.Writer) {
	in, out := bufio.NewReader(_r), bufio.NewWriter(_w)
	defer out.Flush()
	var c int
	var n int
	Fscan(in, &c)
	for ; c > 0; c-- {
		Fscan(in, &n)
		arr := []int{}
		mp := make(map[int]int)
		for i := 2; i <= n; i++ {
			v := 0
			Fscan(in, &v)
			mp[v]++
		}
		for _, v := range mp {
			arr = append(arr, v)
		}
		arr = append(arr, 1)
		sort.Slice(arr, func(i, j int) bool {
			return arr[i] > arr[j]
		})
		Println(arr)
		p := 0
		x := 0
		ans := 0
		for n > 0 {
			ans++
			//注入
			for p < len(arr) && arr[p] <= 0 {
				p++
			}
			if p == len(arr) {
				p = 0
			}
			for p < len(arr) && arr[p] <= 0 {
				p++
			}
			arr[p]--
			n--
			p++
			if p == len(arr) {
				p = 0
			}
			//扩散
			for i := 0; i < min(x, len(arr)); i++ {
				if arr[i] > 0 {
					n--
					arr[i]--
				}
			}
			x++
		}
		Fprintf(out, "%d\n", ans)
	}
}
func min(i, j int) int {
	if i < j {
		return i
	}
	return j
}

package main

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

func main_div2_781_C() {
	_in, _out := os.Stdin, os.Stdout
	in, out := bufio.NewReader(_in), bufio.NewWriter(_out)
	defer out.Flush()
	var cas int = 1
	Fscan(in, &cas)
	var sol func()
	/* Begin Coding */
	sol = func() {
		var n int
		Fscan(in, &n)
		children := make([][]int, n+1)
		var pa int
		parent := map[int]struct{}{}
		for i := 2; i <= n; i++ {
			Fscan(in, &pa)
			children[pa] = append(children[pa], i)
			parent[pa] = struct{}{}
		}
		sort.Slice(children, func(i, j int) bool {
			return len(children[i]) > len(children[j])
		})
		Inject := len(parent) + 1
		//Fprintln(out, "Inject:", Inject)
		for i := 0; i < len(children); i++ {
			if len(children[i]) == 0 {
				break
			}
			spreadTime := Inject - i - 1
			if len(children[i])-1 > spreadTime {
				Inject += (len(children[i]) - 1 - spreadTime + 1) / 2
			}
			//	Fprintln(out, "Inject:", Inject)
		}
		Fprintln(out, Inject)
	}
	/* Code Ending */
	for i := 0; i < cas; i++ {
		sol()
	}

}

func main() {
	main_div2_781_C()
}


The Fair Nut likes kvass very much. On his birthday parents presented him $$$n$$$ kegs of kvass. There are $$$v_i$$$ liters of kvass in the $$$i$$$-th keg. Each keg has a lever. You can pour your glass by exactly $$$1$$$ liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by $$$s$$$ liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by $$$s$$$ liters of kvass.

The first line contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 10^3$$$, $$$1 \le s \le 10^{12}$$$) — the number of kegs and glass volume. The second line contains $$$n$$$ integers $$$v_1, v_2, \ldots, v_n$$$ ($$$1 \le v_i \le 10^9$$$) — the volume of $$$i$$$-th keg.

If the Fair Nut cannot pour his glass by $$$s$$$ liters of kvass, print $$$-1$$$. Otherwise, print a single integer — how much kvass in the least keg can be.

NoteIn the first example, the answer is $$$3$$$, the Fair Nut can take $$$1$$$ liter from the first keg and $$$2$$$ liters from the third keg. There are $$$3$$$ liters of kvass in each keg.In the second example, the answer is $$$2$$$, the Fair Nut can take $$$3$$$ liters from the first keg and $$$1$$$ liter from the second keg.In the third example, the Fair Nut can't pour his cup by $$$7$$$ liters, so the answer is $$$-1$$$.

package main

import (
	"fmt"
)

func main() {
	var n int
	var s int64
	fmt.Scan(&n)
	fmt.Scan(&s)
	var arr [100001]int64
	for i := 0; i < n ; i++ {
		fmt.Scan(&arr[i])
	}
	lo := (int64)(0)
	hi := (int64)(1000*1000*1000)
	res := (int64)(-1)
	for ;lo <= hi; {

		mid := (int64)(lo + (hi - lo) / 2)
		var cur int64
		cur = 0
		for i := 0 ; i < n ; i++ {
			if arr[i] >= mid {
				cur += arr[i] - mid;
			} else {
				cur = -1;
				break;
			}
		}
		if cur >= s {
			lo = mid + 1
			res = mid
		} else {
			hi = mid - 1;
		}
	}
	fmt.Println(res)
}

package main

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

var scanner = bufio.NewScanner(os.Stdin)

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

func main() {
	scanner.Split(bufio.ScanWords)
	n, s := scanNum(), scanNum()
	v := make([]int64, n)
	min := int64(1e13)
	for i := int64(0); i < n; i++ {
		v[i] = scanNum()
		if v[i] < min {
			min = v[i]
		}
	}
	extra := int64(0)
	for i := int64(0); i < n; i++ {
		extra += v[i] - min
	}
	if extra >= s {
		fmt.Println(min)
		os.Exit(0)
	}

	cut := int64(math.Ceil(float64(s-extra) / float64(n)))
	if cut > min {
		fmt.Println(-1)
		os.Exit(0)
	}
	fmt.Println(min - cut)
}

package main

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

var scanner = bufio.NewScanner(os.Stdin)

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

func main() {
	scanner.Split(bufio.ScanWords)
	n := scanInt64()
	s := scanInt64()
	kegs := make([]int64, n)
	extra := int64(0)
	sum := int64(0)
	min := int64(math.MaxInt64)

	for i := int64(0); i < n; i++ {
		k := scanInt64()
		if k < min {
			min = k
		}
		kegs[i] = k
	}

	for i := int64(0); i < n; i++ {
		sum += kegs[i]
		extra += kegs[i] - min
	}

	if sum < s {
		fmt.Println(-1)
		return
	}
	if extra >= s {
		fmt.Println(min)
		return
	}
	fmt.Println(min - int64(math.Ceil(float64(s-extra) / float64(n))))
}

package main

import (
	"fmt"
	"sort"
)

type SortByInt64 []int64

func (n SortByInt64) Len() int           { return len(n) }
func (n SortByInt64) Less(i, j int) bool { return n[i] < n[j] }
func (n SortByInt64) Swap(i, j int)      { n[i], n[j] = n[j], n[i] }

func main() {
	var n int64
	var s int64
	_, _ = fmt.Scan(&n)
	_, _ = fmt.Scan(&s)
	var k int64
	var kegs []int64
	sumKegs := int64(0)

	for i := int64(0); i < n; i++ {
		_, _ = fmt.Scan(&k)
		sumKegs += k
		kegs = append(kegs, k)
	}

	if k < s {
		fmt.Println(-1)
		return
	}

	p := int64(0)
	for p < s {
		sort.Sort(SortByInt64(kegs))
		subtract := kegs[len(kegs) - 1] - kegs[0]
		if subtract == 0 {
			subtract = 1
		}
		p += subtract
		kegs[len(kegs) - 1] -= subtract
	}
	sort.Sort(SortByInt64(kegs))
	fmt.Println(kegs[0])
}


You're given a list of n strings a1, a2, ..., an. You'd like to concatenate them together in some order such that the resulting string would be lexicographically smallest.Given the list of strings, output the lexicographically smallest concatenation.

The first line contains integer n — the number of strings (1 ≤ n ≤ 5·104). Each of the next n lines contains one string ai (1 ≤ |ai| ≤ 50) consisting of only lowercase English letters. The sum of string lengths will not exceed 5·104.

Print the only string a — the lexicographically smallest string concatenation.

package main

import (
	"fmt"
	"sort"
)

const MAXC = 26
const MAXN = 50006

var arr[] string

type ByLex[] string

func (s ByLex) Len() int {
	return len(s)
}

func (s ByLex) Swap(i,j int) {
	s[i], s[j] = s[j], s[i]
}

func (s ByLex) Less(i,j int) bool {
	// lenI, lenJ, k, ml := len(s[i]), len(s[j]), 0, 0
	// if lenI < lenJ {
	// 	ml = lenI
	// } else {
	// 	ml = lenJ
	// }
	// for k=0; k<ml; k++ {
	// 	if s[i][k] != s[j][k] {return s[i][k] < s[j][k]}
	// }
	// if lenI == lenJ {return false}
	// if lenI < lenJ {
	// 	for ; k<lenJ; k++ {
	// 		if s[j][k-ml] != s[j][k] {return s[j][k-ml] != s[j][k]}
	// 	}
	// }
	IJ, JI := s[i]+s[j], s[j]+s[i]
	return IJ < JI
}

func main() {
	var N int
	var str string
	fmt.Scanf("%d\n", &N)
	for ; N>0; N-- {
		fmt.Scanf("%s\n", &str)
		arr = append(arr,str)
	}
	sort.Sort(ByLex(arr))
	lengtharr := len(arr)
	for i:=0; i<lengtharr; i++ {
		lengthstr := len(arr[i])
		for j:=0; j<lengthstr; j++ {
			fmt.Printf("%c", arr[i][j])
		}
	}
	fmt.Printf("\n")
}

package main

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

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

type StringSlice []string

func (p StringSlice) Len() int      { return len(p) }
func (p StringSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p StringSlice) Less(i, j int) bool {
	s, t := p[i], p[j]
	isSwap := false

	for t != "" {
		if len(s) > len(t) {
			s, t = t, s

			if isSwap {
				isSwap = false
			} else {
				isSwap = true
			}
		}

		l := len(s)

		if s != t[:l] {
			if isSwap {
				return t[:l] < s
			} else {
				return s < t[:l]
			}
		}

		t = t[l:]
	}

	return true
}

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

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	a := make([]string, n)

	for i := range a {
		a[i] = next()
	}

	sort.Sort(StringSlice(a))

	for i := range a {
		print(a[i])
	}

	println()
}

func next() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func nextInt64() int64 {
	n, _ := strconv.ParseInt(next(), 0, 64)
	return n
}

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{})               { fmt.Fprintln(writer, a...) }
func print(a ...interface{})                 { fmt.Fprint(writer, a...) }
func printf(format string, a ...interface{}) { fmt.Fprintf(writer, format, a...) }

package main

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

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

	var n int
	Fscan(in, &n)
	s := make([]string, n)
	for i := range s {
		Fscan(in, &s[i])
	}
	sort.Slice(s, func(i, j int) (less bool) { return s[i]+s[j] < s[j]+s[i] })
	for _, val := range s {
		Fprint(out, val)
	}
}

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

package main

import (
	"fmt"
	"sort"
)

const MAXC = 26
const MAXN = 50006

var arr[] string

func main() {
	var N int
	var str string
	fmt.Scanf("%d\n", &N)
	for ; N>0; N-- {
		fmt.Scanf("%s\n", &str)
		str = str + string(65)
		arr = append(arr,str)
	}
	sort.Strings(arr)
	lengtharr := len(arr)
	for i:=0; i<lengtharr; i++ {
		lengthstr := len(arr[i])
		lengthstr--
		lengthstr--
		for j:=0; j<lengthstr; j++ {
			fmt.Printf("%c", arr[i][j])
		}
	}
	fmt.Printf("\n")
}

package main

import (
	"fmt"
	"sort"
)

const MAXC = 26
const MAXN = 50006

var arr[] string

func main() {
	var N int
	var str string
	fmt.Scanf("%d\n", &N)
	for ; N>0; N-- {
		fmt.Scanf("%s\n", &str)
		str = str + string(150)
		arr = append(arr,str)
	}
	sort.Strings(arr)
	lengtharr := len(arr)
	for i:=0; i<lengtharr; i++ {
		lengthstr := len(arr[i])
		lengthstr--
		for j:=0; j<lengthstr; j++ {
			fmt.Printf("%c", arr[i][j])
		}
	}
	fmt.Printf("\n")
}

package main

import "fmt"

const MAXC = 26
const MAXN = 50006

var TrieSZ int
var Trie[MAXN][MAXC] int
var Cnt[MAXN] int

func insert(str string) {
	i,length,cur,ch := 0, len(str), 0, 0
	for i=0; i<length; i++ {
		ch = int(str[i]) - 97
		if Trie[cur][ch] == 0 {
			TrieSZ++
			Trie[cur][ch] = TrieSZ
			Cnt[TrieSZ] = 0
		}
		cur = Trie[cur][ch]
		Cnt[cur]++
	}
}

var ans string

func print(cur,ch int) {
	ans = ans + string(ch+97)
	Cnt[cur]--
	for ch=0; ch<MAXC; ch++ {
		nxt := Trie[cur][ch]
		if nxt != 0 && Cnt[nxt] > 0 {
			print(nxt,ch)
			break
		}
	}
}

func gen() {
	ch, cur, nxt := 0,0,0
	ans = ""
	for ch=0; ch<MAXC; {
		nxt = Trie[cur][ch]
		if nxt != 0 && Cnt[nxt] > 0 {
			print(nxt,ch)
		} else {
			ch++
			continue
		}
	}
	fmt.Printf("%s\n", ans)
}

func main() {
	var N int
	var str string
	fmt.Scanf("%d", &N)
	for ; N>0; N-- {
		fmt.Scanf("%s", &str)
		insert(str)
	}
	gen()
}

package main

import (
	"fmt"
	"sort"
)

const MAXC = 26
const MAXN = 50006

var arr[] string

func main() {
	var N int
	var str string
	fmt.Scanf("%d\n", &N)
	for ; N>0; N-- {
		fmt.Scanf("%s\n", &str)
		str = str + string(96)
		arr = append(arr,str)
	}
	sort.Strings(arr)
	lengtharr := len(arr)
	for i:=0; i<lengtharr; i++ {
		lengthstr := len(arr[i])
		lengthstr--
		lengthstr--
		for j:=0; j<lengthstr; j++ {
			fmt.Printf("%c", arr[i][j])
		}
	}
	fmt.Printf("\n")
}

package main

import (
	"fmt"
	"sort"
)

const MAXC = 26
const MAXN = 50006

var arr[] string

func main() {
	var N int
	var str string
	fmt.Scanf("%d\n", &N)
	for ; N>0; N-- {
		fmt.Scanf("%s\n", &str)
		str = str + string(150)
		arr = append(arr,str)
	}
	sort.Strings(arr)
	lengtharr := len(arr)
	for i:=0; i<lengtharr; i++ {
		lengthstr := len(arr[i])
		lengthstr--
		lengthstr--
		for j:=0; j<lengthstr; j++ {
			fmt.Printf("%c", arr[i][j])
		}
	}
	fmt.Printf("\n")
}

package main

import (
	"fmt"
	"sort"
)

const MAXC = 26
const MAXN = 50006

var arr[] string

type ByLex[] string

func (s ByLex) Len() int {
	return len(s)
}

func (s ByLex) Swap(i,j int) {
	s[i], s[j] = s[j], s[i]
}

func (s ByLex) Less(i,j int) bool {
	// lenI, lenJ, k, ml := len(s[i]), len(s[j]), 0, 0
	// if lenI < lenJ {
	// 	ml = lenI
	// } else {
	// 	ml = lenJ
	// }
	// for k=0; k<ml; k++ {
	// 	if s[i][k] != s[j][k] {return s[i][k] < s[j][k]}
	// }
	// if lenI == lenJ {return false}
	// if lenI < lenJ {
	// 	for ; k<lenJ; k++ {
	// 		if s[j][k-ml] != s[j][k] {return s[j][k-ml] != s[j][k]}
	// 	}
	// }
	IJ, JI := s[i]+s[j], s[j]+s[i]
	return IJ < JI
}

func main() {
	var N int
	var str string
	fmt.Scanf("%d\n", &N)
	for ; N>0; N-- {
		fmt.Scanf("%s\n", &str)
		str = str + string(65)
		arr = append(arr,str)
	}
	sort.Strings(ByLex(arr))
	lengtharr := len(arr)
	for i:=0; i<lengtharr; i++ {
		lengthstr := len(arr[i])
		lengthstr--
		for j:=0; j<lengthstr; j++ {
			fmt.Printf("%c", arr[i][j])
		}
	}
	fmt.Printf("\n")
}

package main

import "fmt"

const MAXC = 26
const MAXN = 50006

var TrieSZ int
var Trie[MAXN][MAXC] int
var Cnt[MAXN] int

func insert(str string) {
	i,length,cur,ch := 0, len(str), 0, 0
	for i=0; i<length; i++ {
		ch = int(str[i]) - 97
		if Trie[cur][ch] == 0 {
			TrieSZ++
			Trie[cur][ch] = TrieSZ
			Cnt[TrieSZ] = 0
		}
		cur = Trie[cur][ch]
		Cnt[cur]++
	}
}

var ans string

func print(cur,ch int) {
	ans = ans + string(ch+97)
	Cnt[cur]--
	for ch=0; ch<MAXC; ch++ {
		nxt := Trie[cur][ch]
		if nxt != 0 && Cnt[nxt] > 0 {
			print(nxt,ch)
			break
		}
	}
}

func gen() {
	ch, cur, nxt := 0,0,0
	ans = ""
	for ch=0; ch<MAXC; {
		nxt = Trie[cur][ch]
		if nxt != 0 && Cnt[nxt] > 0 {
			print(nxt,ch)
		} else {
			ch++
			continue
		}
	}
	fmt.Printf("%s\n", ans)
}

func main() {
	var N int
	var str string
	fmt.Scanf("%d\n", &N)
	for ; N>0; N-- {
		fmt.Scanf("%s\n", &str)
		insert(str)
	}
	gen()
}

package main

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

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

type Strings struct {
	sort.StringSlice
}

func (s Strings) Less(i, j int) bool {
	l := len(s.StringSlice[i])

	if len(s.StringSlice[j]) < len(s.StringSlice[i]) {
		l = len(s.StringSlice[j])
	}

	for k := 0; k < l; k++ {
		if s.StringSlice[i][k] != s.StringSlice[j][k] {
			return s.StringSlice[i][k] < s.StringSlice[j][k]
		}
	}

	return l == len(s.StringSlice[j])
}

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	a := make([]string, n)

	for i := range a {
		a[i] = next()
	}

	sort.Sort(Strings{sort.StringSlice(a)})

	for i := range a {
		print(a[i])
	}

	println()
}

func next() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func nextInt64() int64 {
	n, _ := strconv.ParseInt(next(), 0, 64)
	return n
}

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{})               { fmt.Fprintln(writer, a...) }
func print(a ...interface{})                 { fmt.Fprint(writer, a...) }
func printf(format string, a ...interface{}) { fmt.Fprintf(writer, format, a...) }

package main

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

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

	var n int
	Fscan(in, &n)
	ss := make([]string, n)
	for i := range ss {
		Fscan(in, &ss[i])
	}
	sort.Slice(ss, func(i, j int) (less bool) {
		swap := false
		defer func() {
			if swap {
				less = !less
			}
		}()
		si, sj := ss[i], ss[j]
		if len(si) > len(sj) {
			si, sj = sj, si
			swap = true
		}
		for s, t := 0, len(si); s < t; {
			if si != sj[s:t] {
				return si < sj[s:t]
			}
			s = t
			t += len(si)
			if t > len(sj) {
				t = len(sj)
			}
		}
		return true
	})
	for _, s := range ss {
		Fprint(out, s)
	}
}

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