package main

import "fmt"

var t, n int
var a,b [110]int

type Stack struct {
	slice []int
}

func (s *Stack) Push(v int) {
	s.slice = append(s.slice, v)
}

func (s *Stack) Pop() int {
	var top = s.slice[len(s.slice)-1]
	s.slice = s.slice[0:len(s.slice)-1]
	return top
}

func main() {

	fmt.Scan(&t)

	for i := 0; i < t; i++ {
		fmt.Scan(&n)

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

		var bal int = 0
		var m int = 0
		dec := &Stack{[]int{}}
		inc := &Stack{[]int{}}

		for j := 0; j < n; j++ {
			bal += a[j]-b[j]
			if a[j] > b[j] {
				for k := 0; k < a[j]-b[j]; k++ {
					dec.Push(j+1)
				}
				m += a[j]-b[j]
			} else {
				for k := 0; k < b[j]-a[j]; k++ {
					inc.Push(j+1)
				}
			}
		}

		if bal != 0 {
			fmt.Println("-1")
		} else {
			fmt.Println(m)
			for m > 0 {
				fmt.Println(dec.Pop(), inc.Pop())
				m--
			}
		}
	}

}

package main

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

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

func pairs(a, b []int) [][]int {
	res := make([][]int, len(a))

	for i := range res {
		res[i] = []int{a[i] + 1, b[i] + 1}
	}

	return res
}

func Solve(a, b []int) (int, [][]int) {
	var ups, downs []int

	for i := range a {
		if a[i] == b[i] {
			continue
		}

		if a[i] < b[i] {
			diff := b[i] - a[i]

			for j := 0; j < diff; j++ {
				ups = append(ups, i)
			}
		} else {
			diff := a[i] - b[i]

			for j := 0; j < diff; j++ {
				downs = append(downs, i)
			}
		}
	}

	if len(ups) != len(downs) {
		return -1, nil
	}

	return len(ups), pairs(downs, ups)
}

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

	for i := 0; i < n; i++ {
		_ = readInt(in)

		a := readArrInt(in)
		b := readArrInt(in)

		count, res := Solve(a, b)

		fmt.Printf("%d\n", count)

		if len(res) > 0 {
			for i := 0; i < len(res); i++ {
				fmt.Printf("%d %d\n", res[i][0], res[i][1])
			}
		}
	}
}

package main

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

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

func init() {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 4096), 1e9)
}

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

func sint64() int64 {
	s := sstring()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

func sint64s(n int64) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = sint64()
	}
	return a
}

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

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		a := sint64s(n)
		b := sint64s(n)
		res := sol(n, a, b)
		if res == nil {
			printf("-1\n")
		} else {
			printf("%d\n", len(res))
			for _, r := range res {
				printf("%d %d\n", r[0], r[1])
			}
		}
	}
}

// i--, j++
func sol(n int64, a, b []int64) [][2]int64 {
	ret := [][2]int64{}
	inc := [][2]int64{} // idx, num
	dec := [][2]int64{} // idx, num
	for i := range a {
		if b[i]-a[i] == 0 {
			continue
		}
		if b[i]-a[i] > 0 {
			inc = append(inc, [2]int64{int64(i), b[i] - a[i]})
		} else {
			dec = append(dec, [2]int64{int64(i), a[i] - b[i]})
		}
	}
	// printf("inc=%v dec=%v\n", inc, dec)
	for len(inc) > 0 && len(dec) > 0 {
		ret = append(ret, [2]int64{
			dec[len(dec)-1][0] + 1,
			inc[len(inc)-1][0] + 1,
		})
		dec[len(dec)-1][1]--
		inc[len(inc)-1][1]--
		if dec[len(dec)-1][1] == 0 {
			dec = dec[:len(dec)-1]
		}
		if inc[len(inc)-1][1] == 0 {
			inc = inc[:len(inc)-1]
		}
	}
	if len(dec) > 0 || len(inc) > 0 {
		return nil
	}
	return ret
}

package main

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

type Scanner struct {
	*bufio.Scanner
}

func NewWordScanner(reader io.Reader) *Scanner {
	scanner := bufio.NewScanner(reader)
	scanner.Split(bufio.ScanWords)
	return &Scanner{scanner}
}

func (s *Scanner) Word() string {
	s.Scan()
	return s.Text()
}

func (s *Scanner) Int() int {
	s.Scan()
	n, _ := strconv.Atoi(s.Text())
	return n
}

func (s *Scanner) Int64() int64 {
	s.Scan()
	n, _ := strconv.ParseInt(s.Text(), 10, 64)
	return n
}

func (s *Scanner) Ints(count int) []int {
	ints := make([]int, count)
	for i := range ints {
		ints[i] = s.Int()
	}
	return ints
}

func (s *Scanner) Int64s(count int) []int64 {
	int64s := make([]int64, count)
	for i := range int64s {
		int64s[i] = s.Int64()
	}
	return int64s
}

func same(a, b []int) bool {
	for i := range a {
		if a[i] != b[i] {
			return false
		}
	}
	return true
}

func solve(a, b []int) ([][]int, bool) {
	res := [][]int{}
	for i := range a {
		for a[i] != b[i] {
			found := false
			if a[i] < b[i] {
				for j := range a {
					if a[j] > b[j] {
						res = append(res, []int{j, i})
						a[j] -= 1
						a[i] += 1
						found = true
						break
					}
				}
			} else if a[i] > b[i] {
				for j := range a {
					if a[j] < b[j] {
						res = append(res, []int{i, j})
						a[i] -= 1
						a[j] += 1
						found = true
						break
					}
				}
			}
			if !found {
				return [][]int{}, false
			}
		}
	}
	return res, same(a, b)
}

func main() {
	stdin := NewWordScanner(os.Stdin)
	tests := stdin.Int()

	for i := 0; i < tests; i += 1 {
		n := stdin.Int()
		a := stdin.Ints(n)
		b := stdin.Ints(n)

		res, exists := solve(a, b)
		if exists {
			fmt.Println(len(res))
			for _, p := range res {
				fmt.Println(p[0]+1, p[1]+1)
			}
		} else {
			fmt.Println(-1)
		}
	}
}

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	t, n   int
)

func solve(n int, a, b []int) string {
	suma, sumb := 0, 0
	var c, d []string
	for i := 0; i < n; i++ {
		suma += a[i]
		sumb += b[i]
		if a[i] > b[i] {
			for j := 0; j < a[i]-b[i]; j++ {
				c = append(c, fmt.Sprint(i+1))
			}
		}
		if a[i] < b[i] {
			for j := 0; j < b[i]-a[i]; j++ {
				d = append(d, fmt.Sprint(i+1))
			}
		}
	}
	if suma != sumb {
		return "-1"
	}
	for i := 0; i < len(c); i++ {
		c[i] += " " + d[i]
	}
	return fmt.Sprint(len(c)) + "\n" + strings.Join(c, "\n")
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n)
		a, b := make([]int, n), make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a[i])
		}
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &b[i])
		}
		fmt.Println(solve(n, a, b))
	}
}

package main

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

func main() {

	r := bufio.NewReader(os.Stdin)

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

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

		a := make([]int, n)
		b := make([]int, n)

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

		for j := 0; j < n; j++ {
			fmt.Fscan(r, &b[j])
		}

		solve(a, b, n)
	}
}

func solve(a, b []int, n int) {

	var al, bl int
	var smaller []int
	var larger []int

	for i := 0; i < n; i++ {
		if a[i] > b[i] {
			al += a[i] - b[i]
			larger = append(larger, i)
		} else if b[i] > a[i] {
			bl += b[i] - a[i]
			smaller = append(smaller, i)
		}
	}

	if al != bl {
		fmt.Println(-1)
		return
	}

	fmt.Println(al)

	for s,l := 0, 0; s < len(smaller) && l < len(larger); {
		smIndex := smaller[s]
		lIndex := larger[l]

		fmt.Print(lIndex + 1)
		fmt.Print(" ")
		fmt.Println(smIndex + 1)

		a[smIndex]++
		a[lIndex]--

		if a[smIndex] == b[smIndex] {
			s++
		}
		if a[lIndex] == b[lIndex] {
			l++
		}
	}
}

package main

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

type CF1546A struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1546A) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1546A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1546A) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1546A) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1546A) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1546A
Date: 17/05/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1546/A
Problem: CF1546A
**/
func (in *CF1546A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		a := make([]int, n)
		s := []int{}
		r := []int{}
		for i := 0; i < n; i++ {
			a[i] = in.NextInt()
		}
		b := make([]int, n)
		ans := 0
		for i := 0; i < n; i++ {
			b[i] = in.NextInt()
			if a[i]-b[i] > 0 {
				s = append(s, i)
			} else if a[i]-b[i] < 0 {
				r = append(r, i)
			}
			ans += a[i] - b[i]
		}
		if ans != 0 {
			fmt.Println(-1)
		} else {
			var sol strings.Builder
			ans := 0
			for i := 0; i < len(s); i++ {
				if a[s[i]] > b[s[i]] {
					for j := 0; j < len(r); j++ {
						if a[r[j]] < b[r[j]] {
							a[s[i]]--
							a[r[j]]++
							sol.WriteString(fmt.Sprintf("%d %d\n", s[i]+1, r[j]+1))
							ans ++
							if a[s[i]]== b[s[i]]{
								break
							}

							j = -1

						}
					}
					i=-1
				}
			}

			fmt.Println(ans)
			fmt.Print(sol.String())
		}

	}
}

func NewCF1546A(r *bufio.Reader) *CF1546A {
	return &CF1546A{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1546A(bufio.NewReader(os.Stdin)).Run()
}

package main

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

type CF1546A struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1546A) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1546A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1546A) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1546A) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1546A) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1546A
Date: 17/05/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1546/A
Problem: CF1546A
**/
func (in *CF1546A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		a := make([]int, n)
		s := []int{}
		r := []int{}
		for i := 0; i < n; i++ {
			a[i] = in.NextInt()
		}
		b := make([]int, n)
		ans := 0
		for i := 0; i < n; i++ {
			b[i] = in.NextInt()
			if a[i]-b[i] > 0 {
				s = append(s, i)
			} else if a[i]-b[i] < 0 {
				r = append(r, i)
			}
			ans += a[i] - b[i]
		}
		if ans != 0 {
			fmt.Println(-1)
		} else {
			var sol strings.Builder
			ans := 0
			for i := 0; i < len(s); i++ {
				if a[s[i]] > b[s[i]] {
					for j := 0; j < len(r); j++ {
						if a[r[j]] < b[r[j]] {
							a[s[i]]--
							a[r[j]]++
							sol.WriteString(fmt.Sprintf("%d %d\n", s[i]+1, r[j]+1))
							ans ++
							if a[s[i]]== b[s[i]]{
								fmt.Println("??")
								break
							}

							j = -1

						}
					}
					i=-1
				}
			}

			fmt.Println(ans)
			fmt.Print(sol.String())
		}

	}
}

func NewCF1546A(r *bufio.Reader) *CF1546A {
	return &CF1546A{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1546A(bufio.NewReader(os.Stdin)).Run()
}

package main

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

type CF1546A struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1546A) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1546A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1546A) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1546A) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1546A) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1546A
Date: 17/05/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1546/A
Problem: CF1546A
**/
func (in *CF1546A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		a := make([]int, n)
		s := []int{}
		r := []int{}
		for i := 0; i < n; i++ {
			a[i] = in.NextInt()
		}
		b := make([]int, n)
		ans := 0
		for i := 0; i < n; i++ {
			b[i] = in.NextInt()
			if a[i]-b[i] > 0 {
				s = append(s, i)
			} else if a[i]-b[i] < 0 {
				r = append(r, i)
			}
			ans += a[i] - b[i]
		}
		if ans != 0 {
			fmt.Println(-1)
		} else {
			var sol strings.Builder
			ans := 0
			for i := 0; i < len(s)&&i < 10; i++ {
				if a[s[i]] > b[s[i]] {
					for j := 0; j < len(r); j++ {
						if a[r[j]] < b[r[j]] {
							a[s[i]]--
							a[r[j]]++
							sol.WriteString(fmt.Sprintf("%d %d\n", s[i]+1, r[j]+1))
							ans ++
							if a[s[i]]== b[s[i]]{
								break
							}

							j = 0

						}
					}
					i=0
				}
			}

			fmt.Println(ans)
			fmt.Print(sol.String())
		}

	}
}

