You are playing another computer game, and now you have to slay $$$n$$$ monsters. These monsters are standing in a circle, numbered clockwise from $$$1$$$ to $$$n$$$. Initially, the $$$i$$$-th monster has $$$a_i$$$ health.You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $$$1$$$ (deals $$$1$$$ damage to it). Furthermore, when the health of some monster $$$i$$$ becomes $$$0$$$ or less than $$$0$$$, it dies and explodes, dealing $$$b_i$$$ damage to the next monster (monster $$$i + 1$$$, if $$$i < n$$$, or monster $$$1$$$, if $$$i = n$$$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on.You have to calculate the minimum number of bullets you have to fire to kill all $$$n$$$ monsters in the circle.

The first line contains one integer $$$T$$$ ($$$1 \le T \le 150000$$$) — the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $$$n$$$ ($$$2 \le n \le 300000$$$) — the number of monsters. Then $$$n$$$ lines follow, each containing two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i, b_i \le 10^{12}$$$) — the parameters of the $$$i$$$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $$$300000$$$.

For each test case, print one integer — the minimum number of bullets you have to fire to kill all of the monsters.

package main

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

func main() {
	var t, n int64
	var a0, ta, tb, pb int64
	s := int64(0)
	ms := a0

	tokens := []int64{}
	tmp := int64(0)

	reader := bufio.NewReader(os.Stdin)
	for b, err := reader.ReadByte(); err == nil; b, err = reader.ReadByte() {
		switch b {
		case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
			tmp = tmp*10 + int64(b-'0')
		default:
			if tmp > 0 {
				tokens = append(tokens, tmp)
				tmp = 0
			}
		}
	}
	tokens = append(tokens, tmp)

	p := 0
	t = tokens[p]
	p++
	output := bufio.NewWriter(os.Stdout)

	for ; t > 0; t-- {
		n = tokens[p]
		p++

		a0 = tokens[p]
		p++
		pb = tokens[p]
		p++

		s = int64(0)
		ms = a0
		if ms > pb {
			ms = pb
		}
		for i := int64(1); i < n; i++ {
			ta = tokens[p]
			p++
			tb = tokens[p]
			p++

			if ta > pb {
				s += ta - pb
			}
			pb = tb

			if ms > ta {
				ms = ta
			}
			if ms > tb {
				ms = tb
			}
		}
		if a0 > pb {
			s += a0 - pb
		}
		s += ms

		output.WriteString(fmt.Sprintln(s))
	}
	output.Flush()
}

/*
URL:
https://codeforces.com/problemset/problem/1334/C
*/

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 (
	t    int
	n    int
	A, B []int64
)

func main() {
	t = ReadInt()
	for i := 0; i < t; i++ {
		n = ReadInt()
		A, B = []int64{}, []int64{}
		for j := 0; j < n; j++ {
			a, b := ReadInt64_2()
			A = append(A, a)
			B = append(B, b)
		}

		solve()
	}
}

func solve() {
	R := make([]int64, n)
	for i := 1; i < n; i++ {
		R[i] = Max(0, A[i]-B[i-1])
	}
	R[0] = Max(0, A[0]-B[n-1])

	rs := Sum(R...)

	ans := int64(INF_BIT60)
	for i := 0; i < n; i++ {
		tmp := A[i] + rs - R[i]
		ChMin(&ans, tmp)
	}
	fmt.Println(ans)
}

// 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 *int64, target int64) bool {
	if *updatedValue > target {
		*updatedValue = target
		return true
	}
	return false
}

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

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

	return s
}

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

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

