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

		a := make([]int64, n)
		b := make([]int64, n)
		for i := range a {
			fmt.Fscan(in, &a[i])
		}
		for i := range b {
			fmt.Fscan(in, &b[i])
		}

		m := n - 1
		cands1 := [3]int{0, m, bestCand(b, a[0])}
		cands2 := [3]int{0, m, bestCand(b, a[m])}

		var min int64 = 5_000_000_000
		for _, v := range cands1 {
			for _, u := range cands2 {
				cur := abs(a[0]-b[v]) + abs(a[m]-b[u])

				if v != 0 && u != 0 {
					cur += abs(b[0] - a[bestCand(a, b[0])])
				}
				if v != m && u != m {
					cur += abs(b[m] - a[bestCand(a, b[m])])
				}

				min = Min(cur, min)
			}
		}

		res := min
		fmt.Fprintln(out, res)
	}
}

func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}
func Min(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func bestCand(ar []int64, x int64) int {
	best := int64(5_000_000_000)
	pos := 0
	for i := range ar {
		if t := abs(x - ar[i]); t < best {
			best = t
			pos = i
		}
	}
	return pos
}

package main

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

// github.com/EndlessCheng/codeforces-go
func f(a []int64, v int64) int64 {
	res := int64(1e9)
	for _, w := range a {
		res = min(res, abs(w-v))
	}
	return res
}

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

	var T, n int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		a := make([]int64, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		b := make([]int64, n)
		for i := range b {
			Fscan(in, &b[i])
		}
		Fprintln(out, min(
			abs(a[0]-b[0])+abs(a[n-1]-b[n-1]),
			abs(a[0]-b[n-1])+abs(a[n-1]-b[0]),
			abs(a[0]-b[0])+f(a, b[n-1])+f(b, a[n-1]),
			abs(a[0]-b[n-1])+f(a, b[0])+f(b, a[n-1]),
			abs(a[n-1]-b[0])+f(a, b[n-1])+f(b, a[0]),
			abs(a[n-1]-b[n-1])+f(a, b[0])+f(b, a[0]),
			f(a, b[0])+f(a, b[n-1])+f(b, a[0])+f(b, a[n-1]),
		))
	}
}

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

func min(a ...int64) int64 {
	ans := a[0]
	for _, val := range a[1:] {
		if val < ans {
			ans = val
		}
	}
	return ans
}

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

package main

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

var in = bufio.NewReader(os.Stdin)

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

func findMinDiffIdx(x int, ys []int) int {
	minDiff := abs(x - ys[0])
	minDiffIdx := 0
	for i, y := range ys {
		if abs(x-y) < minDiff {
			minDiff = abs(x - y)
			minDiffIdx = i
		}
	}
	return minDiffIdx
}

type line struct {
	ai, bi int
}

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

	a := make([]int, n)
	for i := range a {
		fmt.Fscan(in, &a[i])
	}
	b := make([]int, n)
	for i := range b {
		fmt.Fscan(in, &b[i])
	}

	aLefts := []line{
		{0, 0},
		{0, n - 1},
		{0, findMinDiffIdx(a[0], b)},
	}
	aRights := []line{
		{n - 1, 0},
		{n - 1, n - 1},
		{n - 1, findMinDiffIdx(a[n-1], b)},
	}
	bLefts := []line{
		{0, 0},
		{n - 1, 0},
		{findMinDiffIdx(b[0], a), 0},
	}
	bRights := []line{
		{0, n - 1},
		{n - 1, n - 1},
		{findMinDiffIdx(b[n-1], a), n - 1},
	}

	var minSum int64 = 4_000_000_000
	for _, aL := range aLefts {
		for _, aR := range aRights {
			for _, bL := range bLefts {
				for _, bR := range bRights {
					lines := make(map[line]struct{})
					lines[aL] = struct{}{}
					lines[aR] = struct{}{}
					lines[bL] = struct{}{}
					lines[bR] = struct{}{}

					var sum int64 = 0
					for line := range lines {
						sum += int64(abs(a[line.ai] - b[line.bi]))
					}
					if sum < minSum {
						minSum = sum
					}
				}
			}
		}
	}
	fmt.Println(minSum)
}

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

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

package main

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

const mod = 998244353
const mx = int64(1e18 + 10)

func getIdx(arr []int64,val int64)int64{
	idx := int64(-1)
	cur := int64(1e9)
	for i,v := range arr{
		if abs(v - val) < cur{
			idx = int64(i)
			cur = abs(v - val)
		}
	}
	return idx
}

func solve(in *bufio.Reader, out *bufio.Writer) {
	defer out.Flush()
	var n int64
	fmt.Fscan(in,&n)
	a := make([]int64,n)
	b := make([]int64,n)
	for i := int64(0); i < n; i++ {
		fmt.Fscan(in,&a[i])
	}
	for i := int64(0); i < n; i++ {
		fmt.Fscan(in,&b[i])
	}
	var1 := []int64{0,getIdx(b,a[0]),n-1}
	var2 := []int64{0,getIdx(b,a[n - 1]),n-1}
	ans := mx
	for _,i := range var1{
		for _,j := range var2{
			cur := abs(a[0] - b[i]) + abs(a[n - 1] - b[j])
			if i != 0 && j != 0{
				cur += abs(b[0] - a[getIdx(a,b[0])])
			}
			if i != n - 1 && j != n - 1{
				cur += abs(b[n - 1] - a[getIdx(a,b[n - 1])])
			}
			ans = min(ans,cur)
		}
	}
	fmt.Fprintln(out,ans)
}

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

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

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

package main

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

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

func solve(cin *scanner, cout *writer, cerr *writer) {
	n := cin.NextInt()
	a, b := cin.NextInt64s(n), cin.NextInt64s(n)
	ll, lr := abs(a[0]-b[0]), abs(a[0]-b[n-1])
	rl, rr := abs(a[n-1]-b[0]), abs(a[n-1]-b[n-1])
	lc, rc := ll, rr
	cl, cr := ll, rr
	for c := 0; c < n; c++ {
		if abs(a[0]-b[c]) < lc {
			lc = abs(a[0] - b[c])
		}
		if abs(a[n-1]-b[c]) < rc {
			rc = abs(a[n-1] - b[c])
		}
		if abs(a[c]-b[0]) < cl {
			cl = abs(a[c] - b[0])
		}
		if abs(a[c]-b[n-1]) < cr {
			cr = abs(a[c] - b[n-1])
		}
	}
	ans := ll + rr
	if lr+rl < ans {
		ans = lr + rl
	}
	if ll+rc+cr < ans {
		ans = ll + rc + cr
	}
	if lr+rc+cl < ans {
		ans = lr + rc + cl
	}
	if rl+lc+cr < ans {
		ans = rl + lc + cr
	}
	if rr+lc+cl < ans {
		ans = rr + lc + cl
	}
	if lc+rc+cl+cr < ans {
		ans = lc + rc + cl + cr
	}
	cout.Println(ans)
}

func main() {
	cin := NewScanner(os.Stdin)
	cout, cerr := NewWriter(os.Stdout), NewWriter(os.Stderr)
	defer cout.Flush()
	defer cerr.Flush()
	t := cin.NextInt()
	// t := 1
	for ti := 0; ti < t; ti++ {
		solve(cin, cout, cerr)
	}
}

// Template Bolierplate {{{
type scanner struct {
	*bufio.Scanner
}

func NewScanner(r io.Reader) *scanner {
	s := bufio.NewScanner(r)
	s.Split(bufio.ScanWords)
	s.Buffer(nil, 1024*1024)
	return &scanner{s}
}

func (i *scanner) Next() string {
	i.Scan()
	return i.Text()
}

func (i *scanner) Nexts(n int) []string {
	res := make([]string, n)
	for j := range res {
		res[j] = i.Next()
	}
	return res
}

func (i *scanner) NextInt() int {
	v, _ := strconv.Atoi(i.Next())
	return v
}

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

func (i *scanner) NextInts(n int) []int {
	res := make([]int, n)
	for j := range res {
		res[j] = i.NextInt()
	}
	return res
}

func (i *scanner) NextInt64s(n int) []int64 {
	res := make([]int64, n)
	for j := range res {
		res[j] = i.NextInt64()
	}
	return res
}

type writer struct {
	w *bufio.Writer
}

