There are $$$n$$$ athletes in front of you. Athletes are numbered from $$$1$$$ to $$$n$$$ from left to right. You know the strength of each athlete — the athlete number $$$i$$$ has the strength $$$s_i$$$.You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team.You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams $$$A$$$ and $$$B$$$ so that the value $$$|\max(A) - \min(B)|$$$ is as small as possible, where $$$\max(A)$$$ is the maximum strength of an athlete from team $$$A$$$, and $$$\min(B)$$$ is the minimum strength of an athlete from team $$$B$$$.For example, if $$$n=5$$$ and the strength of the athletes is $$$s=[3, 1, 2, 6, 4]$$$, then one of the possible split into teams is:   first team: $$$A = [1, 2, 4]$$$,  second team: $$$B = [3, 6]$$$. In this case, the value $$$|\max(A) - \min(B)|$$$ will be equal to $$$|4-3|=1$$$. This example illustrates one of the ways of optimal split into two teams.Print the minimum value $$$|\max(A) - \min(B)|$$$.

The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case consists of two lines.  The first line contains positive integer $$$n$$$ ($$$2 \le n \le 50$$$) — number of athletes.  The second line contains $$$n$$$ positive integers $$$s_1, s_2, \ldots, s_n$$$ ($$$1 \le s_i \le 1000$$$), where $$$s_i$$$ — is the strength of the $$$i$$$-th athlete. Please note that $$$s$$$ values may not be distinct.

For each test case print one integer — the minimum value of $$$|\max(A) - \min(B)|$$$ with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams.

NoteThe first test case was explained in the statement. In the second test case, one of the optimal splits is $$$A=[2, 1]$$$, $$$B=[3, 2, 4, 3]$$$, so the answer is $$$|2-2|=0$$$.

package main

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

func main() {
    // rd := bufio.NewReader(os.Stdin)
    // wr := bufio.NewWriter(os.Stdout)
    // defer wr.Flush()

    var input io.Reader
    input = bufio.NewReader(os.Stdin)

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

    for t := 0; t < T; t++ {
        var n int
        fmt.Fscan(input, &n)
        a := make([]int, n)

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

        sort.Ints(a)

        ans := 100000
        for i := 0; i < (n - 1); i++ {
            q := a[i+1] - a[i]
            if q < ans {
                ans = q
            }
        }

        fmt.Println(ans)
    }
}

package main

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

func main() {
	defer out.Flush()
	for t := ScanInt(); t > 0; t-- {
		n := ScanInt()
		s := ScanInts(n)
		sort.Ints(s)
		mnDiff := math.MaxInt32
		//mnIdx := 0
		i := 0
		for i < n-1 {
			if Abs(s[i]-s[i+1]) < mnDiff {
				mnDiff = Abs(s[i] - s[i+1])
				//mnIdx = i
			}
			i++
		}
		P(mnDiff)
	}
}

/*********************** I/O ***********************/
var in *bufio.Scanner
var out *bufio.Writer

func init() {
	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(1e+9))
	in.Split(bufio.ScanWords)
	out = bufio.NewWriter(os.Stdout)
}

func P(args ...interface{}) {
	print("\n", args...)
}

func PS(args ...interface{}) {
	print(" ", args...)
	print("\n", nil)
}

func print(seperator string, args ...interface{}) {
	for _, arg := range args {
		str := ""
		switch v := arg.(type) {
		case byte:
			str = string(v)
		case rune:
			str = string(v)
		case int:
			str = strconv.Itoa(v)
		case []int:
			sb := strings.Builder{}
			for i := 0; i < len(v); i++ {
				sb.WriteString(strconv.Itoa(v[i]) + " ")
			}
			str = sb.String()
		case int64:
			str = strconv.FormatInt(v, 10)
		case []int64:
			sb := strings.Builder{}
			for i := 0; i < len(v); i++ {
				sb.WriteString(strconv.FormatInt(v[i], 10) + " ")
			}
			str = sb.String()
		case string:
			str = v
		default:
			panic("invalid print arg: " + fmt.Sprintf("%v", arg))

		}
		out.WriteString(str + seperator)
	}
}

func ScanWord() string { in.Scan(); return in.Text() }
func ScanWords(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ScanWord()
	}
	return s
}
func ScanInt() int { i, _ := strconv.Atoi(ScanWord()); return i }
func ScanInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = ScanInt()
	}
	return a
}

func MnMx(args ...int) (int, int) {
	minVal, maxVal := math.MaxInt32, math.MinInt32
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
		if v > maxVal {
			maxVal = v
		}
	}
	return minVal, maxVal
}
func Mn(args ...int) int { mn, _ := MnMx(args...); return mn }
func Mx(args ...int) int { _, mx := MnMx(args...); return mx }
func LCM(a, b int) int   { return a / GCD(a, b) * b }
func GCD(a, b int) int {
	if a == 0 {
		return b
	}
	return GCD(b%a, a)
}
func Abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var t int
	fmt.Scan(&t)
	for ; t != 0; t-- {
		var n int
		var a []int
		fmt.Scan(&n)
		for i, x := 0, 0; i < n; i++ {
			fmt.Scan(&x)
			a = append(a, x)
		}
		sort.Ints(a)
		var m = a[1] - a[0]
		for i := 1; i < n; i++ {
			if m > a[i]-a[i-1] {
				m = a[i] - a[i-1]
			}
		}
		fmt.Println(m)
	}
}

package main
import (
        "fmt"
        "os"
        "sort"
        "bufio"
)
func main(){
    var t, n, ans int
    var data [55]int
    in := bufio.NewReader(os.Stdin)
    fmt.Fscan(in, &t)
    for ; t > 0; t-- {
        fmt.Fscan(in, &n)
        for i := 0; i < n; i++ {
            fmt.Fscan(in, &data[i])
        }
        d := data[:n]
        sort.Ints(d)
        ans = 100000
        for i := 1; i < n; i++ {
            if ans > (d[i] - d[i-1]) {
                ans = d[i] - d[i-1]
            }
        }
        fmt.Println(ans)
    }
}

package main
import (
        "fmt"
        "os"
        "sort"
)
func main(){
    var t, n, ans int
    var data [55]int
    in := os.Stdin
    fmt.Fscan(in, &t)
    for ; t > 0; t-- {
        fmt.Fscan(in, &n)
        for i := 0; i < n; i++ {
            fmt.Fscan(in, &data[i])
        }
        d := data[:n]
        sort.Ints(d)
        ans = 100000
        for i := 1; i < n; i++ {
            if ans > (d[i] - d[i-1]) {
                ans = d[i] - d[i-1]
            }
        }
        fmt.Println(ans)
    }
}

package main

import (
	"fmt"
	"sort"
)

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

func main() {
	var t int
	fmt.Scanf("%d\n", &t)
	for tc := 0; tc < t; tc++ {
		var n int
		fmt.Scanf("%d\n", &n)
		s := make([]int, n)
		for i := 0; i < n; i++ {
		    fmt.Scanf("%d", &s[i])
		}
		fmt.Scanf("\n")
		sort.Ints(s)
		res := s[n - 1]
		for i := 0; i < n - 1; i++ {
		    res = min(res, s[i + 1] - s[i])
		}
		fmt.Println(res)
	}
}

package main

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

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

func main() {
	defer out.Flush()
	inn.Split(bufio.ScanWords)
	for tt := rd(); tt > 0; tt-- {
		n := rd()
		a := make([]int, n)
		for i := 0; i < n; i++ {
			a[i] = rd()
		}
		sort.Ints(a)
		ans := int(1e9)
		for i := 0; i < n-1; i++ {
			ans = min(ans, a[i+1]-a[i])
		}
		Fprintln(out, ans)
	}

}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func rd() (x int) {
	inn.Scan()
	data := inn.Bytes()
	f := 1
	if data[0] == '-' {
		f = -1
		data = data[1:]
	}
	for _, b := range data {
		x = x*10 + int(b&15)
	}
	return f * x
}
func rdll() (x int64) {
	inn.Scan()
	data := inn.Bytes()
	f := int64(1)
	if data[0] == '-' {
		f = -1
		data = data[1:]
	}
	for _, b := range data {
		x = x*10 + int64(b&15)
	}
	return f * x
}

package main

import (
	"fmt"
	"sort"
)

func main(){
	var T int
	fmt.Scan(&T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Scan(&n)
		A := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Scan(&A[i])
		}
		sort.Ints(A)
		ans := 100000
		for i := 0; i < n-1; i++ {
			tmp := A[i + 1] - A[i]
			if tmp < ans {
				ans = tmp
			}
		}
		fmt.Println(ans)
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	var t int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		var n int
		fmt.Fscan(in, &n)
		s := make([]int, n)
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &s[j])
		}
		sort.Ints(s)
		min := 1001
		for j := 1; j < n; j++ {
			if min > s[j]-s[j-1] {
				min = s[j] - s[j-1]
			}
		}
		fmt.Fprintln(out, min)
	}
	out.Flush()
}

package main

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

func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()
	var t int
	fmt.Fscan(r, &t)

	for i := 0; i < t; i++ {
		var n int
		fmt.Fscan(r, &n)
		a := make([]int, n)
		for j := 0; j < n; j++ {
			fmt.Fscan(r, &a[j])
		}
		sort.Ints(a)
		m := a[1] - a[0]
		for j := 2; j < n; j++ {
			if a[j]-a[j-1] < m {
				m = a[j] - a[j-1]
			}
		}
		fmt.Fprintln(w, m)
	}
}