/*********** 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"
	_ "math"
	"os"
	_ "sort"
)

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...) }

type intPair struct {
	a int64
	b int64
}

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

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

func main() {
	defer writer.Flush()

	var t int
	scanf("%d\n", &t)

	for t>0 {
		t--
		var n int
		scanf("%d\n", &n)

		var c []intPair

		for i := 0; i < n;i++ {
			var ab intPair
			scanf("%d %d\n", &ab.a, &ab.b)
			c = append(c, ab)
		}
		var ans int64 = 0
		var wt int64 = 1e18

		for i := 0; i<n;i++ {
			var next = (i+1)%n

			ans += max(int64(0), c[next].a - c[i].b)
			wt = min(wt, min(c[next].a, c[i].b))
		}

		printf("%d\n", (ans + wt))
	}
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
	"runtime/debug"
)

// github.com/EndlessCheng/codeforces-go
func CF1334C(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	type pair struct{ hp, dmg int64 }

	var t, n int
	for Fscan(in, &t); t > 0; t-- {
		ans := int64(0)
		Fscan(in, &n)
		a := make([]pair, n)
		for i := range a {
			Fscan(in, &a[i].hp, &a[i].dmg)
		}
		minDmg := int64(1e18)
		for i, p := range a {
			if hp := a[(i+1)%n].hp; p.dmg > hp {
				p.dmg = hp
			}
			ans += p.hp - p.dmg
			if p.dmg < minDmg {
				minDmg = p.dmg
			}
		}
		Fprintln(out, ans+minDmg)
	}
}

func main() {
	debug.SetGCPercent(-1)
	CF1334C(os.Stdin, os.Stdout)
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1334C(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	type pair struct{ hp, dmg int64 }

	var t, n int
	for Fscan(in, &t); t > 0; t-- {
		ans := int64(0)
		Fscan(in, &n)
		a := make([]pair, n)
		for i := range a {
			Fscan(in, &a[i].hp, &a[i].dmg)
		}
		minDmg := int64(1e18)
		for i, p := range a {
			if hp := a[(i+1)%n].hp; p.dmg > hp {
				p.dmg = hp
			}
			ans += p.hp - p.dmg
			if p.dmg < minDmg {
				minDmg = p.dmg
			}
		}
		Fprintln(out, ans+minDmg)
	}
}

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

package main

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

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

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

	var t int
	fmt.Fscan(r, &t)
	for ; t > 0; t-- {
		var n int
		fmt.Fscan(r, &n)
		a := make([]int64, n)
		b := make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(r, &a[i], &b[i])
		}

		c := make([]int64, n)
		c[0] = max(a[0]-b[n-1], 0)
		sum := c[0]
		for i := 1; i < n; i++ {
			c[i] = max(a[i]-b[i-1], 0)
			sum += c[i]
		}

		var ans int64
		ans = math.MaxInt64
		for i := 0; i < n; i++ {
			cost := sum + a[i] - c[i]
			if ans > cost {
				ans = cost
			}
		}
		fmt.Fprintln(w, ans)
	}
}

// Author: sighduck
// URL: https://codeforces.com/problemset/problem/1334/C

package main

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

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

func Solve(n int, a, b []int64) int64 {
	s := int64(0)
	low := int64(math.MaxInt64)
	for i := 0; i < n; i++ {
		ai, bi := a[i], b[((i-1) % n + n)%n]
		m := max(0, ai-bi)
		s += m
		d := ai
		if m > 0 {
			d = bi
		}
		if d < low {
			low = d
		}
	}
	return s + low
}

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

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

package main

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

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

type writerImpl struct {
	*bufio.Writer
}

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

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

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

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

type wordScannerImpl struct {
	*bufio.Scanner
}

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

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

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

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

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

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

func sol(aa [][2]int64) int64 {
	for i := range aa {
		nextI := (i + 1) % len(aa)
		aa[i][1] = min(aa[i][1], aa[nextI][0])
	}
    leastDmgDealt := aa[0][1]
    totalDmgDealt := int64(0)
    totalHp := int64(0)
    for i := range aa {
        totalHp += aa[i][0]
        totalDmgDealt += aa[i][1]
        leastDmgDealt = min(leastDmgDealt, aa[i][1])
    }
    return totalHp - (totalDmgDealt - leastDmgDealt)
}

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

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 max(a,b int) int {
	if a>b {
		return a
	} else {
		return b
	}
}
func min(a,b int64) int64 {
	if a>b {
		return b
	} else {
		return a
	}
}

func main() {

	defer writer.Flush()

	var t int
	scanf("%d\n",&t)
	for ;t>0;t-- {
	    var n int
	    scanf("%d\n",&n)
	    var a, b []int64
	    for i:=0;i<n;i++ {
	        var x, y int64
	        scanf("%d %d\n",&x,&y)
	        a=append(a,x)
	        b=append(b,y)
	    }
	    var sum int64 = 0
	    var mn int64
	    mn=b[0]
	    for i:=0;i<n;i++ {
	        b[i]=min(b[i],a[(i+1)%n])
	        mn=min(mn,b[i])
	        sum+=a[i]-b[i]
	    }
	    printf("%d\n",sum+mn)
	}

}

package main

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

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

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

		monsA := make([]int64, N)
		monsB := make([]int64, N)
		var sa int64

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

			scanf("%d %d\n", &a, &b)
			sa += a
			monsA[i] = a
			monsB[i] = b
		}

		var ss int64 = 0
		var ms int64 = 1E12 + 1

		for i := 0; i < N; i++ {
			b = monsB[(i - 1 + N) % N]
			a = monsA[i]
			if b < a {
				ss += b
				if b < ms {
					ms = b
				}
			} else {
				ss += a
				if a < ms {
					ms = a
				}
			}
		}

		ans := sa - ss + ms
		defer writer.Flush()
		printf("%d\n", ans)
	}
}

package main

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

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

func main() {
	var T int
	scanf("%d\n", &T)
	var a, b int64
	monsA := make([]int64, 3 * 1E5 + 1)
	monsB := make([]int64, 3 * 1E5 + 1)

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

		var sa int64

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

			scanf("%d %d\n", &a, &b)
			sa += a
			monsA[i] = a
			monsB[i] = b
		}

		var ss int64 = 0
		var ms int64 = 1E12 + 1

		for i := 0; i < N; i++ {
			b = monsB[(i - 1 + N) % N]
			a = monsA[i]
			if b < a {
				ss += b
				if b < ms {
					ms = b
				}
			} else {
				ss += a
				if a < ms {
					ms = a
				}
			}
		}

		ans := sa - ss + ms
		defer writer.Flush()
		printf("%d\n", ans)
	}
}

//https://codeforces.com/problemset/problem/1334/C
//Educational Codeforces Round 85 (Rated for Div. 2)

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...) }

const maxn int = 3*1e5

var n int
var a, b [maxn]int64
var ans int64

func main() {
    defer writer.Flush()

    var t int
    scanf("%d\n", &t)

    for t > 0 {
        t--
        scanf("%d\n", &n)
        for i := 0 ; i < n ; i++ {
            scanf("%d %d\n", &a[i], &b[i])
        }
        ans = 0
        mn := a[0]
        for i := 0 ; i < n ; i++ {
            prev := i-1
            if prev < 0 {
                prev = n-1
            }
            if a[i] > b[prev] {
                ans += a[i] - b[prev]
                a[i] = b[prev]
            }
            if a[i] < mn {
                mn = a[i]
            }
        }
        ans += mn
        printf("%d\n", ans)
    }
}

package main

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

func main() {
	var t, n int64
	var a0, ta, tb, pb int64
	s := int64(0)
	ms := a0

	in := make([]byte, 25000000)
	tn, _ := io.ReadFull(os.Stdin, in)

	in = in[0 : tn+1]
	in[tn] = ' '
	tokens := []int64{}
	tmp := int64(0)
	output := bufio.NewWriter(os.Stdout)

	for _, b := range in {
		switch b {
		case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
			tmp = tmp*10 + int64(b-'0')
		default:
			if tmp > 0 {
				tokens = append(tokens, tmp)
				tmp = 0
			}
		}
	}
	tokens = append(tokens, tmp)

	if true {
		p := 0
		t = tokens[p]
		p++

		for ; t > 0; t-- {
			n = tokens[p]
			p++

			a0 = tokens[p]
			p++
			pb = tokens[p]
			p++

			s = int64(0)
			ms = a0
			if ms > pb {
				ms = pb
			}
			for i := int64(1); i < n; i++ {
				ta = tokens[p]
				p++
				tb = tokens[p]
				p++

				if ta > pb {
					s += ta - pb
				}
				pb = tb

				if ms > ta {
					ms = ta
				}
				if ms > tb {
					ms = tb
				}
			}
			if a0 > pb {
				s += a0 - pb
			}
			s += ms

			output.WriteString(strconv.FormatInt(s, 10) + "\n")
		}
		output.Flush()
	}
}

package main

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

func main() {
	var t, n int64
	var a0, ta, tb, pb int64
	s := int64(0)
	ms := a0

	in := make([]byte, 30000)
	tn, _ := io.ReadFull(os.Stdin, in)
	if false {

		in = in[0 : tn+1]
		in[tn] = ' '
		tokens := []int64{}
		tmp := int64(0)
		output := bufio.NewWriter(os.Stdout)

		for _, b := range in {
			switch b {
			case ' ', '\n':
				tokens = append(tokens, tmp)
				tmp = 0
			default:
				tmp = tmp*10 + int64(b-'0')
			}
		}
		tokens = append(tokens, tmp)

		p := 0
		t = tokens[p]
		p++

		for ; t > 0; t-- {
			n = tokens[p]
			p++

			a0 = tokens[p]
			p++
			pb = tokens[p]
			p++

			s = int64(0)
			ms = a0
			if ms > pb {
				ms = pb
			}
			for i := int64(1); i < n; i++ {
				ta = tokens[p]
				p++
				tb = tokens[p]
				p++

				if ta > pb {
					s += ta - pb
				}
				pb = tb

				if ms > ta {
					ms = ta
				}
				if ms > tb {
					ms = tb
				}
			}
			if a0 > pb {
				s += a0 - pb
			}
			s += ms

			output.WriteString(strconv.FormatInt(s, 10) + "\n")
		}
		output.Flush()
	}
}

package main

import (
	"fmt"
	"runtime"
)

func main() {
	var t, n int
	var a0, ta, tb int64

	ballast := make([]byte, 10000)
	defer runtime.KeepAlive(ballast)

	fmt.Scanf("%d", &t)
	for ; t > 0; t-- {
		fmt.Scanf("%d", &n)
		fmt.Scanf("%d%d", &a0, &tb)

		s := int64(0)
		ms := a0
		if ms > tb {
			ms = tb
		}
		for i := 1; i < n; i++ {
			fmt.Scanf("%d", &ta)
			if ta > tb {
				s += ta - tb
			}
			fmt.Scanf("%d", &tb)
			if ms > ta {
				ms = ta
			}
			if ms > tb {
				ms = tb
			}
		}
		if a0 > tb {
			s += a0 - tb
		}
		s += ms
		fmt.Println(s)
	}
}

package main

import (
	"fmt"
	"runtime"
)

func main() {
	var t, n int
	var a0, ta, tb int64
	s := int64(0)
	ms := a0

	ballast := make([]byte, 10000)
	defer runtime.KeepAlive(ballast)

	fmt.Scanf("%d", &t)
	for ; t > 0; t-- {
		fmt.Scanf("%d", &n)
		fmt.Scanf("%d%d", &a0, &tb)

		s = int64(0)
		ms = a0
		if ms > tb {
			ms = tb
		}
		for i := 1; i < n; i++ {
			fmt.Scanf("%d", &ta)
			if ta > tb {
				s += ta - tb
			}
			fmt.Scanf("%d", &tb)
			if ms > ta {
				ms = ta
			}
			if ms > tb {
				ms = tb
			}
		}
		if a0 > tb {
			s += a0 - tb
		}
		s += ms
		fmt.Println(s)
	}
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1334C(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	type pair struct{ hp, dmg int64 }
	less := func(a, b pair) bool { return a.hp < b.hp || a.hp == b.hp && a.dmg > b.dmg }

	var t, n int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n)
		minI := 0
		a := make([]pair, n)
		for i := range a {
			Fscan(in, &a[i].hp, &a[i].dmg)
			if less(a[i], a[minI]) {
				minI = i
			}
		}
		ans, dmg := a[minI].hp, a[minI].dmg
		for i := 1; i < n; i++ {
			p := a[(minI+i)%n]
			if hp := p.hp - dmg; hp > 0 {
				ans += hp
			}
			dmg = p.dmg
		}
		Fprintln(out, ans)
	}
}

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

package main

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

type intArr []int64

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

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

	var t int
	fmt.Fscan(r, &t)
	for ; t > 0; t-- {
		var n int
		fmt.Fscan(r, &n)
		a := make(intArr, n)
		b := make(intArr, n)
		var min int64 = math.MaxInt64
		for i := 0; i < n; i++ {
			fmt.Fscan(r, &a[i], &b[i])
			if min > a[i] && a[i] >= b[i] {
				min = a[i]
			}
		}
		c := make(intArr, n)
		c[0] = a[0] - b[n-1]
		if c[0] < 0 {
			c[0] = 0
		}
		sum := c[0]

		for i := 1; i < n; i++ {
			c[i] = a[i] - b[i-1]
			if c[i] < 0 {
				c[i] = 0
			}
			sum += c[i]
		}
		sort.Sort(a)
		if sum == 0 {
			fmt.Fprintln(w, a[0])
		} else {
			if min == math.MaxInt64 {
				fmt.Fprintln(w, sum+a[0])
			} else {
				fmt.Fprintln(w, sum+min)
			}
		}
	}
}

package main

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

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

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

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

func scanString() string {
	if sc.Scan() {
		return sc.Text()
	}
	panic(sc.Err())
}

var sc = newScanner()

func main() {
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()

	for t := scanInt(); t > 0; t-- {
		n := scanInt()
		a := make([]int, n)
		b := make([]int, n)
		for i := 0; i < n; i++ {
			a[i], b[i] = scanInt(), scanInt()
		}
		c := make([]int, n)
		c[0] = a[0] - b[n-1]
		if c[0] < 0 {
			c[0] = 0
		}
		sum := c[0]

		for i := 1; i < n; i++ {
			c[i] = a[i] - b[i-1]
			if c[i] < 0 {
				c[i] = 0
			}
			sum += c[i]
		}
		sort.Ints(a)
		if sum == 0 {
			fmt.Println(a[0])
		} else {
			fmt.Println(sum + a[0])
		}
	}
}

package main

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

type intArr []int64

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

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

	var t int
	fmt.Fscan(r, &t)
	for ; t > 0; t-- {
		var n int
		fmt.Fscan(r, &n)
		a := make(intArr, n)
		b := make(intArr, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(r, &a[i], &b[i])
		}
		c := make(intArr, n)
		c[0] = a[0] - b[n-1]
		if c[0] < 0 {
			c[0] = 0
		}
		sum := c[0]

		for i := 1; i < n; i++ {
			c[i] = a[i] - b[i-1]
			if c[i] < 0 {
				c[i] = 0
			}
			sum += c[i]
		}
		sort.Sort(a)
		if sum == 0 {
			fmt.Fprintln(w, a[0])
		} else {
			fmt.Fprintln(w, sum+a[0])
		}
	}
}

package main

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

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

type writerImpl struct {
	*bufio.Writer
}

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

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

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

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

type wordScannerImpl struct {
	*bufio.Scanner
}

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

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

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

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

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

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

func sol(aa [][2]int64) int64 {
	ret := int64(0)
	for i := range aa {
		nextI := (i + 1) % len(aa)
		aa[i][1] = min(aa[i][1], aa[nextI][0])
	}
    minSoFar := aa[0][0]
    for i := range aa {
        ret += aa[i][0] - aa[i][1]
        minSoFar = min(minSoFar, aa[i][0])
    }
    ret += minSoFar
	return ret
}

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

//https://codeforces.com/problemset/problem/1334/C
//Educational Codeforces Round 85 (Rated for Div. 2)

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...) }

const maxn int = 3*1e5

var n int
var a, b [maxn]int64
var ans int64

func main() {
    defer writer.Flush()

    var t int
    scanf("%d\n", &t)

    for t > 0 {
        t--
        scanf("%d\n", &n)
        for i := 0 ; i < n ; i++ {
            scanf("%d %d\n", &a[i], &b[i])
        }
        ans = 0
        mn := a[0]
        for i := 0 ; i < n ; i++ {
            prev := i-1
            if prev < 0 {
                prev = n-1
            }
            if a[i] > b[prev] {
                ans += a[i] - b[prev]
            }
            if a[i] < mn {
                mn = a[i]
            }
        }
        ans += mn
        printf("%d\n", ans)
    }
}

package main

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

func main() {
	var t, n int64
	var a0, ta, tb, pb int64
	s := int64(0)
	ms := a0

	in := make([]byte, 30000)
	tn, _ := io.ReadFull(os.Stdin, in)

	in = in[0 : tn+1]
	in[tn] = ' '
	tokens := []int64{}
	tmp := int64(0)
	output := bufio.NewWriter(os.Stdout)

	for _, b := range in {
		switch b {
		case ' ', '\n':
			tokens = append(tokens, tmp)
			tmp = 0
		default:
			tmp = tmp*10 + int64(b-'0')
		}
	}
	tokens = append(tokens, tmp)
	fmt.Println(tokens)

	if false {
		p := 0
		t = tokens[p]
		p++

		for ; t > 0; t-- {
			n = tokens[p]
			p++

			a0 = tokens[p]
			p++
			pb = tokens[p]
			p++

			s = int64(0)
			ms = a0
			if ms > pb {
				ms = pb
			}
			for i := int64(1); i < n; i++ {
				ta = tokens[p]
				p++
				tb = tokens[p]
				p++

				if ta > pb {
					s += ta - pb
				}
				pb = tb

				if ms > ta {
					ms = ta
				}
				if ms > tb {
					ms = tb
				}
			}
			if a0 > pb {
				s += a0 - pb
			}
			s += ms

			output.WriteString(strconv.FormatInt(s, 10) + "\n")
		}
		output.Flush()
	}
}

package main

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

func main() {
	var t, n int64
	var a0, ta, tb, pb int64
	s := int64(0)
	ms := a0

	in := make([]byte, 30000)
	tn, _ := io.ReadFull(os.Stdin, in)

	in = in[0 : tn+1]
	in[tn] = ' '
	tokens := []int64{}
	tmp := int64(0)
	output := bufio.NewWriter(os.Stdout)

	for _, b := range in {
		switch b {
		case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
			tmp = tmp*10 + int64(b-'0')
		default:
			tokens = append(tokens, tmp)
			tmp = 0
		}
	}
	tokens = append(tokens, tmp)
	fmt.Println(tokens)

	if true {
		p := 0
		t = tokens[p]
		p++

		for ; t > 0; t-- {
			n = tokens[p]
			p++

			a0 = tokens[p]
			p++
			pb = tokens[p]
			p++

			s = int64(0)
			ms = a0
			if ms > pb {
				ms = pb
			}
			for i := int64(1); i < n; i++ {
				ta = tokens[p]
				p++
				tb = tokens[p]
				p++

				if ta > pb {
					s += ta - pb
				}
				pb = tb

				if ms > ta {
					ms = ta
				}
				if ms > tb {
					ms = tb
				}
			}
			if a0 > pb {
				s += a0 - pb
			}
			s += ms

			output.WriteString(strconv.FormatInt(s, 10) + "\n")
		}
		output.Flush()
	}
}

package main

import (
	"fmt"
	"runtime"
)

func main() {
	var t, n int
	var a0, ta, tb int64
	s := int64(0)
	ms := a0

	ballast := make([]byte, 10000)
	defer runtime.KeepAlive(ballast)

	fmt.Scanf("%d", &t)
	for ; t > 0; t-- {
		fmt.Scanf("%d", &n)
		fmt.Scanf("%d%d", &a0, &tb)

		fmt.Println(a0, tb)

		s = int64(0)
		ms = a0
		if ms > tb {
			ms = tb
		}
		for i := 1; i < n; i++ {
			fmt.Scanf("%d", &ta)
			if ta > tb {
				s += ta - tb
			}
			fmt.Scanf("%d", &tb)
			if ms > ta {
				ms = ta
			}
			if ms > tb {
				ms = tb
			}
			fmt.Println(ta, tb)
		}
		if a0 > tb {
			s += a0 - tb
		}
		s += ms
		fmt.Println(s)
	}
}

package main

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

func main() {
	var t, n int64
	var a0, ta, tb, pb int64
	s := int64(0)
	ms := a0

	in := make([]byte, 30000)
	tn, _ := io.ReadFull(os.Stdin, in)

	in = in[0 : tn+1]
	in[tn] = ' '
	tokens := []int64{}
	tmp := int64(0)
	output := bufio.NewWriter(os.Stdout)

	for _, b := range in {
		switch b {
		case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
			tmp = tmp*10 + int64(b-'0')
		default:
			if tmp > 0 {
				tokens = append(tokens, tmp)
				tmp = 0
			}
		}
	}
	tokens = append(tokens, tmp)
	fmt.Println(tokens)

	if true {
		p := 0
		t = tokens[p]
		p++

		for ; t > 0; t-- {
			n = tokens[p]
			p++

			a0 = tokens[p]
			p++
			pb = tokens[p]
			p++

			s = int64(0)
			ms = a0
			if ms > pb {
				ms = pb
			}
			for i := int64(1); i < n; i++ {
				ta = tokens[p]
				p++
				tb = tokens[p]
				p++

				if ta > pb {
					s += ta - pb
				}
				pb = tb

				if ms > ta {
					ms = ta
				}
				if ms > tb {
					ms = tb
				}
			}
			if a0 > pb {
				s += a0 - pb
			}
			s += ms

			output.WriteString(strconv.FormatInt(s, 10) + "\n")
		}
		output.Flush()
	}
}

package main

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

func main() {
	var t, n int64
	var a0, ta, tb, pb int64
	s := int64(0)
	ms := a0

	in := make([]byte, 30000)
	tn, _ := io.ReadFull(os.Stdin, in)

	in = in[0 : tn+1]
	in[tn] = ' '
	tokens := []int64{}
	tmp := int64(0)
	output := bufio.NewWriter(os.Stdout)

	for _, b := range in {
		switch b {
		case ' ', '\n':
			tokens = append(tokens, tmp)
			tmp = 0
		default:
			tmp = tmp*10 + int64(b-'0')
		}
	}
	tokens = append(tokens, tmp)

	if false {
		p := 0
		t = tokens[p]
		p++

		for ; t > 0; t-- {
			n = tokens[p]
			p++

			a0 = tokens[p]
			p++
			pb = tokens[p]
			p++

			s = int64(0)
			ms = a0
			if ms > pb {
				ms = pb
			}
			for i := int64(1); i < n; i++ {
				ta = tokens[p]
				p++
				tb = tokens[p]
				p++

				if ta > pb {
					s += ta - pb
				}
				pb = tb

				if ms > ta {
					ms = ta
				}
				if ms > tb {
					ms = tb
				}
			}
			if a0 > pb {
				s += a0 - pb
			}
			s += ms

			output.WriteString(strconv.FormatInt(s, 10) + "\n")
		}
		output.Flush()
	}
}

package main

import (
	"fmt"
)

func main() {
	var t, n int
	var a0, ta, tb int

	fmt.Scan(&t)
	fmt.Println(t)
	for ; t > 0; t-- {
		fmt.Scan(&n)
		fmt.Scan(&a0, &tb)

		s := 0
		ms := a0
		if ms > tb {
			ms = tb
		}
		for i := 1; i < n; i++ {
			fmt.Scan(&ta)
			if ta > tb {
				s += ta - tb
			}
			fmt.Scan(&tb)
			if ms > ta {
				ms = ta
			}
			if ms > tb {
				ms = tb
			}
		}
		if a0 > tb {
			s += a0 - tb
		}
		s += ms
		fmt.Println(s)
	}
}


Arcady is a copywriter. His today's task is to type up an already well-designed story using his favorite text editor.Arcady types words, punctuation signs and spaces one after another. Each letter and each sign (including line feed) requires one keyboard click in order to be printed. Moreover, when Arcady has a non-empty prefix of some word on the screen, the editor proposes a possible autocompletion for this word, more precisely one of the already printed words such that its prefix matches the currently printed prefix if this word is unique. For example, if Arcady has already printed «codeforces», «coding» and «codeforces» once again, then there will be no autocompletion attempt for «cod», but if he proceeds with «code», the editor will propose «codeforces».With a single click Arcady can follow the editor's proposal, i.e. to transform the current prefix to it. Note that no additional symbols are printed after the autocompletion (no spaces, line feeds, etc). What is the minimum number of keyboard clicks Arcady has to perform to print the entire text, if he is not allowed to move the cursor or erase the already printed symbols?A word here is a contiguous sequence of latin letters bordered by spaces, punctuation signs and line/text beginnings/ends. Arcady uses only lowercase letters. For example, there are 20 words in «it's well-known that tic-tac-toe is a paper-and-pencil game for two players, x and o.».

The only line contains Arcady's text, consisting only of lowercase latin letters, spaces, line feeds and the following punctuation signs: «.», «,», «?», «!», «'» and «-». The total amount of symbols doesn't exceed 3·105. It's guaranteed that all lines are non-empty.

Print a single integer — the minimum number of clicks.

snow affects sports such as skiing, snowboarding, and snowmachine travel.
snowboarding is a recreational activity and olympic and paralympic sport.

thun-thun-thunder, thunder, thunder
thunder, thun-, thunder
thun-thun-thunder, thunder
thunder, feel the thunder
lightning then the thunder
thunder, feel the thunder
lightning then the thunder
thunder, thunder

NoteIn sample case one it's optimal to use autocompletion for the first instance of «snowboarding» after typing up «sn» and for the second instance of «snowboarding» after typing up «snowb». This will save 7 clicks.In sample case two it doesn't matter whether to use autocompletion or not.

package main

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

var words []string
var count int

type Bor struct {
	next      [26]*Bor
	character byte
	count     int
	can       bool
}

func NewBor(character byte) *Bor {
	return &Bor{[26]*Bor{}, character, 0, true}
}

func main() {
	readWords()

	bor := NewBor(' ')

	for i := 0; i < len(words); i++ {
		r, _ := recurce(&words[i], 0, bor, false, -1, -1, -1)

		count += r
	}

	fmt.Println(count)
}

func readWords() {
	scanner := bufio.NewScanner(os.Stdin)
	// f, _ := os.Open("input")

	// scanner := bufio.NewScanner(f)

	words = make([]string, 0)

	scanner.Buffer(make([]byte, 3*1e5+2), 3*1e5+2)

	for scanner.Scan() {
		word, startIndex, hasLetters := make([]byte, 0, len(scanner.Bytes())), 0, false

		count++

		for _, character := range scanner.Bytes() {
			if character >= 'A' && character <= 'Z' {
				character += 'a' - 'A'
			}

			if character >= 'a' && character <= 'z' {
				word, hasLetters = append(word, character), true
			} else {
				if hasLetters {
					words = append(words, string(word[startIndex:]))
				}

				startIndex, hasLetters = len(word), false

				count++
			}
		}

		if hasLetters {
			words = append(words, string(word[startIndex:]))
		}
	}

	if scanner.Err() != nil {
		fmt.Println(scanner.Err())
	}
}

func recurce(word *string, i int, current *Bor, flag bool, flagI, flagJ, flagK int) (int, int) {
	if i == len(*word) {
		if current.count != 0 {
			current.can = false
			return len(*word), i - 1
		}

		if current.count == 0 && flag {
			if flagJ == -1 {
				return min(len(*word), flagI+1), flagK
			}

			return min(len(*word), len(*word)-(flagJ-flagI)+1), flagK
		}

		return len(*word), flagK
	}

	if next := current.next[(*word)[i]-'a']; next != nil {
		if current.count != 1 {
			return recurce(word, i+1, next, false, -1, -1, -1)
		} else if flag {
			a, b := recurce(word, i+1, next, flag, flagI, -1, -1)
			if b != -1 && i <= b {
				current.can = false
			}
			return a, b
		} else {
			a, b := recurce(word, i+1, next, i != 0 && current.can, i, -1, -1)
			if b != -1 && i <= b {
				current.can = false
			}
			return a, b
		}
	} else {
		current.next[(*word)[i]-'a'] = NewBor((*word)[i])
		next := current.next[(*word)[i]-'a']
		current.count++

		if flagK == -1 {
			flagK = i
		}

		if current.count == 1 && flag {
			if flagJ == -1 {
				flagJ = i
			}

			a, b := recurce(word, i+1, next, flag, flagI, flagJ, flagK)
			if b != -1 && i <= b {
				current.can = false
			}
			return a, b
		}

		a, b := recurce(word, i+1, next, false, -1, -1, flagK)
		if b != -1 && i <= b {
			current.can = false
		}
		return a, b
	}
}

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

	return b
}

package main

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

var words []string
var count int

func main() {
	readWords()

	for i := 0; i < len(words); i++ {
		if len(words[i]) <= 2 {
			count += len(words[i])
		} else {
			count += tryAutocomplete(i)
		}
	}

	fmt.Println(count)
}

func readWords() {
	scanner := bufio.NewScanner(os.Stdin)

	words = make([]string, 0)

	for scanner.Scan() {
		word, startIndex, hasLetters := make([]byte, 0, len(scanner.Bytes())), 0, false

		count++

		for _, character := range scanner.Bytes() {
			if character >= 'a' && character <= 'z' {
				word, hasLetters = append(word, character), true
			} else {
				if hasLetters {
					words = append(words, string(word[startIndex:]))
				}

				startIndex, hasLetters = len(word), false

				count++
			}
		}

		if hasLetters {
			words = append(words, string(word[startIndex:]))
		}
	}
}

func getCandidates(i int) map[int]bool {
	candidates, looked := make(map[int]bool, i), make(map[string]bool, i)

	for j := 0; j < i; j++ {
		if _, skip := looked[words[j]]; !skip {
			looked[words[j]] = true

			for k := 0; k < len(words[j]) && k < len(words[i]); k++ {
				if words[i][k] != words[j][k] {
					break
				}

				_, contains := candidates[k]

				candidates[k] = !contains && len(words[i]) >= len(words[j])
			}
		}
	}

	return candidates
}

func tryAutocomplete(i int) int {
	candidates, minLength, maxLength := getCandidates(i), -1, -1

	for length, use := range candidates {
		if use {
			if length > maxLength {
				maxLength = length
			}

			if length < minLength || minLength == -1 {
				minLength = length
			}
		}
	}

	result := len(words[i]) - maxLength + minLength + 1

	if minLength == maxLength || result > len(words[i]) {
		return len(words[i])
	}

	// fmt.Printf(
	// 	"%-3d%-3d%-3d%-3d%-14s%#v\n",
	// 	i,
	// 	minLength+1,
	// 	maxLength+1,
	// 	len(words[i])-result,
	// 	words[i],
	// 	candidates)

	return result
}

package main

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

var words []string
var count int

func main() {
	readWords()

	for i := 0; i < len(words); i++ {
		if len(words[i]) <= 2 {
			count += len(words[i])
		} else {
			count += tryAutocomplete(i)
		}
	}

	fmt.Println(count)
}

func readWords() {
	scanner := bufio.NewScanner(os.Stdin)
	// f, _ := os.Open("input")

	// scanner := bufio.NewScanner(f)

	words = make([]string, 0)

	for scanner.Scan() {
		word, startIndex, hasLetters := make([]byte, 0, len(scanner.Bytes())), 0, false

		count++

		for _, character := range scanner.Bytes() {
			if character >= 'a' && character <= 'z' {
				word, hasLetters = append(word, character), true
			} else {
				if hasLetters {
					words = append(words, string(word[startIndex:]))
				}

				startIndex, hasLetters = len(word), false

				count++
			}
		}

		if hasLetters {
			words = append(words, string(word[startIndex:]))
		}
	}
}

func getCandidates(i int) *map[int]bool {
	candidates, looked := make(map[int]bool, i), make(map[string]bool, i)

	for j := 0; j < i; j++ {
		if _, skip := looked[words[j]]; !skip {
			looked[words[j]] = true

			var k int

			if len(words[j]) > len(words[i]) {
				for k = 0; k < len(words[j]) && k < len(words[i]) && words[i][k] == words[j][k]; k++ {
					candidates[k] = false
				}
			} else {
				for k < len(words[j]) && words[i][k] == words[j][k] {
					k++
				}

				if k == len(words[j]) {
					for k = 0; k < len(words[j]); k++ {
						_, contains := candidates[k]

						candidates[k] = !contains
					}
				} else {
					for k = 0; k < len(words[j]) && words[i][k] == words[j][k]; k++ {
						candidates[k] = false
					}
				}
			}
		}
	}

	return &candidates
}

func tryAutocomplete(i int) int {
	candidates, minLength, maxLength := *getCandidates(i), -1, -1

	for length, use := range candidates {
		if use {
			if length > maxLength {
				maxLength = length
			}

			if length < minLength || minLength == -1 {
				minLength = length
			}
		}
	}

	result := len(words[i]) - maxLength + minLength + 1

	if minLength == maxLength || result > len(words[i]) {
		return len(words[i])
	}

	// fmt.Printf(
	// 	"%-5d%-3d%-3d%-3d%-14s%#v\n",
	// 	i+1,
	// 	minLength+1,
	// 	maxLength+1,
	// 	len(words[i])-result,
	// 	words[i],
	// 	candidates)

	return result
}

package main

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

var words []string
var count int

func main() {
	readWords()

	for i := 0; i < len(words); i++ {
		if len(words[i]) <= 2 {
			count += len(words[i])
		} else {
			count += tryAutocomplete(i)
		}
	}

	fmt.Println(count)
}

func readWords() {
	scanner := bufio.NewScanner(os.Stdin)
	// f, _ := os.Open("input")

	// scanner := bufio.NewScanner(f)

	words = make([]string, 0)

	for scanner.Scan() {
		word, startIndex, hasLetters := make([]byte, 0, len(scanner.Bytes())), 0, false

		count++

		for _, character := range scanner.Bytes() {
			if character >= 'a' && character <= 'z' {
				word, hasLetters = append(word, character), true
			} else {
				if hasLetters {
					words = append(words, string(word[startIndex:]))
				}

				startIndex, hasLetters = len(word), false

				count++
			}
		}

		if hasLetters {
			words = append(words, string(word[startIndex:]))
		}
	}
}

func getCandidates(i int) *map[int]bool {
	candidates, looked := make(map[int]bool, i), make(map[string]bool, i)

	for j := 0; j < i; j++ {
		if _, skip := looked[words[j]]; !skip {
			looked[words[j]] = true

			var k int

			if len(words[j]) > len(words[i]) {
				for k = 0; k < len(words[j]) && k < len(words[i]) && words[i][k] == words[j][k]; k++ {
					candidates[k] = false
				}
			} else {
				for k < len(words[j]) && words[i][k] == words[j][k] {
					k++
				}

				if k == len(words[j]) {
					for k = 0; k < len(words[j]); k++ {
						_, contains := candidates[k]

						candidates[k] = !contains
					}
				} else {
					for k = 0; k < len(words[j]) && words[i][k] == words[j][k]; k++ {
						candidates[k] = false
					}
				}
			}
		}
	}

	return &candidates
}

func tryAutocomplete(i int) int {
	candidates, minLength, maxLength := *getCandidates(i), -1, -1

	for length, use := range candidates {
		if use {
			if length > maxLength {
				maxLength = length
			}

			if length < minLength || minLength == -1 {
				minLength = length
			}
		}
	}

	result := len(words[i]) - maxLength + minLength + 1

	if minLength == maxLength || result > len(words[i]) {
		return len(words[i])
	}

	fmt.Printf(
		"%-5d%-3d%-3d%-3d%-14s%#v\n",
		i+1,
		minLength+1,
		maxLength+1,
		len(words[i])-result,
		words[i],
		candidates)

	return result
}

package main

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

var words []string
var count int

func main() {
	readWords()

	for i := 0; i < len(words); i++ {
		if len(words[i]) <= 2 {
			count += len(words[i])
		} else {
			count += tryAutocomplete(i)
		}
	}

	fmt.Println(count)
}

func readWords() {
	scanner, words := bufio.NewScanner(os.Stdin), make([]string, 0)

	for scanner.Scan() {
		word, startIndex, hasLetters := make([]byte, 0, len(scanner.Bytes())), 0, false

		count++

		for _, character := range scanner.Bytes() {
			if character >= 'a' && character <= 'z' {
				word, hasLetters = append(word, character), true
			} else {
				if hasLetters {
					words = append(words, string(word[startIndex:]))
				}

				startIndex, hasLetters = len(word), false

				count++
			}
		}

		if hasLetters {
			words = append(words, string(word[startIndex:]))
		}
	}
}

func getCandidates(i int) map[int]bool {
	candidates, looked := make(map[int]bool, i), make(map[string]bool, i)

	for j := 0; j < i; j++ {
		if _, skip := looked[words[j]]; !skip {
			looked[words[j]] = true

			for k := 0; k < len(words[j]) && k < len(words[i]); k++ {
				if words[i][k] != words[j][k] {
					break
				}

				_, contains := candidates[k]

				candidates[k] = !contains && len(words[i]) >= len(words[j])
			}
		}
	}

	return candidates
}

func tryAutocomplete(i int) int {
	candidates, minLength, maxLength := getCandidates(i), -1, -1

	for length, use := range candidates {
		if use {
			if length > maxLength {
				maxLength = length
			}

			if length < minLength || minLength == -1 {
				minLength = length
			}
		}
	}

	result := len(words[i]) - maxLength + minLength + 1

	if minLength == maxLength || result > len(words[i]) {
		return len(words[i])
	}

	fmt.Printf(
		"%-3d%-3d%-3d%-3d%-14s%#v\n",
		i,
		minLength+1,
		maxLength+1,
		len(words[i])-result,
		words[i],
		candidates)

	return result
}

package main

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

var words []string
var count int

func main() {
	readWords()

	for i := 0; i < len(words); i++ {
		if len(words[i]) <= 2 {
			count += len(words[i])
		} else {
			count += tryAutocomplete(i)
		}
	}

	fmt.Println(count)
}

func readWords() {
	scanner := bufio.NewScanner(os.Stdin)
	// f, _ := os.Open("input")

	// scanner := bufio.NewScanner(f)

	words = make([]string, 0)

	for scanner.Scan() {
		word, startIndex, hasLetters := make([]byte, 0, len(scanner.Bytes())), 0, false

		count++

		for _, character := range scanner.Bytes() {
			if character >= 'a' && character <= 'z' {
				word, hasLetters = append(word, character), true
			} else {
				if hasLetters {
					words = append(words, string(word[startIndex:]))
				}

				startIndex, hasLetters = len(word), false

				count++
			}
		}

		if hasLetters {
			words = append(words, string(word[startIndex:]))
		}
	}
}

func getCandidates(i int) *map[int]bool {
	candidates, looked := make(map[int]bool, i), make(map[string]bool, i)

	for j := 0; j < i; j++ {
		if _, skip := looked[words[j]]; !skip {
			looked[words[j]] = true

			var k int

			if len(words[j]) > len(words[i]) {
				for k = 0; k < len(words[j]) && k < len(words[i]) && words[i][k] == words[j][k]; k++ {
					candidates[k] = false
				}
			} else {
				for k < len(words[j]) && words[i][k] == words[j][k] {
					k++
				}

				if k == len(words[j]) {
					for k = 0; k < len(words[j]); k++ {
						_, contains := candidates[k]

						candidates[k] = !contains
					}
				} else {
					for k = 0; k < len(words[j]) && words[i][k] == words[j][k]; k++ {
						candidates[k] = false
					}
				}
			}
		}
	}

	return &candidates
}

func tryAutocomplete(i int) int {
	candidates, minLength, maxLength := *getCandidates(i), -1, -1

	for length, use := range candidates {
		if use {
			if length > maxLength {
				maxLength = length
			}

			if length < minLength || minLength == -1 {
				minLength = length
			}
		}
	}

	result := len(words[i]) - maxLength + minLength + 1

	if minLength == maxLength || result > len(words[i]) || result < 2 {
		return len(words[i])
	}

	// fmt.Printf(
	// 	"%-5d%-3d%-3d%-3d%-14s%#v\n",
	// 	i+1,
	// 	minLength+1,
	// 	maxLength+1,
	// 	len(words[i])-result,
	// 	words[i],
	// 	candidates)

	return result
}

package main

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

var words []string
var count int

func main() {
	readWords()

	for i := 0; i < len(words); i++ {
		count += getCount(i)
	}

	fmt.Println(count)
}

func readWords() {
	scanner := bufio.NewScanner(os.Stdin)

	words = make([]string, 0)

	for scanner.Scan() {
		count++

		word := make([]byte, 0, len(scanner.Bytes()))

		startIndex := 0

		hasLetters := false

		for _, character := range scanner.Bytes() {
			if character >= 'a' && character <= 'z' {
				word = append(word, character)

				hasLetters = true
			} else {
				count++

				if hasLetters {
					words = append(words, string(word[startIndex:]))
				}

				startIndex = len(word)

				hasLetters = false
			}
		}

		if hasLetters {
			words = append(words, string(word[startIndex:]))
		}
	}
}

func getCount(i int) int {
	if len(words[i]) <= 2 {
		return len(words[i])
	}

	candidates := make(map[int]bool, i)
	looked := make(map[string]bool, i)

	for j := 0; j < i; j++ {
		if _, skip := looked[words[j]]; skip {
			continue
		}

		looked[words[j]] = true

		for k := 0; k < len(words[j]) && k < len(words[i]); k++ {
			if words[i][k] != words[j][k] {
				break
			}

			if _, contains := candidates[k]; contains {
				candidates[k] = false
			} else {
				candidates[k] = len(words[i]) >= len(words[j])
			}
		}
	}

	minLength := -1

	maxLength := -1

	for length, use := range candidates {
		if use {
			if length > maxLength {
				maxLength = length
			}

			if length < minLength || minLength == -1 {
				minLength = length
			}
		}
	}

	if minLength == maxLength {
		return len(words[i])
	}

	result := len(words[i]) - maxLength + minLength + 1

	if result > len(words[i]) {
		return len(words[i])
	}

	// fmt.Printf(
	// 	"%-2d %-2d %-2d %-2d %-13s %#v\n",
	// 	i,
	// 	minLength+1,
	// 	maxLength+1,
	// 	len(words[i])-result,
	// 	words[i],
	// 	candidates)

	return result
}

package main

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

var words []string
var count int

func main() {
	readWords()

	tmp, a := 0, 0

	for i := 0; i < len(words); i++ {
		if len(words[i]) <= 2 {
			a = len(words[i])

			count += a

			tmp += a / 4
		} else {
			a = tryAutocomplete(i)

			count += a

			tmp += a / 4
		}
	}

	if count == 0 {
		fmt.Println(words)

		fmt.Println(tmp)
	}

	fmt.Println(count)
}

func readWords() {
	scanner := bufio.NewScanner(os.Stdin)
	// f, _ := os.Open("input")

	// scanner := bufio.NewScanner(f)

	words = make([]string, 0)

	for scanner.Scan() {
		word, startIndex, hasLetters := make([]byte, 0, len(scanner.Bytes())), 0, false

		count++

		for _, character := range scanner.Bytes() {
			if character >= 'a' && character <= 'z' {
				word, hasLetters = append(word, character), true
			} else {
				if hasLetters {
					words = append(words, string(word[startIndex:]))
				}

				startIndex, hasLetters = len(word), false

				count++
			}
		}

		if hasLetters {
			words = append(words, string(word[startIndex:]))
		}
	}
}

func getCandidates(i int) *map[int]bool {
	candidates, looked := make(map[int]bool, i), make(map[string]bool, i)

	for j := 0; j < i; j++ {
		if _, skip := looked[words[j]]; !skip {
			looked[words[j]] = true

			var k int

			if len(words[j]) > len(words[i]) {
				for k = 0; k < len(words[j]) && k < len(words[i]) && words[i][k] == words[j][k]; k++ {
					candidates[k] = false
				}
			} else {
				for k < len(words[j]) && words[i][k] == words[j][k] {
					k++
				}

				if k == len(words[j]) {
					for k = 0; k < len(words[j]); k++ {
						_, contains := candidates[k]

						candidates[k] = !contains
					}
				} else {
					for k = 0; k < len(words[j]) && words[i][k] == words[j][k]; k++ {
						candidates[k] = false
					}
				}
			}
		}
	}

	return &candidates
}

func tryAutocomplete(i int) int {
	candidates, minLength, maxLength := *getCandidates(i), -1, -1

	for length, use := range candidates {
		if use {
			if length > maxLength {
				maxLength = length
			}

			if length < minLength || minLength == -1 {
				minLength = length
			}
		}
	}

	result := len(words[i]) - maxLength + minLength + 1

	if minLength == maxLength || result > len(words[i]) || result < 2 {
		return len(words[i])
	}

	// fmt.Printf(
	// 	"%-5d%-3d%-3d%-3d%-14s%#v\n",
	// 	i+1,
	// 	minLength+1,
	// 	maxLength+1,
	// 	len(words[i])-result,
	// 	words[i],
	// 	candidates)

	return result
}

package main

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

var words []string
var count int

func main() {
	readWords()

	for i := 0; i < len(words); i++ {
		if len(words[i]) <= 2 {
			count += len(words[i])
		} else {
			count += tryAutocomplete(i)
		}
	}

	if count == 0 {
		fmt.Println(words)
	}

	fmt.Println(count)
}

func readWords() {
	scanner := bufio.NewScanner(os.Stdin)
	// f, _ := os.Open("input")

	// scanner := bufio.NewScanner(f)

	words = make([]string, 0)

	for scanner.Scan() {
		word, startIndex, hasLetters := make([]byte, 0, len(scanner.Bytes())), 0, false

		count++

		for _, character := range scanner.Bytes() {
			if character >= 'a' && character <= 'z' {
				word, hasLetters = append(word, character), true
			} else {
				if hasLetters {
					words = append(words, string(word[startIndex:]))
				}

				startIndex, hasLetters = len(word), false

				count++
			}
		}

		if hasLetters {
			words = append(words, string(word[startIndex:]))
		}
	}

	if scanner.Err() != nil {
		fmt.Println(scanner.Err())
	}
}

func getCandidates(i int) *map[int]bool {
	candidates, looked := make(map[int]bool, i), make(map[string]bool, i)

	for j := 0; j < i; j++ {
		if _, skip := looked[words[j]]; !skip {
			looked[words[j]] = true

			var k int

			if len(words[j]) > len(words[i]) {
				for k = 0; k < len(words[j]) && k < len(words[i]) && words[i][k] == words[j][k]; k++ {
					candidates[k] = false
				}
			} else {
				for k < len(words[j]) && words[i][k] == words[j][k] {
					k++
				}

				if k == len(words[j]) {
					for k = 0; k < len(words[j]); k++ {
						_, contains := candidates[k]

						candidates[k] = !contains
					}
				} else {
					for k = 0; k < len(words[j]) && words[i][k] == words[j][k]; k++ {
						candidates[k] = false
					}
				}
			}
		}
	}

	return &candidates
}

func tryAutocomplete(i int) int {
	candidates, minLength, maxLength := *getCandidates(i), -1, -1

	for length, use := range candidates {
		if use {
			if length > maxLength {
				maxLength = length
			}

			if length < minLength || minLength == -1 {
				minLength = length
			}
		}
	}

	result := len(words[i]) - maxLength + minLength + 1

	if minLength == maxLength || result > len(words[i]) || result < 2 {
		return len(words[i])
	}

	// fmt.Printf(
	// 	"%-5d%-3d%-3d%-3d%-14s%#v\n",
	// 	i+1,
	// 	minLength+1,
	// 	maxLength+1,
	// 	len(words[i])-result,
	// 	words[i],
	// 	candidates)

	return result
}


The length of the longest common prefix of two strings $$$s = s_1 s_2 \ldots s_n$$$ and $$$t = t_1 t_2 \ldots t_m$$$ is defined as the maximum integer $$$k$$$ ($$$0 \le k \le min(n,m)$$$) such that $$$s_1 s_2 \ldots s_k$$$ equals $$$t_1 t_2 \ldots t_k$$$.Koa the Koala initially has $$$n+1$$$ strings $$$s_1, s_2, \dots, s_{n+1}$$$.For each $$$i$$$ ($$$1 \le i \le n$$$) she calculated $$$a_i$$$ — the length of the longest common prefix of $$$s_i$$$ and $$$s_{i+1}$$$.Several days later Koa found these numbers, but she couldn't remember the strings.So Koa would like to find some strings $$$s_1, s_2, \dots, s_{n+1}$$$ which would have generated numbers $$$a_1, a_2, \dots, a_n$$$. Can you help her?If there are many answers print any. We can show that answer always exists for the given constraints.

Each test contains multiple test cases. The first line contains $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$) — the number of elements in the list $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 50$$$) — the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.

For each test case: Output $$$n+1$$$ lines. In the $$$i$$$-th line print string $$$s_i$$$ ($$$1 \le |s_i| \le 200$$$), consisting of lowercase Latin letters. Length of the longest common prefix of strings $$$s_i$$$ and $$$s_{i+1}$$$ has to be equal to $$$a_i$$$. If there are many answers print any. We can show that answer always exists for the given constraints.

aeren
ari
arousal
around
ari
monogon
monogamy
monthly
kevinvu
kuroni
kurioni
korone
anton
loves
adhoc
problems

NoteIn the $$$1$$$-st test case one of the possible answers is $$$s = [aeren, ari, arousal, around, ari]$$$.Lengths of longest common prefixes are:  Between $$$\color{red}{a}eren$$$ and $$$\color{red}{a}ri$$$ $$$\rightarrow 1$$$  Between $$$\color{red}{ar}i$$$ and $$$\color{red}{ar}ousal$$$ $$$\rightarrow 2$$$  Between $$$\color{red}{arou}sal$$$ and $$$\color{red}{arou}nd$$$ $$$\rightarrow 4$$$  Between $$$\color{red}{ar}ound$$$ and $$$\color{red}{ar}i$$$ $$$\rightarrow 2$$$

package main

import (
	"fmt"
	"strings"
)

// Given length of longest common prefix,
// return valid string
func solve(arr []int) {
	initString := strings.Repeat("a", 200)
	fmt.Println(initString)

	ans := []byte(initString)
	for i := 0; i < len(arr); i++ {
		if ans[arr[i]] == 'a' {
			ans[arr[i]] = 'b'
		} else {
			ans[arr[i]] = 'a'
		}
		fmt.Println(string(ans))
	}
}

func main() {
	var TC int
	fmt.Scan(&TC)
	for TC != 0 {
		var n int
		fmt.Scan(&n)

		var arr []int
		var a int
		for i := 0; i < n; i++ {
			fmt.Scan(&a)
			arr = append(arr, a)
		}
		solve(arr)

		TC--
	}
}

// https://codeforces.com/contest/1374/problem/A
package main

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

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

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

func prints(s []uint16) {
	for _, v := range s {
		printf("%c", v)
	}
	printf("\n")
}

func main() {
	defer writer.Flush()

	var t int
	scanf("%d\n", &t)

	for ti := 0; ti < t; ti++ {
		// Read input
		var n int
		scanf("%d\n", &n)

		ml := 1

		a := make([]int, n)
		for i := 0; i < n; i++ {
			scanf("%d", &a[i])
			if ml < a[i] {
				ml = a[i]
			}
		}
		scanf("\n")

		s := make([]uint16, ml)
		for i := 0; i < ml; i++ {
			s[i] = uint16('a')
		}
		prints(s)

		// Continue
		for i := 0; i < n; i++ {
			if a[i] < ml {
				if s[a[i]] == 'a' {
					s[a[i]] = uint16('b')
				} else {
					s[a[i]] = uint16('a')
				}
			}
			prints(s)
		}
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := 0
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		s := make([]byte, 51)
		for i := range s {
			s[i] = 'v'
		}
		fmt.Fprintln(out, string(s))
		n := 0
		fmt.Fscan(in, &n)
		for ; n > 0; n-- {
			a := 0
			fmt.Fscan(in, &a)
			if s[a] == 'p' {
				s[a] = 'v'
			} else {
				s[a] = 'p'
			}
			fmt.Fprintln(out, string(s))
		}
	}
}

package main

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

const (
	SCANNER_BUF_SIZE = 2 << 20

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

var (
	writer  *bufio.Writer
	scanner *bufio.Scanner
)

func init() {
	writer = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, SCANNER_BUF_SIZE), SCANNER_BUF_SIZE)
}

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

func sint64() int64 {
	scanner.Scan()
	bb := scanner.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 suint64() uint64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func sstring() string {
	scanner.Scan()
	return scanner.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
}

func main() {
	defer writer.Flush()
	t := sint64()
	for ; t > 0; t-- {
		n := sint64()
		a := make([]int64, n)
		for i := range a {
			a[i] = sint64()
		}
		res := sol(a)
		for _, s := range res {
			printf("%s\n", s)
		}
	}
}

func sol(a []int64) []string {
	ret := []string{}
	tmp := make([]byte, 51)
	for i := range tmp {
		tmp[i] = 'a'
	}
	ret = append(ret, string(tmp))
	for _, i := range a {
		tmp[i]++
		if tmp[i] > 'z' {
			tmp[i] -= 26
		}
		ret = append(ret, string(tmp))
	}
	return ret
}

package main

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

// Given length of longest common prefix,
// return valid string
func solve(arr []int) {
	initString := strings.Repeat("a", 200)
	fmt.Println(initString)

	ans := []byte(initString)
	for i := 0; i < len(arr); i++ {
		if ans[arr[i]] == 'a' {
			ans[arr[i]] = 'b'
		} else {
			ans[arr[i]] = 'a'
		}
		fmt.Println(string(ans))
	}
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	var TC int
	fmt.Scanf("%d\n", &TC)
	for TC != 0 {
		var n int
		fmt.Scanf("%d\n", &n)

		var arr []int
		str, _ := reader.ReadString('\n')
		str = strings.TrimSpace(str)
		strArr := strings.Split(str, " ")

		for i := 0; i < len(strArr); i++ {
			a, _ := strconv.Atoi(string(strArr[i]))
			arr = append(arr, a)
		}
		solve(arr)

		TC--
	}
}

package main

import (
	"fmt"
	"strings"
)

// Given length of longest common prefix,
// return valid string
func solve(arr []int) {
	initString := strings.Repeat("a", 200)
	fmt.Println(initString)
	prevString := initString
	for i := 0; i < len(arr); i++ {
		ans := []byte(prevString)
		if ans[arr[i]] == 'a' {
			ans[arr[i]] = 'b'
		} else {
			ans[arr[i]] = 'a'
		}
		fmt.Println(string(ans))
		prevString = string(ans)
	}
}

func main() {
	var TC int
	fmt.Scanf("%d\n", &TC)
	for TC != 0 {
		var n int
		fmt.Scanf("%d\n", &n)

		var arr []int
		var a int
		for i := 0; i < n; i++ {
			fmt.Scanf("%d", &a)
			arr = append(arr, a)
		}
		solve(arr)
		TC--
	}
}

package main

import (
	"fmt"
	"strings"
)

// Given length of longest common prefix,
// return valid string
func solve(arr []int) {
	initString := strings.Repeat("a", 200)
	fmt.Println(initString)

	ans := []byte(initString)
	for i := 0; i < len(arr); i++ {
		if ans[arr[i]] == 'a' {
			ans[arr[i]] = 'b'
		} else {
			ans[arr[i]] = 'a'
		}
		fmt.Println(string(ans))
	}
}

func main() {
	var TC int
	fmt.Scanf("%d\n", &TC)
	for TC != 0 {
		var n int
		fmt.Scanf("%d\n", &n)

		var arr []int
		var a int
		for i := 0; i < n; i++ {
			fmt.Scanf("%d", &a)
			arr = append(arr, a)
		}
		solve(arr)
		TC--
	}
}

package main

import (
	"fmt"
	"strings"
)

// Given length of longest common prefix,
// return valid string
func solve(arr []int) {
	initString := strings.Repeat("a", 50)
	fmt.Println(initString)
	prevString := initString
	for i := 0; i < len(arr); i++ {
		ans := []byte(prevString)
		if ans[arr[i]] == 'a' {
			ans[arr[i]] = 'b'
		} else {
			ans[arr[i]] = 'a'
		}
		prevString = string(ans)
		fmt.Println(prevString)
	}
}

func main() {
	var TC int
	fmt.Scanf("%d\n", &TC)
	for TC != 0 {
		var n int
		fmt.Scanf("%d\n", &n)

		var arr []int
		var a int
		for i := 0; i < n; i++ {
			if i == (n - 1) {
				fmt.Scanf("%d\n", &a)
			} else {
				fmt.Scanf("%d ", &a)
			}
			arr = append(arr, a)
		}
		solve(arr)
		TC--
	}
}

package main

import (
	"fmt"
	"strings"
)

// Given length of longest common prefix,
// return valid string
func solve(arr []int) {
	initString := strings.Repeat("a", 200)
	fmt.Println(initString)
	prevString := initString
	for i := 0; i < len(arr); i++ {
		ans := []byte(prevString)
		if ans[arr[i]] == 'a' {
			ans[arr[i]] = 'b'
		} else {
			ans[arr[i]] = 'a'
		}
		prevString = string(ans)
		fmt.Println(prevString)
	}
}

func main() {
	var TC int
	fmt.Scanf("%d\n", &TC)
	for TC != 0 {
		var n int
		fmt.Scanf("%d\n", &n)

		var arr []int
		var a int
		for i := 0; i < n; i++ {
			fmt.Scanf("%d", &a)
			arr = append(arr, a)
		}
		solve(arr)
		TC--
	}
}

package main

import (
	"fmt"
	"strings"
)

// Given length of longest common prefix,
// return valid string
func solve(arr []int) {
	initString := strings.Repeat("a", 50)
	fmt.Println(initString)
	prevString := initString
	for i := 0; i < len(arr); i++ {
		ans := []byte(prevString)
		if ans[arr[i]] == 'a' {
			ans[arr[i]] = 'b'
		} else {
			ans[arr[i]] = 'a'
		}
		prevString = string(ans)
		fmt.Println(prevString)
	}
}

func main() {
	var TC int
	fmt.Scanf("%d\n", &TC)
	for TC != 0 {
		var n int
		fmt.Scanf("%d\n", &n)

		var arr []int
		var a int
		for i := 0; i < n; i++ {
			fmt.Scanf("%d", &a)
			arr = append(arr, a)
		}
		solve(arr)
		TC--
	}
}

package main

import (
	"fmt"
	"strings"
)

// Given length of longest common prefix,
// return valid string
func solve(arr []int) {
	initString := strings.Repeat("a", 200)
	fmt.Println(initString)
	prevString := initString
	for i := 0; i < len(arr); i++ {
		ans := []rune(prevString)
		if ans[arr[i]] == 'a' {
			ans[arr[i]] = 'b'
		} else {
			ans[arr[i]] = 'a'
		}
		fmt.Println(string(ans))
		prevString = string(ans)
	}
}

func main() {
	var TC int
	fmt.Scanf("%d\n", &TC)
	for TC != 0 {
		var n int
		fmt.Scanf("%d\n", &n)

		var arr []int
		var a int
		for i := 0; i < n; i++ {
			fmt.Scanf("%d", &a)
			arr = append(arr, a)
		}
		solve(arr)
		TC--
	}
}

// https://codeforces.com/contest/1374/problem/A
package main

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

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

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

func main() {
	defer writer.Flush()

	var t int
	scanf("%d\n", &t)

	for ti := 0; ti < t; ti++ {
		// Read input
		var n int
		scanf("%d\n", &n)

		var a int
		var s1, s2 string

		s1 = "a"
		c := 'a'

		// Continue
		for i := 0; i < n; i++ {
			scanf("%d", &a)

			len1 := len(s1)

			s2 = ""

			if a == 0 {
				c++
				s2 += string(c)
			} else if a < len1 {
				for j := 0; j < a; j++ {
					s2 += string(s1[j])
				}
			} else {
				c++
				s2 = s1
				for j := 0; j < a-len1; j++ {
					s2 += string(c)
					s1 += string(c)
				}
			}
			printf("%s\n", s1)

			s1, s2 = s2, s1
		}
		printf("%s\n", s1)

		scanf("\n")
	}
}

// https://codeforces.com/contest/1374/problem/A
package main

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

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

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

func main() {
	defer writer.Flush()

	var t int
	scanf("%d\n", &t)

	for ti := 0; ti < t; ti++ {
		// Read input
		var n int
		scanf("%d\n", &n)

		var a int
		var s1, s2 string

		s1 = "a"
		c := 'a'

		// Continue
		for i := 0; i < n; i++ {
			scanf("%d", &a)

			len1 := len(s1)

			s2 = ""
			c++

			if a == 0 {
				s2 += string(c)
			} else if a < len1 {
				for j := 0; j < a; j++ {
					s2 += string(s1[j])
				}
			} else {
				s2 = s1
				for j := 0; j < a-len1; j++ {
					s2 += string(c)
					s1 += string(c)
				}
			}
			printf("%s\n", s1)

			s1, s2 = s2, s1
		}
		printf("%s\n", s1)

		scanf("\n")
	}
}

// https://codeforces.com/contest/1374/problem/A
package main

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

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

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

func main() {
	defer writer.Flush()

	var t int
	scanf("%d\n", &t)

	cs := []rune{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k'}

	for ti := 0; ti < t; ti++ {
		// Read input
		var n int
		scanf("%d\n", &n)

		var a int
		var s1, s2 string

		s1 = "a"
		c := 0
		m := len(cs)

		// Continue
		for i := 0; i < n; i++ {
			scanf("%d", &a)

			len1 := len(s1)

			s2 = ""

			if a == 0 {
				c++
				s2 += string(cs[c%m])
			} else if a < len1 {
				for j := 0; j < a; j++ {
					s2 += string(s1[j])
				}
			} else {
				c++
				s2 = s1
				for j := 0; j < a-len1; j++ {
					s2 += string(cs[c%m])
					s1 += string(cs[c%m])
				}
			}
			printf("%s\n", s1)

			s1, s2 = s2, s1
		}
		printf("%s\n", s1)

		scanf("\n")
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := 0
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		s := make([]byte, 50)
		for i := range s {
			s[i] = '0'
		}
		fmt.Fprintln(out, string(s))
		n := 0
		fmt.Fscan(in, &n)
		for ; n > 0; n-- {
			a := 0
			fmt.Fscan(in, &a)
			if s[a] == '1' {
				s[a] = '0'
			} else {
				s[a] = '1'
			}
			fmt.Fprintln(out, string(s))
		}
	}

}

package main

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

const (
	SCANNER_BUF_SIZE = 2 << 20

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

var (
	writer  *bufio.Writer
	scanner *bufio.Scanner
)

func init() {
	writer = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, SCANNER_BUF_SIZE), SCANNER_BUF_SIZE)
}

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

func sint64() int64 {
	scanner.Scan()
	bb := scanner.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 suint64() uint64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func sstring() string {
	scanner.Scan()
	return scanner.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
}

func main() {
	defer writer.Flush()
	t := sint64()
	for ; t > 0; t-- {
		n := sint64()
		a := make([]int64, n)
		for i := range a {
			a[i] = sint64()
		}
		res := sol(a)
		for _, s := range res {
			printf("%s\n", s)
		}
		printf("====\n")
	}
}

func sol(a []int64) []string {
	ret := []string{}
	tmp := make([]byte, 51)
	for i := range tmp {
		tmp[i] = 'a'
	}
	ret = append(ret, string(tmp))
	for _, i := range a {
		tmp[i]++
		if tmp[i] > 'z' {
			tmp[i] -= 26
		}
		ret = append(ret, string(tmp))
	}
	return ret
}


A non-empty string is called palindrome, if it reads the same from the left to the right and from the right to the left. For example, "abcba", "a", and "abba" are palindromes, while "abab" and "xy" are not.A string is called a substring of another string, if it can be obtained from that string by dropping some (possibly zero) number of characters from the beginning and from the end of it. For example, "abc", "ab", and "c" are substrings of the string "abc", while "ac" and "d" are not.Let's define a palindromic count of the string as the number of its substrings that are palindromes. For example, the palindromic count of the string "aaa" is $$$6$$$ because all its substrings are palindromes, and the palindromic count of the string "abc" is $$$3$$$ because only its substrings of length $$$1$$$ are palindromes.You are given a string $$$s$$$. You can arbitrarily rearrange its characters. You goal is to obtain a string with the maximum possible value of palindromic count.

The first line contains an integer $$$n$$$ ($$$1 \le n \le 100\,000$$$) — the length of string $$$s$$$. The second line contains string $$$s$$$ that consists of exactly $$$n$$$ lowercase characters of Latin alphabet.

Print string $$$t$$$, which consists of the same set of characters (and each characters appears exactly the same number of times) as string $$$s$$$. Moreover, $$$t$$$ should have the maximum possible value of palindromic count among all such strings strings. If there are multiple such strings, print any of them.

NoteIn the first example, string "ololo" has $$$9$$$ palindromic substrings: "o", "l", "o", "l", "o", "olo", "lol", "olo", "ololo". Note, that even though some substrings coincide, they are counted as many times as they appear in the resulting string.In the second example, the palindromic count of string "abccbaghghghgdfd" is $$$29$$$.

package main

import (
	"fmt"
)

func main() {
	n, s := 0, ""
	fmt.Scan(&n, &s)
	c, b := map[byte]int{}, make([]byte, n)
	for i := 0; i < n; i++ {
		c[s[i]]++
	}

	pointer := 0
	for k, v := range c {
		for i := 0; i < v; i++ {
			b[i+pointer] = k
		}
		pointer += v
	}
	fmt.Println(string(b))
}


The ship crashed into a reef and is sinking. Now the entire crew must be evacuated. All n crew members have already lined up in a row (for convenience let's label them all from left to right with positive integers from 1 to n) and await further instructions. However, one should evacuate the crew properly, in a strict order. Specifically:The first crew members to leave the ship are rats. Then women and children (both groups have the same priority) leave the ship. After that all men are evacuated from the ship. The captain leaves the sinking ship last.If we cannot determine exactly who should leave the ship first for any two members of the crew by the rules from the previous paragraph, then the one who stands to the left in the line leaves the ship first (or in other words, the one whose number in the line is less).For each crew member we know his status as a crew member, and also his name. All crew members have different names. Determine the order in which to evacuate the crew.

The first line contains an integer n, which is the number of people in the crew (1 ≤ n ≤ 100). Then follow n lines. The i-th of those lines contains two words — the name of the crew member who is i-th in line, and his status on the ship. The words are separated by exactly one space. There are no other spaces in the line. The names consist of Latin letters, the first letter is uppercase, the rest are lowercase. The length of any name is from 1 to 10 characters. The status can have the following values: rat for a rat, woman for a woman, child for a child, man for a man, captain for the captain. The crew contains exactly one captain.

Print n lines. The i-th of them should contain the name of the crew member who must be the i-th one to leave the ship.

6
Jack captain
Alice woman
Charlie man
Teddy rat
Bob child
Julia woman

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

func main() {
	var s [2][100]string
	var n int
	fmt.Scan(&n)
	for i:=0;i!=n;i++ {
		fmt.Scan(&s[0][i],&s[1][i])
	}
	for i:=0;i!=n;i++ {
		if s[1][i]=="rat" {
			fmt.Println(s[0][i])
		}
	}
	for i:=0;i!=n;i++ {
		if s[1][i]=="woman" || s[1][i]=="child" {
			fmt.Println(s[0][i])
		}
	}
	for i:=0;i!=n;i++ {
		if s[1][i]=="man" {
			fmt.Println(s[0][i])
		}
	}
	for i:=0;i!=n;i++ {
		if s[1][i]=="captain" {
			fmt.Println(s[0][i])
		}
	}
}

package main

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

func main() {
	var scanner *bufio.Scanner
	var data map[string][]string
	var tmp []string
	var n int
	var e error

	scanner = bufio.NewScanner(os.Stdin)
	data = make(map[string][]string, 1)

	scanner.Scan()

	n, e = strconv.Atoi(strings.TrimSpace(scanner.Text()))
	if e != nil {
		panic(e)
	}

	for i := 0; i < n; i++ {
		scanner.Scan()
		tmp = strings.SplitN(strings.TrimSpace(scanner.Text()), " ", 2)
		tmp[0] = strings.TrimSpace(tmp[0])
		tmp[1] = strings.TrimSpace(tmp[1])
		if tmp[1] == "child" {
			data["woman"] = append(data["woman"], tmp[0])
		} else {
			data[tmp[1]] = append(data[tmp[1]], tmp[0])
		}
	}

	// rat
	for i := 0; i < len(data["rat"]); i++ {
		fmt.Println(data["rat"][i])
	}
	// woman & child
	for i := 0; i < len(data["woman"]); i++ {
		fmt.Println(data["woman"][i])
	}
	// all mens
	for i := 0; i < len(data["man"]); i++ {
		fmt.Println(data["man"][i])
	}
	// captain
	for i := 0; i < len(data["captain"]); i++ {
		fmt.Println(data["captain"][i])
	}
}

package main

import "fmt"

func main() {
    var n int
    var name, status string
    fmt.Scan(&n)
    r, wac, m, c := make([]string,0,0), make([]string,0,0), make([]string,0,0), ""
    for i := 0; i < n; i++ {
        fmt.Scan(&name,&status)
        if status == "rat" {
            r = append(r,name)
        } else if (status == "woman") || (status == "child") {
            wac = append(wac,name)
        } else if status == "man" {
            m = append(m,name)
        } else {
            c = name
        }
    }
    for _, v := range r { fmt.Println(v) }
    for _, v := range wac { fmt.Println(v) }
    for _, v := range m { fmt.Println(v) }
    fmt.Println(c)
}

package main

import "fmt"

func main() {
	var (
		count int
	)

	fmt.Scanf("%d\n", &count)

	womenAndChildren := make([]string, 0)
	men := make([]string, 0)
	rats := make([]string, 0)
	var captain string
	for i := 0; i < count; i++ {

		var name, status string
		fmt.Scanf("%s %s\n", &name, &status)

		switch status {
		case "rat":
			rats = append(rats, copyString(name))
		case "woman":
			womenAndChildren = append(womenAndChildren, copyString(name))
		case "child":
			womenAndChildren = append(womenAndChildren, copyString(name))
		case "man":
			men = append(men, copyString(name))
		case "captain":
			captain = copyString(name)
		}
	}

	for _, name := range rats {
		fmt.Println(name)
	}

	for _, name := range womenAndChildren {
		fmt.Println(name)
	}

	for _, name := range men {
		fmt.Println(name)
	}

	fmt.Println(captain)
}

func copyString(str string) string {

	dst := make([]byte, len(str))
	copy(dst, str)
	return string(dst)
}

// 63A-mic
package main

import (
    "fmt"
)

func main() {
    var nama, tipe string
    var n, a, b, c, d int
    var rat, wac, men, capt [100]string
    fmt.Scan(&n)

    for i := 0; i < n; i++ {
        fmt.Scan(&nama, &tipe)
        if tipe == "captain" {
            capt[a] = nama
            a++
        } else if tipe == "rat" {
            rat[b] = nama
            b++
        } else if tipe == "woman" || tipe == "child" {
            wac[c] = nama
            c++
        } else {
            men[d] = nama
            d++
        }
    }
    for i := 0; i < b; i++ {
        fmt.Println(rat[i])
    }
    for i := 0; i < c; i++ {
        fmt.Println(wac[i])
    }
    for i := 0; i < d; i++ {
        fmt.Println(men[i])
    }
    fmt.Println(capt[0])
}

package main

import "fmt"

func main() {
	var (
		count int
	)

	fmt.Scanf("%d", &count)
	fmt.Println(count)
	womenAndChildren := make([]string, 0)
	men := make([]string, 0)
	//var captain string
	for range make([]int, count) {

		var name, status string
		fmt.Scanf("%s %s", &name, &status)

		fmt.Println(name, status)

		switch status {
		case "rat":
			//fmt.Println(name)
		case "woman":
			fallthrough
		case "child":
			womenAndChildren = append(womenAndChildren, name)
		case "man":
			men = append(men, name)
		case "captain":
			//captain = name
		}
	}

	/*for _, name := range womenAndChildren {
		fmt.Println(name)
	}

	for _, name := range men {
		fmt.Println(name)
	}

	fmt.Println(captain)*/
}