func NewWriter(w io.Writer) *writer {
	return &writer{bufio.NewWriter(w)}
}

func (i *writer) Print(a ...interface{}) {
	fmt.Fprint(i.w, a...)
}

func (i *writer) Println(a ...interface{}) {
	fmt.Fprintln(i.w, a...)
}

func (i *writer) Printf(format string, a ...interface{}) {
	fmt.Fprintf(i.w, format, a...)
}

func (i *writer) Flush() {
	i.w.Flush()
}

// }}}

// vim: fdm=marker

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

		a := make([]int64, n)
		b := make([]int64, n)
		for i := range a {
			fmt.Fscan(in, &a[i])
		}
		for i := range b {
			fmt.Fscan(in, &b[i])
		}

		m := n - 1
		cands1 := [3]int{0, m, bestCand(b, a[0])}
		cands2 := [3]int{0, m, bestCand(b, a[m])}

		var min int64 = 5_000_000_000
		for _, v := range cands1 {
			for _, u := range cands2 {
				cur := abs(a[0]-b[v]) + abs(a[m]-b[u])

				if v > 0 && u > 0 {
					cur += abs(b[0] - a[bestCand(a, b[0])])
				}
				if v < m && u < m {
					cur += abs(b[m] - a[bestCand(a, b[m])])
				}

				min = Min(cur, min)
			}
		}

		res := min
		fmt.Fprintln(out, res)
	}
}

func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}
func Min(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func bestCand(ar []int64, x int64) int {
	best := int64(5_000_000_000)
	pos := 0
	for i := range ar {
		if t := abs(x - ar[i]); t < best {
			best = t
			pos = i
		}
	}
	return pos
}

package main

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

/*
1
6
5 6 4 1 6 1
2 4 1 5 4 1
---
1 = min(3, 0+1)
=====
1
5
9 5 5 5 1
1 5 5 5 9
---
0
=======
1
4
1 5 1 1
9 9 3 9
---
14 = min(8+8, 2+4+8)
*/

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

	a := make([]int, n)

	for i := range a {
		scanf("%d", &a[i])
	}
	scanf("\n")

	b := make([]int, n)

	for i := range b {
		scanf("%d", &b[i])
	}
	scanf("\n")

	findBestCandidateIdx := func(cur int, nums []int) int {
		bestIdx := 0

		for i, num := range nums {
			if abs(num-cur) < abs(nums[bestIdx]-cur) {
				bestIdx = i
			}
		}

		return bestIdx
	}

	fstCnds := []int{0, findBestCandidateIdx(a[0], b), n - 1}
	lstCnds := []int{0, findBestCandidateIdx(a[n-1], b), n - 1}

	best := int64(maxInt)

	for _, i := range fstCnds {
		for _, j := range lstCnds {
			ans := int64(abs(a[0]-b[i]) + abs(a[n-1]-b[j]))

			if i > 0 && j > 0 {
				ans += int64(abs(b[0] - a[findBestCandidateIdx(b[0], a)]))
			}

			if i < n-1 && j < n-1 {
				ans += int64(abs(b[n-1] - a[findBestCandidateIdx(b[n-1], a)]))
			}

			best = min(best, ans)
		}
	}

	fmt.Fprintln(writer, best)
}

//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 int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int64) int64 {
	return nums[minIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func minIdx(nums ...int64) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	return nums[maxIdx(nums...)]
}

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

	return maxNumIdx
}

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

//goland:noinspection GoUnusedFunction
func findSet(parent []int, v int) int {
	if v == parent[v] {
		return v
	}

	parent[v] = findSet(parent, parent[v])

	return parent[v]
}

//goland:noinspection GoUnusedFunction
func unionSets(parent []int, v, u int) {
	v = findSet(parent, v)
	u = findSet(parent, u)

	if v != u {
		parent[v] = u
	}
}

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

		a := make([]int64, n)
		b := make([]int64, n)
		for i := range a {
			fmt.Fscan(in, &a[i])
		}
		for i := range b {
			fmt.Fscan(in, &b[i])
		}

		corners := [4]bool{false, false, false, false}
		res := int64(0)

		az := 0
		for i := range b {
			if abs(a[0]-b[i]) < abs(a[0]-b[az]) {
				az = i
			}
		}
		res += abs(a[0] - b[az])
		corners[0] = true
		if az == 0 {
			corners[2] = true
		} else if az == n-1 {
			corners[3] = true
		}

		ae := 0
		for i := range b {
			if abs(a[n-1]-b[i]) < abs(a[n-1]-b[ae]) {
				ae = i
			}
		}
		res += abs(a[n-1] - b[ae])
		corners[1] = true
		if ae == 0 {
			corners[2] = true
		} else if ae == n-1 {
			corners[3] = true
		}

		if !corners[2] {
			bz := 0
			for i := range a {
				if abs(b[0]-a[i]) < abs(b[0]-a[bz]) {
					bz = i
				}
			}
			res += abs(b[0] - a[bz])
			corners[2] = true
		}
		if !corners[3] {
			be := 0
			for i := range a {
				if abs(b[n-1]-a[i]) < abs(b[n-1]-a[be]) {
					be = i
				}
			}
			res += abs(b[n-1] - a[be])
			corners[3] = true
		}

		fmt.Fprintln(out, res)
	}
}

func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}
func min(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

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

		a := make([]int64, n)
		b := make([]int64, n)
		for i := range a {
			fmt.Fscan(in, &a[i])
		}
		for i := range b {
			fmt.Fscan(in, &b[i])
		}

		simple := min(
			abs(a[0]-b[0])+abs(a[n-1]-b[n-1]),
			abs(a[0]-b[n-1])+abs(a[n-1]-b[0]),
		)

		corners := [4]bool{false, false, false, false}
		res := int64(0)

		az := 0
		for i := range b {
			if abs(a[0]-b[i]) < abs(a[0]-b[az]) {
				az = i
			}
		}
		res += abs(a[0] - b[az])
		corners[0] = true
		if az == 0 {
			corners[2] = true
		} else if az == n-1 {
			corners[3] = true
		}

		ae := 0
		for i := range b {
			if abs(a[n-1]-b[i]) < abs(a[n-1]-b[ae]) {
				ae = i
			}
		}
		res += abs(a[n-1] - b[ae])
		corners[1] = true
		if ae == 0 {
			corners[2] = true
		} else if ae == n-1 {
			corners[3] = true
		}

		if !corners[2] {
			bz := 0
			for i := range a {
				if abs(b[0]-a[i]) < abs(b[0]-a[bz]) {
					bz = i
				}
			}
			res += abs(b[0] - a[bz])
			corners[2] = true
		}
		if !corners[3] {
			be := 0
			for i := range a {
				if abs(b[n-1]-a[i]) < abs(b[n-1]-a[be]) {
					be = i
				}
			}
			res += abs(b[n-1] - a[be])
			corners[3] = true
		}

		res = min(simple, res)
		fmt.Fprintln(out, res)
	}
}

func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}
func min(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

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

		a := make([]int64, n)
		b := make([]int64, n)
		for i := range a {
			fmt.Fscan(in, &a[i])
		}
		for i := range b {
			fmt.Fscan(in, &b[i])
		}

		simple := min(
			abs(a[0]-b[0])+abs(a[n-1]-b[n-1]),
			abs(a[0]-b[n-1])+abs(a[n-1]-b[0]),
		)

		az := 0
		for i := range b {
			if abs(a[0]-b[i]) < abs(a[0]-b[az]) {
				az = i
			}
		}
		ae := 0
		for i := range b {
			if abs(a[n-1]-b[i]) < abs(a[n-1]-b[ae]) {
				ae = i
			}
		}
		bz := 0
		for i := range b {
			if abs(b[0]-a[i]) < abs(b[0]-a[bz]) {
				bz = i
			}
		}
		be := 0
		for i := range b {
			if abs(b[n-1]-a[i]) < abs(b[n-1]-a[be]) {
				be = i
			}
		}

		res := abs(a[0]-b[az]) + abs(a[n-1]-b[ae]) +
			abs(b[0]-a[bz]) + abs(b[n-1]-a[be])

		if az == bz && az == 0 {
			res -= abs(a[0] - b[az])
		}
		if az == n-1 && be == 0 {
			res -= abs(a[0] - b[az])
		}
		if ae == be && ae == n-1 {
			res -= abs(a[n-1] - b[ae])
		}
		if ae == 0 && bz == n-1 {
			res -= abs(a[n-1] - b[ae])
		}

		res = min(simple, res)
		fmt.Fprintln(out, res)
	}
}