package main

import (
	"fmt"
	"sort"
	"strings"
)

type test struct {
	a []int
}

const TEST_MAX_SIZE = 50

// https://codeforces.com/problemset/problem/1360/B
// комментарий к решению
// Т.к. размер конечных команд не определен, то можно разделять спортсменов
// хоть как 1 vs n-1 , поэтому наша задача сводится к разделению на
// самых слабых vs самых сильных так, чтобы разница между max(слабых) и min(сильных)
// была минимальной. Т.е. сортируем всех спротсменов по силе и ищем минимальную
// разницу между любыми соседними - это и будет ответом
func taskSolution(in []test) string {
	ans := make([]int, len(in))
	for i := 0; i < len(in); i += 1 {
		a := in[i].a
		sort.Ints(a)
		ans[i] = a[1] - a[0]
		for j := 2; j < len(a); j += 1 {
			if a[j]-a[j-1] < ans[i] {
				ans[i] = a[j] - a[j-1]
			}
		}
	}
	res := fmt.Sprint(ans)
	return strings.Replace(res[1:len(res)-1], " ", "\n", len(ans)-1)
}

func main() {
	var t int
	if _, err := fmt.Scan(&t); err != nil {
		panic(err)
	}

	in := make([]test, 0, t)
	for i := 0; i < t; i += 1 {
		var n, ai int
		a := make([]int, 0, TEST_MAX_SIZE)
		if _, err := fmt.Scan(&n); err != nil {
			panic(err)
		}
		for j := 0; j < n; j += 1 {
			if _, err := fmt.Scan(&ai); err != nil {
				panic(err)
			}
			a = append(a, ai)
		}
		in = append(in, test{a})
	}

	fmt.Println(taskSolution(in))
}

package main

import (
	"fmt"
	"sort"
)

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

func solve() {
	var n int
	fmt.Scanf("%d\n", &n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &a[i])
	}
	fmt.Scanf("\n")
	sort.Ints(a)
	ans := int(1e3)
	for i := 0; i < n - 1; i++ {
		ans = min(ans, a[i + 1] - a[i])
	}
	fmt.Println(ans)
}

func main() {
	var t int
	fmt.Scanf("%d\n", &t)
	for i := 0; i < t; i++ {
		solve()
	}
}

package main

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

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

type writerImpl struct {
	*bufio.Writer
}

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

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

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

type WordScanner interface {
	NextInt64() int64
	NextUInt64() uint64
	NextString() string
}

type wordScannerImpl struct {
	*bufio.Scanner
}

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

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

func (impl *wordScannerImpl) NextUInt64() uint64 {
	impl.Scan()
	bb := impl.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

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

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

func lcm(a, b int64) int64 {
	return a * b / gcd(a, b)
}

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

func min(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r < a {
			a = r
		}
	}
	return a
}

func max(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r > a {
			a = r
		}
	}
	return a
}

const (
	INF = math.MaxInt64
	MOD = 1e9 + 7
)

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

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

func sol(s []int64) int64 {
	sort.Slice(s, func(a, b int) bool {
		return s[a] < s[b]
	})
	minSoFar := s[len(s)-1] - s[0]
	for i := 1; i < len(s); i++ {
		minSoFar = min(minSoFar, s[i]-s[i-1])
	}
	return minSoFar
}

package main

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

func main() {
	var t, n, a int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &n)
		aa := make([]int, 0)
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &a)
			aa = append(aa, a)
		}
		sort.Slice(aa, func(i, j int) bool {
			return aa[i] < aa[j]
		})
		min := aa[n-1] - aa[0]
		for j := 0; j < n-1; j++ {
			if min > aa[j+1]-aa[j] {
				min = aa[j+1] - aa[j]
			}
		}
		fmt.Println(min)
	}
}

package main

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

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 main() {
	reader := bufio.NewReader(os.Stdin)

	tc := readNum(reader)

	for tc > 0 {
		tc--
		n := readNum(reader)
		A := readNNums(reader, n)
		fmt.Println(solve(n, A))
	}
}

func solve(n int, A []int) int {
	sort.Ints(A)

	var best = math.MaxInt32

	for i := 0; i < n-1; i++ {
		if A[i+1]-A[i] < best {
			best = A[i+1] - A[i]
		}
	}
	return best
}

package main

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

func main() {
	s := NewScanner()
	t := s.NextInt()

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

		sort.Ints(a)

		min := 1001
		for j := 0; j < n-1; j++ {
			if a[j+1]-a[j] < min {
				min = a[j+1] - a[j]
			}
		}

		fmt.Println(min)
	}
}

type scanner struct {
	*bufio.Scanner
}

func NewScanner() *scanner {
	s := bufio.NewScanner(os.Stdin)
	s.Split(bufio.ScanWords)
	return &scanner{s}
}

func (s *scanner) NextInt() int {
	s.Scan()
	i, _ := strconv.Atoi(s.Text())
	return i
}

func (s *scanner) NextInt64() int64 {
	s.Scan()
	i, _ := strconv.ParseInt(s.Text(), 10, 64)
	return i
}

func (s *scanner) NextString() string {
	s.Scan()
	return s.Text()
}

// https://codeforces.com/contest/1355/problem/B
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
var input io.Reader

func main() {
	var (
		T int
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &T)
	for i := 0; i < T; i++ {
		fmt.Println(solver())
	}
}

func solver() int {
	var n int
	fmt.Fscan(input, &n)
	s := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(input, &s[i])
	}

	sort.Ints(s)
	m := 10000
	for i := 1; i < n; i++ {
		m = min(m, s[i] - s[i - 1])
	}

	return m
}

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

// You can edit this code!
// Click here and start typing.
package main

import(
		"fmt"
		"sort"
	)

// Min returns the smaller of x or y.
func Min(x, y int) int {
    if x > y {
        return y
    }
    return x
}
func AbsInt(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func main() {
	var t,n int

	//var array [] int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		fmt.Scan(&n)
		array := make([]int, n)
		var dif int
		dif = 5000
		for y := 0; y < n; y++ {
			fmt.Scan(&array[y])
		}
		sort.Ints(array)
		for y := 1; y <n ; y++ {
			//if array[y] != array[y-1]{
			dif = Min(dif,AbsInt(array[y]-array[y-1]))
			//}
		}
		if dif == 5000{
			dif = 0
		}
		fmt.Println(dif)

	}

}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	cases, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < cases; i++ {
		scanner.Scan()
		text := scanner.Text()
		l, _ := strconv.Atoi(text)
		scanner.Scan()
		strength := scanner.Text()
		arr := strings.Split(strength, " ")
		arr_i := make([]int, 0, l)
		for i := 0; i < l; i++ {
			v, _ := strconv.Atoi(arr[i])
			arr_i = append(arr_i, v)
		}
		sort.Ints(arr_i)
		res := -1
		for i := 0; i < l-1; i++ {
			diff := int(math.Abs(float64(arr_i[i] - arr_i[i+1])))
			if res == -1 {
				res = diff
			} else if diff < res {
				res = diff
			}
			if res == 0 {
				break
			}
		}
		fmt.Println(res)
	}
}

package main

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

func main(){
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t,n int
	fmt.Scan(&t)
	for ;t>0;t--{
		fmt.Scan(&n)
		a := make([]int,n)
		for i:=0;i<len(a);i++{
			fmt.Scan(&a[i])
		}
		fmt.Fprintln(out,solve(a))
	}
}
func solve(a []int)int{
	sort.Ints(a)
	min := a[1]-a[0]
	for i:=0;i<len(a)-1;i++{
		temp := a[i+1]-a[i]
		if temp < min{
			min = temp
		}
	}
	return min
}

package main

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

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

func main() {
	defer writer.Flush()
	t := reader.NextInt64()
	for ; t > 0; t-- {
		n:= reader.NextInt64()
		aa := make([]int, n)
		for i := 0; i < len(aa); i++ {
			aa[i] = reader.NextInt()
		}
		sort.Ints(aa)
		writer.Printf("%d\n", sol(aa))
	}
}

func sol(aa []int) int {
	minDiff:=aa[1]-aa[0]
	for i:=1;i<len(aa)-1;i++ {
		if(aa[i+1]-aa[i] < minDiff){
			minDiff = aa[i+1]-aa[i]
		}
	}
	return minDiff

}

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

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

type writerImpl struct {
	*bufio.Writer
}

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

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

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

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

type wordScannerImpl struct {
	*bufio.Scanner
}

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

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

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

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

func S(v interface{}) string { return fmt.Sprintf("%v", v) }
func SArr(a []int) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.Itoa(a[i]) + " ")
	}
	return sb.String()
}
func SArr64(a []int64) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.FormatInt(a[i], 10) + " ")
	}
	return sb.String()
}
func MnMx(args ...int64) (int64, int64) {
	minVal, maxVal := int64(math.MaxInt64), int64(math.MinInt64)
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
		if v > maxVal {
			maxVal = v
		}
	}
	return minVal, maxVal
}
func Mn(args ...int64) int64 { mn, _ := MnMx(args...); return mn }
func Mx(args ...int64) int64 { _, mx := MnMx(args...); return mx }
func LCM(a, b int64) int64   { return a / GCD(a, b) * b }
func GCD(a, b int64) int64 {
	if a == 0 {
		return b
	}
	return GCD(b%a, a)
}
func sortInt64Slice(a []int64) { sort.Slice(a, func(i, j int) bool { return a[i] < a[j] }) }
func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}