package main

import "fmt"

func main() {
	var (
		count int
	)

	fmt.Scanf("%d", &count)

	womenAndChildren := make([]string, 0)
	men := make([]string, 0)
	var captain string
	for range make([]int, count) {

		var name, status string
		fmt.Scanf("%s %s", &name, &status)

		switch status {
		case "rat":
			fmt.Println(name)
		case "woman":
			fallthrough
		case "child":
			womenAndChildren = append(womenAndChildren, name)
		case "man":
			men = append(men, name)
		case "captain":
			captain = name
		}
	}

	for _, name := range womenAndChildren {
		fmt.Println(name)
	}

	for _, name := range men {
		fmt.Println(name)
	}

	fmt.Println(captain)
}

package main

import "fmt"

func main() {
	var (
		count int
	)

	fmt.Scanf("%d", &count)
	fmt.Println(count)
	womenAndChildren := make([]string, 0)
	men := make([]string, 0)
	//var captain string
	for range make([]int, count, count) {

		var name, status string
		fmt.Scanf("%s %s", &name, &status)

		fmt.Println(name, status)

		switch status {
		case "rat":
			//fmt.Println(name)
		case "woman":
			fallthrough
		case "child":
			womenAndChildren = append(womenAndChildren, name)
		case "man":
			men = append(men, name)
		case "captain":
			//captain = name
		}
	}

	/*for _, name := range womenAndChildren {
		fmt.Println(name)
	}

	for _, name := range men {
		fmt.Println(name)
	}

	fmt.Println(captain)*/
}