func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}
func min(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

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

		a := make([]int, n)
		b := make([]int, n)
		for i := range a {
			fmt.Fscan(in, &a[i])
		}
		for i := range b {
			fmt.Fscan(in, &b[i])
		}

		simple := min(
			abs(a[0]-b[0])+abs(a[n-1]-b[n-1]),
			abs(a[0]-b[n-1])+abs(a[n-1]-b[0]),
		)

		az := 0
		for i := range b {
			if abs(a[0]-b[i]) < abs(a[0]-b[az]) {
				az = i
			}
		}
		ae := 0
		for i := range b {
			if abs(a[n-1]-b[i]) < abs(a[n-1]-b[ae]) {
				ae = i
			}
		}
		bz := 0
		for i := range b {
			if abs(b[0]-a[i]) < abs(b[0]-a[bz]) {
				bz = i
			}
		}
		be := 0
		for i := range b {
			if abs(b[n-1]-a[i]) < abs(b[n-1]-a[be]) {
				be = i
			}
		}

		res := abs(a[0]-b[az]) + abs(a[n-1]-b[ae]) +
			abs(b[0]-a[bz]) + abs(b[n-1]-a[be])

		if az == bz && az == 0 {
			res -= abs(a[0] - b[az])
		}
		if az == n-1 && be == 0 {
			res -= abs(a[0] - b[az])
		}
		if ae == be && ae == n-1 {
			res -= abs(a[n-1] - b[ae])
		}
		if ae == 0 && bz == n-1 {
			res -= abs(a[n-1] - b[ae])
		}

		res = min(simple, res)
		fmt.Fprintln(out, res)
	}
}

func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

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-- {
		var n int
		fmt.Fscan(in, &n)
		ara := make([]int64, n)
		arb := make([]int64, n)
		for i := range ara {
			fmt.Fscan(in, &ara[i])
		}
		for i := range arb {
			fmt.Fscan(in, &arb[i])
		}

		l := n - 1
		mins := []int64{
			1_000_000_001, 1_000_000_001, 1_000_000_001, 1_000_000_001}
		for i := 1; i < l; i++ {
			if t := abs(ara[0] - arb[i]); mins[0] > t {
				mins[0] = t
			}
		}
		for i := 1; i < l; i++ {
			if t := abs(ara[l] - arb[i]); mins[1] > t {
				mins[1] = t
			}
		}
		for i := 1; i < l; i++ {
			if t := abs(arb[0] - ara[i]); mins[2] > t {
				mins[2] = t
			}
		}
		for i := 1; i < l; i++ {
			if t := abs(arb[l] - ara[i]); mins[3] > t {
				mins[3] = t
			}
		}

		easy := abs(ara[0]-arb[0]) + abs(ara[l]-arb[l])
		semieasy := abs(ara[0]-arb[l]) + abs(ara[l]-arb[0])
		hard := mins[0] + mins[1] + mins[2] + mins[3]
		stoopid := []int64{
			abs(arb[0]-ara[l]) + abs(ara[l]-arb[l]) + mins[0],
			abs(ara[0]-arb[0]) + abs(ara[0]-arb[l]) + mins[1],
			abs(ara[0]-arb[l]) + abs(arb[l]-ara[l]) + mins[2],
			abs(ara[0]-arb[0]) + abs(arb[0]-ara[l]) + mins[3],
		}
		omegaHard := min(
			min(stoopid[0], stoopid[1]), min(stoopid[2], stoopid[3]))

		fmt.Fprintln(out, min(min(omegaHard, hard), min(semieasy, easy)))

	}
}

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

func min(x, y int64) int64 {
	if x < y {
		return x
	}
	return y
}

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-- {
		var n int
		fmt.Fscan(in, &n)
		ara := make([]int64, n)
		arb := make([]int64, n)
		for i := range ara {
			fmt.Fscan(in, &ara[i])
		}
		for i := range arb {
			fmt.Fscan(in, &arb[i])
		}

		l := n - 1
		mins := []int64{
			1_000_000_001, 1_000_000_001, 1_000_000_001, 1_000_000_001}
		for i := 1; i < l; i++ {
			if t := abs(ara[0] - arb[i]); mins[0] > t {
				mins[0] = t
			}
		}
		for i := 1; i < l; i++ {
			if t := abs(ara[l] - arb[i]); mins[1] > t {
				mins[1] = t
			}
		}
		for i := 1; i < l; i++ {
			if t := abs(arb[0] - ara[i]); mins[2] > t {
				mins[2] = t
			}
		}
		for i := 1; i < l; i++ {
			if t := abs(arb[l] - ara[i]); mins[3] > t {
				mins[3] = t
			}
		}

		easy := abs(ara[0]-arb[0]) + abs(ara[l]-arb[l])
		semieasy := abs(ara[0]-arb[l]) + abs(ara[0]-arb[l])
		hard := mins[0] + mins[1] + mins[2] + mins[3]
		stoopid := []int64{
			abs(arb[0]-ara[l]) + abs(ara[l]-arb[l]) + mins[0],
			abs(ara[0]-arb[0]) + abs(ara[0]-arb[l]) + mins[1],
			abs(ara[0]-arb[l]) + abs(arb[l]-ara[l]) + mins[2],
			abs(ara[0]-arb[0]) + abs(arb[0]-ara[l]) + mins[3],
		}
		omegaHard := min(
			min(stoopid[0], stoopid[1]), min(stoopid[2], stoopid[3]))

		fmt.Fprintln(out, min(min(omegaHard, hard), min(semieasy, easy)))

	}
}

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

func min(x, y int64) int64 {
	if x < y {
		return x
	}
	return y
}

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-- {
		var n int
		fmt.Fscan(in, &n)
		ara := make([]int64, n)
		arb := make([]int64, n)
		for i := range ara {
			fmt.Fscan(in, &ara[i])
		}
		for i := range arb {
			fmt.Fscan(in, &arb[i])
		}

		l := n - 1
		mins := []int64{
			1_000_000_001, 1_000_000_001, 1_000_000_001, 1_000_000_001}
		for i := 1; i < l; i++ {
			if t := abs(ara[0] - arb[i]); mins[0] > t {
				mins[0] = t
			}
		}
		for i := 1; i < l; i++ {
			if t := abs(ara[l] - arb[i]); mins[1] > t {
				mins[1] = t
			}
		}
		for i := 1; i < l; i++ {
			if t := abs(arb[0] - ara[i]); mins[2] > t {
				mins[2] = t
			}
		}
		for i := 1; i < l; i++ {
			if t := abs(arb[l] - ara[i]); mins[3] > t {
				mins[3] = t
			}
		}

		easy := abs(ara[0]-arb[0]) + abs(ara[l]-arb[l])
		semieasy := abs(ara[0]-arb[l]) + abs(ara[0]-arb[l])
		hard := mins[0] + mins[1] + mins[2] + mins[3]

		fmt.Fprintln(out, min(hard, (min(semieasy, easy))))

	}
}

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

func min(x, y int64) int64 {
	if x < y {
		return x
	}
	return y
}

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-- {
		var n int
		fmt.Fscan(in, &n)
		ara := make([]int64, n)
		arb := make([]int64, n)
		for i := range ara {
			fmt.Fscan(in, &ara[i])
		}
		for i := range arb {
			fmt.Fscan(in, &arb[i])
		}

		l := n - 1
		mins := []int64{
			1_000_000_001, 1_000_000_001, 1_000_000_001, 1_000_000_001}
		for i := 0; i < n; i++ {
			if t := abs(ara[0] - arb[i]); mins[0] > t {
				mins[0] = t
			}
		}
		for i := 0; i < n; i++ {
			if t := abs(ara[l] - arb[i]); mins[1] > t {
				mins[1] = t
			}
		}
		for i := 1; i < l; i++ {
			if t := abs(arb[0] - ara[i]); mins[2] > t {
				mins[2] = t
			}
		}
		for i := 1; i < l; i++ {
			if t := abs(arb[l] - ara[i]); mins[3] > t {
				mins[3] = t
			}
		}

		easy := abs(ara[0]-arb[0]) + abs(ara[l]-arb[l])
		semieasy := abs(ara[0]-arb[l]) + abs(ara[0]-arb[l])
		hard := mins[0] + mins[1] + mins[2] + mins[3]

		fmt.Fprintln(out, min(hard, (min(semieasy, easy))))

	}
}

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