func NewCF1546A(r *bufio.Reader) *CF1546A {
	return &CF1546A{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1546A(bufio.NewReader(os.Stdin)).Run()
}

package main

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

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

func pairs(a, b []int) [][]int {
	res := make([][]int, len(a))

	for i := range res {
		res[i] = []int{a[i] + 1, b[i] + 1}
	}

	return res
}

func Solve(a, b []int) (int, [][]int) {
	var ups, downs []int

	for i := range a {
		if a[i] == b[i] {
			continue
		}

		if a[i] < b[i] {
			diff := b[i] - a[i]

			for j := 0; j < diff; j++ {
				ups = append(ups, i)
			}
		} else {
			diff := a[i] - b[i]

			for j := 0; j < diff; j++ {
				downs = append(downs, i)
			}
		}
	}

	if len(ups) != len(downs) {
		return -1, nil
	}

	return len(ups), pairs(ups, downs)
}

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

	for i := 0; i < n; i++ {
		_ = readInt(in)

		a := readArrInt(in)
		b := readArrInt(in)

		count, res := Solve(a, b)

		fmt.Printf("%d\n", count)

		if len(res) > 0 {
			for i := 0; i < len(res); i++ {
				fmt.Printf("%d %d\n", res[i][0], res[i][1])
			}
		}
	}
}

package main

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

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

func init() {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 4096), 1e9)
}

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

func sint64() int64 {
	s := sstring()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

func sint64s(n int64) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = sint64()
	}
	return a
}

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

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		a := sint64s(n)
		b := sint64s(n)
		res := sol(n, a, b)
		if res == nil {
			printf("-1\n")
		} else {
			printf("%d\n", len(res))
			for _, r := range res {
				printf("%d %d\n", r[0], r[1])
			}
		}
	}
}

// i--, j++
func sol(n int64, a, b []int64) [][2]int64 {
	ret := [][2]int64{}
	for i := range a {
		if int64(i) == n-1 {
			if a[n-1] != b[n-1] {
				return nil
			}
		}
		for a[i] != b[i] {
			if a[i] < b[i] {
				ret = append(ret, [2]int64{n, int64(i) + 1})
				a[i]++
				a[n-1]--
			} else {
				ret = append(ret, [2]int64{int64(i) + 1, n})
				a[i]--
				a[n-1]++
			}
		}
	}
	return ret
}

package main

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

type Scanner struct {
	*bufio.Scanner
}

func NewWordScanner(reader io.Reader) *Scanner {
	scanner := bufio.NewScanner(reader)
	scanner.Split(bufio.ScanWords)
	return &Scanner{scanner}
}

func (s *Scanner) Word() string {
	s.Scan()
	return s.Text()
}

func (s *Scanner) Int() int {
	s.Scan()
	n, _ := strconv.Atoi(s.Text())
	return n
}

func (s *Scanner) Int64() int64 {
	s.Scan()
	n, _ := strconv.ParseInt(s.Text(), 10, 64)
	return n
}

func (s *Scanner) Ints(count int) []int {
	ints := make([]int, count)
	for i := range ints {
		ints[i] = s.Int()
	}
	return ints
}

func (s *Scanner) Int64s(count int) []int64 {
	int64s := make([]int64, count)
	for i := range int64s {
		int64s[i] = s.Int64()
	}
	return int64s
}

func solve(a, b []int) ([][]int, bool) {
	delta := 0
	for _, x := range a {
		delta += x
	}
	for _, x := range b {
		delta -= x
	}
	if delta != 0 {
		return [][]int{}, false
	}

	res := [][]int{}
	for i := 1; i < len(a); i += 1 {
		for a[i] > 0 {
			res = append(res, []int{i, 0})
			a[i] -= 1
			a[0] += 1
		}
	}
	for i := 1; i < len(b); i += 1 {
		for a[i] != b[i] {
			res = append(res, []int{0, i})
			a[i] += 1
			a[0] -= 1
		}
	}
	return res, true
}

func main() {
	stdin := NewWordScanner(os.Stdin)
	tests := stdin.Int()

	for i := 0; i < tests; i += 1 {
		n := stdin.Int()
		a := stdin.Ints(n)
		b := stdin.Ints(n)

		res, exists := solve(a, b)
		if exists {
			fmt.Println(len(res))
			for _, p := range res {
				fmt.Println(p[0]+1, p[1]+1)
			}
		} else {
			fmt.Println(-1)
		}
	}
}


package main

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

func main() {
	a, b := ScanInt(), ScanInt()
	n := a + b + 1
	for i := 1; i <= a; i++{
		fmt.Print(i, " ")
	}
	fmt.Print(n, " ")
	for i := 1; i <= b; i++{
		fmt.Print(n-i, " ")
	}
	close(O)
	oWg.Wait()
}

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

var O chan string
var in *bufio.Scanner
var oWg *sync.WaitGroup
var P func(a ...interface{}) (n int, err error)

func init() {
	P = fmt.Println

	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(1e+9))
	in.Split(bufio.ScanWords)

	O = make(chan string, 16)
	oWg = &sync.WaitGroup{}
	oWg.Add(1)

	writer := bufio.NewWriter(os.Stdout)
	go func() {
		defer oWg.Done()
		defer writer.Flush()

		for line := range O {
			writer.WriteString(line + "\n")
		}
	}()
}

func ScanWord() string { in.Scan(); return in.Text() }
func ScanInt() int     { i, _ := strconv.Atoi(ScanWord()); return i }
func ScanInt64() int64 { i, _ := strconv.ParseInt(ScanWord(), 10, 64); return i }
func ScanStringSlice(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ScanWord()
	}
	return s
}
func ScanIntSlice(n int) []int {
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = ScanInt()
	}
	return arr
}
func ScanInt64Slice(n int) []int64 {
	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		arr[i] = ScanInt64()
	}
	return arr
}

/*********************** Utils ***********************/

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

package main

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

func main() {
	a, b := ScanInt(), ScanInt()
	for i := 1; i <= a; i++ {
		fmt.Print(i, " ")
	}

	fmt.Print((a + b + 1), " ")

	for i := a + b; i > a; i-- {
		fmt.Print(i, " ")
	}

	close(O)
	oWg.Wait()
}

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

var O chan string
var in *bufio.Scanner
var oWg *sync.WaitGroup
var P func(a ...interface{}) (n int, err error)

func init() {
	P = fmt.Println

	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(1e+9))
	in.Split(bufio.ScanWords)

	O = make(chan string, 16)
	oWg = &sync.WaitGroup{}
	oWg.Add(1)

	writer := bufio.NewWriter(os.Stdout)
	go func() {
		defer oWg.Done()
		defer writer.Flush()

		for line := range O {
			writer.WriteString(line + "\n")
		}
	}()
}

func ScanWord() string { in.Scan(); return in.Text() }
func ScanInt() int     { i, _ := strconv.Atoi(ScanWord()); return i }
func ScanInt64() int64 { i, _ := strconv.ParseInt(ScanWord(), 10, 64); return i }
func ScanStringSlice(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ScanWord()
	}
	return s
}
func ScanIntSlice(n int) []int {
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = ScanInt()
	}
	return arr
}
func ScanInt64Slice(n int) []int64 {
	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		arr[i] = ScanInt64()
	}
	return arr
}

/*********************** Utils ***********************/

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

package main

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

func main() {
	a, b := ScanInt(), ScanInt()
	for i := 1; i < a; i++ {
		fmt.Print(i, " ")
	}
	fmt.Print(a, " ")

	for i := a + b + 1; i > a; i-- {
		fmt.Print(i, " ")
	}

	close(O)
	oWg.Wait()
}

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

var O chan string
var in *bufio.Scanner
var oWg *sync.WaitGroup
var P func(a ...interface{}) (n int, err error)

func init() {
	P = fmt.Println

	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(1e+9))
	in.Split(bufio.ScanWords)

	O = make(chan string, 16)
	oWg = &sync.WaitGroup{}
	oWg.Add(1)

	writer := bufio.NewWriter(os.Stdout)
	go func() {
		defer oWg.Done()
		defer writer.Flush()

		for line := range O {
			writer.WriteString(line + "\n")
		}
	}()
}

func ScanWord() string { in.Scan(); return in.Text() }
func ScanInt() int     { i, _ := strconv.Atoi(ScanWord()); return i }
func ScanInt64() int64 { i, _ := strconv.ParseInt(ScanWord(), 10, 64); return i }
func ScanStringSlice(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ScanWord()
	}
	return s
}
func ScanIntSlice(n int) []int {
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = ScanInt()
	}
	return arr
}
func ScanInt64Slice(n int) []int64 {
	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		arr[i] = ScanInt64()
	}
	return arr
}

/*********************** Utils ***********************/

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


package main

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

var _, _, _ = main, rw, do

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

func rw(r io.Reader, w io.Writer) {
	var t int
	fmt.Fscan(r, &t)
	for ti := 0; ti < t; ti++ {
		var m int
		fmt.Fscan(r, &m)
		a := [2][]int{}
		for i := range a {
			a[i] = make([]int, m)
			for j := range a[i] {
				fmt.Fscan(r, &a[i][j])
			}
		}
		w1 := do(m, a)
		fmt.Fprintln(w, w1)
	}
}

func max(x ...int) int {
	for _, v := range x {
		if x[0] < v {
			x[0] = v
		}
	}
	return x[0]
}

func min(x ...int) int {
	for _, v := range x {
		if x[0] > v {
			x[0] = v
		}
	}
	return x[0]
}

func do(m int, a [2][]int) int {
	h := [2][]int{}
	for i := 0; i < 2; i++ {
		h[i] = make([]int, m+1)
		for j := m - 1; j >= 0; j-- {
			last := (m-j-1)*2 + 2
			h[i][j] = max(a[i][j], h[i][j+1]-1, a[1-i][j]-last+1)
		}
	}
	s := 0               // snake
	l := 0               // snake len
	mx := int(1e9) + 2*m // ret
	k := 0
	for j := 0; j < m; j++ {
		if j > 0 {
			l++
			s = max(s, a[k][j]-l+1)
		}
		g := max(s, h[k][j+1]-l, a[1-k][j]-(2*m-1)+1)
		mx = min(mx, g)

		k = 1 - k

		l++
		s = max(s, a[k][j]-l+1)
		g = max(s, h[k][j+1]-l)
		mx = min(mx, g)
	}
	return mx + 2*m - 1
}

package main
import("bufio";."fmt";"os")
func main(){
	in:=bufio.NewReader(os.Stdin)
	T,m:=0,0
	for Fscan(in,&T);T>0;T--{
		Fscan(in,&m)
		a:=make([][2]int,m)
		for j:=0;j<2;j++{for i:=0;i<m;i++{Fscan(in,&a[i][j])}}
		a[0][0]=-1
		f:=make([][2]int,m+1)
		for j:=0;j<2;j++{for i:=m-1;i>=0;i--{
			f[i][j]=max(f[i+1][j]-1,max(a[i][j],a[i][j^1]-(m-i)*2+1))
		}}
		ans,t:=f[0][0]+m*2,-1
		for i,c:=range a{
			t=max(t,c[i&1])+1
			t=max(t,c[i&1^1])+1
			r:=max(t,f[i+1][i&1^1])+(m-1-i)*2
			if r<ans{ans=r}
		}
		Println(ans)
	}
}
func max(a,b int)int{if a<b{return b};return a}

package main
import("bufio";."fmt";"os")

//github.com/EndlessCheng/codeforces-go
func main(){
	in:=bufio.NewReader(os.Stdin)
	T,m:=0,0
	for Fscan(in,&T);T>0;T--{
		Fscan(in,&m)
		a:=make([][2]int,m)
		for j:=0;j<2;j++{for i:=0;i<m;i++{Fscan(in,&a[i][j])}}
		a[0][0]=-1
		f:=make([][2]int,m+1)
		for i:=m-1;i>=0;i--{
			f[i][0]=max(f[i+1][0]-1,max(a[i][0],a[i][1]-(m-i)*2+1))
			f[i][1]=max(f[i+1][1]-1,max(a[i][1],a[i][0]-(m-i)*2+1))
		}
		ans,t:=f[0][0]+m*2,-1
		for i,c:=range a{
			t=max(t,c[i&1])+1
			t=max(t,c[i&1^1])+1
			r:=max(t,f[i+1][i&1^1])+(m-1-i)*2
			if r<ans{
				ans=r
			}
		}
		Println(ans)
	}
}
func max(a,b int)int{if a<b{return b};return a}