/*
URL:
https://codeforces.com/contest/1360/problem/B
*/

package main

import (
	"bufio"
	"errors"
	"fmt"
	"io"
	"math"
	"os"
	"sort"
	"strconv"
)

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

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

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

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

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

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

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

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

func init() {
	// bufio.ScanWords <---> bufio.ScanLines
	ReadString = newReadString(os.Stdin, bufio.ScanWords)
	stdout = bufio.NewWriter(os.Stdout)
}

var (
	t int
	n int
	S []int
)

func main() {
	t = ReadInt()
	for i := 0; i < t; i++ {
		n = ReadInt()
		S = ReadIntSlice(n)
		solve(n, S)
	}
}

func solve(n int, S []int) {
	sort.Sort(sort.IntSlice(S))

	D := []int{}
	for i := 1; i < n; i++ {
		D = append(D, AbsInt(S[i]-S[i-1]))
	}

	fmt.Println(Min(D...))
}

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

// Min returns the min integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Min(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m > integer {
			m = integer
		}
	}
	return m
}

// ChMin accepts a pointer of integer and a target value.
// If target value is SMALLER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMin(updatedValue *int, target int) bool {
	if *updatedValue > target {
		*updatedValue = target
		return true
	}
	return false
}

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

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

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func newReadString(ior io.Reader, sf bufio.SplitFunc) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	r.Split(sf)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

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

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

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

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

	return string(res)
}

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

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

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

	return string(res)
}

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

// PrintfBufStdout is function for output strings to buffered os.Stdout.
// You may have to call stdout.Flush() finally.
func PrintfBufStdout(format string, a ...interface{}) {
	fmt.Fprintf(stdout, format, a...)
}

package main

import (
  "fmt"
  "sort"
)

func main() {
  var t int
  fmt.Scan(&t)
  for t > 0 {
    t--
    var n int
    fmt.Scan(&n)
    var s []int
    s = make([]int, n)
    for i := 0; i < n; i++ {
      fmt.Scan(&s[i])
    }
    sort.Ints(s)
    ans := 1000
    for i := 1; i < n; i++ {
      if s[i] - s[i-1] < ans {
        ans = s[i] - s[i-1]
      }
    }
    fmt.Println(ans)
  }
}

package main

import "fmt"

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

func minDiff(arr []int, size int) int {
	if size < 1 {
		return -1
	}
	least := Abs(arr[1] - arr[0])
	for i := 0; i < size-1; i++ {
		for j := i + 1; j < size; j++ {
			diff := Abs(arr[j] - arr[i])
			if diff < least {
				least = diff
			}
		}
	}
	return least
}

func main() {
	var testCases int
	var sLen int
	var res int
	fmt.Scanf("%d\n", &testCases)
	var result []int
	for i := 0; i < testCases; i++ {
		fmt.Scanf("%d\n", &sLen)
		s := make([]int, sLen)
		for j := 0; j < sLen; j++ {
			fmt.Scanf("%d", &s[j])
		}
		fmt.Scanf("\n")
		res = minDiff(s, sLen)
		result = append(result, res)
	}
	for _, val := range result {
		fmt.Println(val)
	}
}

package main

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

func solve() {
	t := readInt()
	for i := 0; i < t; i++ {
		n := readInt()
		a := readIntArray(n)
		sort.Ints(a)
		res := abs(a[0] - a[1])
		for i := 1; i < len(a); i++ {
			res = min(res, abs(a[i]-a[i-1]))
		}
		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()
}

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

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

// IO

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

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

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

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

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

package main

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

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

	var t, n int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		sort.Ints(a)
		ans := int(1e9)
		for i := 1; i < n; i++ {
			if a[i]-a[i-1]<ans {
				ans = a[i]-a[i-1]
			}
		}
		Fprintln(out, ans)
	}
}

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

package main

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

func main() {
    // rd := bufio.NewReader(os.Stdin)
    // wr := bufio.NewWriter(os.Stdout)
    // defer wr.Flush()

    var input io.Reader
    input = bufio.NewReader(os.Stdin)

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

    for t := 0; t < T; t++ {
        var n int
        fmt.Fscan(input, &n)
        a := make([]int, n)

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

        sort.Ints(a)

        anss := 10000
        for i := 0; i < (n - 1); i++ {
            q := a[i+1] - a[i]
            if q < anss {
                anss = q
            }
        }

        fmt.Println(anss)
    }
}

package main

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

func main() {
    // rd := bufio.NewReader(os.Stdin)
    // wr := bufio.NewWriter(os.Stdout)
    // defer wr.Flush()

    var input io.Reader
    input = bufio.NewReader(os.Stdin)

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

    for t := 0; t < T; t++ {
        var n int
        fmt.Fscan(input, &n)
        a := make([]int, n)

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

        sort.Ints(a)

        ans := 10000
        for i := 0; i < (n - 1); i++ {
            q := a[i+1] - a[i]
            if q < ans {
                ans = q
            }
        }

        fmt.Println(ans)
    }
}

package main

import (
    "fmt"
    "sort"
)

func main() {
    // rd := bufio.NewReader(os.Stdin)
    // wr := bufio.NewWriter(os.Stdout)
    // defer wr.Flush()

    var T int

    fmt.Scan(&T)

    for t := 0; t < T; t++ {
        var n int
        fmt.Scan(&n)
        a := make([]int, n)

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

        sort.Ints(a)

        ans := 100000
        for i := 0; i < (n - 1); i++ {
            q := a[i+1] - a[i]
            if q < ans {
                ans = q
            }
        }

        fmt.Println(ans)
    }
}

package main

import "fmt"

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

func minDiff(arr []int, size int) int {
	if size < 1 {
		return -1
	}
	least := Abs(arr[1] - arr[0])
	for i := 0; i < size-1; i++ {
		for j := i + 1; j < size; j++ {
			diff := Abs(arr[j] - arr[i])
			if diff < least {
				least = diff
			}
		}
	}
	return least
}

func main() {
	var testCases int
	var sLen int
	var res int
	fmt.Scanf("%d\n", &testCases)
	var result []int
	for i := 0; i < testCases; i++ {
		fmt.Scanf("%d\n", &sLen)
		s := make([]int, sLen)
		for j := 0; j < sLen; j++ {
			fmt.Scanf("%d", &s[j])
		}
		res = minDiff(s, sLen)
		result = append(result, res)
	}
	for _, val := range result {
		fmt.Println(val)
	}
}

package main

import "fmt"

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

func minDiff(arr []int, size int) int {
	if size == 1 {
		return arr[0]
	}
	least := Abs(arr[1] - arr[0])
	for i := 0; i < size-1; i++ {
		for j := i + 1; j < size; j++ {
			diff := Abs(arr[j] - arr[i])
			if diff < least {
				least = diff
			}
		}
	}
	return least
}

func main() {
	var testCases int
	var sLen int
	//var dummy int
	//var res int
	fmt.Scanf("%d\n", &testCases)
	fmt.Printf("testcases: %d\n", testCases)
	for i := 0; i < testCases; i++ {
		fmt.Scanf("%d\n", &sLen)
		fmt.Printf("Array Len: %d\n", sLen)
		//s := make([]int, sLen)
		for j := 0; j < sLen; j++ {
			var temp int
			fmt.Scanf("%d\n", &temp)
			fmt.Printf("%d ", temp)
		}
		// res = minDiff(s, sLen)
		// fmt.Printf("%d\n", res)
	}
}

package main

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

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

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

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

func minDiff(arr []int, size int) int {
	if size == 1 {
		return arr[0]
	}
	least := Abs(arr[1] - arr[0])
	for i := 0; i < size-1; i++ {
		for j := i + 1; j < size; j++ {
			diff := Abs(arr[j] - arr[i])
			if diff < least {
				least = diff
			}
		}
	}
	return least
}

func main() {
	defer writer.Flush()
	var testCases int
	var sLen int
	//var res int
	fmt.Scanln(&testCases)
	fmt.Printf("testcases: %d\n", testCases)
	for i := 0; i < testCases; i++ {
		fmt.Scanln(&sLen)
		fmt.Printf("Array Len: %d\n", sLen)
		//s := make([]int, sLen)
		for j := 0; j < sLen; j++ {
			var temp int
			fmt.Scanln(&temp)
			fmt.Printf("%d ", temp)
		}
		// res = minDiff(s, sLen)
		// fmt.Printf("%d\n", res)
	}
}

package main

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

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

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

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

func minDiff(arr []int, size int) int {
	if size == 1 {
		return arr[0]
	}
	least := Abs(arr[1] - arr[0])
	for i := 0; i < size-1; i++ {
		for j := i + 1; j < size; j++ {
			diff := Abs(arr[j] - arr[i])
			if diff < least {
				least = diff
			}
		}
	}
	return least
}

func main() {
	defer writer.Flush()
	var testCases int
	var sLen int
	var dummy int
	//var res int
	fmt.Scanf("%d%d", &testCases, &dummy)
	fmt.Printf("testcases: %d\n", testCases)
	for i := 0; i < testCases; i++ {
		fmt.Scanf("%d%d", &sLen, &dummy)
		fmt.Printf("Array Len: %d\n", sLen)
		//s := make([]int, sLen)
		for j := 0; j < sLen; j++ {
			var temp int
			fmt.Scanf("%d", &temp)
			fmt.Printf("%d ", temp)
		}
		fmt.Scanf("%d", &dummy)
		// res = minDiff(s, sLen)
		// fmt.Printf("%d\n", res)
	}
}