func min(x, y int64) int64 {
	if x < y {
		return x
	}
	return y
}

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-- {
		var n int
		fmt.Fscan(in, &n)
		ara := make([]int64, n)
		arb := make([]int64, n)
		for i := range ara {
			fmt.Fscan(in, &ara[i])
		}
		for i := range arb {
			fmt.Fscan(in, &arb[i])
		}

		l := n - 1
		mins := []int64{
			1_000_000_001, 1_000_000_001, 1_000_000_001, 1_000_000_001}
		for i := 0; i < n; i++ {
			if t := abs(ara[0] - arb[i]); mins[0] > t {
				mins[0] = t
			}
		}
		for i := 0; i < n; i++ {
			if t := abs(ara[l] - arb[i]); mins[1] > t {
				mins[1] = t
			}
		}
		for i := 1; i < l; i++ {
			if t := abs(arb[0] - ara[i]); mins[2] > t {
				mins[2] = t
			}
		}
		for i := 1; i < l; i++ {
			if t := abs(arb[l] - ara[i]); mins[3] > t {
				mins[3] = t
			}
		}

		easy := abs(ara[0]-arb[0]) + abs(ara[l]-arb[l])
		hard := mins[0] + mins[1] + mins[2] + mins[3]
		if easy < hard {
			fmt.Fprintln(out, easy)
		} else {
			fmt.Fprintln(out, hard)
		}
	}
}

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

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-- {
		var n int
		fmt.Fscan(in, &n)
		ara := make([]int64, n)
		arb := make([]int64, n)
		for i := range ara {
			fmt.Fscan(in, &ara[i])
		}
		for i := range arb {
			fmt.Fscan(in, &arb[i])
		}

		l := n - 1
		mins := []int64{
			1_000_000_001, 1_000_000_001, 1_000_000_001, 1_000_000_001}
		for i := 0; i < n; i++ {
			if t := abs(ara[0] - arb[i]); mins[0] > t {
				mins[0] = t
			}
		}
		for i := 0; i < n; i++ {
			if t := abs(ara[l] - arb[i]); mins[1] > t {
				mins[1] = t
			}
		}
		for i := 0; i < n; i++ {
			if t := abs(ara[i] - arb[0]); mins[2] > t {
				mins[2] = t
			}
		}
		for i := 0; i < n; i++ {
			if t := abs(ara[i] - arb[l]); mins[3] > t {
				mins[3] = t
			}
		}

		easy := abs(ara[0]-arb[0]) + abs(ara[l]-arb[l])
		hard := mins[0] + mins[1] + mins[2] + mins[3]
		if easy < hard {
			fmt.Fprintln(out, easy)
		} else {
			fmt.Fprintln(out, hard)
		}
	}
}

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

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-- {
		var n int
		fmt.Fscan(in, &n)
		ara := make([]int, n)
		arb := make([]int, n)
		for i := range ara {
			fmt.Fscan(in, &ara[i])
		}
		for i := range arb {
			fmt.Fscan(in, &arb[i])
		}

		l := n - 1
		mins := []int{
			1_000_000_001, 1_000_000_001, 1_000_000_001, 1_000_000_001}
		for i := 0; i < n; i++ {
			if t := abs(ara[0] - arb[i]); mins[0] > t {
				mins[0] = t
			}
		}
		for i := 0; i < n; i++ {
			if t := abs(ara[l] - arb[i]); mins[1] > t {
				mins[1] = t
			}
		}
		for i := 0; i < n; i++ {
			if t := abs(ara[i] - arb[0]); mins[2] > t {
				mins[2] = t
			}
		}
		for i := 0; i < n; i++ {
			if t := abs(ara[i] - arb[l]); mins[3] > t {
				mins[3] = t
			}
		}

		easy := abs(ara[0]-arb[0]) + abs(ara[l]-arb[l])
		hard := mins[0] + mins[1] + mins[2] + mins[3]
		if easy < hard {
			fmt.Fprintln(out, easy)
		} else {
			fmt.Fprintln(out, hard)
		}
	}
}

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

package main

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

/*
1
6
5 6 4 1 6 1
2 4 1 5 4 1
---
1 = min(3, 0+1)
=====
1
5
9 5 5 5 1
1 5 5 5 9
---
0
=======
1
4
1 5 1 1
9 9 3 9
---
14 = min(8+8, 2+4+8)
*/

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

	a := make([]int, n)

	for i := range a {
		scanf("%d", &a[i])
	}
	scanf("\n")

	b := make([]int, n)

	for i := range b {
		scanf("%d", &b[i])
	}
	scanf("\n")

	findBestCandidateIdx := func(cur int, nums []int) int {
		bestIdx := 0

		for i, num := range nums {
			if abs(num-cur) < abs(nums[bestIdx]-cur) {
				bestIdx = i
			}
		}

		return bestIdx
	}

	fstCnds := []int{0, findBestCandidateIdx(a[0], b), n - 1}
	lstCnds := []int{0, findBestCandidateIdx(a[n-1], b), n - 1}

	best := maxInt

	for _, i := range fstCnds {
		for _, j := range lstCnds {
			ans := abs(a[0]-b[i]) + abs(a[n-1]-b[j])

			if i > 0 && j > 0 {
				ans += abs(b[0] - a[findBestCandidateIdx(b[0], a)])
			}

			if i < n-1 && j < n-1 {
				ans += abs(b[n-1] - a[findBestCandidateIdx(b[n-1], a)])
			}

			best = min(best, ans)
		}
	}

	fmt.Fprintln(writer, best)
}

//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 int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int) int {
	return nums[minIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func minIdx(nums ...int) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	return nums[maxIdx(nums...)]
}

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

	return maxNumIdx
}

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

//goland:noinspection GoUnusedFunction
func findSet(parent []int, v int) int {
	if v == parent[v] {
		return v
	}

	parent[v] = findSet(parent, parent[v])

	return parent[v]
}

//goland:noinspection GoUnusedFunction
func unionSets(parent []int, v, u int) {
	v = findSet(parent, v)
	u = findSet(parent, u)

	if v != u {
		parent[v] = u
	}
}

package main

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

/*
1
6
5 6 4 1 6 1
2 4 1 5 4 1
---
1 = min(3, 0+1)
=====
1
5
9 5 5 5 1
1 5 5 5 9
---
0
=======
1
4
1 5 1 1
9 9 3 9
---
14 = min(8+8, 2+4+8)
*/

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

	a := make([]int, n)

	for i := range a {
		scanf("%d", &a[i])
	}
	scanf("\n")

	b := make([]int, n)

	for i := range b {
		scanf("%d", &b[i])
	}
	scanf("\n")

	cost1 := abs(a[0]-b[0]) + abs(a[n-1]-b[n-1])
	cost2 := abs(a[0]-b[n-1]) + abs(a[n-1]-b[0])

	cost := 0
	{

		a0bi := maxInt
		a0biIdx := -1

		for i := 0; i < n; i++ {
			if abs(a[0]-b[i]) < a0bi {
				a0bi = abs(a[0] - b[i])
				a0biIdx = i
			}
		}
		cost += a0bi

		b0ai := maxInt
		b0aiIdx := -1
		if a0biIdx != 0 {
			for i := 1; i < n; i++ {
				if abs(b[0]-a[i]) < b0ai {
					b0ai = abs(b[0] - a[i])
					b0aiIdx = i
				}
			}

			cost += b0ai
		}

		anbi := maxInt
		anbiIdx := -1

		for i := n - 1; i > 0; i-- {
			if i == a0biIdx {
				continue
			}

			if abs(a[n-1]-b[i]) < anbi {
				anbi = abs(a[n-1] - b[i])
				anbiIdx = i
			}
		}
		cost += anbi

		bnai := maxInt
		if anbiIdx != n-1 {
			for i := n - 1; i > 0; i-- {
				if i == b0aiIdx {
					continue
				}

				if abs(b[n-1]-a[i]) < bnai {
					bnai = abs(b[n-1] - a[i])
					//b0aiIdx = i
				}
			}

			cost += bnai
		}
	}

	bestCost := min(cost, cost1, cost2)

	fmt.Fprintln(writer, bestCost)
}