package main

import "fmt"

func main() {
	var (
		count int
	)

	fmt.Scanf("%d", &count)

	womenAndChildren := make([]string, 0)
	men := make([]string, 0)
	var captain string
	for i := 0; i < count; i++ {

		var name, status string
		fmt.Scanf("%s %s", &name, &status)

		switch status {
		case "rat":
			fmt.Println(name)
		case "woman":
			fallthrough
		case "child":
			womenAndChildren = append(womenAndChildren, name)
		case "man":
			men = append(men, name)
		case "captain":
			captain = name
		}
	}

	for _, name := range womenAndChildren {
		fmt.Println(name)
	}

	for _, name := range men {
		fmt.Println(name)
	}

	fmt.Println(captain)
}

package main

import "fmt"

func main() {
	var (
		count int
	)

	fmt.Scanf("%d", &count)

	womenAndChildren := make([]string, 0)
	men := make([]string, 0)
	rats := make([]string, 0)
	var captain string
	for i := 0; i < count; i++ {

		var name, status string
		fmt.Scanf("%s %s\n", &name, &status)

		switch status {
		case "rat":
			rats = append(rats, copyString(name))
		case "woman":
			fallthrough
		case "child":
			womenAndChildren = append(womenAndChildren, copyString(name))
		case "man":
			men = append(men, copyString(name))
		case "captain":
			captain = copyString(name)
		}
	}

	for _, name := range rats {
		fmt.Println(name)
	}

	for _, name := range womenAndChildren {
		fmt.Println(name)
	}

	for _, name := range men {
		fmt.Println(name)
	}

	fmt.Println(captain)
}