package main

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

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

	var T, m int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &m)
		a := make([][2]int, m)
		for j := 0; j < 2; j++ {
			for i := 0; i < m; i++ {
				Fscan(in, &a[i][j])
			}
		}
		// 为了方便描述，下面的坐标以 (0,0) 为左上角
		// 小技巧：把起点当成 (0,-1)，这样的话需要把 a[0][0] 置为 -1 从而与原问题保持一致
		a[0][0] = -1
		// suf[i][0] 表示能够「一刻不停地」从 (0,i) 走个反 C 型到 (1,i)，在进入 (0,i) 前的最小时间
		// suf[i][1] 表示能够「一刻不停地」从 (1,i) 走个反 C 型到 (0,i)，在进入 (1,i) 前的最小时间
		suf := make([][2]int, m+1)
		for i := m - 1; i >= 0; i-- {
			suf[i][0] = max(suf[i+1][0]-1, max(a[i][0], a[i][1]-(m-i)*2+1))
			suf[i][1] = max(suf[i+1][1]-1, max(a[i][1], a[i][0]-(m-i)*2+1))
		}
		// i 列及其前的格子走之字，i+1 列及其后的格子走反 C 型
		// 模拟即可
		ans, t := suf[0][0]+m*2, -1
		for i, col := range a[:m-1] {
			t = max(t, col[i&1]) + 1
			t = max(t, col[i&1^1]) + 1
			ans = min(ans, max(t, suf[i+1][i&1^1])+(m-1-i)*2)
		}
		Fprintln(out, ans)
	}
}

func main() { CF1716C(os.Stdin, os.Stdout) }
func max(a, b int) int { if a < b { return b }; return a }
func min(a, b int) int { if a > b { return b }; return a }

package main

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

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

	var T, m int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &m)
		a := make([][2]int, m)
		for j := 0; j < 2; j++ {
			for i := 0; i < m; i++ {
				Fscan(in, &a[i][j])
			}
		}
		a[0][0] = -1
		suf := make([][2]int, m+1)
		for i := m - 1; i >= 0; i-- {
			suf[i][0] = max(suf[i+1][0]-1, max(a[i][0], a[i][1]-(m-i)*2+1))
			suf[i][1] = max(suf[i+1][1]-1, max(a[i][1], a[i][0]-(m-i)*2+1))
		}
		ans, t := suf[0][0]+m*2, -1
		for i, col := range a[:m-1] {
			t = max(t, col[i&1]) + 1
			t = max(t, col[i&1^1]) + 1
			ans = min(ans, max(t, suf[i+1][i&1^1])+(m-1-i)*2)
		}
		Fprintln(out, ans)
	}
}

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

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

package main

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

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

	var T, m int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &m)
		a := make([][2]int, m)
		for j := 0; j < 2; j++ {
			for i := 0; i < m; i++ {
				Fscan(in, &a[i][j])
			}
		}
		suf := make([][2]int, m+1)
		for i := m - 1; i > 0; i-- {
			suf[i][0] = max(suf[i+1][0]-1, max(a[i][0], a[i][1]-(m-i)*2+1))
			suf[i][1] = max(suf[i+1][1]-1, max(a[i][1], a[i][0]-(m-i)*2+1))
		}
		t := a[0][1] + 1
		ans := min(max(suf[1][0]+2*m-2, a[0][1])+1, max(t, suf[1][1])+2*m-2)
		for i := 1; i < m-1; i++ {
			t = max(t, a[i][i&1]) + 1
			t = max(t, a[i][i&1^1]) + 1
			ans = min(ans, max(t, suf[i+1][i&1^1])+(m-1-i)*2)
		}
		Fprintln(out, ans)
	}
}

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

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

package main

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

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

	var T, m int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &m)
		a := make([][2]int, m)
		for j := 0; j < 2; j++ {
			for i := 0; i < m; i++ {
				Fscan(in, &a[i][j])
			}
		}
		suf := make([][2]int, m+1)
		for i := m - 1; i >= 0; i-- {
			suf[i][0] = max(suf[i+1][0]-1, max(a[i][0], a[i][1]-(m-i)*2+1))
			suf[i][1] = max(suf[i+1][1]-1, max(a[i][1], a[i][0]-(m-i)*2+1))
		}
		ans := max(suf[1][0]+2*m-2, a[0][1]) + 1
		t := a[0][1] + 1
		ans = min(ans, max(t, suf[1][1])+2*m-2)
		for i := 1; i < m-1; i++ {
			if i&1 > 0 {
				t = max(t, a[i][1]) + 1
				t = max(t, a[i][0]) + 1
				ans = min(ans, max(t, suf[i+1][0])+(m-1-i)*2)
			} else {
				t = max(t, a[i][0]) + 1
				t = max(t, a[i][1]) + 1
				ans = min(ans, max(t, suf[i+1][1])+(m-1-i)*2)
			}
		}
		Fprintln(out, ans)
	}
}

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

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

package main

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

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

	var T, m int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &m)
		a := [2][]int{}
		for i := range a {
			a[i] = make([]int, m)
			for j := range a[i] {
				Fscan(in, &a[i][j])
			}
		}
		suf := make([][2]int, m+1)
		for i := m - 1; i >= 0; i-- {
			suf[i][0] = max(suf[i+1][0]-1, max(a[0][i], a[1][i]-(m-i)*2+1))
			suf[i][1] = max(suf[i+1][1]-1, max(a[1][i], a[0][i]-(m-i)*2+1))
		}
		ans := max(suf[1][0]+2*m-2, a[1][0]) + 1
		t := a[1][0] + 1
		ans = min(ans, max(t, suf[1][1])+2*m-2)
		for j := 1; j < m-1; j++ {
			if j&1 > 0 {
				t = max(t, a[1][j]) + 1
				t = max(t, a[0][j]) + 1
				ans = min(ans, max(t, suf[j+1][0])+(m-1-j)*2)
			} else {
				t = max(t, a[0][j]) + 1
				t = max(t, a[1][j]) + 1
				ans = min(ans, max(t, suf[j+1][1])+(m-1-j)*2)
			}
		}
		Fprintln(out, ans)
	}
}

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

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

package main

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

var rr = bufio.NewReader(os.Stdin)
var ww = bufio.NewWriter(os.Stdout)

func slove() {
	var n int
	fmt.Fscan(rr, &n)
	a := make([][]int, 2)
	a[0] = make([]int, n)
	for i:=0;i<n;i++ {
		fmt.Fscan(rr, &a[0][i])
		a[0][i]++
	}
	a[1] = make([]int, n)
	for i:=0;i<n;i++ {
		fmt.Fscan(rr, &a[1][i])
		a[1][i]++
	}
	right0, right1 := 0, 0
	left0, left1 := n-1, n-1
	a[0][0] = 0
	cur := 0
	for i := range a[0] {
		if a[0][i] > cur {
			right0 = i
		}
		cur = max(a[0][i] + 1, cur + 1)
	}
	cur = 0
	for i:=n-1;i>=0;i-- {
		if a[0][i] > cur {
			left0 = i
		}
		cur = max(a[0][i] + 1, cur + 1)
	}
	cur = 0
	for i := range a[1] {
		if a[1][i] > cur {
			right1 = i
		}
		cur = max(a[1][i] + 1, cur + 1)
	}
	cur = 0
	ans := 1 << 30
	for i:=n-1;i>=0;i-- {
		if a[1][i] > cur {
			left1 = i
		}
		cur = max(a[1][i] + 1, cur + 1)
	}
	i, j := 0, 0
	diff := 1
	time := -1
	new_time := 0
	new_time = max(new_time + right0, a[0][right0])
	new_time += n - right0 - 1
	new_time = max(new_time + 1, a[1][n-1])
	new_time = max(new_time + n - left1 - 1, a[1][left1])
	new_time += left1
	ans = min(ans, new_time)
	for j < n {
		time = max(time+1, a[i][j])
		new_time := time
		next_i, next_j := i + diff, j
		if next_i == 2 || next_i == -1 {
			next_i = i
			next_j = j+1
			diff = -diff
			if i == 0 {
				if j >= right0 {
					new_time += n - j - 1
				} else {
					new_time = max(new_time + right0 - j, a[0][right0])
					new_time += n - right0 - 1
				}
				new_time = max(new_time + 1, a[1][n-1])
				if j >= left1 {
					new_time += n - j - 2
				} else {
					new_time = max(new_time + n - left1 - 1, a[1][left1])
					new_time += left1 - j - 1
				}
				ans = min(ans, new_time)
			} else {
				if j >= right1 {
					new_time += n - j - 1
				} else {
					new_time = max(new_time + right1 - j, a[1][right1])
					new_time += n - right1 - 1
				}
				new_time = max(new_time + 1, a[0][n-1])
				if j >= left0 {
					new_time += n - j - 2
				} else {
					new_time = max(new_time + n - left0 - 1, a[0][left0])
					new_time += left0 - j - 1
				}
				ans = min(ans, new_time)
			}
		}
		i, j = next_i, next_j
	}
	fmt.Fprintln(ww, ans)
}

func max(a, b int) int {if a > b {return a} else {return b}}
func min(a, b int) int {if a < b {return a} else {return b}}
func main() {
	defer ww.Flush()
	var n int
	fmt.Fscan(rr, &n)
	for i:=1;i<=n;i++ {
		slove()
	}
}

package main

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

func CF1716C(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var n, t int
	Fscan(in, &n)
	for n > 0 {
		Fscan(in, &t)
		a := make([][2]int, t)
		for j := 0; j < 2; j++ {
			for i := 0; i < t; i++ {
				Fscan(in, &a[i][j])
			}
		}
		Fprintln(out, solve(a))
		n -= 1
	}
}
func solve(n [][2]int) int {
	n[0][0] = -1
	var a, b, c, d, cc int
	var a1, b1, c1, d1 int
	for j := len(n) - 1; j >= 0; j-- {
		b1 = max(n[j][0], max(b-1, n[j][1]-cc-1))
		d1 = max(n[j][1], max(d-1, n[j][0]-cc-1))
		a1 = min(b1, max(n[j][0], max(n[j][1]-1, c-2)))
		c1 = min(d1, max(n[j][1], max(n[j][0]-1, a-2)))
		a, b, c, d = a1, b1, c1, d1
		cc += 2
	}
	return a + cc
}

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 main() { CF1716C(os.Stdin, os.Stdout) }

package main

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

const MAXN int = 2e5

var a, startTime, finishTime [2][MAXN + 7]int

func maxIntVal(u, v int) int {
	if u > v {
		return u
	}
	return v
}