//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 ...int) int {
	minimum := nums[0]
	for _, num := range nums {
		if num < minimum {
			minimum = num
		}
	}
	return minimum
}

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

	return nums[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"
	"math"
	"os"
)

/*
1
6
5 6 4 1 6 1
2 4 1 5 4 1
---
1 = min(3, 0+1)
=====
1
5
9 5 5 5 1
1 5 5 5 9
---
0
*/

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

	a := make([]int, n)

	for i := range a {
		scanf("%d", &a[i])
	}
	scanf("\n")

	b := make([]int, n)

	for i := range b {
		scanf("%d", &b[i])
	}
	scanf("\n")

	//cost1 := abs(a[0]-b[0]) + abs(a[n-1]-b[n-1])
	//cost2 := abs(a[0]-b[n-1]) + abs(a[n-1]-b[0])

	cost := 0
	{

		a0bi := maxInt
		a0biIdx := -1

		for i := 0; i < n; i++ {
			if abs(a[0]-b[i]) < a0bi {
				a0bi = abs(a[0] - b[i])
				a0biIdx = i
			}
		}
		cost += a0bi

		if a0biIdx != 0 {
			b0ai := maxInt
			//b0a1Idx := -1

			for i := 1; i < n; i++ {
				if abs(b[0]-a[i]) < b0ai {
					b0ai = abs(b[0] - a[i])
					//b0a1Idx = i
				}
			}

			cost += b0ai
		}

		anbi := maxInt
		anbiIdx := -1

		for i := n - 1; i >= 0; i-- {
			if abs(a[n-1]-b[i]) < anbi {
				anbi = abs(a[n-1] - b[i])
				anbiIdx = i
			}
		}
		cost += anbi

		if anbiIdx != n-1 {
			bnai := maxInt
			for i := n - 1; i >= 0; i-- {
				if abs(b[n-1]-a[i]) < bnai {
					bnai = abs(b[n-1] - a[i])
					//b0a1Idx = i
				}
			}

			cost += bnai
		}
	}

	bestCost := min(cost)

	fmt.Fprintln(writer, bestCost)
}

//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 ...int) int {
	minimum := nums[0]
	for _, num := range nums {
		if num < minimum {
			minimum = num
		}
	}
	return minimum
}

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

	return nums[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"
)

// github.com/EndlessCheng/codeforces-go
func f(a []int64, v int64) int64 {
	res := int64(1e9)
	for _, w := range a {
		res = min(res, abs(w-v))
	}
	return res
}

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

	var T, n int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		a := make([]int64, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		b := make([]int64, n)
		for i := range b {
			Fscan(in, &b[i])
		}
		ans := min(abs(a[0]-b[0])+abs(a[n-1]-b[n-1]), abs(a[0]-b[n-1])+abs(a[n-1]-b[0]))
		ans = min(ans, abs(a[0]-b[0])+f(a, b[n-1])+f(b, a[n-1]))
		ans = min(ans, abs(a[0]-b[n-1])+f(a, b[0])+f(b, a[n-1]))
		ans = min(ans, abs(a[n-1]-b[n-1])+f(a, b[0])+f(b, a[0]))
		ans = min(ans, f(a, b[0])+f(a, b[n-1])+f(b, a[0])+f(b, a[n-1]))
		Fprintln(out, ans)
	}
}

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

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

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


package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
	"strconv"
)
const (
	MAXN=int(1e5+7)
)
var a,b [MAXN] int64
var dp [MAXN][3] int64

func read() (int64, error) {
	b, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		return 0, err
	}
	// use strconv.ParseUint and strconv.ParseFloat in a similar way
	return strconv.ParseInt(string(b[:len(b)-1]), 10, 0)
}

func main(){
	input, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		return
	}
	inputReader := bytes.NewReader(input)
	var N int
	_,_=fmt.Fscanf(inputReader, "%d\n", &N)
	for i:=0 ;i<N-1;i++ {
		_,_=fmt.Fscanf(inputReader,"%d ",&a[i])
		//fmt.Println(a[i])
	}
	_,_=fmt.Fscanf(inputReader,"%d\n",&a[N-1])

	for i:=0;i<N-1;i++ {
		_,_=fmt.Fscanf(inputReader,"%d",&b[i])
		//fmt.Println(b[i])
	}
	_,_=fmt.Fscanf(inputReader,"%d\n",&b[N-1])
	//solve
	max:= func(a,b int64)int64 {
		if a>b{
			return a
		}else{
			return b
		}
	}

	for i:=1;i<=N;i++ {
		//00
		for j:=0;j<3;j++{
			dp[i][0]=max(dp[i][0],dp[i-1][j])
		}
		//01
		dp[i][1]=max(dp[i][1],dp[i-1][0]+b[i-1])//00
		dp[i][1]=max(dp[i][1],dp[i-1][2]+b[i-1])
		//10
		dp[i][2]=max(dp[i][2],dp[i-1][0]+a[i-1])
		dp[i][2]=max(dp[i][2],dp[i-1][1]+a[i-1])
		//11
		//fmt.Println(dp[i][0],dp[i][1],dp[i][2])
	}
	var ans int64=0
	for i:=0;i<3;i++ {
		ans=max(ans,dp[N][i])
	}
	fmt.Println(ans)

}

package main

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

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

func min(x, y int64) int64 {
	if x < y {
		return x
	}
	return y
}

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

	var n int64
	fmt.Fscanf(input, "%d\n", &n)

	hs := make([][]int64, 2)
	for i := range hs {
		for j := int64(0); j < n; j++ {
			var h int64
			fmt.Fscanf(input, "%d", &h)
			hs[i] = append(hs[i], h)
		}
		fmt.Fscanf(input, "\n")
	}

	dp := make([][]int64, 2)
	for i := range dp {
		dp[i] = make([]int64, n+1)
	}

	for i := int64(1); i <= n; i++ {
		dp[0][i] = max(dp[0][i-1], dp[1][i-1]+hs[0][i-1])
		dp[1][i] = max(dp[1][i-1], dp[0][i-1]+hs[1][i-1])
	}
	// fmt.Println(hs[0], hs[1], dp)
	fmt.Println(max(dp[0][n], dp[1][n]))
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n := 0
	fmt.Fscanf(in, "%d\n", &n)
	a := make([]int64, n)
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(in, "%d", &a[i])
	}
	fmt.Fscanf(in, "\n")
	for i := 0; i < n; i++ {
		fmt.Fscanf(in, "%d", &b[i])
	}
	fmt.Fscanf(in, "\n")
	f := make([][]int64, n)
	for i := 0; i < n; i++ {
		f[i] = make([]int64, 2)
	}
	f[0][0] = a[0]
	f[0][1] = b[0]
	maxa := a[0]
	maxb := b[0]
	for i := 1; i < n; i++ {
		f[i][0] = maxb + a[i]
		f[i][1] = maxa + b[i]
		if f[i][0] > maxa {
			maxa = f[i][0]
		}
		if f[i][1] > maxb {
			maxb = f[i][1]
		}
	}
	ans := maxa
	if maxb > ans {
		ans = maxb
	}
	fmt.Fprintf(out, "%d\n", ans)
	return
}

func gcd(a, b uint64) uint64 {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

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
}

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

func ok(a, b int) bool {
	if min(abs(a-b), abs(a+b)) <= min(abs(a), abs(b)) && max(abs(a+b), abs(a-b)) >= max(abs(a), abs(b)) {
		return true
	}
	return false
}

package main

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

func main() {
	defer out.Flush()
	n := ScanInt()
	h1 := ScanInts(n)
	h2 := ScanInts(n)

	dp := make([][2]int64, n+2)
	for i := 1; i <= n; i++ {
		dp[i][0] = Mx(dp[i-1][1]+int64(h2[i-1]), dp[i-1][0])
		dp[i][1] = Mx(dp[i-1][0]+int64(h1[i-1]), dp[i-1][1])
	}
	P(Mx(dp[n][0], dp[n][1]))
}

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

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

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

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

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

		}
		out.WriteString(str + seperator)
	}
}

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