func copyString(str string) string {

	dst := make([]byte, len(str))
	copy(dst, str)
	return string(dst)
}

package main

import "fmt"

func main() {
	var (
		count int
	)

	fmt.Scanf("%d", &count)

	womenAndChildren := make([]string, 0)
	men := make([]string, 0)
	rats := make([]string, 0)
	var captain string
	for i := 0; i < count; i++ {

		var name, status string
		fmt.Scanf("%s %s\n", &name, &status)

		switch status {
		case "rat":
			rats = append(rats, name)
		case "woman":
			fallthrough
		case "child":
			womenAndChildren = append(womenAndChildren, name)
		case "man":
			men = append(men, name)
		case "captain":
			captain = name
		}
	}

	for _, name := range rats {
		fmt.Println(name)
	}

	for _, name := range womenAndChildren {
		fmt.Println(name)
	}

	for _, name := range men {
		fmt.Println(name)
	}

	fmt.Println(captain)
}

package main

import "fmt"

func main() {
	var (
		count int
	)

	fmt.Scanf("%d", &count)

	womenAndChildren := make([]string, 0)
	men := make([]string, 0)
	rats := make([]string, 0)
	var captain string
	for i := 0; i < count; i++ {

		var name, status string
		fmt.Scanf("%s %s\n", &name, &status)

		switch status {
		case "rat":
			rats = append(rats, copyString(name))
		case "woman":
			womenAndChildren = append(womenAndChildren, copyString(name))
		case "child":
			womenAndChildren = append(womenAndChildren, copyString(name))
		case "man":
			men = append(men, copyString(name))
		case "captain":
			captain = copyString(name)
		}
	}

	for _, name := range rats {
		fmt.Println(name)
	}

	for _, name := range womenAndChildren {
		fmt.Println(name)
	}

	for _, name := range men {
		fmt.Println(name)
	}

	fmt.Println(captain)
}