package main

import "fmt"

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

func minDiff(arr []int, size int) int {
	if size == 1 {
		return arr[0]
	}
	least := Abs(arr[1] - arr[0])
	for i := 0; i < size-1; i++ {
		for j := i + 1; j < size; j++ {
			diff := Abs(arr[j] - arr[i])
			if diff < least {
				least = diff
			}
		}
	}
	return least
}

func main() {
	var testCases int
	var sLen int
	var res int
	fmt.Scan(&testCases)
	var result []int
	for i := 0; i < testCases; i++ {
		fmt.Scan(&sLen)
		s := make([]int, sLen)
		for j := 0; j < sLen; j++ {
			fmt.Scanf("%d", &s[j])
		}
		fmt.Println(s)
		res = minDiff(s, sLen)
		result = append(result, res)
	}
	for _, val := range result {
		fmt.Println(val)
	}
}

package main

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

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

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

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

func minDiff(arr []int, size int) int {
	if size == 1 {
		return arr[0]
	}
	least := Abs(arr[1] - arr[0])
	for i := 0; i < size-1; i++ {
		for j := i + 1; j < size; j++ {
			diff := Abs(arr[j] - arr[i])
			if diff < least {
				least = diff
			}
		}
	}
	return least
}

func main() {
	defer writer.Flush()
	var testCases int
	var sLen int
	//var res int
	fmt.Scanf("%d", &testCases)
	fmt.Printf("testcases: %d\n", testCases)
	for i := 0; i < testCases; i++ {
		fmt.Scanf("%d", &sLen)
		fmt.Printf("Array Len: %d\n", sLen)
		//s := make([]int, sLen)
		for j := 0; j < sLen; j++ {
			var temp int
			fmt.Scanf("%d", &temp)
			fmt.Printf("%d ", temp)
		}
		// res = minDiff(s, sLen)
		// fmt.Printf("%d\n", res)
	}
}

package main

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

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

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

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

func minDiff(arr []int, size int) int {
	if size == 1 {
		return arr[0]
	}
	least := Abs(arr[1] - arr[0])
	for i := 0; i < size-1; i++ {
		for j := i + 1; j < size; j++ {
			diff := Abs(arr[j] - arr[i])
			if diff < least {
				least = diff
			}
		}
	}
	return least
}

func main() {
	defer writer.Flush()
	var testCases int
	var sLen int
	//var dummy int
	//var res int
	fmt.Scanf("%d\n", &testCases)
	fmt.Printf("testcases: %d\n", testCases)
	for i := 0; i < testCases; i++ {
		fmt.Scanf("%d\n", &sLen)
		fmt.Printf("Array Len: %d\n", sLen)
		//s := make([]int, sLen)
		for j := 0; j < sLen; j++ {
			var temp int
			fmt.Scanf("%d\n", &temp)
			fmt.Printf("%d ", temp)
		}
		// res = minDiff(s, sLen)
		// fmt.Printf("%d\n", res)
	}
}

package main

import "fmt"

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

func minDiff(arr []int, size int) int {
	if size == 1 {
		return arr[0]
	}
	least := Abs(arr[1] - arr[0])
	for i := 0; i < size-1; i++ {
		for j := i + 1; j < size; j++ {
			diff := Abs(arr[j] - arr[i])
			if diff < least {
				least = diff
			}
		}
	}
	return least
}

func main() {
	var testCases int
	var sLen int
	//var dummy int
	//var res int
	fmt.Scanf("%d\n", &testCases)
	fmt.Printf("testcases: %d\n", testCases)
	for i := 0; i < testCases; i++ {
		fmt.Scanf("%d\n", &sLen)
		fmt.Printf("Array Len: %d\n", sLen)
		//s := make([]int, sLen)
		for j := 0; j < sLen; j++ {
			var temp int
			fmt.Scanf("%d", &temp)
			fmt.Printf("%d ", temp)
		}
		// res = minDiff(s, sLen)
		// fmt.Printf("%d\n", res)
	}
}

package main

import "fmt"

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

func minDiff(arr []int, size int) int {
	if size == 1 {
		return arr[0]
	}
	least := Abs(arr[1] - arr[0])
	for i := 0; i < size-1; i++ {
		for j := i + 1; j < size; j++ {
			diff := Abs(arr[j] - arr[i])
			if diff < least {
				least = diff
			}
		}
	}
	return least
}

func main() {
	var testCases int
	var sLen int
	var res int
	fmt.Scan(&testCases)
	var result []int
	for i := 0; i < testCases; i++ {
		fmt.Scan(&sLen)
		s := make([]int, sLen)
		for j := 0; j < sLen; j++ {
			fmt.Scanf("%d", &s[j])
		}
		res = minDiff(s, sLen)
		result = append(result, res)
	}
	for _, val := range result {
		fmt.Println(val)
	}
}

package main

import "fmt"

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

func minDiff(arr []int, size int) int {
	if size == 1 {
		return arr[0]
	}
	least := Abs(arr[1] - arr[0])
	for i := 0; i < size-1; i++ {
		for j := i + 1; j < size; j++ {
			diff := Abs(arr[j] - arr[i])
			if diff < least {
				least = diff
			}
		}
	}
	return least
}

func main() {
	var testCases int
	var sLen int
	//var dummy int
	//var res int
	fmt.Scanf("%d\n", &testCases)
	fmt.Printf("testcases: %d\n", testCases)
	for i := 0; i < testCases; i++ {
		fmt.Scanf("%d\n", &sLen)
		fmt.Printf("Array Len: %d\n", sLen)
		//s := make([]int, sLen)
		for j := 0; j < sLen; j++ {
			var temp int
			fmt.Scanf("%d", &temp)
			fmt.Printf("%d ", temp)
		}
		fmt.Scanf("\n")
		// res = minDiff(s, sLen)
		// fmt.Printf("%d\n", res)
	}
}

package main

import (
	"fmt"
)

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

func minDiff(arr []int, size int) int {
	if size == 1 {
		return arr[0]
	}
	least := Abs(arr[1] - arr[0])
	for i := 0; i < size-1; i++ {
		for j := i + 1; j < size; j++ {
			diff := Abs(arr[j] - arr[i])
			if diff < least {
				least = diff
			}
		}
	}
	return least
}

func main() {
	var testCases int
	var sLen int
	var res int
	// f, err := os.Open("a.txt")
	// if err != nil {
	// 	panic(err)
	// }
	// defer f.Close()

	// oldStdin := os.Stdin
	// defer func() { os.Stdin = oldStdin }()

	// os.Stdin = f
	fmt.Scan(&testCases)
	//var result []int
	for i := 0; i < testCases; i++ {
		fmt.Scan(&sLen)
		s := make([]int, sLen)
		for j := 0; j < sLen; j++ {
			fmt.Scanf("%d", &s[j])
		}
		//fmt.Println(s)
		res = minDiff(s, sLen)
		fmt.Printf("%d\n", res)
		//result = append(result, res)
	}
	// for _, val := range result {
	// 	fmt.Println(val)
	// }
}

package main

import (
	"fmt"
)

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

func minDiff(arr []int, size int) int {
	if size == 1 {
		return arr[0]
	}
	least := Abs(arr[1] - arr[0])
	for i := 0; i < size-1; i++ {
		for j := i + 1; j < size; j++ {
			diff := Abs(arr[j] - arr[i])
			if diff < least {
				least = diff
			}
		}
	}
	return least
}

func main() {
	var testCases int
	var sLen int
	var res int
	// f, err := os.Open("a.txt")
	// if err != nil {
	// 	panic(err)
	// }
	// defer f.Close()

	// oldStdin := os.Stdin
	// defer func() { os.Stdin = oldStdin }()

	// os.Stdin = f
	fmt.Scan(&testCases)
	fmt.Printf("testcases: %d\n", testCases)
	//var result []int
	for i := 0; i < testCases; i++ {
		fmt.Scan(&sLen)
		fmt.Println("Array Len: %d\n", sLen)
		s := make([]int, sLen)
		for j := 0; j < sLen; j++ {
			fmt.Scanf("%d", &s[j])
			fmt.Printf("%d ", s[j])
		}
		fmt.Println()
		//fmt.Println(s)
		res = minDiff(s, sLen)
		fmt.Printf("%d\n", res)
		//result = append(result, res)
	}
	// for _, val := range result {
	// 	fmt.Println(val)
	// }
}

package main

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

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

    var t int

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

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

        for i := 0; i < n; i++ {
            fmt.Fscanf(reader, "%d", &a[i])
        }

        sort.Ints(a)

        ans := 100000
        for i := 0; i < (n - 1); i++ {
            q := a[i+1] - a[i]
            if q < ans {
                ans = q
            }
        }

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