func MnMx(args ...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 int) int       { return a / GCD(a, b) * b }
func GCD(a, b int) int {
	if a == 0 {
		return b
	}
	return GCD(b%a, a)
}
func Abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

package main

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

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

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

	n := 0
	h := [2][100100]int64{}
	fmt.Fscanf(input, "%d\n", &n)
	for j := 0; j < 2; j++ {
		for i := 0; i < n; i++ {
			fmt.Fscanf(input, "%d", &h[j][i])
		}
		fmt.Fscanf(input, "\n")
	}

	ans0, ans1, ansx := int64(0), int64(0), int64(0)
	for i := n - 1; i >= 0; i-- {
		ans0, ans1, ansx = h[0][i]+max(ans1, ansx), h[1][i]+max(ans0, ansx), max(ans1, max(ans0, ansx))
		// fmt.Println(h[0][i], h[1][i], max(ans0, max(ans1, ansx)))
	}
	fmt.Println(max(ans0, max(ans1, ansx)))
}

package main

import (
	"bufio"
	"os"
	"runtime"
	"strconv"
)

/* solution */

func solve() {

	n := readInt()
	a := make([]int64, n)
	b := make([]int64, n)

	for i := 0; i < n; i++ {
		a[i] = readInt64()
	}

	for i := 0; i < n; i++ {
		b[i] = readInt64()
	}

	if n > 1 {
		a[1] += b[0]
		b[1] += a[0]
	}

	for i := 2; i < n; i++ {
		if a[i-2] > a[i-1] {
			b[i] += a[i-2]
		} else {
			b[i] += a[i-1]
		}
		if b[i-2] > b[i-1] {
			a[i] += b[i-2]
		} else {
			a[i] += b[i-1]
		}
	}

	if a[n-1] > b[n-1] {
		printInt64(a[n-1])
	} else {
		printInt64(b[n-1])
	}

}

/* stubs */

func main() {

	var f *os.File
	if runtime.GOROOT() == "C:\\Soft\\Go" {
		f, _ = os.Open("input.txt")
		defer f.Close()
	} else {
		f = os.Stdin
	}

	writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	reader = bufio.NewScanner(f)

	const maxCapacity = 1024 * 1024
	buf := make([]byte, maxCapacity)
	reader.Buffer(buf, maxCapacity)

	reader.Split(bufio.ScanWords)

	solve()

}

var reader *bufio.Scanner
var writer *bufio.Writer

func read() string {
	reader.Scan()
	return reader.Text()
}

func readInt() int {
	reader.Scan()
	ret, _ := strconv.Atoi(reader.Text())
	return ret
}

func print(s string) {
	writer.WriteString(s)
	writer.WriteString("\n")
}

func printInt(i int) {
	print(strconv.Itoa(i))
}

func printInt64(i int64) {
	print(strconv.FormatInt(i, 10))
}

func readInt64() int64 {
	reader.Scan()
	ret, _ := strconv.ParseInt(reader.Text(), 10, 64)
	return ret
}

// Author: sighduck
// URL: https://codeforces.com/contest/1195/problem/C

package main

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

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

func Solve(n int, h1 []int64, h2 []int64) int64 {
	if n == 1 {
		return max(h1[0], h2[0])
	}
	h1[1] += h2[0]
	h2[1] += h1[0]
	for i := 2; i < n; i++ {
		h1[i] += max(h2[i-1], h2[i-2])
		h2[i] += max(h1[i-1], h1[i-2])
	}
	return max(h1[n-1], h2[n-1])
}

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

	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	h1 := make([]int64, n)
	h2 := make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &h1[i])
	}
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &h2[i])
	}

	fmt.Fprintf(writer, "%d\n", Solve(n, h1, h2))
}

package main

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

var (
	n        int64
	students [2][100005]int64
	dp       [2][100005]int64
)

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

func recurse(row int64, idx int64) int64 {
	// fmt.Printf("row = %d, idx = %d\n", row, idx)
	if idx == n {
		return 0
	}
	if dp[row][idx] != -1 {
		return dp[row][idx]
	}
	dp[row][idx] = max(
		// include student row, idx
		students[row][idx]+recurse(1-row, idx+1),
		// do not include student row, idx
		recurse(row, idx+1),
	)
	return dp[row][idx]
}

func main() {
	var i, j int64
	in := bufio.NewReader(os.Stdin)

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

	for i = 0; i < 2; i++ {
		for j = 0; j < n; j++ {
			dp[i][j] = -1
		}
	}

	ans := max(recurse(0, 0), recurse(1, 0))
	fmt.Println(ans)
}

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
	"strconv"
)
const (
	MAXN=int(1e5+7)
)
var a,b [MAXN] int64
var dp [MAXN][3] int64

func read() (int64, error) {
	b, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		return 0, err
	}
	// use strconv.ParseUint and strconv.ParseFloat in a similar way
	return strconv.ParseInt(string(b[:len(b)-1]), 10, 0)
}

func main(){
	input, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		return
	}
	inputReader := bytes.NewReader(input)
	var N int
	_,_=fmt.Fscanf(inputReader, "%d\n", &N)
	for i:=0 ;i<N-1;i++ {
		_,_=fmt.Fscanf(inputReader,"%d ",&a[i])
		//fmt.Println(a[i])
	}
	_,_=fmt.Fscanf(inputReader,"%d ",&a[N-1])

	for i:=0;i<N-1;i++ {
		_,_=fmt.Fscanf(inputReader,"%d",&b[i])
		//fmt.Println(b[i])
	}
	_,_=fmt.Fscanf(inputReader,"%d ",&b[N-1])
	//solve
	max:= func(a,b int64)int64 {
		if a>b{
			return a
		}else{
			return b
		}
	}

	for i:=1;i<=N;i++ {
		//00
		for j:=0;j<3;j++{
			dp[i][0]=max(dp[i][0],dp[i-1][j])
		}
		//01
		dp[i][1]=max(dp[i][1],dp[i-1][0]+b[i-1])//00
		dp[i][1]=max(dp[i][1],dp[i-1][2]+b[i-1])
		//10
		dp[i][2]=max(dp[i][2],dp[i-1][0]+a[i-1])
		dp[i][2]=max(dp[i][2],dp[i-1][1]+a[i-1])
		//11
		//fmt.Println(dp[i][0],dp[i][1],dp[i][2])
	}
	var ans int64=0
	for i:=0;i<3;i++ {
		ans=max(ans,dp[N][i])
	}
	fmt.Println(ans)

}

package main

import "fmt"

var n int64
var students, dp [2][100005]int64

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

func recurse(row int64, idx int64) int64 {
	// fmt.Printf("row = %d, idx = %d\n", row, idx)
	if idx == n {
		return 0
	}
	if dp[row][idx] != -1 {
		return dp[row][idx]
	}
	dp[row][idx] = max(
		// include student row, idx
		students[row][idx]+recurse(1-row, idx+1),
		// do not include student row, idx
		recurse(row, idx+1),
	)
	return dp[row][idx]
}

func main() {

	var i, j int64
	fmt.Scanf("%d", &n)
	for i = 0; i < n; i++ {
		fmt.Scanf("%d", &students[0][i])
	}
	for i = 0; i < n; i++ {
		fmt.Scanf("%d", &students[1][i])
	}

	for i = 0; i < 2; i++ {
		for j = 0; j < n; j++ {
			dp[i][j] = -1
		}
	}
	ans := max(recurse(0, 0), recurse(1, 0))
	fmt.Println(ans)
}

package main

import "fmt"

var n int64
var students, dp [2][100005]int64

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

func recurse(row int64, idx int64) int64 {
	// fmt.Printf("row = %d, idx = %d\n", row, idx)
	if idx == n {
		return 0
	}
	if dp[row][idx] != -1 {
		return dp[row][idx]
	}
	dp[row][idx] = max(
		// include student row, idx
		students[row][idx]+recurse(1-row, idx+1),
		// do not include student row, idx
		recurse(row, idx+1),
	)
	return dp[row][idx]
}

func main() {

	var i, j int64
	fmt.Scanln(&n)
	for i = 0; i < n; i++ {
		fmt.Scanf("%d ", &students[0][i])
	}
	for i = 0; i < n; i++ {
		fmt.Scanf("%d ", &students[1][i])
	}

	for i = 0; i < 2; i++ {
		for j = 0; j < n; j++ {
			dp[i][j] = -1
		}
	}
	ans := max(recurse(0, 0), recurse(1, 0))
	fmt.Println(ans)
}