func copyString(str string) string {

	dst := make([]byte, len(str))
	copy(dst, str)
	return string(dst)
}


Lena is a beautiful girl who likes logical puzzles.As a gift for her birthday, Lena got a matrix puzzle!The matrix consists of $$$n$$$ rows and $$$m$$$ columns, and each cell is either black or white. The coordinates $$$(i,j)$$$ denote the cell which belongs to the $$$i$$$-th row and $$$j$$$-th column for every $$$1\leq i \leq n$$$ and $$$1\leq j \leq m$$$. To solve the puzzle, Lena has to choose a cell that minimizes the Manhattan distance to the farthest black cell from the chosen cell.More formally, let there be $$$k \ge 1$$$ black cells in the matrix with coordinates $$$(x_i,y_i)$$$ for every $$$1\leq i \leq k$$$. Lena should choose a cell $$$(a,b)$$$ that minimizes $$$$$$\max_{i=1}^{k}(|a-x_i|+|b-y_i|).$$$$$$As Lena has no skill, she asked you for help. Will you tell her the optimal cell to choose?

There are several test cases in the input data. The first line contains a single integer $$$t$$$ ($$$1\leq t\leq 10\,000$$$) — the number of test cases. This is followed by the test cases description. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2\leq n,m \leq 1000$$$)  — the dimensions of the matrix.  The following $$$n$$$ lines contain $$$m$$$ characters each, each character is either 'W' or 'B'. The $$$j$$$-th character in the $$$i$$$-th of these lines is 'W' if the cell $$$(i,j)$$$ is white, and 'B' if the cell $$$(i,j)$$$ is black.  It is guaranteed that at least one black cell exists. It is guaranteed that the sum of $$$n\cdot m$$$ does not exceed $$$10^6$$$.