func minIntVal(u, v int) int {
	if u < v {
		return u
	}
	return v
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t, m, u, v, temp int
	fmt.Fscan(in, &t)

	for ff := 1; ff <= t; ff++ {
		fmt.Fscan(in, &m)
		for i := 0; i < 2; i++ {
			for j := 0; j < m; j++ {
				fmt.Fscan(in, &a[i][j])
			}
		}
		a[0][0] = -1
		if a[0][m-1] < a[1][m-1] {
			startTime[1][m-1], finishTime[1][m-1] = a[1][m-1], a[1][m-1]+1
			startTime[0][m-1], finishTime[0][m-1] = a[1][m-1]+1, a[1][m-1]+2
		} else if a[0][m-1] == a[1][m-1] {
			startTime[1][m-1], finishTime[1][m-1] = a[0][m-1]+1, a[1][m-1]+2
			startTime[0][m-1], finishTime[0][m-1] = startTime[1][m-1], finishTime[1][m-1]
		} else {
			startTime[0][m-1], finishTime[0][m-1] = a[0][m-1], a[0][m-1]+1
			startTime[1][m-1], finishTime[1][m-1] = a[0][m-1]+1, a[0][m-1]+2
		}
		for j := m - 2; j >= 0; j-- {
			for i := 0; i < 2; i++ {
				if a[i^1][j]+1 < startTime[i][j+1] && a[i][j]+1 <= finishTime[i][j+1] {
					// a[i^1][j] + 1 < st[i][j+1] && a[i][j] + 1 < ed[i][j+1]
					/* st[i][j] = st[i][j+1] - 1, ed[i][j] = ed[i][j+1] + 1 */
					// a[i^1][j] + 1 < st[i][j+1] && a[i][j] + 1 == ed[i][j+1]
					/* st[i][j] = st[i][j+1] - 1, ed[i][j] = ed[i][j+1] + 1 */
					startTime[i][j], finishTime[i][j] = startTime[i][j+1]-1, finishTime[i][j+1]+1
				} else if a[i^1][j]+1 < startTime[i][j+1] && a[i][j]+1 > finishTime[i][j+1] {
					// a[i^1][j] + 1 < st[i][j+1] && a[i][j] + 1 > ed[i][j+1]
					/* st[i][j] = a[i][j] + 1 - (2*(m-j)-1), ed[i][j] = a[i][j] + 1 */
					startTime[i][j], finishTime[i][j] = a[i][j]+1-(2*(m-j)-1), a[i][j]+1
				} else if a[i^1][j]+1 == startTime[i][j+1] && a[i][j]+1 <= finishTime[i][j+1] {
					// a[i^1][j] + 1 = st[i][j+1] && a[i][j] + 1 < ed[i][j+1]
					/* st[i][j] = a[i^1][j] + 1, ed[i][j] = ed[i][j] + 2 */
					// a[i^1][j] + 1 = st[i][j+1] && a[i][j] + 1 = ed[i][j+1]
					/* st[i][j] = a[i^1][j] + 1, ed[i][j] = ed[i][j] + 2 */
					startTime[i][j], finishTime[i][j] = a[i^1][j]+1, finishTime[i][j+1]+2
				} else if a[i^1][j]+1 == startTime[i][j+1] && a[i][j]+1 > finishTime[i][j+1] {
					// a[i^1][j] + 1 = st[i][j+1] && a[i][j] + 1 > ed[i][j+1]
					/* st[i][j] = max(a[i^1][j] + 1, a[i][j] + 1 - (2*(m-j)-1)
					   ed[i][j] = st[i][j] + (2*(m-j)-1) */
					startTime[i][j] = maxIntVal(a[i][j]+1-(2*(m-j)-1), a[i^1][j]+1)
					finishTime[i][j] = startTime[i][j] + (2*(m-j) - 1)
				} else if a[i^1][j]+1 > startTime[i][j+1] && a[i][j]+1 <= finishTime[i][j+1] {
					// a[i^1][j] + 1 > st[i][j+1] && a[i][j] + 1 < ed[i][j+1]
					/* st[i][j] = a[i^1][j] + 1, ed[i][j] = a[i^1][j] + 1 + (2*(m-j)-1) */
					// a[i^1][j] + 1 > st[i][j+1] && a[i][j] + 1 = ed[i][j+1]
					/* st[i][j] = a[i^1][j] + 1, ed[i][j] = a[i^1][j] + 1 + (2*(m-j)-1) */
					startTime[i][j], finishTime[i][j] = a[i^1][j]+1, a[i^1][j]+1+(2*(m-j)-1)
				} else {
					// a[i^1][j] + 1 > st[i][j+1] && a[i][j] + 1 > ed[i][j+1]
					/* st[i][j] = max(a[i^1][j] + 1, a[i][j] + 1 - (2*(m-j)-1))
					   ed[i][j] = st[i][j] + (2*(m-j)-1) */
					startTime[i][j] = maxIntVal(a[i^1][j]+1, a[i][j]+1-(2*(m-j)-1))
					finishTime[i][j] = startTime[i][j] + (2*(m-j) - 1)
				}
			}
		}
		// fmt.Fprintln(out, "ans:")
		// for i := 0; i < 2; i++ {
		// 	for j := 0; j < m; j++ {
		// 		fmt.Fprintf(out, "\t%d:%d", startTime[i][j], finishTime[i][j])
		// 	}
		// 	fmt.Fprintln(out)
		// }
		// fmt.Fprintln(out, "--------------------------")
		ans, count := math.MaxInt, -100
		for i := 0; i < 2*m; i++ {
			temp = i % 4
			if temp == 0 || temp == 3 {
				u, v = 0, i/2
			} else {
				u, v = 1, i/2
			}
			if temp == 0 || temp == 2 {
				if count < startTime[u^1][v] {
					ans = minIntVal(ans, finishTime[u^1][v])
				} else {
					ans = minIntVal(ans, finishTime[u^1][v]+(count+1-startTime[u^1][v]))
				}
			}
			count++
			if count <= a[u][v] {
				count = a[u][v] + 1
			}
		}
		fmt.Fprintln(out, ans)
		// t--
	}
}

package main

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

const MAXN int = 2e5

var a, startTime, finishTime [2][MAXN + 7]int

func maxIntVal(u, v int) int {
	if u > v {
		return u
	}
	return v
}

func minIntVal(u, v int) int {
	if u < v {
		return u
	}
	return v
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t, m, u, v, temp int
	fmt.Fscan(in, &t)

	for ff := 1; ff <= t; ff++ {
		fmt.Fscan(in, &m)
		for i := 0; i < 2; i++ {
			for j := 0; j < m; j++ {
				fmt.Fscan(in, &a[i][j])
			}
		}
		a[0][0] = -1
		if a[0][m-1] < a[1][m-1] {
			startTime[1][m-1], finishTime[1][m-1] = a[1][m-1], a[1][m-1]+1
			startTime[0][m-1], finishTime[0][m-1] = a[1][m-1]+1, a[1][m-1]+2
		} else if a[0][m-1] == a[1][m-1] {
			startTime[1][m-1], finishTime[1][m-1] = a[0][m-1]+1, a[1][m-1]+2
			startTime[0][m-1], finishTime[0][m-1] = startTime[1][m-1], finishTime[1][m-1]
		} else {
			startTime[0][m-1], finishTime[0][m-1] = a[0][m-1], a[0][m-1]+1
			startTime[1][m-1], finishTime[1][m-1] = a[0][m-1]+1, a[0][m-1]+2
		}
		for j := m - 2; j >= 0; j-- {
			for i := 0; i < 2; i++ {
				if a[i^1][j]+1 < startTime[i][j+1] && a[i][j]+1 <= finishTime[i][j+1] {
					// a[i^1][j] + 1 < st[i][j+1] && a[i][j] + 1 < ed[i][j+1]
					/* st[i][j] = st[i][j+1] - 1, ed[i][j] = ed[i][j+1] + 1 */
					// a[i^1][j] + 1 < st[i][j+1] && a[i][j] + 1 == ed[i][j+1]
					/* st[i][j] = st[i][j+1] - 1, ed[i][j] = ed[i][j+1] + 1 */
					startTime[i][j], finishTime[i][j] = startTime[i][j+1]-1, finishTime[i][j+1]+1
				} else if a[i^1][j]+1 < startTime[i][j+1] && a[i][j]+1 > finishTime[i][j+1] {
					// a[i^1][j] + 1 < st[i][j+1] && a[i][j] + 1 > ed[i][j+1]
					/* st[i][j] = a[i][j] + 1 - (2*(m-j)-1), ed[i][j] = a[i][j] + 1 */
					startTime[i][j], finishTime[i][j] = a[i][j]+1-(2*(m-j)-1), a[i][j]+1
				} else if a[i^1][j]+1 == startTime[i][j+1] && a[i][j]+1 <= finishTime[i][j+1] {
					// a[i^1][j] + 1 = st[i][j+1] && a[i][j] + 1 < ed[i][j+1]
					/* st[i][j] = a[i^1][j] + 1, ed[i][j] = ed[i][j] + 2 */
					// a[i^1][j] + 1 = st[i][j+1] && a[i][j] + 1 = ed[i][j+1]
					/* st[i][j] = a[i^1][j] + 1, ed[i][j] = ed[i][j] + 2 */
					startTime[i][j], finishTime[i][j] = a[i^1][j]+1, finishTime[i][j+1]+2
				} else if a[i^1][j]+1 == startTime[i][j+1] && a[i][j]+1 > finishTime[i][j+1] {
					// a[i^1][j] + 1 = st[i][j+1] && a[i][j] + 1 > ed[i][j+1]
					/* st[i][j] = max(a[i^1][j] + 1, a[i][j] + 1 - (2*(m-j)-1)
					   ed[i][j] = st[i][j] + (2*(m-j)-1) */
					startTime[i][j] = maxIntVal(a[i][j]+1-(2*(m-j)-1), a[i^1][j]+1)
					finishTime[i][j] = startTime[i][j] + (2*(m-j) - 1)
				} else if a[i^1][j]+1 > startTime[i][j+1] && a[i][j]+1 <= finishTime[i][j+1] {
					// a[i^1][j] + 1 > st[i][j+1] && a[i][j] + 1 < ed[i][j+1]
					/* st[i][j] = a[i^1][j] + 1, ed[i][j] = a[i^1][j] + 1 + (2*(m-j)-1) */
					// a[i^1][j] + 1 > st[i][j+1] && a[i][j] + 1 = ed[i][j+1]
					/* st[i][j] = a[i^1][j] + 1, ed[i][j] = a[i^1][j] + 1 + (2*(m-j)-1) */
					startTime[i][j], finishTime[i][j] = a[i^1][j]+1, a[i^1][j]+1+(2*(m-j)-1)
				} else {
					// a[i^1][j] + 1 > st[i][j+1] && a[i][j] + 1 > ed[i][j+1]
					/* st[i][j] = max(a[i^1][j] + 1, a[i][j+1] + 1 - (2*(m-j)-1))
					   ed[i][j] = st[i][j] + (2*(m-j)-1) */
					startTime[i][j] = maxIntVal(a[i^1][j]+1, a[i][j+1]+1-(2*(m-j)-1))
					finishTime[i][j] = startTime[i][j] + (2*(m-j) - 1)
				}
			}
		}
		// fmt.Fprintln(out, "ans:")
		// for i := 0; i < 2; i++ {
		// 	for j := 0; j < m; j++ {
		// 		fmt.Fprintf(out, "\t%d:%d", startTime[i][j], finishTime[i][j])
		// 	}
		// 	fmt.Fprintln(out)
		// }
		// fmt.Fprintln(out, "--------------------------")
		ans, count := math.MaxInt, -100
		for i := 0; i < 2*m; i++ {
			temp = i % 4
			if temp == 0 || temp == 3 {
				u, v = 0, i/2
			} else {
				u, v = 1, i/2
			}
			if temp == 0 || temp == 2 {
				if count < startTime[u^1][v] {
					ans = minIntVal(ans, finishTime[u^1][v])
				} else {
					ans = minIntVal(ans, finishTime[u^1][v]+(count+1-startTime[u^1][v]))
				}
			}
			count++
			if count <= a[u][v] {
				count = a[u][v] + 1
			}
		}
		if ff == 542 {
			var sb strings.Builder
			sb.WriteString(strconv.Itoa(m))
			for i := 0; i < 2; i++ {
				for j := 0; j < m; j++ {
					sb.WriteByte(',')
					sb.WriteString(strconv.Itoa(a[i][j]))
				}
			}
			fmt.Fprintln(out, sb.String())
		} else {
			fmt.Fprintln(out, ans)
		}
		// fmt.Fprintln(out, ans)
		// t--
	}
}

package main

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

const MAXN int = 2e5

var a, startTime, finishTime [2][MAXN + 7]int

func maxIntVal(u, v int) int {
	if u > v {
		return u
	}
	return v
}