package main

import (
	"fmt"
	"os"
)

var (
	n        int64
	students [2][100005]int64
	dp       [2][100005]int64
)

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

func recurse(row int64, idx int64) int64 {
	// fmt.Printf("row = %d, idx = %d\n", row, idx)
	if idx == n {
		return 0
	}
	if dp[row][idx] != -1 {
		return dp[row][idx]
	}
	dp[row][idx] = max(
		// include student row, idx
		students[row][idx]+recurse(1-row, idx+1),
		// do not include student row, idx
		recurse(row, idx+1),
	)
	return dp[row][idx]
}

func main() {

	var i, j int64
	fmt.Fscanf(os.Stdin, "%d", &n)
	for i = 0; i < n; i++ {
		fmt.Fscanf(os.Stdin, "%d", &students[0][i])
	}
	for i = 0; i < n; i++ {
		fmt.Fscanf(os.Stdin, "%d", &students[1][i])
	}

	for i = 0; i < 2; i++ {
		for j = 0; j < n; j++ {
			dp[i][j] = -1
		}
	}
	ans := max(recurse(0, 0), recurse(1, 0))
	fmt.Println(ans)
}

package main

import "fmt"

var n int64
var students, dp [2][100005]int64

// var dp [2][100005]int64

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

func recurse(row int64, idx int64) int64 {
	// fmt.Printf("row = %d\n idx = %d\n", row, idx)
	if idx == n {
		return 0
	}
	if dp[row][idx] != -1 {
		return dp[row][idx]
	}
	dp[row][idx] = max(
		// include student row, idx
		students[row][idx]+recurse(1-row, idx+1),
		// do not include student row, idx
		recurse(row, idx+1),
	)
	return dp[row][idx]
}

func main() {

	var i, j int64
	fmt.Scanf("%d\n", &n)
	for i = 0; i < n; i++ {
		fmt.Scanf("%d", &students[0][i])
	}
	for i = 0; i < n; i++ {
		fmt.Scanf("%d", &students[1][i])
	}

	for i = 0; i < 2; i++ {
		for j = 0; j < n; j++ {
			dp[i][j] = -1
		}
	}
	ans := max(recurse(0, 0), recurse(1, 0))
	fmt.Printf("%d\n", ans)
}

package main

import "fmt"

var n int
var students [2][100005]int
var dp [2][100005]int

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

func recurse(row int, idx int) int {
	// fmt.Printf("row = %d\n idx = %d\n", row, idx)
	if idx == n {
		return 0
	}
	if dp[row][idx] != -1 {
		return dp[row][idx]
	}
	ans := max(
		// include student row, idx
		students[row][idx]+recurse(1-row, idx+1),
		// do not include student row, idx
		recurse(row, idx+1),
	)
	dp[row][idx] = ans
	return ans
}

func main() {

	fmt.Scanf("%d\n", &n)
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &students[0][i])
	}
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &students[1][i])
	}

	for i := 0; i < 2; i++ {
		for j := 0; j < n; j++ {
			dp[i][j] = -1
		}
	}
	ans := max(recurse(0, 0), recurse(1, 0))
	fmt.Printf("%d\n", ans)
}

package main

import "fmt"

var n int
var students [2][100005]int
var dp [2][100005]int

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

func recurse(row int, idx int) int {
	// fmt.Printf("row = %d\n idx = %d\n", row, idx)
	if idx == n {
		return 0
	}
	if dp[row][idx] != -1 {
		return dp[row][idx]
	}
	ans := max(
		// include student row, idx
		students[row][idx]+recurse(1-row, idx+1),
		// do not include student row, idx
		recurse(row, idx+1),
	)
	dp[row][idx] = ans
	return ans
}

func main() {

	fmt.Scanf("%d\n", &n)
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &students[0][i])
	}
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &students[1][i])
	}

	for i := 0; i < 2; i++ {
		for j := 0; j < n; j++ {
			dp[i][j] = -1
		}
	}
	// fmt.Printf("n = %d\n", n)
	// for i := 0; i < n; i++ {
	// 	fmt.Printf("%d ", students[0][i])
	// }
	// fmt.Println()
	// for i := 0; i < n; i++ {
	// 	fmt.Printf("%d ", students[1][i])
	// }
	// fmt.Println()

	ans := max(recurse(0, 0), recurse(1, 0))
	fmt.Printf("%d\n", ans)
}

package main

import (
	"fmt"
)

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

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

func main() {
	var n int
	fmt.Scanf("%d\n", &n)

	hs := make([][]int, 2)
	for i := range hs {
		for j := 0; j < n; j++ {
			var h int
			fmt.Scanf("%d", &h)
			hs[i] = append(hs[i], h)
		}
	}

	dp := make([][]int, 2)
	for i := range dp {
		dp[i] = make([]int, n+1)
	}

	for i := 1; i <= n; i++ {
		dp[0][i] = max(dp[0][i-1], dp[1][i-1]+hs[0][i-1])
		dp[1][i] = max(dp[1][i-1], dp[0][i-1]+hs[1][i-1])
	}
	// fmt.Println(hs[0], hs[1], dp)
	fmt.Println(max(dp[0][n], dp[1][n]))
}

package main

import (
	"fmt"
)

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

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

func main() {
	var n int
	fmt.Scanf("%d\n", &n)

	hs := make([][]int, 2)
	for i := range hs {
		for j := 0; j < n; j++ {
			var h int
			fmt.Scanf("%d%c", &h)
			hs[i] = append(hs[i], h)
		}
	}

	dp := make([][]int, 2)
	for i := range dp {
		dp[i] = make([]int, n+1)
	}

	for i := 1; i <= n; i++ {
		dp[0][i] = max(dp[0][i-1], dp[1][i-1]+hs[0][i-1])
		dp[1][i] = max(dp[1][i-1], dp[0][i-1]+hs[1][i-1])
	}
	// fmt.Println(hs[0], hs[1], dp)
	fmt.Println(max(dp[0][n], dp[1][n]))
}

// Copyright (C) 2017 Kazumasa Kohtaka <kkohtaka@gmail.com> All right reserved
// This file is available under the MIT license.

package main

import (
	"fmt"
)

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

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

func main() {
	var n int
	fmt.Scanf("%d\n", &n)

	hs := make([][]int, 2)
	for i := range hs {
		for j := 0; j < n; j++ {
			var h int
			fmt.Scanf("%d", &h)
			hs[i] = append(hs[i], h)
		}
	}

	dp := make([][]int, 2)
	for i := range dp {
		dp[i] = make([]int, n+1)
	}

	for i := 1; i <= n; i++ {
		dp[0][i] = max(dp[0][i-1], dp[1][i-1]+hs[0][i-1])
		dp[1][i] = max(dp[1][i-1], dp[0][i-1]+hs[1][i-1])
	}

	fmt.Println(max(dp[0][n], dp[1][n]))
}

// Copyright (C) 2017 Kazumasa Kohtaka <kkohtaka@gmail.com> All right reserved
// This file is available under the MIT license.

package main

import (
	"fmt"
	"math"
)

func solve(hs1, hs2 []int, max, idx, sum int) int {
	if idx == len(hs1) {
		return sum
	}
	x1 := hs1[idx]
	x2 := hs2[idx]
	var ans int
	if x1 < max {
		ans = solve(hs1, hs2, x1, idx+1, sum+x1)
	}
	if x2 < max {
		a := solve(hs1, hs2, x2, idx+1, sum+x2)
		if a > ans {
			ans = a
		}
	}
	a := solve(hs1, hs2, max, idx+1, sum)
	if a > ans {
		ans = a
	}
	return ans
}

func main() {
	var n int
	fmt.Scanf("%d\n", &n)
	var hs1 []int
	for i := 0; i < n; i++ {
		var h int
		fmt.Scanf("%d", &h)
		hs1 = append(hs1, h)
	}
	var hs2 []int
	for i := 0; i < n; i++ {
		var h int
		fmt.Scanf("%d", &h)
		hs2 = append(hs2, h)
	}

	fmt.Println(solve(hs1, hs2, math.MaxInt32, 0, 0))
}

