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

// 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"
  "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)
	}
}

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
}

//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"
	"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)
	}
}

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


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

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
}

// 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"
	"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
}


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"
	//"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 (
		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)
}


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

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