func minIntVal(u, v int) int {
	if u < v {
		return u
	}
	return v
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t, m, u, v, temp int
	fmt.Fscan(in, &t)

	for ff := 1; ff <= t; ff++ {
		fmt.Fscan(in, &m)
		for i := 0; i < 2; i++ {
			for j := 0; j < m; j++ {
				fmt.Fscan(in, &a[i][j])
			}
		}
		a[0][0] = -1
		if a[0][m-1] < a[1][m-1] {
			startTime[1][m-1], finishTime[1][m-1] = a[1][m-1], a[1][m-1]+1
			startTime[0][m-1], finishTime[0][m-1] = a[1][m-1]+1, a[1][m-1]+2
		} else if a[0][m-1] == a[1][m-1] {
			startTime[1][m-1], finishTime[1][m-1] = a[0][m-1]+1, a[1][m-1]+2
			startTime[0][m-1], finishTime[0][m-1] = startTime[1][m-1], finishTime[1][m-1]
		} else {
			startTime[0][m-1], finishTime[0][m-1] = a[0][m-1], a[0][m-1]+1
			startTime[1][m-1], finishTime[1][m-1] = a[0][m-1]+1, a[0][m-1]+2
		}
		for j := m - 2; j >= 0; j-- {
			for i := 0; i < 2; i++ {
				if a[i^1][j]+1 < startTime[i][j+1] && a[i][j]+1 <= finishTime[i][j+1] {
					// a[i^1][j] + 1 < st[i][j+1] && a[i][j] + 1 < ed[i][j+1]
					/* st[i][j] = st[i][j+1] - 1, ed[i][j] = ed[i][j+1] + 1 */
					// a[i^1][j] + 1 < st[i][j+1] && a[i][j] + 1 == ed[i][j+1]
					/* st[i][j] = st[i][j+1] - 1, ed[i][j] = ed[i][j+1] + 1 */
					startTime[i][j], finishTime[i][j] = startTime[i][j+1]-1, finishTime[i][j+1]+1
				} else if a[i^1][j]+1 < startTime[i][j+1] && a[i][j]+1 > finishTime[i][j+1] {
					// a[i^1][j] + 1 < st[i][j+1] && a[i][j] + 1 > ed[i][j+1]
					/* st[i][j] = a[i][j] + 1 - (2*(m-j)-1), ed[i][j] = a[i][j] + 1 */
					startTime[i][j], finishTime[i][j] = a[i][j]+1-(2*(m-j)-1), a[i][j]+1
				} else if a[i^1][j]+1 == startTime[i][j+1] && a[i][j]+1 <= finishTime[i][j+1] {
					// a[i^1][j] + 1 = st[i][j+1] && a[i][j] + 1 < ed[i][j+1]
					/* st[i][j] = a[i^1][j] + 1, ed[i][j] = ed[i][j] + 2 */
					// a[i^1][j] + 1 = st[i][j+1] && a[i][j] + 1 = ed[i][j+1]
					/* st[i][j] = a[i^1][j] + 1, ed[i][j] = ed[i][j] + 2 */
					startTime[i][j], finishTime[i][j] = a[i^1][j]+1, finishTime[i][j+1]+2
				} else if a[i^1][j]+1 == startTime[i][j+1] && a[i][j]+1 > finishTime[i][j+1] {
					// a[i^1][j] + 1 = st[i][j+1] && a[i][j] + 1 > ed[i][j+1]
					/* st[i][j] = max(a[i^1][j] + 1, a[i][j] + 1 - (2*(m-j)-1)
					   ed[i][j] = st[i][j] + (2*(m-j)-1) */
					startTime[i][j] = maxIntVal(a[i][j]+1-(2*(m-j)-1), a[i^1][j]+1)
					finishTime[i][j] = startTime[i][j] + (2*(m-j) - 1)
				} else if a[i^1][j]+1 > startTime[i][j+1] && a[i][j]+1 <= finishTime[i][j+1] {
					// a[i^1][j] + 1 > st[i][j+1] && a[i][j] + 1 < ed[i][j+1]
					/* st[i][j] = a[i^1][j] + 1, ed[i][j] = a[i^1][j] + 1 + (2*(m-j)-1) */
					// a[i^1][j] + 1 > st[i][j+1] && a[i][j] + 1 = ed[i][j+1]
					/* st[i][j] = a[i^1][j] + 1, ed[i][j] = a[i^1][j] + 1 + (2*(m-j)-1) */
					startTime[i][j], finishTime[i][j] = a[i^1][j]+1, a[i^1][j]+1+(2*(m-j)-1)
				} else {
					// a[i^1][j] + 1 > st[i][j+1] && a[i][j] + 1 > ed[i][j+1]
					/* st[i][j] = max(a[i^1][j] + 1, a[i][j+1] + 1 - (2*(m-j)-1))
					   ed[i][j] = st[i][j] + (2*(m-j)-1) */
					startTime[i][j] = maxIntVal(a[i^1][j]+1, a[i][j+1]+1-(2*(m-j)-1))
					finishTime[i][j] = startTime[i][j] + (2*(m-j) - 1)
				}
			}
		}
		// fmt.Fprintln(out, "ans:")
		// for i := 0; i < 2; i++ {
		// 	for j := 0; j < m; j++ {
		// 		fmt.Fprintf(out, "\t%d:%d", startTime[i][j], finishTime[i][j])
		// 	}
		// 	fmt.Fprintln(out)
		// }
		// fmt.Fprintln(out, "--------------------------")
		ans, count := math.MaxInt, -100
		for i := 0; i < 2*m; i++ {
			temp = i % 4
			if temp == 0 || temp == 3 {
				u, v = 0, i/2
			} else {
				u, v = 1, i/2
			}
			if temp == 0 || temp == 2 {
				if count < startTime[u^1][v] {
					ans = minIntVal(ans, finishTime[u^1][v])
				} else {
					ans = minIntVal(ans, finishTime[u^1][v]+(count+1-startTime[u^1][v]))
				}
			}
			count++
			if count <= a[u][v] {
				count = a[u][v] + 1
			}
		}
		if ff == 542 {
			var sb strings.Builder
			sb.WriteString(strconv.Itoa(m))
			for i := 0; i < 2; i++ {
				for j := 0; j < m; j++ {
					sb.WriteByte(',')
					sb.WriteString(strconv.Itoa(a[i][j]))
				}
				fmt.Fprintln(out, sb.String())
			}
		} else {
			fmt.Fprintln(out, ans)
		}
		// fmt.Fprintln(out, ans)
		// t--
	}
}

package main

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

const MAXN int = 2e5

var a, startTime, finishTime [2][MAXN + 7]int

func maxIntVal(u, v int) int {
	if u > v {
		return u
	}
	return v
}

func minIntVal(u, v int) int {
	if u < v {
		return u
	}
	return v
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t, m, u, v, temp int
	fmt.Fscan(in, &t)

	for t != 0 {
		fmt.Fscan(in, &m)
		for i := 0; i < 2; i++ {
			for j := 0; j < m; j++ {
				fmt.Fscan(in, &a[i][j])
			}
		}
		a[0][0] = -1
		if a[0][m-1] < a[1][m-1] {
			startTime[1][m-1], finishTime[1][m-1] = a[1][m-1], a[1][m-1]+1
			startTime[0][m-1], finishTime[0][m-1] = a[1][m-1]+1, a[1][m-1]+2
		} else if a[0][m-1] == a[1][m-1] {
			startTime[1][m-1], finishTime[1][m-1] = a[0][m-1]+1, a[1][m-1]+2
			startTime[0][m-1], finishTime[0][m-1] = startTime[1][m-1], finishTime[1][m-1]
		} else {
			startTime[0][m-1], finishTime[0][m-1] = a[0][m-1], a[0][m-1]+1
			startTime[1][m-1], finishTime[1][m-1] = a[0][m-1]+1, a[0][m-1]+2
		}
		for j := m - 2; j >= 0; j-- {
			for i := 0; i < 2; i++ {
				if a[i^1][j]+1 < startTime[i][j+1] && a[i][j]+1 <= finishTime[i][j+1] {
					// a[i^1][j] + 1 < st[i][j+1] && a[i][j] + 1 < ed[i][j+1]
					/* st[i][j] = st[i][j+1] - 1, ed[i][j] = ed[i][j+1] + 1 */
					// a[i^1][j] + 1 < st[i][j+1] && a[i][j] + 1 == ed[i][j+1]
					/* st[i][j] = st[i][j+1] - 1, ed[i][j] = ed[i][j+1] + 1 */
					startTime[i][j], finishTime[i][j] = startTime[i][j+1]-1, finishTime[i][j+1]+1
				} else if a[i^1][j]+1 < startTime[i][j+1] && a[i][j]+1 > finishTime[i][j+1] {
					// a[i^1][j] + 1 < st[i][j+1] && a[i][j] + 1 > ed[i][j+1]
					/* st[i][j] = a[i][j] + 1 - (2*(m-j)-1), ed[i][j] = a[i][j] + 1 */
					startTime[i][j], finishTime[i][j] = a[i][j]+1-(2*(m-j)-1), a[i][j]+1
				} else if a[i^1][j]+1 == startTime[i][j+1] && a[i][j]+1 <= finishTime[i][j+1] {
					// a[i^1][j] + 1 = st[i][j+1] && a[i][j] + 1 < ed[i][j+1]
					/* st[i][j] = a[i^1][j] + 1, ed[i][j] = ed[i][j] + 2 */
					// a[i^1][j] + 1 = st[i][j+1] && a[i][j] + 1 = ed[i][j+1]
					/* st[i][j] = a[i^1][j] + 1, ed[i][j] = ed[i][j] + 2 */
					startTime[i][j], finishTime[i][j] = a[i^1][j]+1, finishTime[i][j+1]+2
				} else if a[i^1][j]+1 == startTime[i][j+1] && a[i][j]+1 > finishTime[i][j+1] {
					// a[i^1][j] + 1 = st[i][j+1] && a[i][j] + 1 > ed[i][j+1]
					/* st[i][j] = max(a[i^1][j] + 1, a[i][j] + 1 - (2*(m-j)-1)
					   ed[i][j] = st[i][j] + (2*(m-j)-1) */
					startTime[i][j] = maxIntVal(a[i][j]+1-(2*(m-j)-1), a[i^1][j]+1)
					finishTime[i][j] = startTime[i][j] + (2*(m-j) - 1)
				} else if a[i^1][j]+1 > startTime[i][j+1] && a[i][j]+1 <= finishTime[i][j+1] {
					// a[i^1][j] + 1 > st[i][j+1] && a[i][j] + 1 < ed[i][j+1]
					/* st[i][j] = a[i^1][j] + 1, ed[i][j] = a[i^1][j] + 1 + (2*(m-j)-1) */
					// a[i^1][j] + 1 > st[i][j+1] && a[i][j] + 1 = ed[i][j+1]
					/* st[i][j] = a[i^1][j] + 1, ed[i][j] = a[i^1][j] + 1 + (2*(m-j)-1) */
					startTime[i][j], finishTime[i][j] = a[i^1][j]+1, a[i^1][j]+1+(2*(m-j)-1)
				} else {
					// a[i^1][j] + 1 > st[i][j+1] && a[i][j] + 1 > ed[i][j+1]
					/* st[i][j] = max(a[i^1][j] + 1, a[i][j+1] + 1 - (2*(m-j)-1))
					   ed[i][j] = st[i][j] + (2*(m-j)-1) */
					startTime[i][j] = maxIntVal(a[i^1][j]+1, a[i][j+1]+1-(2*(m-j)-1))
					finishTime[i][j] = startTime[i][j] + (2*(m-j) - 1)
				}
			}
		}
		// fmt.Fprintln(out, "ans:")
		// for i := 0; i < 2; i++ {
		// 	for j := 0; j < m; j++ {
		// 		fmt.Fprintf(out, "\t%d:%d", startTime[i][j], finishTime[i][j])
		// 	}
		// 	fmt.Fprintln(out)
		// }
		// fmt.Fprintln(out, "--------------------------")
		ans, count := math.MaxInt, -100
		for i := 0; i < 2*m; i++ {
			temp = i % 4
			if temp == 0 || temp == 3 {
				u, v = 0, i/2
			} else {
				u, v = 1, i/2
			}
			if temp == 0 || temp == 2 {
				if count < startTime[u^1][v] {
					ans = minIntVal(ans, finishTime[u^1][v])
				} else {
					ans = minIntVal(ans, finishTime[u^1][v]+(count+1-startTime[u^1][v]))
				}
			}
			count++
			if count <= a[u][v] {
				count = a[u][v] + 1
			}
		}
		fmt.Fprintln(out, ans)
		t--
	}
}

package main

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

const MAXN int = 2e5 + 7

var i, j, u, v, k, ff int
var a [2][MAXN]int

func minValInt(u, v int) int {
	if u < v {
		return u
	}
	return v
}

func path1(m, startTime int) int {
	temp := startTime
	for i = 1; i < m; i++ {
		temp++
		if temp <= a[0][i] {
			return math.MaxInt
		}
	}
	for i = m - 1; i >= 0; i-- {
		temp++
		if temp <= a[1][i] {
			return math.MaxInt
		}
	}
	return startTime + 2*m - 1
}

func path2(m, startTime int) int {
	temp := startTime
	for i = 0; i < m; i++ {
		temp++
		if temp <= a[1][i] {
			return math.MaxInt
		}
	}
	for i = m - 1; i > 0; i-- {
		temp++
		if temp <= a[0][i] {
			return math.MaxInt
		}
	}
	return startTime + 2*m - 1
}

func path3(m, startTime int) int {
	temp := startTime
	for i = 1; i < 2*m; i++ {
		if i%4 == 0 {
			u, v = 0, i/2
		} else if i%4 == 1 {
			u, v = 1, i/2
		} else if i%4 == 2 {
			u, v = 1, i/2
		} else {
			u, v = 0, i/2
		}
		temp++
		if temp <= a[u][v] {
			return math.MaxInt
		}
	}
	return startTime + 2*m - 1
}

func isOk(m, startTime int) bool {
	k = math.MaxInt
	k = minValInt(k, path1(m, startTime))
	k = minValInt(k, path2(m, startTime))
	k = minValInt(k, path3(m, startTime))
	return k != math.MaxInt
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t, m, ans, l, r, mid int
	fmt.Fscan(in, &t)
	for ff = 1; ff <= t; ff++ {
		fmt.Fscan(in, &m)
		for i = 0; i < 2; i++ {
			for j = 0; j < m; j++ {
				fmt.Fscan(in, &a[i][j])
			}
		}
		l, r, ans = 0, 1e9+MAXN, math.MaxInt
		for l <= r {
			mid = (l + r) >> 1
			if isOk(m, mid) {
				ans = mid + 2*m - 1
				r = mid - 1
			} else {
				l = mid + 1
			}
		}
		if ff == 1920 {
			var sb strings.Builder
			sb.WriteString(strconv.Itoa(m))
			for i = 0; i < 2; i++ {
				for j = 0; j < m; j++ {
					sb.WriteByte(',')
					sb.WriteString(strconv.Itoa(a[i][j]))
				}
			}
			fmt.Fprintln(out, sb.String())
		} else {
			fmt.Fprintln(out, ans)
		}
	}
}