package main

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

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

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

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

	var n int
	fmt.Fscanf(input, "%d\n", &n)

	hs := make([][]int, 2)
	for i := range hs {
		for j := 0; j < n; j++ {
			var h int
			fmt.Fscanf(input, "%d", &h)
			hs[i] = append(hs[i], h)
		}
		fmt.Fscanf(input, "\n")
	}

	dp := make([][]int, 2)
	for i := range dp {
		dp[i] = make([]int, n+1)
	}

	for i := 1; i <= n; i++ {
		dp[0][i] = max(dp[0][i-1], dp[1][i-1]+hs[0][i-1])
		dp[1][i] = max(dp[1][i-1], dp[0][i-1]+hs[1][i-1])
	}
	// fmt.Println(hs[0], hs[1], dp)
	fmt.Println(max(dp[0][n], dp[1][n]))
}

package main

import (
	"fmt"
)

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

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

func main() {
	var n int
	fmt.Scanf("%d\n", &n)

	hs := make([][]int, 2)
	for i := range hs {
		for j := 0; j < n; j++ {
			var h int
			fmt.Scanf("%d", &h)
			hs[i] = append(hs[i], h)
		}
	}

	dp := make([][]int, 2)
	for i := range dp {
		dp[i] = make([]int, n+1)
	}

	for i := 1; i <= n; i++ {
		dp[0][i] = max(dp[0][i-1], dp[1][i-1]+hs[0][i-1])
		dp[1][i] = max(dp[1][i-1], dp[0][i-1]+hs[1][i-1])
	}
	fmt.Println(hs[0], hs[1], dp)
	fmt.Println(max(dp[0][n], dp[1][n]))
}

package main

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

func max(x, y int) int {
	if x < y {
		return y
	}
	return x
}

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

	n := 0
	h := [2][100100]int{}
	fmt.Fscanf(input, "%d\n", &n)
	for j := 0; j < 2; j++ {
		for i := 0; i < n; i++ {
			fmt.Fscanf(input, "%d ", &h[j][i])
		}
	}

	ans0, ans1, ansx := 0, 0, 0
	for i := n - 1; i >= 0; i-- {
		ans0, ans1, ansx = h[0][i]+max(ans1, ansx), h[1][i]+max(ans0, ansx), max(ans1, max(ans0, ansx))
		// fmt.Println(h[0][i], h[1][i], max(ans0, max(ans1, ansx)))
	}
	fmt.Println(max(ans0, max(ans1, ansx)))
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)
func max(i, j int) int {
    if i > j {
        return i
    }
    return j
}
func main() {
	input := bufio.NewScanner(os.Stdin)
	buf := make([]byte, 100500)
	input.Buffer(buf, 100500)
	input.Split(bufio.ScanWords)

	input.Scan()
	n, _ := strconv.Atoi(input.Text())
    h := [2][100100]int {}
    ans0, ans1, ans3 := 0, 0, 0

    for j := 0; j < 2; j ++ {
        for i := 0; i < n; i ++ {
            input.Scan()
            x, _ := strconv.Atoi(input.Text())
            h[j][i] = x
        }
    }
    for i := n - 1; i >= 0; i -- {
        cur_ans0, cur_ans1, cur_ans3 := max(h[0][i] + ans1, h[0][i] + ans3), max(h[1][i] + ans0, h[1][i] + ans3), max(ans1, ans0)
        ans0 = max(cur_ans0, ans0)
        ans1 = max(cur_ans1, ans1)
        ans3 = max(cur_ans3, ans3)
    }
    fmt.Println(max(ans0, max(ans3, ans1)))
}

package main

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

func max(x, y int) int {
	if x < y {
		return y
	}
	return x
}

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

	n := 0
	h := [2][100100]int{}
	fmt.Fscanf(input, "%d\n", &n)
	for j := 0; j < 2; j++ {
		for i := 0; i < n; i++ {
			fmt.Fscanf(input, "%d", &h[j][i])
		}
		fmt.Fscanf(input, "\n")
	}

	ans0, ans1, ansx := 0, 0, 0
	for i := n - 1; i >= 0; i-- {
		ans0, ans1, ansx = h[0][i]+max(ans1, ansx), h[1][i]+max(ans0, ansx), max(ans1, max(ans0, ansx))
		// fmt.Println(h[0][i], h[1][i], max(ans0, max(ans1, ansx)))
	}
	fmt.Println(max(ans0, max(ans1, ansx)))
}

package main

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

func max(x, y int) int {
	if x < y {
		return y
	}
	return x
}

func main() {
	input := bufio.NewScanner(os.Stdin)
	buf := make([]byte, 100500)
	input.Buffer(buf, 100500)
	input.Split(bufio.ScanWords)

	input.Scan()
	n, _ := strconv.Atoi(input.Text())
	h := [2][100100]int{}
	for j := 0; j < 2; j++ {
		for i := 0; i < n; i++ {
			input.Scan()
			x, _ := strconv.Atoi(input.Text())
			h[j][i] = x
		}
	}
	ans0, ans1, ansx := 0, 0, 0
	for i := n - 1; i >= 0; i-- {
		ans0, ans1, ansx = h[0][i]+max(ans1, ansx), h[1][i]+max(ans0, ansx), max(ans1, max(ans0, ansx))
	}
	fmt.Println(max(ans0, max(ans1, ansx)))
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)
func max(i, j int) int {
    if i > j {
        return i
    }
    return j
}
func main() {
	input := bufio.NewScanner(os.Stdin)
	buf := make([]byte, 10000003)
	input.Buffer(buf, 10000003)
	input.Split(bufio.ScanWords)

	input.Scan()
	n, _ := strconv.Atoi(input.Text())
    h := [2][100100]int {}
    ans0, ans1, ans3 := 0, 0, 0

    for j := 0; j < 2; j ++ {
        for i := 0; i < n; i ++ {
            input.Scan()
            x, _ := strconv.Atoi(input.Text())
            h[j][i] = x
        }
    }
    for i := n - 1; i >= 0; i -- {
        cur_ans0, cur_ans1, cur_ans3 := max(h[0][i] + ans1, h[0][i] + ans3), max(h[1][i] + ans0, h[1][i] + ans3), max(ans1, ans0)
        ans0 = max(cur_ans0, ans0)
        ans1 = max(cur_ans1, ans1)
        ans3 = max(cur_ans3, ans3)
    }
    fmt.Println(max(ans0, max(ans3, ans1)))
}

package main

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

func max(x, y int) int {
	if x < y {
		return y
	}
	return x
}

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

	n := 0
	h := [2][100100]int{}
	fmt.Fscanf(input, "%d\n", &n)
	for j := 0; j < 2; j++ {
		for i := 0; i < n; i++ {
			fmt.Fscanf(input, "%d", &h[j][i])
		}
		fmt.Fscanf(input, "\n")
	}

	ans0, ans1, ansx := int(0), int(0), int(0)
	for i := n - 1; i >= 0; i-- {
		ans0, ans1, ansx = h[0][i]+max(ans1, ansx), h[1][i]+max(ans0, ansx), max(ans1, max(ans0, ansx))
		// fmt.Println(h[0][i], h[1][i], max(ans0, max(ans1, ansx)))
	}
	fmt.Println(max(ans0, max(ans1, ansx)))
}

package main

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

func max(x, y int) int {
	if x < y {
		return y
	}
	return x
}

func main() {
	input := bufio.NewScanner(os.Stdin)
	buf := make([]byte, 100500)
	input.Buffer(buf, 100500)
	input.Split(bufio.ScanWords)

	input.Scan()
	n, _ := strconv.Atoi(input.Text())
	h := [2][100100]int{}
	for j := 0; j < 2; j++ {
		for i := 0; i < n; i++ {
			input.Scan()
			x, err := strconv.Atoi(input.Text())
			if err != nil {
				os.Exit(-1)
			}
			h[j][i] = x
		}
	}
	ans0, ans1, ansx := 0, 0, 0
	for i := n - 1; i >= 0; i-- {
		ans0, ans1, ansx = h[0][i]+max(ans1, ansx), h[1][i]+max(ans0, ansx), max(ans1, max(ans0, ansx))
		// fmt.Println(h[0][i], h[1][i], max(ans0, max(ans1, ansx)))
	}
	fmt.Println(max(ans0, max(ans1, ansx)))
}