The robot is located on a checkered rectangular board of size $$$n \times m$$$ ($$$n$$$ rows, $$$m$$$ columns). The rows in the board are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and the columns — from $$$1$$$ to $$$m$$$ from left to right.The robot is able to move from the current cell to one of the four cells adjacent by side.The sequence of commands $$$s$$$ executed by the robot is given. Each command is denoted by one of the symbols 'L', 'R', 'D' or 'U', and triggers the movement to left, right, down or up, respectively.The robot can start its movement in any cell. The robot executes the commands starting from the first one, strictly in the order in which they are listed in $$$s$$$. If the robot moves beyond the edge of the board, it falls and breaks. A command that causes the robot to break is not considered successfully executed.The robot's task is to execute as many commands as possible without falling off the board. For example, on board $$$3 \times 3$$$, if the robot starts a sequence of actions $$$s=$$$"RRDLUU" ("right", "right", "down", "left", "up", "up") from the central cell, the robot will perform one command, then the next command will force him to cross the edge. If the robot starts moving from the cell $$$(2, 1)$$$ (second row, first column) then all commands will be executed successfully and the robot will stop at the cell $$$(1, 2)$$$ (first row, second column).    The robot starts from cell $$$(2, 1)$$$ (second row, first column). It moves right, right, down, left, up, and up. In this case it ends in the cell $$$(1, 2)$$$ (first row, second column). Determine the cell from which the robot should start its movement in order to execute as many commands as possible.

The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. In the description of each test case, the first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 10^6$$$) — the height and width of the field that the robot is located on. The second line of the description is a string $$$s$$$ consisting solely of characters 'L', 'R', 'D' and 'U' — the sequence of commands the robot executes. The string has a length from $$$1$$$ to $$$10^6$$$ commands. It is guaranteed that the total length of $$$s$$$ over all test cases does not exceed $$$10^6$$$.

Print $$$t$$$ lines, each of which contains the answer to the corresponding test case. The answer to the test case are two integers $$$r$$$ ($$$1 \leq r \leq n$$$) and $$$c$$$ ($$$1 \leq c \leq m$$$), separated by a space — the coordinates of the cell (row number and column number) from which the robot should start moving to perform as many commands as possible. If there are several such cells, you may output any of them.

4
1 1
L
1 2
L
3 3
RRDLUU
4 3
LUURRDDLLLUU

package main

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

const (
	T_MIN        = 1
	T_MAX        = int(1e4)
	N_MIN        = 1
	N_MAX        = int(1e6)
	M_MIN        = 1
	M_MAX        = int(1e6)
	S_LENGTH_MIN = 1
	S_LENGTH_MAX = int(1e6)
	LEFT         = 'L'
	UP           = 'U'
	RIGHT        = 'R'
	DOWN         = 'D'
)

var inputFile = os.Stdin
var outputFile = os.Stdout

var t int
var n, m [T_MAX]int
var s [T_MAX]string
var r, c [T_MAX]int

func in() {
	reader := bufio.NewReader(inputFile)
	fmt.Fscan(reader, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(reader, &n[i], &m[i], &s[i])
	}
}

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

func solution(i int) {
	c[i] = 1
	r[i] = 1

	var left, up, right, down int
	var vertical, horizontal int
	for _, direction := range s[i] {
		switch direction {
		case LEFT:
			horizontal--
		case UP:
			vertical++
		case RIGHT:
			horizontal++
		case DOWN:
			vertical--
		}
		left = max(left, -horizontal)
		up = max(up, vertical)
		right = max(right, horizontal)
		down = max(down, -vertical)

		if left+right >= m[i] || up+down >= n[i] {
			return
		}

		c[i] = left + 1
		r[i] = up + 1
	}
	return
}

func out() {
	writer := bufio.NewWriter(outputFile)
	for i := 0; i < t; i++ {
		fmt.Fprintln(writer, r[i], c[i])
	}
	writer.Flush()
}

func main() {
	in()
	for i := 0; i < t; i++ {
		solution(i)
	}
	out()
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1607E(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	dir4 := []struct{ x, y int }{'L': {-1, 0}, 'R': {1, 0}, 'D': {0, -1}, 'U': {0, 1}}

	var T, n, m int
	var s string
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &m, &s)
		var x, y, l, r, d, u int
		ansX, ansY := 1, 1
		for _, b := range s {
			x += dir4[b].x
			y += dir4[b].y
			if x < l {l = x} else if x > r {r = x}
			if y < d {d = y} else if y > u {u = y}
			if r-l >= m || u-d >= n {break}
			ansX, ansY = 1+u, 1-l
		}
		Fprintln(out, ansX, ansY)
	}
}

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

package main

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

const (
	T_MIN        = 1
	T_MAX        = int(1e4)
	N_MIN        = 1
	N_MAX        = int(1e6)
	M_MIN        = 1
	M_MAX        = int(1e6)
	S_LENGTH_MIN = 1
	S_LENGTH_MAX = int(1e6)
	LEFT         = 'L'
	UP           = 'U'
	RIGHT        = 'R'
	DOWN         = 'D'
)

var inputFile = os.Stdin
var outputFile = os.Stdout

var t int
var n, m [T_MAX]int
var s [T_MAX]string
var r, c [T_MAX]int

func in() {
	reader := bufio.NewReader(inputFile)
	fmt.Fscan(reader, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(reader, &n[i], &m[i], &s[i])
	}
}

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

func solution(i int) {
	c[i] = m[i]
	r[i] = n[i]

	var left, up, right, down int
	var vertical, horizontal int
	for _, direction := range s[i] {
		switch direction {
		case LEFT:
			horizontal--
		case UP:
			vertical++
		case RIGHT:
			horizontal++
		case DOWN:
			vertical--
		}
		left = max(left, -horizontal)
		up = max(up, vertical)
		right = max(right, horizontal)
		down = max(down, -vertical)

		if 1+left+right >= m[i] || 1+up+down >= n[i] {
			return
		}

		c[i] = left + 1
		r[i] = up + 1
	}
	return
}

func out() {
	writer := bufio.NewWriter(outputFile)
	for i := 0; i < t; i++ {
		fmt.Fprintln(writer, r[i], c[i])
	}
	writer.Flush()
}

func main() {
	in()
	for i := 0; i < t; i++ {
		solution(i)
	}
	out()
}

package main

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

const (
	T_MIN        = 1
	T_MAX        = int(1e4)
	N_MIN        = 1
	N_MAX        = int(1e6)
	M_MIN        = 1
	M_MAX        = int(1e6)
	S_LENGTH_MIN = 1
	S_LENGTH_MAX = int(1e6)
	LEFT         = 'L'
	UP           = 'U'
	RIGHT        = 'R'
	DOWN         = 'D'
)

var inputFile = os.Stdin
var outputFile = os.Stdout

var t int
var n, m [T_MAX]int
var s [T_MAX]string
var r, c [T_MAX]int

func in() {
	reader := bufio.NewReader(inputFile)
	fmt.Fscan(reader, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(reader, &n[i], &m[i], &s[i])
	}
}

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

func solution(i int) {
	c[i] = n[i]
	r[i] = m[i]

	var left, up, right, down int
	var vertical, horizontal int
	for _, direction := range s[i] {
		switch direction {
		case LEFT:
			horizontal--
		case UP:
			vertical++
		case RIGHT:
			horizontal++
		case DOWN:
			vertical--
		}
		left = max(left, -horizontal)
		up = max(up, vertical)
		right = max(right, horizontal)
		down = max(down, -vertical)

		if left+right >= m[i] || up+down >= n[i] {
			return
		}

		c[i] = left + 1
		r[i] = up + 1
	}
	return
}

func out() {
	writer := bufio.NewWriter(outputFile)
	for i := 0; i < t; i++ {
		fmt.Fprintln(writer, r[i], c[i])
	}
	writer.Flush()
}

func main() {
	in()
	for i := 0; i < t; i++ {
		solution(i)
	}
	out()
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1607E(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	dir4 := []struct{ x, y int }{'L': {-1, 0}, 'R': {1, 0}, 'D': {0, -1}, 'U': {0, 1}}

	var T, n, m int
	var s string
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &m, &s)
		var x, y, l, r, d, u int
		ansX, ansY := 1, 1
		for _, b := range s {
			x += dir4[b].x
			y += dir4[b].y
			if x < l {l = x} else if x > r {r = x}
			if y < d {d = y} else if y > u {u = y}
			if r-l >= m || u-d >= n {break}
			ansX, ansY = 1-d, 1-l
		}
		Fprintln(out, ansX, ansY)
	}
}

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


Gottfried learned about binary number representation. He then came up with this task and presented it to you.You are given a collection of $$$n$$$ non-negative integers $$$a_1, \ldots, a_n$$$. You are allowed to perform the following operation: choose two distinct indices $$$1 \leq i, j \leq n$$$. If before the operation $$$a_i = x$$$, $$$a_j = y$$$, then after the operation $$$a_i = x~\mathsf{AND}~y$$$, $$$a_j = x~\mathsf{OR}~y$$$, where $$$\mathsf{AND}$$$ and $$$\mathsf{OR}$$$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).After all operations are done, compute $$$\sum_{i=1}^n a_i^2$$$ — the sum of squares of all $$$a_i$$$. What is the largest sum of squares you can achieve?

The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, \ldots, a_n$$$ ($$$0 \leq a_i < 2^{20}$$$).

Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.