package main

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

const MAXN int = 2e5 + 7

var i, j, u, v, k, ff int
var a [2][MAXN]int

func minValInt(u, v int) int {
	if u < v {
		return u
	}
	return v
}

func path1(m, startTime int) int {
	temp := startTime
	for i = 1; i < m; i++ {
		temp++
		if temp <= a[0][i] {
			return math.MaxInt
		}
	}
	for i = m - 1; i >= 0; i-- {
		temp++
		if temp <= a[1][i] {
			return math.MaxInt
		}
	}
	return startTime + 2*m - 1
}

func path2(m, startTime int) int {
	temp := startTime
	for i = 0; i < m; i++ {
		temp++
		if temp <= a[1][i] {
			return math.MaxInt
		}
	}
	for i = m - 1; i > 0; i-- {
		temp++
		if temp <= a[0][i] {
			return math.MaxInt
		}
	}
	return startTime + 2*m - 1
}

func path3(m, startTime int) int {
	temp := startTime
	for i = 1; i < 2*m; i++ {
		if i%4 == 0 {
			u, v = 0, i/2
		} else if i%4 == 1 {
			u, v = 1, i/2
		} else if i%4 == 2 {
			u, v = 1, i/2
		} else {
			u, v = 0, i/2
		}
		temp++
		if temp <= a[u][v] {
			return math.MaxInt
		}
	}
	return startTime + 2*m - 1
}

func isOk(m, startTime int) bool {
	k = math.MaxInt
	k = minValInt(k, path1(m, startTime))
	k = minValInt(k, path2(m, startTime))
	k = minValInt(k, path3(m, startTime))
	return k != math.MaxInt
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t, m, ans, l, r, mid int
	fmt.Fscan(in, &t)
	for ff = 1; ff <= t; ff++ {
		fmt.Fscan(in, &m)
		for i = 0; i < 2; i++ {
			for j = 0; j < m; j++ {
				fmt.Fscan(in, &a[i][j])
			}
		}
		l, r, ans = 0, 1e9+MAXN, math.MaxInt
		for l <= r {
			mid = (l + r) >> 1
			if isOk(m, mid) {
				ans = mid + 2*m - 1
				r = mid - 1
			} else {
				l = mid + 1
			}
		}
		if ff == 1920 {
			var sb strings.Builder
			sb.WriteString(strconv.Itoa(m))
			for i = 0; i < 2; i++ {
				for j = 0; j < m; j++ {
					sb.WriteByte(',')
					sb.WriteString(strconv.Itoa(a[i][j]))
				}
				fmt.Fprintln(out, sb.String())
			}
		} else {
			fmt.Fprintln(out, ans)
		}
	}
}

package main

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

const MAXN int = 2e5 + 7

var i, j, u, v, k, ff int
var a [2][MAXN]int

func minValInt(u, v int) int {
	if u < v {
		return u
	}
	return v
}

func path1(m, startTime int) int {
	temp := startTime
	for i = 1; i < m; i++ {
		temp++
		if temp <= a[0][i] {
			return math.MaxInt
		}
	}
	for i = m - 1; i >= 0; i-- {
		temp++
		if temp <= a[1][i] {
			return math.MaxInt
		}
	}
	return startTime + 2*m - 1
}

func path2(m, startTime int) int {
	temp := startTime
	for i = 0; i < m; i++ {
		temp++
		if temp <= a[1][i] {
			return math.MaxInt
		}
	}
	for i = m - 1; i > 0; i-- {
		temp++
		if temp <= a[0][i] {
			return math.MaxInt
		}
	}
	return startTime + 2*m - 1
}

func path3(m, startTime int) int {
	temp := startTime
	for i = 1; i < 2*m; i++ {
		if i%4 == 0 {
			u, v = 0, i/2
		} else if i%4 == 1 {
			u, v = 1, i/2
		} else if i%4 == 2 {
			u, v = 1, i/2
		} else {
			u, v = 0, i/2
		}
		temp++
		if temp <= a[u][v] {
			return math.MaxInt
		}
	}
	return startTime + 2*m - 1
}

func isOk(m, startTime int) bool {
	k = math.MaxInt
	k = minValInt(k, path1(m, startTime))
	k = minValInt(k, path2(m, startTime))
	k = minValInt(k, path3(m, startTime))
	return k != math.MaxInt
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t, m, ans, l, r, mid int
	fmt.Fscan(in, &t)
	for ff = 1; ff <= t; ff++ {
		fmt.Fscan(in, &m)
		for i = 0; i < 2; i++ {
			for j = 0; j < m; j++ {
				fmt.Fscan(in, &a[i][j])
			}
		}
		l, r, ans = 0, 1e9+MAXN, math.MaxInt
		for l <= r {
			mid = (l + r) >> 1
			if isOk(m, mid) {
				ans = mid + 2*m - 1
				r = mid - 1
			} else {
				l = mid + 1
			}
		}
		if ff == 1920 {
			fmt.Fprintf(out, " %d", m)
			for i = 0; i < 2; i++ {
				for j = 0; j < m; j++ {
					fmt.Fprintf(out, " %d", a[i][j])
				}
				fmt.Fprintln(out)
			}
		} else {
			fmt.Fprintln(out, ans)
		}
	}
}

package main

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

const MAXN int = 2e5 + 7

var i, j, u, v, k, ff int
var a [2][MAXN]int

func minValInt(u, v int) int {
	if u < v {
		return u
	}
	return v
}

func path1(m, startTime int) int {
	temp := startTime
	for i = 1; i < m; i++ {
		temp++
		if temp <= a[0][i] {
			return math.MaxInt
		}
	}
	for i = m - 1; i >= 0; i-- {
		temp++
		if temp <= a[1][i] {
			return math.MaxInt
		}
	}
	return startTime + 2*m - 1
}

func path2(m, startTime int) int {
	temp := startTime
	for i = 0; i < m; i++ {
		temp++
		if temp <= a[1][i] {
			return math.MaxInt
		}
	}
	for i = m - 1; i > 0; i-- {
		temp++
		if temp <= a[0][i] {
			return math.MaxInt
		}
	}
	return startTime + 2*m - 1
}

func path3(m, startTime int) int {
	temp := startTime
	for i = 1; i < 2*m; i++ {
		if i%4 == 0 {
			u, v = 0, i/2
		} else if i%4 == 1 {
			u, v = 1, i/2
		} else if i%4 == 2 {
			u, v = 1, i/2
		} else {
			u, v = 0, i/2
		}
		temp++
		if temp <= a[u][v] {
			return math.MaxInt
		}
	}
	return startTime + 2*m - 1
}

func isOk(m, startTime int) bool {
	k = math.MaxInt
	k = minValInt(k, path1(m, startTime))
	k = minValInt(k, path2(m, startTime))
	k = minValInt(k, path3(m, startTime))
	return k != math.MaxInt
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t, m, ans, l, r, mid int
	fmt.Fscan(in, &t)
	for ff = 1; ff <= t; ff++ {
		fmt.Fscan(in, &m)
		for i = 0; i < 2; i++ {
			for j = 0; j < m; j++ {
				fmt.Fscan(in, &a[i][j])
			}
		}
		l, r, ans = 0, 1e9+MAXN, math.MaxInt
		for l <= r {
			mid = (l + r) >> 1
			if isOk(m, mid) {
				ans = mid + 2*m - 1
				r = mid - 1
			} else {
				l = mid + 1
			}
		}
		if ff == 1920 {
			for i = 0; i < 2; i++ {
				for j = 0; j < m; j++ {
					fmt.Fprintf(out, " %d", a[i][j])
				}
				fmt.Fprintln(out)
			}
		}
		fmt.Fprintln(out, ans)
	}
}

package main

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

const MAXN int = 2e5 + 7

var i, j, u, v, k int
var a [2][MAXN]int

func minValInt(u, v int) int {
	if u < v {
		return u
	}
	return v
}

func path1(m, startTime int) int {
	temp := startTime
	for i = 1; i < m; i++ {
		temp++
		if temp <= a[0][i] {
			return math.MaxInt
		}
	}
	for i = m - 1; i >= 0; i-- {
		temp++
		if temp <= a[1][i] {
			return math.MaxInt
		}
	}
	return startTime + 2*m - 1
}

func path2(m, startTime int) int {
	temp := startTime
	for i = 0; i < m; i++ {
		temp++
		if temp <= a[1][i] {
			return math.MaxInt
		}
	}
	for i = m - 1; i > 0; i-- {
		temp++
		if temp <= a[0][i] {
			return math.MaxInt
		}
	}
	return startTime + 2*m - 1
}

func path3(m, startTime int) int {
	temp := startTime
	for i = 1; i < 2*m; i++ {
		if i%4 == 0 {
			u, v = 0, i/2
		} else if i%4 == 1 {
			u, v = 1, i/2
		} else if i%4 == 2 {
			u, v = 1, i/2
		} else {
			u, v = 0, i/2
		}
		temp++
		if temp <= a[u][v] {
			return math.MaxInt
		}
	}
	return startTime + 2*m - 1
}

func isOk(m, startTime int) bool {
	k = math.MaxInt
	k = minValInt(k, path1(m, startTime))
	k = minValInt(k, path2(m, startTime))
	k = minValInt(k, path3(m, startTime))
	return k != math.MaxInt
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t, m, ans, l, r, mid int
	fmt.Fscan(in, &t)
	for t != 0 {
		fmt.Fscan(in, &m)
		for i = 0; i < 2; i++ {
			for j = 0; j < m; j++ {
				fmt.Fscan(in, &a[i][j])
			}
		}
		l, r, ans = 0, 1e9+MAXN, math.MaxInt
		for l <= r {
			mid = (l + r) >> 1
			if isOk(m, mid) {
				ans = mid + 2*m - 1
				r = mid - 1
			} else {
				l = mid + 1
			}
		}
		fmt.Fprintln(out, ans)
		t--
	}
}

package main

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

const MAXN int = 2e5 + 7

func minValInt64(u, v int64) int64 {
	if u < v {
		return u
	}
	return v
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t, m, i, j, ans, temp, visNum, u, v int64
	var a [2][MAXN]int64
	fmt.Fscan(in, &t)
	for t != 0 {
		fmt.Fscan(in, &m)
		for i = 0; i < 2; i++ {
			for j = 0; j < m; j++ {
				fmt.Fscan(in, &a[i][j])
			}
		}
		a[0][0] = 0
		ans, temp = math.MaxInt64, 0
		for i = 1; i < m; i++ {
			temp++
			if temp <= a[0][i] {
				temp = a[0][i] + 1
			}
		}
		for i = m - 1; i >= 0; i-- {
			temp++
			if temp <= a[1][i] {
				temp = a[1][i] + 1
			}
		}
		// fmt.Fprintf(out, "temp1=%d\n", temp)
		ans, temp = minValInt64(ans, temp), 0
		for i = 0; i < m; i++ {
			temp++
			if temp <= a[1][i] {
				temp = a[1][i] + 1
			}
		}
		for i = m - 1; i > 0; i-- {
			temp++
			if temp <= a[0][i] {
				temp = a[0][i] + 1
			}
		}
		// fmt.Fprintf(out, "temp2=%d\n", temp)
		ans, temp, visNum = minValInt64(ans, temp), 0, 2*m
		for i = 2; i <= visNum; i++ {
			if i%4 == 0 {
				u, v = 0, i/2-1
			} else if i%4 == 1 {
				u, v = 0, i/2
			} else if i%4 == 2 {
				u, v = 1, i/2-1
			} else {
				u, v = 1, i/2
			}
			// fmt.Fprintf(out, "i=%d, u=%d, v=%d\n", i, u, v)
			temp++
			if temp <= a[u][v] {
				temp = a[u][v] + 1
			}
		}
		// fmt.Fprintf(out, "temp3=%d\n", temp)
		ans = minValInt64(ans, temp)
		fmt.Fprintln(out, ans)
		t--
	}
}

package main

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

const MAXN int = 2e5 + 7