For each test case, output the optimal cell $$$(a,b)$$$ to choose. If multiple answers exist, output any.

5
3 2
BW
WW
WB
3 3
WWB
WBW
BWW
2 3
BBB
BBB
5 5
BWBWB
WBWBW
BWBWB
WBWBW
BWBWB
9 9
WWWWWWWWW
WWWWWWWWW
BWWWWWWWW
WWWWWWWWW
WWWWBWWWW
WWWWWWWWW
WWWWWWWWW
WWWWWWWWW
WWWWWWWWB

NoteIn the first test case the two black cells have coordinates $$$(1,1)$$$ and $$$(3,2)$$$. The four optimal cells are $$$(1,2)$$$, $$$(2,1)$$$, $$$(2,2)$$$ and $$$(3,1)$$$. It can be shown that no other cell minimizes the maximum Manhattan distance to every black cell.In the second test case it is optimal to choose the black cell $$$(2,2)$$$ with maximum Manhattan distance being $$$2$$$.

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)
var wrtr = bufio.NewWriterSize(os.Stdout, 10_000_000)
var rdr = bufio.NewScanner(os.Stdin)
func gs() string  { rdr.Scan(); return rdr.Text() }
func gi() int     { i,e := strconv.Atoi(gs()); if e != nil {panic(e)}; return i }
func gi2() (int,int) { return gi(),gi() }
func gi3() (int,int,int) { return gi(),gi(),gi() }
func gi4() (int,int,int,int) { return gi(),gi(),gi(),gi() }
func gis(n int) []int  { res := make([]int,n); for i:=0;i<n;i++ { res[i] = gi() }; return res }
func gf() float64 { f,e := strconv.ParseFloat(gs(),64); if e != nil {panic(e)}; return f }
func gbs() []byte { return []byte(gs()) }
func gfs(n int) []float64  { res := make([]float64,n); for i:=0;i<n;i++ { res[i] = gf() }; return res }
func gss(n int) []string  { res := make([]string,n); for i:=0;i<n;i++ { res[i] = gs() }; return res }
func ia(m int) []int { return make([]int,m) }
func iai(m int,v int) []int { a := make([]int,m); for i:=0;i<m;i++ { a[i] = v }; return a }
func twodi(n int,m int,v int) [][]int {
	r := make([][]int,n); for i:=0;i<n;i++ { x := make([]int,m); for j:=0;j<m;j++ { x[j] = v }; r[i] = x }; return r
}
func fill2(m int) ([]int,[]int) { a,b := ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i] = gi(),gi()}; return a,b }
func fill3(m int) ([]int,[]int,[]int) { a,b,c := ia(m),ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i],c[i] = gi(),gi(),gi()}; return a,b,c }
func fill4(m int) ([]int,[]int,[]int,[]int) { a,b,c,d := ia(m),ia(m),ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i],c[i],d[i] = gi(),gi(),gi(),gi()}; return a,b,c,d }
func abs(a int) int { if a < 0 { return -a }; return a }
func rev(a []int) { i,j := 0,len(a)-1; for i < j { a[i],a[j] = a[j],a[i]; i++; j-- } }
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 tern(cond bool, a int, b int) int { if cond { return a }; return b }
func terns(cond bool, a string, b string) string { if cond { return a }; return b }
func maxarr(a []int) int { ans := a[0]; for _,aa := range(a) { if aa > ans { ans = aa } }; return ans }
func minarr(a []int) int { ans := a[0]; for _,aa := range(a) { if aa < ans { ans = aa } }; return ans }
func sumarr(a []int) int { ans := 0; for _,aa := range(a) { ans += aa }; return ans }
func zeroarr(a []int) { for i:=0; i<len(a); i++ { a[i] = 0 } }
func powmod(a,e,mod int) int { res, m := 1, a; for e > 0 { if e&1 != 0 { res = res * m % mod }; m = m * m % mod; e >>= 1 }; return res }
func powint(a,e int) int { res, m := 1, a; for e > 0 { if e&1 != 0 { res = res * m }; m = m * m; e >>= 1 }; return res }
func gcd(a,b int) int { for b != 0 { t:=b; b=a%b; a=t }; return a }
func gcdExtended(a,b int) (int,int,int) { if a == 0 { return b,0,1 }; gcd,x1,y1 := gcdExtended(b%a,a); return gcd, y1-(b/a)*x1,x1 }
func modinv(a,m int) (int,bool) { g,x,_ := gcdExtended(a,m); if g != 1 { return 0,false }; return (x % m + m) % m,true  }
func makefact(n int,mod int) ([]int,[]int) {
	fact,factinv := make([]int,n+1),make([]int,n+1)
	fact[0] = 1; for i:=1;i<=n;i++ { fact[i] = fact[i-1] * i % mod }
	factinv[n] = powmod(fact[n],mod-2,mod); for i:=n-1;i>=0;i-- { factinv[i] = factinv[i+1] * (i+1) % mod }
	return fact,factinv
}
func vecintstring(a []int) string { astr := make([]string,len(a)); for i,a := range a { astr[i] = strconv.Itoa(a) }; return strings.Join(astr," ") }
const inf = 1000000000
func main() {
	//f1, _ := os.Create("cpu.prof"); pprof.StartCPUProfile(f1); defer pprof.StopCPUProfile()
	defer wrtr.Flush()
	infn := ""; if infn == "" && len(os.Args) > 1 {	infn = os.Args[1] }
	if infn != "" {	f, e := os.Open(infn); if e != nil { panic(e) }; rdr = bufio.NewScanner(f) }
	rdr.Split(bufio.ScanWords); rdr.Buffer(make([]byte,1024),1_000_000_000)
	// PROGRAM STARTS HERE
	T := gi()
	for tt:=1;tt<=T;tt++ {
		N,M := gi(),gi(); bd := make([]string,N); for i:=0;i<N;i++ { bd[i] = gs() }
		maxd1,mind1,maxd2,mind2 := -inf,inf,-inf,inf
		maxdist,ii,jj := inf,-1,-1
		for i:=0;i<N;i++ {
			for j:=0;j<M;j++ {
				if bd[i][j] == 'B' { maxd1 = max(maxd1,i+j); mind1 = min(mind1,i+j); maxd2 = max(maxd2,i-j); mind2 = min(mind2,i-j) }
			}
		}
		for i:=0;i<N;i++ {
			for j:=0;j<M;j++ {
				d := 0; d = max(d,abs(i+j-maxd1)); d = max(d,abs(i+j-mind1)); d = max(d,abs(i-j-maxd2)); d = max(d,abs(i-j-mind2))
				if d < maxdist { maxdist,ii,jj = d,i,j}
			}
		}
		fmt.Fprintf(wrtr,"%v %v\n",ii+1,jj+1)
	}
}