NoteIn the first sample no operation can be made, thus the answer is $$$123^2$$$.In the second sample we can obtain the collection $$$1, 1, 7$$$, and $$$1^2 + 1^2 + 7^2 = 51$$$.If $$$x$$$ and $$$y$$$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $$$x~\mathsf{AND}~y$$$ is set to $$$1$$$ if and only if both corresponding bits of $$$x$$$ and $$$y$$$ are set to $$$1$$$. Similarly, each bit of $$$x~\mathsf{OR}~y$$$ is set to $$$1$$$ if and only if at least one of the corresponding bits of $$$x$$$ and $$$y$$$ are set to $$$1$$$. For example, $$$x = 3$$$ and $$$y = 5$$$ are represented as $$$011_2$$$ and $$$101_2$$$ (highest bit first). Then, $$$x~\mathsf{AND}~y = 001_2 = 1$$$, and $$$x~\mathsf{OR}~y = 111_2 = 7$$$.

package main

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

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

	n := readNum(reader)
	A := readNNums(reader, n)

	fmt.Println(solve(n, A))
}

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) int64 {
	cnt := make([]int, 21)

	for i := 0; i < 21; i++ {
		for j := 0; j < n; j++ {
			if A[j]&(1<<uint(i)) > 0 {
				cnt[i]++
			}
		}
	}

	B := make([]int, n)

	for i := 0; i < 21; i++ {
		for x := 0; x < cnt[i]; x++ {
			B[x] = B[x] | (1 << uint(i))
		}
	}

	var res int64

	for i := 0; i < n; i++ {
		res += int64(B[i]) * int64(B[i])
	}

	return res
}

package main

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

var nn int

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	q := 1
	//fmt.Println(len(primes))
	for ii := 0; ii < q; ii++ {
		n := 0
		fmt.Fscanf(in, "%d\n", &n)
		tmp := 0
		a := make([]int, 21)
		weight := make([]int, 21)
		weight[0] = 1
		for i := 1; i < 21; i++ {
			weight[i] = weight[i-1] * 2
		}
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d", &tmp)
			id := 0
			for tmp > 0 {
				a[id] += tmp % 2
				tmp /= 2
				id++
			}
		}
		fmt.Fscanf(in, "\n")
		ans := int64(0)
		for true {
			cur := 0
			for i := 20; i >= 0; i-- {
				if a[i] > 0 {
					a[i]--
					cur += weight[i]
				}
			}
			if cur == 0 {
				break
			}
			ans += int64(cur) * int64(cur)
		}
		fmt.Fprintf(out, "%d\n", ans)
	}
	return
}

func gcd(a, b int) int {
	//fmt.Println(a, b)
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	bf := make([]byte, 1024*1024)
	sc.Buffer(bf, 10*1024*1024)
	sc.Split(bufio.ScanWords)

	in := func() uint64 {
		sc.Scan()
		i, _ := strconv.ParseUint(sc.Text(), 10, 64)
		return i
	}

	n := in()

	a := make([]uint64, n)

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

	var mask, sum uint64
	for i := 0; i < 20; i++ {
		st := 0
		mask = 1 << i

		for j := range a {
			if mask&a[j] == mask {
				st++
			}
		}

		for j := range a {
			if st != 0 {
				a[j] |= mask
				st--
			} else {
				a[j] &^= (mask)
			}
		}
	}

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

	fmt.Println(sum)
}

/*
URL:
https://codeforces.com/problemset/problem/1368/D
*/

package main

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

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

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

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

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

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

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

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

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

func init() {
	// bufio.ScanWords <---> bufio.ScanLines
	ReadString = newReadString(os.Stdin, bufio.ScanWords)
	stdout = bufio.NewWriter(os.Stdout)
}

var (
	n int
	A []int

	cnts [60]int
)

func main() {
	n = ReadInt()
	A = ReadIntSlice(n)

	for _, a := range A {
		for i := 0; i < 30; i++ {
			if NthBit(a, i) == 1 {
				cnts[i]++
			}
		}
	}

	B := make([]int, n)
	for i := 0; i < n; i++ {
		val := 0
		for j := 0; j < 30; j++ {
			if cnts[j] > 0 {
				val = OnBit(val, j)
				cnts[j]--
			}
		}

		B[i] = val
	}

	ans := int64(0)
	for _, b := range B {
		ans += int64(b) * int64(b)
	}
	fmt.Println(ans)
}

// NthBit returns nth bit value of an argument.
// n starts from 0.
func NthBit(num int, nth int) int {
	return num >> uint(nth) & 1
}

// OnBit returns the integer that has nth ON bit.
// If an argument has nth ON bit, OnBit returns the argument.
func OnBit(num int, nth int) int {
	return num | (1 << uint(nth))
}

// OffBit returns the integer that has nth OFF bit.
// If an argument has nth OFF bit, OffBit returns the argument.
func OffBit(num int, nth int) int {
	return num & ^(1 << uint(nth))
}

// PopCount returns the number of ON bit of an argument.
func PopCount(num int) int {
	res := 0

	for i := 0; i < 70; i++ {
		if ((num >> uint(i)) & 1) == 1 {
			res++
		}
	}

	return res
}

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

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

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func newReadString(ior io.Reader, sf bufio.SplitFunc) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	r.Split(sf)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

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

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

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

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

	return string(res)
}

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

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

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

	return string(res)
}

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

// PrintfBufStdout is function for output strings to buffered os.Stdout.
// You may have to call stdout.Flush() finally.
func PrintfBufStdout(format string, a ...interface{}) {
	fmt.Fprintf(stdout, format, a...)
}

package main

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

func main() {
	var n int
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	a := [200000]int{}
	bit := [20][]bool{}
	fmt.Fscan(in, &n)

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

	for i := 0; i < 20; i++ {
		for j := 0; j < n; j++ {
			b := (a[j] & (1 << i)) != 0
			// fmt.Println(a[j])
			bit[i] = append(bit[i], b)
		}

		sort.Slice(bit[i], func(x, y int) bool {
			return bit[i][x] && !bit[i][y]
		})

		// for j := 0; j < n; j++ {
		// 	fmt.Printf("%v ", bit[i][j])
		// }

		// fmt.Println("")
	}

	sum := int64(0)
	for i := 0; i < n; i++ {
		val := int64(0)
		for j := 0; j < 20; j++ {
			if bit[j][i] {
				val += 1 << j
			}
		}

		sum += val * val
	}

	fmt.Fprint(out, sum)
	out.Flush()
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1368D(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	cnt := [20]int{}
	var n, v int
	Fscan(in, &n)
	for i := 0; i < n; i++ {
		Fscan(in, &v)
		for j := range cnt {
			cnt[j] += v >> j & 1
		}
	}
	s := int64(0)
	for ; n > 0; n-- {
		v = 0
		for i, c := range cnt {
			if c > 0 {
				cnt[i]--
				v |= 1 << i
			}
		}
		s += int64(v) * int64(v)
	}
	Fprint(out, s)
}

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

package main
import (
        "fmt"
        "os"
        "bufio"
)
func main(){
    in := bufio.NewReader(os.Stdin)
    out := bufio.NewWriter(os.Stdout)
    defer out.Flush()
    var n int
    fmt.Fscan(in, &n)
    d := make([]int, 20)
    b := make([]int, 20)
    for i := 0; i < 20; i++ {
        d[i] = 1 << i
        b[i] = 0
    }
    for ; n > 0; n-- {
        var tmp int
        fmt.Fscan(in, &tmp)
        for j := 0; j < 20; j++ {
            if d[j] & tmp != 0 {
                b[j]++
            }
        }
    }
    var ans int64 = 0
    for true {
        var v int64 = 0
        for j := 0; j < 20; j++ {
            if b[j] != 0 {
                v += int64(d[j])
                b[j]--
            }
        }
        if v == 0 {
            break;
        } else {
            ans += v * v
        }
    }

    fmt.Fprintln(out, ans)
}

package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	bf := make([]byte, 1024*1024)
	sc.Buffer(bf, 10*1024*1024)
	sc.Split(bufio.ScanWords)

	in := func() uint64 {
		sc.Scan()
		i, _ := strconv.ParseUint(sc.Text(), 10, 64)
		return i
	}

	n := in()

	a := make([]uint64, n)

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

	var mask, i, sum uint64
	for i := 0; i < 20; i++ {
		st := 0
		mask = 1 << i
		for j := range a {
			if mask&a[j] == 0 {
				a[j], a[st] = a[st], a[j]
				st++
			}
		}
	}

	for i = 1; i < n; i++ {
		a[i-1], a[i] = a[i]&a[i-1], a[i]|a[i-1]
	}

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

	fmt.Println(sum)
}

package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	bf := make([]byte, 1024*1024)
	sc.Buffer(bf, 10*1024*1024)
	sc.Split(bufio.ScanWords)

	in := func() uint64 {
		sc.Scan()
		i, _ := strconv.ParseUint(sc.Text(), 10, 64)
		return i
	}

	n := in()

	a := make([]uint64, n)

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

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

	var sum uint64

	for i := range a {
		a[i], a[0] = a[i]&a[0], a[0]|a[i]
	}

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

	fmt.Println(sum)
}

package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	bf := make([]byte, 1024*1024)
	sc.Buffer(bf, 10*1024*1024)
	sc.Split(bufio.ScanWords)

	in := func() uint64 {
		sc.Scan()
		i, _ := strconv.ParseUint(sc.Text(), 10, 64)
		return i
	}

	n := in()

	a := make([]uint64, n)

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

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

	var sum uint64

	for i := range a {
		a[i], a[n-1] = a[i]&a[n-1], a[n-1]|a[i]
	}

	// fmt.Println(a)

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

	fmt.Println(sum)
}