func minValInt64(u, v int64) int64 {
	if u < v {
		return u
	}
	return v
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t, m, i, j, ans, temp, visNum, u, v int64
	var a [2][MAXN]int64
	fmt.Fscan(in, &t)
	for t != 0 {
		fmt.Fscan(in, &m)
		for i = 0; i < 2; i++ {
			for j = 0; j < m; j++ {
				fmt.Fscan(in, &a[i][j])
			}
		}
		ans, temp = math.MaxInt64, 0
		for i = 1; i < m; i++ {
			temp++
			if temp <= a[0][i] {
				temp = a[0][i] + 1
			}
		}
		for i = m - 1; i >= 0; i-- {
			temp++
			if temp <= a[1][i] {
				temp = a[1][i] + 1
			}
		}
		// fmt.Fprintf(out, "temp1=%d\n", temp)
		ans, temp = minValInt64(ans, temp), 0
		for i = 0; i < m; i++ {
			temp++
			if temp <= a[1][i] {
				temp = a[1][i] + 1
			}
		}
		for i = m - 1; i > 0; i-- {
			temp++
			if temp <= a[0][i] {
				temp = a[0][i] + 1
			}
		}
		// fmt.Fprintf(out, "temp2=%d\n", temp)
		ans, temp, visNum = minValInt64(ans, temp), 0, 2*m
		for i = 2; i <= visNum; i++ {
			if i%4 == 0 {
				u, v = 0, i/2-1
			} else if i%4 == 1 {
				u, v = 0, i/2
			} else if i%4 == 2 {
				u, v = 1, i/2-1
			} else {
				u, v = 1, i/2
			}
			temp++
			if temp <= a[u][v] {
				temp = a[u][v] + 1
			}
		}
		// fmt.Fprintf(out, "temp3=%d\n", temp)
		ans = minValInt64(ans, temp)
		fmt.Fprintln(out, ans)
		t--
	}
}

package main

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

var rr = bufio.NewReader(os.Stdin)
var ww = bufio.NewWriter(os.Stdout)

func slove() {
	var n int
	fmt.Fscan(rr, &n)
	a := make([][]int, 2)
	a[0] = make([]int, n)
	for i:=0;i<n;i++ {
		fmt.Fscan(rr, &a[0][i])
		a[0][i]++
	}
	a[1] = make([]int, n)
	for i:=0;i<n;i++ {
		fmt.Fscan(rr, &a[1][i])
		a[1][i]++
	}
	right0, right1 := 0, 0
	left := make([][]int, 2)
	left[0] = make([]int, n)
	left[1] = make([]int, n)
	left0, left1 := left[0][0], left[1][0]
	cur := 0
	for i := range a[0] {
		if a[0][i] > cur {
			right0 = i
		}
		cur = max(a[0][i] + 1, cur + 1)
	}
	cur = 0
	cur_i := n-1
	for i:=n-1;i>=0;i-- {
		left[0][i] = cur_i
		if a[0][i] > cur {
			cur = a[0][i]
			cur_i = i
			left0 = cur_i
		}
		cur++
	}
	a[0][0] = 0
	cur = 0
	for i := range a[1] {
		if a[1][i] > cur {
			right1 = i
		}
		cur = max(a[1][i] + 1, cur + 1)
	}
	cur = 0
	cur_i = n-1
	ans := 1 << 30
	for i:=n-1;i>=0;i-- {
		left[1][i] = cur_i
		if a[1][i] > cur {
			cur = a[1][i]
			cur_i = i
			left1 = cur_i
		}
		cur++
	}
	i, j := 0, 0
	diff := 1
	time := -1
	new_time := 0
	new_time = max(new_time + right0, a[0][right0])
	new_time += n - right0 - 1
	new_time = max(new_time + 1, a[1][n-1])
	new_time = max(new_time + n - left1 - 1, a[1][left1])
	new_time += left1
	ans = min(ans, new_time)
	for j < n {
		time = max(time+1, a[i][j])
		new_time := time
		next_i, next_j := i + diff, j
		if i == 0 {
			left0 = left[i][j]
		} else {
			left1 = left[i][j]
		}
		if next_i == 2 || next_i == -1 {
			next_i = i
			next_j = j+1
			diff = -diff
			if i == 0 {
				if j >= right0 {
					new_time += n - j - 1
				} else {
					new_time = max(new_time + right0 - j, a[0][right0])
					new_time += n - right0 - 1
				}
				new_time = max(new_time + 1, a[1][n-1])
				new_time = max(new_time + n - left1 - 1, a[1][left1])
				new_time += left1 - j - 1
				ans = min(ans, new_time)
			} else {
				if j >= right1 {
					new_time += n - j - 1
				} else {
					new_time = max(new_time + right1 - j, a[1][right1])
					new_time += n - right1 - 1
				}
				new_time = max(new_time + 1, a[0][n-1])
				new_time = max(new_time + n - left0 - 1, a[0][left0])
				new_time += left0 - j - 1
				ans = min(ans, new_time)
			}
		}
		i, j = next_i, next_j
	}
	fmt.Fprintln(ww, ans)
}

func max(a, b int) int {if a > b {return a} else {return b}}
func min(a, b int) int {if a < b {return a} else {return b}}
func main() {
	defer ww.Flush()
	var n int
	fmt.Fscan(rr, &n)
	for i:=1;i<=n;i++ {
		slove()
	}
}

package main

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

var rr = bufio.NewReader(os.Stdin)
var ww = bufio.NewWriter(os.Stdout)

func slove() {
	var n int
	fmt.Fscan(rr, &n)
	a := make([][]int, 2)
	a[0] = make([]int, n)
	for i:=0;i<n;i++ {
		fmt.Fscan(rr, &a[0][i])
		a[0][i]++
	}
	a[1] = make([]int, n)
	for i:=0;i<n;i++ {
		fmt.Fscan(rr, &a[1][i])
		a[1][i]++
	}
	right0, right1 := 0, 0
	left := make([][]int, 2)
	left[0] = make([]int, n)
	left[1] = make([]int, n)
	left0, left1 := left[0][0], left[1][0]
	cur := 0
	for i := range a[0] {
		if a[0][i] > cur {
			right0 = i
		}
		cur = max(a[0][i] + 1, cur + 1)
	}
	cur = 0
	cur_i := n-1
	for i:=n-1;i>=0;i-- {
		left[0][i] = cur_i
		if a[0][i] > cur {
			cur = a[0][i]
			cur_i = i
			left0 = cur_i
		}
		cur++
	}
	cur = 0
	for i := range a[1] {
		if a[1][i] > cur {
			right1 = i
		}
		cur = max(a[1][i] + 1, cur + 1)
	}
	cur = 0
	cur_i = n-1
	ans := 1 << 30
	for i:=n-1;i>=0;i-- {
		left[1][i] = cur_i
		if a[1][i] > cur {
			cur = a[1][i]
			cur_i = i
			left1 = cur_i
		}
		cur++
	}
	i, j := 0, 0
	diff := 1
	time := -1
	new_time := 0
	if j >= right0 {
		new_time += n - j - 1
	} else {
		new_time = max(new_time + right0 - j, a[0][right0])
		new_time += n - right0 - 1
	}
	new_time = max(new_time + 1, a[1][n-1])
	new_time = max(new_time + n - left1 - 1, a[1][left1])
	new_time += left1 - j
	ans = min(ans, new_time)
	for j < n {
		time = max(time+1, a[i][j])
		new_time := time
		next_i, next_j := i + diff, j
		if i == 0 {
			left0 = left[i][j]
		} else {
			left1 = left[i][j]
		}
		if next_i == 2 || next_i == -1 {
			next_i = i
			next_j = j+1
			diff = -diff
			if i == 0 {
				if j >= right0 {
					new_time += n - j - 1
				} else {
					new_time = max(new_time + right0 - j, a[0][right0])
					new_time += n - right0 - 1
				}
				new_time = max(new_time + 1, a[1][n-1])
				new_time = max(new_time + n - left1 - 1, a[1][left1])
				new_time += left1 - j - 1
				ans = min(ans, new_time)
			} else {
				if j >= right1 {
					new_time += n - j - 1
				} else {
					new_time = max(new_time + right1 - j, a[1][right1])
					new_time += n - right1 - 1
				}
				new_time = max(new_time + 1, a[0][n-1])
				new_time = max(new_time + n - left0 - 1, a[0][left0])
				new_time += left0 - j - 1
				ans = min(ans, new_time)
			}
		}
		i, j = next_i, next_j
	}
	fmt.Fprintln(ww, ans)
}

func max(a, b int) int {if a > b {return a} else {return b}}
func min(a, b int) int {if a < b {return a} else {return b}}
func main() {
	defer ww.Flush()
	var n int
	fmt.Fscan(rr, &n)
	for i:=1;i<=n;i++ {
		slove()
	}
}


package main

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

// A. Square Counting

func solve() {

	var n, s, c int64

	scanf("%d %d\n", &n, &s)

	if n > 0 {
		c = s / (n * n)
	}

	println(c)
}

func main() {

	var t int

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

	for t > 0 {
		solve()
		t--
	}

	writer.Flush()
}

// Constants

const (
	B  = 1e9
	BB = 1e18
)

// Data structures

type Pair struct {
	First, Second int
}

type Pairs []Pair

func (p Pairs) Len() int           { return len(p) }
func (p Pairs) Less(i, j int) bool { return p[i].First < p[j].First }
func (p Pairs) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }

// Util functions

func tos(n int) string {
	return strconv.Itoa(n)
}

func toi(s string) int {
	// ignore error
	n, _ := strconv.Atoi(s)

	return n
}

func pow(b, n uint) uint {

	if n == 0 {
		return 1
	}

	var a uint = 1

	for n >= 1 {
		a = b * a
		n--
	}

	return a
}

func flip(b uint) uint {

	if b == 0 {
		return 1
	}

	return 0
}

func bin(dec uint) []uint {

	bin := []uint{}

	for dec > 0 {
		if dec%2 != 0 {
			bin = append(bin, 1)
		} else {
			bin = append(bin, 0)
		}

		dec = dec / 2
	}

	for i, j := 0, len(bin)-1; i < j; i, j = i+1, j-1 {
		bin[i], bin[j] = bin[j], bin[i]
	}

	return bin
}

func dec(b []uint) uint {

	var dec uint = 0

	for i, v := range b {
		dec += (2 << i) * v
	}

	return dec
}

// Use buffering when reading/writing data as the default fmt.Scanx/fmt.Printx
// functions doesn't.

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

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

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

func println(f ...interface{}) {
	fmt.Fprintln(writer, f...)
}

package main

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

func solve(s int64, n int64) int64 {
	return s / (n * n)
}

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

	var T int
	var s, n int64
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &s)
		Fprintln(out, solve(s, n))
	}
}

package main

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

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

func main() {
	defer out.Flush()
	var t, n, s int64
	for fmt.Fscan(in, &t); t > 0; t-- {
		fmt.Fscan(in, &n, &s)
		fmt.Fprintln(out, s/(n*n))
	}
}

package main

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

type CF1646A struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1646A) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1646A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1646A) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1646A) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1646A) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1646A
Date: 5/03/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1646/A
Problem: CF1646A
**/
func (in *CF1646A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n, s := in.NextInt64(), in.NextInt64()
		dn := n * n
		if s == 0 {
			fmt.Println(0)
		}else {
			sol := int64(math.Floor(float64(s / dn)))
			fmt.Println(sol)
		}

	}
}

func NewCF1646A(r *bufio.Reader) *CF1646A {
	return &CF1646A{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1646A(bufio.NewReader(os.Stdin)).Run()
}

package main

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

func solve(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var T int
	var s, n int64
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &s)
		Fprintln(out, s/(n*n))
	}
}

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

package main

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

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

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

		fmt.Fprintln(out, s/(n*n))
	}
}

package main
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var n,s int
		fmt.Fscan(in,&n,&s)
		fmt.Fprintln(out,s/(n*n))
	}
}

//nolint
package main

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

//{
var reader = bufio.NewReader(os.Stdin)
var 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 print(a ...interface{})            { fmt.Fprint(writer, a...) }
func println(a ...interface{})          { fmt.Fprintln(writer, a...) }
func scan(a ...interface{})             { fmt.Fscan(reader, a...) }

func main() {
	defer writer.Flush()

	Main()
}

func Main() {
	var t int
	scan(&t)
	for ; t > 0; t-- {
		Solve()
	}
}

//}

func Solve() {
	var n, s int64
	scan(&n, &s)
	fmt.Println(s / (n * n))
}

package main

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

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, n, s := 0, int64(0), int64(0)
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &n, &s)
		fmt.Fprintln(w, s/(n*n))
	}
	w.Flush()
}

func main() {
	solve()
}

package main

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

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readInt64(in *bufio.Reader) int64 {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.ParseInt(nStr, 10, 64)
	return n
}

func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

func readArrInt64(in *bufio.Reader) []int64 {
	numbs := readLineNumbs(in)
	arr := make([]int64, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.ParseInt(n, 10, 64)
		arr[i] = val
	}
	return arr
}

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

	defer out.Flush()

	n := readInt(in)

	for i := 0; i < n; i++ {
		ins := readArrInt64(in)

		res := Solve(ins[0], ins[1])

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

func Solve(n, s int64) int64 {
	return s / (n * n)
}

package main

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

var in, out = bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)