package main

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

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

func abs(n int) int {
	if n < 0 {
		return n * -1
	}
	return n
}

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, m int
		fmt.Fscan(in, &n, &m)

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

		a, b, c, d := []int{-1, -1}, []int{-1, -1}, []int{-1, -1}, []int{-1, -1}
		for i := 0; i < n; i++ {
			for j := 0; j < m; j++ {
				if mat[i][j] == 'B' {
					if a[0] < 0 || i+j > a[0]+a[1] {
						a = []int{i, j}
					}
					if b[0] < 0 || i+j < b[0]+b[1] {
						b = []int{i, j}
					}
					if c[0] < 0 || i-j > c[0]-c[1] {
						c = []int{i, j}
					}
					if d[0] < 0 || i-j < d[0]-d[1] {
						d = []int{i, j}
					}
				}
			}
		}

		ans := math.MaxInt32
		var x, y int
		for i := 0; i < n; i++ {
			for j := 0; j < m; j++ {
				d := max(abs(a[0]-i)+abs(a[1]-j),
					abs(b[0]-i)+abs(b[1]-j),
					abs(c[0]-i)+abs(c[1]-j),
					abs(d[0]-i)+abs(d[1]-j))
				if d < ans {
					ans = d
					x, y = i, j
				}
			}
		}
		fmt.Fprintln(out, x+1, y+1)
	}
}