package main
import (
        "fmt"
        "os"
        "bufio"
)
func main(){
    in := bufio.NewReader(os.Stdin)
    out := bufio.NewWriter(os.Stdout)
    defer out.Flush()
    var n int
    fmt.Fscan(in, &n)
    d := make([]int, 20)
    b := make([]int, 20)
    for i := 0; i < 20; i++ {
        d[i] = 1 << i
        b[i] = 0
    }
    for ; n > 0; n-- {
        var tmp int
        fmt.Fscan(in, &tmp)
        for j := 0; j < 20; j++ {
            if d[j] & tmp != 0 {
                b[j]++
            }
        }
    }
    var ans int64 = 0
    for true {
        v := 0
        for j := 0; j < 20; j++ {
            if b[j] != 0 {
                v += d[j]
                b[j]--
            }
        }
        if v == 0 {
            break;
        } else {
            ans += int64(v * v)
        }
    }

    fmt.Fprintln(out, ans)
}


Emuskald considers himself a master of flow algorithms. Now he has completed his most ingenious program yet — it calculates the maximum flow in an undirected graph. The graph consists of n vertices and m edges. Vertices are numbered from 1 to n. Vertices 1 and n being the source and the sink respectively.However, his max-flow algorithm seems to have a little flaw — it only finds the flow volume for each edge, but not its direction. Help him find for each edge the direction of the flow through this edges. Note, that the resulting flow should be correct maximum flow.More formally. You are given an undirected graph. For each it's undirected edge (ai, bi) you are given the flow volume ci. You should direct all edges in such way that the following conditions hold:  for each vertex v (1 < v < n), sum of ci of incoming edges is equal to the sum of ci of outcoming edges;  vertex with number 1 has no incoming edges;  the obtained directed graph does not have cycles.

The first line of input contains two space-separated integers n and m (2 ≤ n ≤ 2·105, n - 1 ≤ m ≤ 2·105), the number of vertices and edges in the graph. The following m lines contain three space-separated integers ai, bi and ci (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ ci ≤ 104), which means that there is an undirected edge from ai to bi with flow volume ci. It is guaranteed that there are no two edges connecting the same vertices; the given graph is connected; a solution always exists.

Output m lines, each containing one integer di, which should be 0 if the direction of the i-th edge is ai → bi (the flow goes from vertex ai to vertex bi) and should be 1 otherwise. The edges are numbered from 1 to m in the order they are given in the input. If there are several solutions you can print any of them.

NoteIn the first test case, 10 flow units pass through path , and 5 flow units pass directly from source to sink: .

package main

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

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

	var n, m, v, w, c int
	Fscan(in, &n, &m)
	type edge struct{ to, i, d, f int }
	g := make([][]edge, n+1)
	f := make([]int, n+1)
	for i := 0; i < m; i++ {
		Fscan(in, &v, &w, &c)
		g[v] = append(g[v], edge{w, i, 0, c})
		g[w] = append(g[w], edge{v, i, 1, c})
		f[v] += c
		f[w] += c
	}
	for i := range f {
		f[i] >>= 1
	}

	ans := make([]int, m)
	for i := range ans {
		ans[i] = -1
	}
	q := []int{1}
	for len(q) > 0 {
		v := q[0]
		q = q[1:]
		for _, e := range g[v] {
			if ans[e.i] == -1 {
				ans[e.i] = e.d
				w := e.to
				f[w] -= e.f
				if w != n && f[w] == 0 {
					q = append(q, w)
				}
			}
		}
	}
	for _, d := range ans {
		Fprintln(out, d)
	}
}

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


The letters shop showcase is a string $$$s$$$, consisting of $$$n$$$ lowercase Latin letters. As the name tells, letters are sold in the shop.Letters are sold one by one from the leftmost to the rightmost. Any customer can only buy some prefix of letters from the string $$$s$$$.There are $$$m$$$ friends, the $$$i$$$-th of them is named $$$t_i$$$. Each of them is planning to estimate the following value: how many letters (the length of the shortest prefix) would s/he need to buy if s/he wanted to construct her/his name of bought letters. The name can be constructed if each letter is presented in the equal or greater amount.  For example, for $$$s$$$="arrayhead" and $$$t_i$$$="arya" $$$5$$$ letters have to be bought ("arrayhead").  For example, for $$$s$$$="arrayhead" and $$$t_i$$$="harry" $$$6$$$ letters have to be bought ("arrayhead").  For example, for $$$s$$$="arrayhead" and $$$t_i$$$="ray" $$$5$$$ letters have to be bought ("arrayhead").  For example, for $$$s$$$="arrayhead" and $$$t_i$$$="r" $$$2$$$ letters have to be bought ("arrayhead").  For example, for $$$s$$$="arrayhead" and $$$t_i$$$="areahydra" all $$$9$$$ letters have to be bought ("arrayhead"). It is guaranteed that every friend can construct her/his name using the letters from the string $$$s$$$.Note that the values for friends are independent, friends are only estimating them but not actually buying the letters.

The first line contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of showcase string $$$s$$$. The second line contains string $$$s$$$, consisting of exactly $$$n$$$ lowercase Latin letters. The third line contains one integer $$$m$$$ ($$$1 \le m \le 5 \cdot 10^4$$$) — the number of friends. The $$$i$$$-th of the next $$$m$$$ lines contains $$$t_i$$$ ($$$1 \le |t_i| \le 2 \cdot 10^5$$$) — the name of the $$$i$$$-th friend. It is guaranteed that $$$\sum \limits_{i=1}^m |t_i| \le 2 \cdot 10^5$$$.

For each friend print the length of the shortest prefix of letters from $$$s$$$ s/he would need to buy to be able to construct her/his name of them. The name can be constructed if each letter is presented in the equal or greater amount. It is guaranteed that every friend can construct her/his name using the letters from the string $$$s$$$.

9
arrayhead
5
arya
harry
ray
r
areahydra

package main

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

func main() {
	//var read, _ = os.Open("dat.txt")
	//var write, _ = os.Create("result.txt")
	var read = bufio.NewReader(os.Stdin)
	var write = bufio.NewWriter(os.Stdout)
	defer write.Flush()
	var n int
	var s string
	fmt.Fscan(read, &n)
	fmt.Fscan(read, &s)
	var a = make([][]int, 26)
	for i := 0; i < n; i++ {
		var ai = int(s[i]) - 97
		a[ai] = append(a[ai], i)
	}
	for i := 0; i < 26; i++ {
		if len(a[i]) > 0 {
			sort.Ints(a[i])
		}
	}
	var m int
	fmt.Fscan(read, &m)
	for i := 0; i < m; i++ {
		var t string
		fmt.Fscan(read, &t)
		var tm = map[int]int{}
		for j := 0; j < len(t); j++ {
			var id = int(t[j]) - 97
			if val, ok := tm[id]; ok {
				tm[id] = val + 1
			} else {
				tm[id] = 1
			}
		}
		//Ищем максимальный префикс
		var nmax = -1
		for key, value := range tm {
			var nm = a[key][value-1]
			if nm > nmax {
				nmax = nm
			}
		}
		fmt.Fprintln(write, nmax+1)

	}

	//read.Close()
	//write.Close()
}

package main

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

func main() {
	// var a, b string
	var n, d int
	var t, s string
	scr := bufio.NewReader(os.Stdin)
	// ocr := bufio.NewWriter(os.Stdout)

	fmt.Fscanln(scr, &n)
	fmt.Fscanln(scr, &s)
	fmt.Fscanln(scr, &d)

	m := make(map[rune][]int)

	for x, y := range s {
		m[y] = append(m[y], x+1)
	}

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

		m1 := make(map[rune]int)

		fmt.Fscanln(scr, &t)

		arr := make([]int, 0)

		for _, y := range t {
			arr = append(arr, m[y][m1[y]])
			m1[y]++
		}

		sort.Ints(arr)
		fmt.Println(arr[len(arr)-1])
	}
}

package main

import "fmt"

func convertIndex(c int) int {
	return c - 97
}

type inputType struct {
	poses        []int
	currentIndex int
}

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

	}

	return b
}

func main() {
	var n, m int
	var s string
	var a [26]inputType
	fmt.Scanf("%d\n%s\n%d\n", &n, &s, &m)

	for i := 0; i < n; i++ {
		c := convertIndex(int(s[i]))
		a[c].poses = append(a[c].poses, i)
	}

	var ans int
	var tmp string
	for m > 0 {
		m--
		fmt.Scanf("%s\n", &tmp)

		for i := 0; i < 26; i++ {
			a[i].currentIndex = 0
		}

		ans = -1
		for _, c := range tmp {
			pos := convertIndex(int(c))
			currentIndex := a[pos].currentIndex

			ans = max(ans, a[pos].poses[currentIndex])
			a[pos].currentIndex++
		}

		fmt.Println(ans + 1)
	}
}

package main

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

var DEBUG = true

func main() {
	defer Flush()
	n := readi()
	_ = n
	s := readb()
	x := make([][]int, 26)
	for i, c := range s {
		j := int(c - 'a')
		x[j] = append(x[j], i+1)
	}

	m := readi()
	for i := 0; i < m; i++ {
		t := readb()
		y := make(map[int]int)
		for _, c := range t {
			y[int(c-'a')]++
		}

		var ans int
		for c, k := range y {
			ans = max(ans, x[c][k-1])
		}
		println(ans)
	}

}

// -----------------------------------------------------------------------------

// IO helper functions