func main() {
	defer out.Flush()

	var tc int
	fmt.Fscan(in, &tc)
	for ; tc > 0; tc-- {
		var n, s int64
		fmt.Fscan(in, &n, &s)
		fmt.Fprintln(out, s/(n*n))
	}
}

package main

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

/*
5
4 2
5 6
5 4
1 8
5 74
---
0
0
0
8
2
======
1

*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n, s int64
	scanf("%d %d\n", &n, &s)

	sq := n * n

	ans := s / sq

	fmt.Fprintln(writer, ans)
}

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	scanf("%d\n", &t)

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

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

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

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int) int {
	quot := num / div
	if num%div != 0 {
		quot++
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int64) int64 {
	minimum := nums[0]
	for _, num := range nums {
		if num < minimum {
			minimum = num
		}
	}
	return minimum
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) (max, idx int) {
	for i, num := range nums {
		if num > nums[idx] {
			idx = i
		}
	}

	return nums[idx], idx
}

//goland:noinspection GoUnusedFunction
func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := 5; i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

package main

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

func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewScanner(_r)
	in.Split(bufio.ScanWords)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	read := func() (x int64) {
		in.Scan()
		for _, b := range in.Bytes() {
			x = x*10 + int64(b&15)
		}
		return
	}
	for t := read(); t > 0; t-- {
		n := read()
		s := read()
		Fprintln(out, s/(n*n))
	}
}

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

package main

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

func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewScanner(_r)
	in.Split(bufio.ScanWords)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	read := func() (x int) {
		in.Scan()
		for _, b := range in.Bytes() {
			x = x*10 + int(b&15)
		}
		return
	}
	for t := read(); t > 0; t-- {
		n := read()
		s := read()
		res := 0
		if t == 20000 {
			Fprintln(out, n, s)
		}
		for i := 0; i <= n; i++ {
			s -= n * n
			if s >= 0 {
				res++
				if i == n && s == 0 {
					Fprintln(out, res)
					break
				}
			} else {
				Fprintln(out, res)
				break
			}
		}

	}
}

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

package main

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

func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewScanner(_r)
	in.Split(bufio.ScanWords)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	read := func() (x int) {
		in.Scan()
		for _, b := range in.Bytes() {
			x = x*10 + int(b&15)
		}
		return
	}
	for t := read(); t > 0; t-- {
		n := read()
		s := read()
		res := 0
		for i := 0; i <= n; i++ {
			s -= n * n
			if s >= 0 {
				res++
				if i == n && s == 0 {
					Fprintln(out, res)
					break
				}
			} else {
				Fprintln(out, res)
				break
			}
		}

	}
}

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

package main

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

func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewScanner(_r)
	in.Split(bufio.ScanWords)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	read := func() (x int) {
		in.Scan()
		for _, b := range in.Bytes() {
			x = x*10 + int(b&15)
		}
		return
	}
	for t := read(); t > 0; t-- {
		n, s := read(), read()
		res := 0
		for i := 0; i <= n; i++ {
			s -= n * n
			if s >= 0 {
				res++
				if i == n && s == 0 {
					Fprintln(out, res)
					break
				}
			} else {
				Fprintln(out, res)
				break
			}
		}

	}
}

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

package main

import (
	"fmt"
)

func solve(n int64, s int64) int64 {
	n2 := n * n
	count := s / n2
	return count
}

func main() {
	var t int64
	var i int64
	var n int64
	var s int64

	fmt.Scanln(&t)
	for i = 0; i < t; i++ {
		fmt.Scan(&n)
		fmt.Scan(&s)
		fmt.Println(s)
	}
}

package main

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

// A. Square Counting

func solve() {

	var n, s float64

	scanf("%f %f\n", &n, &s)

	aux := n * n
	c := int64(math.Floor(s / aux))

	println(c)
}

func main() {

	var t int

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

	for t > 0 {
		solve()
		t--
	}

	writer.Flush()
}

// Constants

const (
	B  = 1e9
	BB = 1e18
)

// Data structures

type Pair struct {
	First, Second int
}

type Pairs []Pair

func (p Pairs) Len() int           { return len(p) }
func (p Pairs) Less(i, j int) bool { return p[i].First < p[j].First }
func (p Pairs) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }

// Util functions

func tos(n int) string {
	return strconv.Itoa(n)
}

func toi(s string) int {
	// ignore error
	n, _ := strconv.Atoi(s)

	return n
}

func pow(b, n uint) uint {

	if n == 0 {
		return 1
	}

	var a uint = 1

	for n >= 1 {
		a = b * a
		n--
	}

	return a
}

func flip(b uint) uint {

	if b == 0 {
		return 1
	}

	return 0
}

func bin(dec uint) []uint {

	bin := []uint{}

	for dec > 0 {
		if dec%2 != 0 {
			bin = append(bin, 1)
		} else {
			bin = append(bin, 0)
		}

		dec = dec / 2
	}

	for i, j := 0, len(bin)-1; i < j; i, j = i+1, j-1 {
		bin[i], bin[j] = bin[j], bin[i]
	}

	return bin
}

func dec(b []uint) uint {

	var dec uint = 0

	for i, v := range b {
		dec += (2 << i) * v
	}

	return dec
}

// Use buffering when reading/writing data as the default fmt.Scanx/fmt.Printx
// functions doesn't.

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

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

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

func println(f ...interface{}) {
	fmt.Fprintln(writer, f...)
}

package main

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

//

func solve() {

	var n, s int64

	scanf("%d %d\n", &n, &s)

	n = n*n

	if s % n == 0 {
		println(s/n)
	} else {
		println(0)
	}
}

func main() {

	var t int

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

	for t > 0 {
		solve()
		t--
	}

	writer.Flush()
}

// Constants

const (
	B = 1e9
	BB = 1e18
)

// Data structures

type Pair struct {
	First, Second int
}

type Pairs []Pair

func (p Pairs) Len() int           { return len(p) }
func (p Pairs) Less(i, j int) bool { return p[i].First < p[j].First }
func (p Pairs) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }

// Util functions

func tos(n int) string {
	return strconv.Itoa(n)
}

func toi(s string) int {
	// ignore error
	n, _ := strconv.Atoi(s)

	return n
}

func pow(b, n uint) uint {

	if n == 0 {
		return 1
	}

	var a uint = 1

	for n >= 1 {
		a = b * a
		n--
	}

	return a
}

func flip(b uint) uint {

	if b == 0 {
		return 1
	}

	return 0
}

func bin(dec uint) []uint {

	bin := []uint{}

	for dec > 0 {
		if dec%2 != 0 {
			bin = append(bin, 1)
		} else {
			bin = append(bin, 0)
		}

		dec = dec / 2
	}

	for i, j := 0, len(bin)-1; i < j; i, j = i+1, j-1 {
        bin[i], bin[j] = bin[j], bin[i]
    }

	return bin
}

func dec(b []uint) uint {

	var dec uint = 0

	for i, v := range b {
		dec += (2 << i) * v
	}

	return dec
}

// Use buffering when reading/writing data as the default fmt.Scanx/fmt.Printx
// functions doesn't.

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

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

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

func println(f ...interface{}) {
	fmt.Fprintln(writer, f...)
}

package main

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

type CF1646A struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1646A) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1646A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1646A) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1646A) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1646A) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1646A
Date: 5/03/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1646/A
Problem: CF1646A
**/
func (in *CF1646A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n, s := in.NextInt64(), in.NextInt64()
		dn := n * n
		if s == 0 {
			fmt.Println(0)
		}else {
			sol := math.Floor(float64(s / dn))
			fmt.Println(sol)
		}

	}
}

func NewCF1646A(r *bufio.Reader) *CF1646A {
	return &CF1646A{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1646A(bufio.NewReader(os.Stdin)).Run()
}

package main

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

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

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

		var tot int64
		for s > 0 && n*n <= s {
			d := s / (n * n)
			tot += d
			s -= d * (n * n)
			n--
		}
		fmt.Fprintln(out, tot)
	}
}


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

package main

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

var (
	t int

	x int
)

func main() {
	defer stdout.Flush()

	t = readi()
	for tc := 0; tc < t; tc++ {
		x = readi()

		solve()
	}
}

func solve() {
	y := 0
	for k := 1; k <= 2000; k++ {
		if k*(k+1)/2 >= x {
			y = k
			break
		}
	}

	X := y * (y + 1) / 2

	if X == x {
		printf("%d\n", y)
		return
	} else {
		for i := 1; i <= y; i++ {
			if X-(i+1) == x {
				printf("%d\n", y)
				return
			}
		}

		b := X - x
		ans := y + b
		printf("%d\n", ans)
	}
}

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

/********** common constants **********/

const (
	MOD = 1000000000 + 7
	// MOD          = 998244353
	ALPH_N  = 26
	INF_I64 = math.MaxInt64
	INF_B60 = 1 << 60
	INF_I32 = math.MaxInt32
	INF_B30 = 1 << 30
	NIL     = -1
	EPS     = 1e-10
)

/********** bufio setting **********/

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

// mod can calculate a right residual whether value is positive or negative.
func mod(val, m int) int {
	res := val % m
	if res < 0 {
		res += m
	}
	return res
}

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

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

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

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

// sum returns multiple integers sum.
func sum(integers ...int) int {
	var s int
	s = 0

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

	return s
}

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

// pow is integer version of math.Pow
// pow calculate a power by Binary Power (二分累乗法(O(log e))).
func pow(a, e int) int {
	if a < 0 || e < 0 {
		panic(errors.New("[argument error]: PowInt does not accept negative integers"))
	}

	if e == 0 {
		return 1
	}

	if e%2 == 0 {
		halfE := e / 2
		half := pow(a, halfE)
		return half * half
	}

	return a * pow(a, e-1)
}

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

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

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

//str := reads()
//i := readi()
//X := readis(n)
//S := readrs()
//a := readf()
//A := readfs(n)

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

/*********** Input ***********/

var (
	// reads returns a WORD string.
	reads  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()
	}
}

// readi returns an integer.
func readi() int {
	return int(_readInt64())
}
func readi2() (int, int) {
	return int(_readInt64()), int(_readInt64())
}
func readi3() (int, int, int) {
	return int(_readInt64()), int(_readInt64()), int(_readInt64())
}
func readi4() (int, int, int, int) {
	return int(_readInt64()), int(_readInt64()), int(_readInt64()), int(_readInt64())
}

// readll returns as integer as int64.
func readll() int64 {
	return _readInt64()
}
func readll2() (int64, int64) {
	return _readInt64(), _readInt64()
}
func readll3() (int64, int64, int64) {
	return _readInt64(), _readInt64(), _readInt64()
}
func readll4() (int64, int64, int64, int64) {
	return _readInt64(), _readInt64(), _readInt64(), _readInt64()
}

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

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

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

// readf returns an float64.
func readf() float64 {
	return float64(_readFloat64())
}

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

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

// readrs returns a rune slice.
func readrs() []rune {
	return []rune(reads())
}

/*********** Output ***********/

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

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

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

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

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

package main

import "fmt"

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

func calcNumOfJumps(end int) int {
	if end == 1 {
		return 1
	}
	cnt := 0
	start := 0
	arr := []int{}
	for i := 1; start < end; i++ {
		start += i
		cnt++
		arr = append(arr, i)
	}
	if start == end {
		return cnt
	}
	for i := 0; i < len(arr); i++ {
		t := arr[i]
		arr[i] = -1
		if calcSum(arr) == end {
			return len(arr)
		}
		arr[i] = t
	}
	return cnt + start - end
}

func calcSum(arr []int) int {
	sum := 0
	for i := 0; i < len(arr); i++ {
		sum += arr[i]
	}
	return sum
}

package main

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

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

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

	for ; t > 0; t-- {
		fmt.Fscan(in, &x)
		k := int(math.Ceil((math.Sqrt(float64(8*x+1)) - 1) / 2))
		if k*(k+1)/2-x == 1 {
			k++
		}
		fmt.Println(k)
	}
}

package main

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

var reader = bufio.NewReader(os.Stdin)
var out bytes.Buffer

func main() {
	var t int
	fmt.Fscan(reader, &t)

	for i := 0; i < t; i++ {
		var x int
		fmt.Fscan(reader, &x)

		var sum, j int
		for j, sum = 1, 0; sum < x; j++ {
			sum += j
		}

		j--

		if x == sum-1 {
			fmt.Fprintln(&out, j+1)
		} else {
			fmt.Fprintln(&out, j)
		}
	}

	fmt.Println(out.String())
}