// Returns next token from input.  It must be initialized by SetInput()
// before the first call.
var nextToken func() ([]byte, error)
var nextLine func() ([]byte, error)

// Holds a buffer for output.  It must be initialized by SetOutput().
// All IO fucntions (read*() and [e]print*()) should write to OutputWriter
// instead of this.
var OutputBuffer *bufio.Writer

// Holds an io.Writer.  It must be initialized by SetOutput()
var OutputWriter io.Writer

// Set IO functions for interactive input/output.
func SetInteractive(w io.Writer, r io.Reader) {
	SetUnbefferedInput(r)
	OutputBuffer = nil
	OutputWriter = w
}

// Setup OutputBuffer and OutputWriter.
func SetOutput(w io.Writer) {
	OutputBuffer = bufio.NewWriter(w)
	OutputWriter = OutputBuffer
}

// Flushes OutputBuffer
func Flush() {
	if OutputBuffer != nil {
		OutputBuffer.Flush()
	}
}

// Returns true if c is a white space
func IsSpace(c byte) bool {
	switch c {
	case '\t', '\n', '\v', '\f', '\r', ' ':
		return true
	}
	return false
}

func IsNewLine(c byte) bool {
	switch c {
	case '\n', '\r':
		return true
	}
	return false
}

// Setup nextToken with input buffer.
func SetInput(r io.Reader) {
	buf := new(bytes.Buffer)
	var b []byte

	var i int
	rest := func() ([]byte, error) {
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		return b[j:i], nil
	}
	initial := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextToken = rest
		return rest()
	}
	nextToken = initial

	restLn := func() ([]byte, error) {
		for i < len(b) && IsNewLine(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsNewLine(b[i]) {
			i++
		}
		return b[j:i], nil
	}

	initialLn := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextLine = restLn
		return restLn()
	}
	nextLine = initialLn
}

// Setup nextToken without input buffer.
func SetUnbefferedInput(r io.Reader) {
	buf := bufio.NewReader(r)
	var b []byte

	var i int
	nextToken = func() ([]byte, error) {
		var err error
		if i == len(b) {
			b, err = buf.ReadBytes('\n')
			if err != nil {
				return nil, err
			}
			i = 0
			j := len(b) - 1
			for 0 <= j && IsSpace(b[j]) {
				j--
			}
			b = b[0 : j+1]
		}
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		if i == j {
			return nil, io.ErrUnexpectedEOF
		}
		return b[j:i], nil
	}
}

// -----------------------------------------------------------------------------

// IO functions

// Reads next token and return it as []byte
func readb() []byte {
	b, err := nextToken()
	if err != nil {
		panic(err)
	}
	return b[:len(b):len(b)]
}

// Reads next token and return it as string
func reads() string {
	return string(readb())
}

// Read next line as []byte.  Trailing '\n' will not be included.
// See also comments on readb()
func readbln() []byte {
	b, err := nextLine()
	if err != nil {
		panic(err)
	}
	return b[:len(b):len(b)]
}

// Read next line as string
func readsln() string {
	return string(readbln())
}

// Reads next token and return it as int64
func readll() int64 {
	i, err := strconv.ParseInt(reads(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// Reads next token and return it as int
func readi() int {
	return int(readll())
}

// Reads next token and return it as float64
func readf() float64 {
	f, err := strconv.ParseFloat(reads(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// Write args to OutputWriter with the format f
func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(OutputWriter, f, args...)
}

// Write args to OutputWriter without format
func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(OutputWriter, args...)
}

// Write args to stderr with the format f
func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

// Write args to stderr without format
func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

func dbgf(f string, args ...interface{}) {
	if !DEBUG {
		return
	}
	fmt.Fprintf(os.Stderr, f, args...)
}

func dbg(args ...interface{}) {
	if !DEBUG {
		return
	}
	fmt.Fprintln(os.Stderr, args...)
}

// -----------------------------------------------------------------------------

// Utilities

func sumSlice(a []int) int {
	var res int
	for _, v := range a {
		res += v
	}
	return res
}

func sumSlicell(a []int64) int64 {
	var res int64
	for _, v := range a {
		res += v
	}
	return res
}

func readInts(N int) (int, []int) {
	if N == 0 {
		N = readi()
	}
	a := make([]int, N)
	for i := range a {
		a[i] = readi()
	}
	return N, a
}

func readIntsll(N int) (int, []int64) {
	if N == 0 {
		N = readi()
	}
	a := make([]int64, N)
	for i := range a {
		a[i] = readll()
	}
	return N, a
}

// reverse slice in place
func reverse(a []int) {
	for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 {
		a[i], a[j] = a[j], a[i]
	}
}

// areverse: allocating reverse
func areverse(a []int) []int {
	b := make([]int, len(a))
	for i := range a {
		b[i] = a[len(a)-i-1]
	}
	return b
}

// -----------------------------------------------------------------------------

// Simple math functions

const (
	// big prime
	INF  = 1000000007
	INF2 = 1000000009
	INF3 = 998244353
)

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

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

func minf(a, b float64) float64 {
	if a < b {
		return a
	}
	return b
}

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

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

func maxf(a, b float64) float64 {
	if a < b {
		return b
	}
	return a
}

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

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

func absf(a float64) float64 {
	if a < 0 {
		return -a
	}
	return a
}

// egcd(a, b) returns d, x, y:
//   d is gcd(a,b)
//   x, y are  integers that satisfy ax + by = d
func egcd(a, b int) (int, int, int) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcd(b, a%b)
	return d, y, x - a/b*y
}

func egcdll(a, b int64) (int64, int64, int64) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcdll(b, a%b)
	return d, y, x - a/b*y
}

func gcd(a, b int) int {
	d, _, _ := egcd(a, b)
	return d
}

func gcdll(a, b int64) int64 {
	d, _, _ := egcdll(a, b)
	return d
}

// set up IO functions
func init() {
	// for non-interactive
	SetInput(os.Stdin)
	SetOutput(os.Stdout)

	// Enable below when interactive.  Its ok to leave above intact.
	// SetInteractive(os.Stdout, os.Stdin)
}

package main

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

const nmax = 200000 + 1

 var writer = bufio.NewWriter(os.Stdout)

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

func toInt(buf []byte) (n uint32) {
	for _, v := range buf {
		if v >= '0' && v <= '9' {
			n = n*10 + uint32(v-'0')
		}
	}
	return
}

func solve(m []uint32, t string) uint32 {
	a := [30]uint32{}

	var max uint32

	for _, ch := range t {
		c := ch - 'a'
		a[c]++

		t := (m[a[c]*30+uint32(c)])
		if t > max {
			max = t
		}
	}

	return max
}

/*
type index struct {
	ch    rune
	count uint32
}
*/
func main() {
	reader := bufio.NewReaderSize(os.Stdin, nmax)

	reader.ReadLine()

	m := make([]uint32, nmax*30)

	a := [30]uint32{}

	for isPrefix := true; isPrefix; {
		var str []byte
		str, isPrefix, _ = reader.ReadLine()

		for i, ch := range str {
			c := ch - 'a'
			a[c]++

			m[a[c]*30+uint32(c)] = uint32(i)
		}
	}

	bytes, _ := reader.ReadBytes('\n')
	n := toInt(bytes)

	for i := uint32(0); i < n; i++ {
		t, _ := reader.ReadString('\n')

		t = strings.Trim(t, "\r\n")
		if len(t) > 0 {
			r := solve(m, t) + 1

			printf("%d\n",r)
		}
	}

		writer.Flush()
}

package main

import "fmt"

func convertIndex(c int) int {
	return c - 97
}

type inputType struct {
	poses        []int
	currentIndex int
}

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

	}

	return b
}

func main() {
	var n, m int
	var s string
	var a [26]inputType
	fmt.Scanf("%d\n%s\n%d", &n, &s, &m)

	for i := 0; i < n; i++ {
		c := convertIndex(int(s[i]))
		a[c].poses = append(a[c].poses, i)
	}

	var ans int
	var tmp string
	for m > 0 {
		m--
		fmt.Scanf("%s\n", &tmp)

		for i := 0; i < 26; i++ {
			a[i].currentIndex = 0
		}

		ans = -1
		for _, c := range tmp {
			pos := convertIndex(int(c))
			currentIndex := a[pos].currentIndex

			ans = max(ans, a[pos].poses[currentIndex])
			a[pos].currentIndex++
		}

		fmt.Println(ans + 1)
	}
}


Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.Determine if Snark and Philip can make an interesting problemset!

The first line contains two integers n, k (1 ≤ n ≤ 105, 1 ≤ k ≤ 4) — the number of problems and the number of experienced teams. Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.

Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise. You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").

NoteIn the first example you can't make any interesting problemset, because the first team knows all problems.In the second example you can choose the first and the third problems.

package main

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

// github.com/EndlessCheng/codeforces-go
func CF868C(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, k, a int
	Fscan(in, &n, &k)
	has := make([]bool, 1<<k)
	for ; n > 0; n-- {
		v := 0
		for i := 0; i < k; i++ {
			Fscan(in, &a)
			v |= a << i
		}
		has[v] = true
	}
	if has[0] {
		Fprint(out, "YES")
		return
	} else {
		for i, v := range has {
			for j, w := range has {
				if v && w && j != i && i&j == 0 {
					Fprint(out, "YES")
					return
				}
			}
		}
	}
	Fprint(out, "NO")
}

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