Byteland is a beautiful land known because of its beautiful trees.Misha has found a binary tree with $$$n$$$ vertices, numbered from $$$1$$$ to $$$n$$$. A binary tree is an acyclic connected bidirectional graph containing $$$n$$$ vertices and $$$n - 1$$$ edges. Each vertex has a degree at most $$$3$$$, whereas the root is the vertex with the number $$$1$$$ and it has a degree at most $$$2$$$.Unfortunately, the root got infected.The following process happens $$$n$$$ times:  Misha either chooses a non-infected (and not deleted) vertex and deletes it with all edges which have an end in this vertex or just does nothing.  Then, the infection spreads to each vertex that is connected by an edge to an already infected vertex (all already infected vertices remain infected). As Misha does not have much time to think, please tell him what is the maximum number of vertices he can save from the infection (note that deleted vertices are not counted as saved).

There are several test cases in the input data. The first line contains a single integer $$$t$$$ ($$$1\leq t\leq 5000$$$) — the number of test cases. This is followed by the test cases description. The first line of each test case contains one integer $$$n$$$ ($$$2\leq n\leq 3\cdot 10^5$$$) — the number of vertices of the tree.  The $$$i$$$-th of the following $$$n-1$$$ lines in the test case contains two positive integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \leq u_i, v_i \leq n$$$), meaning that there exists an edge between them in the graph.  It is guaranteed that the graph is a binary tree rooted at $$$1$$$. It is also guaranteed that the sum of $$$n$$$ over all test cases won't exceed $$$3\cdot 10^5$$$.

For each test case, output the maximum number of vertices Misha can save.

NoteIn the first test case, the only possible action is to delete vertex $$$2$$$, after which we save $$$0$$$ vertices in total.In the second test case, if we delete vertex $$$2$$$, we can save vertices $$$3$$$ and $$$4$$$.

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 abs(x int)int{
	if x<0{
		return -x
	}
	return x
}
var u []int
var v []int
var nex []int
var first []int
var dp []int
var tot int
func add(x, y int){
	tot++
	u[tot]=x
	v[tot]=y
	nex[tot]=first[x]
	first[x] = tot
}
func search(x, fa int) int{
	dp[x]=0
	mp := map[int]int{}
	for i:= first[x] ;i!=0;i=nex[i]{
		if v[i]!=fa{
			ret := search(v[i],x)
			mp[v[i]]=ret
		}
	}
	if len(mp) == 1{
		for _, v:= range mp{
			dp[x] = v-1
			return v+1
		}
	}
	if len(mp) == 0{
		return 1
	}
	sum :=  1
	f := []int{}
	siz := []int{}
	for k,v := range mp{
		sum += v
		f = append(f, k)
		siz = append(siz,v)
	}
	dp[x] = max(dp[f[0]]+siz[1]-1, dp[f[1]]+siz[0]-1)
	return sum
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	cnt := 0
	for t > 0{
		cnt++
		t--
		var n int
		fmt.Fscan(in,&n)
		u = make([]int,4*n)
		v = make([]int, 4*n)
		nex = make([]int, 4*n)
		first = make([]int,4*n)
		dp = make([]int, 4*n)
		tot = 0
		var s int64 = 0
		for i:=1;i<=n-1;i++{
			var x, y int
			fmt.Fscan(in,&x,&y)
			s = 10*s + int64(x)
			s = 10*s + int64(y)
			add(x,y)
			add(y,x)
		}
		search(1,-1)
		fmt.Fprintln(out,dp[1])
	}
}

package main

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

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

	return y
}

type Graph struct {
	n     int
	edges [][]int
}

func (graph *Graph) Add(v int, u int) {
	graph.edges[v] = append(graph.edges[v], u)
	graph.edges[u] = append(graph.edges[u], v)
}

func (graph *Graph) Bfs() int {
	if len(graph.edges[0]) == 0 {
		return 0
	}
	if len(graph.edges[0]) == 1 {
		return graph.n - 2
	}
	var visited = make([]bool, graph.n)
	var children = make([]int, graph.n)
	graph.Dfs(visited, 0, children)
	visited = make([]bool, graph.n)
	visited[0] = true
	return Max(children[graph.edges[0][0]]+graph.Visit(visited, graph.edges[0][1], children), children[graph.edges[0][1]]+graph.Visit(visited, graph.edges[0][0], children))
}

func (graph *Graph) Visit(visited []bool, v int, children []int) int {
	visited[v] = true
	if len(graph.edges[v]) == 1 {
		return 0
	}
	if len(graph.edges[v]) == 2 {
		return Max(0, children[v]-1)
	}
	var first = 0
	if visited[graph.edges[v][0]] {
		first = 1
	}
	var second = 1
	if visited[graph.edges[v][0]] {
		second = 2
	} else {
		if visited[graph.edges[v][1]] {
			second = 2
		}
	}

	var max1 = children[graph.edges[v][second]] + graph.Visit(visited, graph.edges[v][first], children)
	var max2 = children[graph.edges[v][first]] + graph.Visit(visited, graph.edges[v][second], children)
	return Max(max1, max2)
}

func (graph *Graph) Dfs(visited []bool, v int, children []int) int {
	visited[v] = true
	var result = 0
	for u := 0; u < len(graph.edges[v]); u++ {
		if !visited[graph.edges[v][u]] {
			result += graph.Dfs(visited, graph.edges[v][u], children)
		}
	}

	children[v] = result

	return result + 1
}

func main() {
	//f, _ := os.Open("input.txt")
	var reader = bufio.NewReader(os.Stdin)
	//var reader = bufio.NewReader(f)
	var tests int
	fmt.Fscan(reader, &tests)
	for ; tests > 0; tests-- {
		var n int
		fmt.Fscan(reader, &n)
		var graph = Graph{n: n, edges: make([][]int, n)}
		for i := 0; i < n-1; i++ {
			var u, v int
			fmt.Fscan(reader, &u, &v)
			graph.Add(v-1, u-1)
		}
		fmt.Println(graph.Bfs())

	}
}

package main

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

type Tree struct {
	elem     int
	children int
	left     *Tree
	right    *Tree
}

func NewTree(elem int, left, right *Tree) *Tree {
	return &Tree{elem: elem, left: left, right: right}
}

func NewTreeFromEdges(edges [][]int, startNode int) *Tree {
	built := make([]bool, len(edges))
	var treeFromEdges func(edges [][]int, currelem int) *Tree
	treeFromEdges = func(edges [][]int, currelem int) *Tree {
		built[currelem] = true
		if len(edges[currelem]) == 1 && currelem != startNode {
			return NewTree(currelem, nil, nil)
		} else {
			slc := make([]int, 0) // len = 1 or len = 2
			for _, el := range edges[currelem] {
				if !built[el] {
					slc = append(slc, el)
				}
			}
			if len(slc) == 2 {
				return NewTree(currelem, treeFromEdges(edges, slc[0]), treeFromEdges(edges, slc[1]))
			} else {
				return NewTree(currelem, nil, treeFromEdges(edges, slc[0]))
			}
		}
	}
	return treeFromEdges(edges, startNode)
}

func (t *Tree) countChildren() int {
	if t == nil {
		return 0
	}
	if t.left == nil && t.right == nil {
		t.children = 0
	} else {
		if t.left != nil && t.right != nil {
			t.children = 2 + t.left.countChildren() + t.right.countChildren()
		} else {
			t.children = 1 + t.left.countChildren() + t.right.countChildren()
		}
	}
	return t.children
}

func getChildren(t *Tree) int {
	if t == nil {
		return 0
	} else {
		return t.children
	}
}

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

func saveFromInfestation(t *Tree) int {
	if t == nil {
		return 0
	} else {
		l := getChildren(t.left)
		r := getChildren(t.right)
		return max(saveFromInfestation(t.left)+r, saveFromInfestation(t.right)+l)
	}
}

func (t *Tree) Print() {
	if t == nil {
		return
	} else {
		//fmt.Println("!TREE!", t.elem, t.children)
		//fmt.Println("!TREE!Children:")
		t.left.Print()
		t.right.Print()
	}
}

func main() {
	scanner := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var ntests int
	fmt.Fscan(scanner, &ntests)
	for iter := 0; iter < ntests; iter++ {
		//fmt.Println("--------------")
		var n int
		fmt.Fscan(scanner, &n)
		graph := make([][]int, n)
		for i := 0; i < n; i++ {
			graph[i] = make([]int, 0)
		}
		for i := 1; i < n; i++ {
			var l, r int
			fmt.Fscan(scanner, &l, &r)
			l--
			r--
			graph[l] = append(graph[l], r)
			graph[r] = append(graph[r], l)
		}
		//fmt.Println(graph)
		t := NewTreeFromEdges(graph, 0)
		t.countChildren()
		//t.Print()
		fmt.Fprintln(writer, saveFromInfestation(t))
	}
}

package main

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

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

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func print(a ...interface{})            { fmt.Println(a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f+"\n", a...) }

func main() {
	defer writer.Flush()

	var testcases int
	scanf("%d", &testcases)
	for i := 0; i < testcases; i++ {
		var n int
		scanf("%d", &n)
		tree := make([][]int, n)
		for j := 0; j < n; j++ {
			tree[j] = make([]int, 0)
		}
		for j := 0; j < n-1; j++ {
			var u, v int
			scanf("%d %d", &u, &v)
			tree[u-1] = append(tree[u-1], v-1)
			tree[v-1] = append(tree[v-1], u-1)
		}
		print(solve(tree, n))
	}
}

func max(nums ...int) int {
	result := math.MinInt32
	for _, num := range nums {
		if num > result {
			result = num
		}
	}
	return result
}

func solve(tree [][]int, n int) int {
	count := make([]int, n)
	var count_child_node func(int, int) int
	count_child_node = func(node, parent int) int {
		children := make([]int, 0)
		result := 0
		for _, child := range tree[node] {
			if child == parent {
				continue
			}
			result += count_child_node(child, node)
			result += 1
			children = append(children, child)
		}
		tree[node] = children
		count[node] = result
		return result
	}
	count_child_node(0, -1)

	var dfs func(int) int
	dfs = func(node int) int {
		children := tree[node]
		switch len(children) {
		case 0:
			return 0
		case 1:
			return count[tree[node][0]]
		case 2:
			return max(count[children[0]]+dfs(children[1]), count[children[1]]+dfs(children[0]))
		default:
			return 0
		}
	}
	return dfs(0)
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)
var wrtr = bufio.NewWriterSize(os.Stdout, 10_000_000)
var rdr = bufio.NewScanner(os.Stdin)
func gs() string  { rdr.Scan(); return rdr.Text() }
func gi() int     { i,e := strconv.Atoi(gs()); if e != nil {panic(e)}; return i }
func gi2() (int,int) { return gi(),gi() }
func gi3() (int,int,int) { return gi(),gi(),gi() }
func gi4() (int,int,int,int) { return gi(),gi(),gi(),gi() }
func gis(n int) []int  { res := make([]int,n); for i:=0;i<n;i++ { res[i] = gi() }; return res }
func gf() float64 { f,e := strconv.ParseFloat(gs(),64); if e != nil {panic(e)}; return f }
func gbs() []byte { return []byte(gs()) }
func gfs(n int) []float64  { res := make([]float64,n); for i:=0;i<n;i++ { res[i] = gf() }; return res }
func gss(n int) []string  { res := make([]string,n); for i:=0;i<n;i++ { res[i] = gs() }; return res }
func ia(m int) []int { return make([]int,m) }
func iai(m int,v int) []int { a := make([]int,m); for i:=0;i<m;i++ { a[i] = v }; return a }
func twodi(n int,m int,v int) [][]int {
	r := make([][]int,n); for i:=0;i<n;i++ { x := make([]int,m); for j:=0;j<m;j++ { x[j] = v }; r[i] = x }; return r
}
func fill2(m int) ([]int,[]int) { a,b := ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i] = gi(),gi()}; return a,b }
func fill3(m int) ([]int,[]int,[]int) { a,b,c := ia(m),ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i],c[i] = gi(),gi(),gi()}; return a,b,c }
func fill4(m int) ([]int,[]int,[]int,[]int) { a,b,c,d := ia(m),ia(m),ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i],c[i],d[i] = gi(),gi(),gi(),gi()}; return a,b,c,d }
func abs(a int) int { if a < 0 { return -a }; return a }
func rev(a []int) { i,j := 0,len(a)-1; for i < j { a[i],a[j] = a[j],a[i]; i++; j-- } }
func max(a,b int) int { if a > b { return a }; return b }
func min(a,b int) int { if a > b { return b }; return a }
func tern(cond bool, a int, b int) int { if cond { return a }; return b }
func terns(cond bool, a string, b string) string { if cond { return a }; return b }
func maxarr(a []int) int { ans := a[0]; for _,aa := range(a) { if aa > ans { ans = aa } }; return ans }
func minarr(a []int) int { ans := a[0]; for _,aa := range(a) { if aa < ans { ans = aa } }; return ans }
func sumarr(a []int) int { ans := 0; for _,aa := range(a) { ans += aa }; return ans }
func zeroarr(a []int) { for i:=0; i<len(a); i++ { a[i] = 0 } }
func powmod(a,e,mod int) int { res, m := 1, a; for e > 0 { if e&1 != 0 { res = res * m % mod }; m = m * m % mod; e >>= 1 }; return res }
func powint(a,e int) int { res, m := 1, a; for e > 0 { if e&1 != 0 { res = res * m }; m = m * m; e >>= 1 }; return res }
func gcd(a,b int) int { for b != 0 { t:=b; b=a%b; a=t }; return a }
func gcdExtended(a,b int) (int,int,int) { if a == 0 { return b,0,1 }; gcd,x1,y1 := gcdExtended(b%a,a); return gcd, y1-(b/a)*x1,x1 }
func modinv(a,m int) (int,bool) { g,x,_ := gcdExtended(a,m); if g != 1 { return 0,false }; return (x % m + m) % m,true  }
func makefact(n int,mod int) ([]int,[]int) {
	fact,factinv := make([]int,n+1),make([]int,n+1)
	fact[0] = 1; for i:=1;i<=n;i++ { fact[i] = fact[i-1] * i % mod }
	factinv[n] = powmod(fact[n],mod-2,mod); for i:=n-1;i>=0;i-- { factinv[i] = factinv[i+1] * (i+1) % mod }
	return fact,factinv
}
func vecintstring(a []int) string { astr := make([]string,len(a)); for i,a := range a { astr[i] = strconv.Itoa(a) }; return strings.Join(astr," ") }
const inf = 1000000000
func main() {
	//f1, _ := os.Create("cpu.prof"); pprof.StartCPUProfile(f1); defer pprof.StopCPUProfile()
	defer wrtr.Flush()
	infn := ""; if infn == "" && len(os.Args) > 1 {	infn = os.Args[1] }
	if infn != "" {	f, e := os.Open(infn); if e != nil { panic(e) }; rdr = bufio.NewScanner(f) }
	rdr.Split(bufio.ScanWords); rdr.Buffer(make([]byte,1024),1_000_000_000)
	// PROGRAM STARTS HERE
	T := gi()
	for tt:=1;tt<=T;tt++ {
		N := gi(); U,V := fill2(N-1); for i:=0;i<N-1;i++ { U[i]--; V[i]-- }
		gr := make([][]int,N)
		for i:=0;i<N-1;i++ { u,v := U[i],V[i]; gr[u] = append(gr[u],v); gr[v] = append(gr[v],u) }
		sz := ia(N)
		saved := ia(N)
		var dfs func(n,p int)
		child := []int{}
		dfs = func(n,p int) {
			for _,c := range gr[n] { if c == p { continue }; dfs(c,n) }
			child = child[:0]
			sz[n] = 1; saved[n] = 0
			for _,c := range gr[n] { if c == p { continue }; sz[n] += sz[c]; child = append(child,c) }
			if len(child) == 1 {
				saved[n] = sz[child[0]]-1
			} else if len(child) == 2 {
				saved[n] = max(sz[child[0]]-1+saved[child[1]],sz[child[1]]-1+saved[child[0]])
			}
			return
		}
		dfs(0,-1)
		fmt.Fprintln(wrtr,saved[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 abs(x int)int{
	if x<0{
		return -x
	}
	return x
}
var u []int
var v []int
var nex []int
var first []int
var dp []int
var tot int
func add(x, y int){
	tot++
	u[tot]=x
	v[tot]=y
	nex[tot]=first[x]
	first[x] = tot
}
func search(x, fa int) int{
	dp[x]=0
	mp := map[int]int{}
	for i:= first[x] ;i!=0;i=nex[i]{
		if v[i]!=fa{
			ret := search(v[i],x)
			mp[v[i]]=ret
		}
	}
	if len(mp) == 1{
		for _, v:= range mp{
			dp[x] = v-1
			return v+1
		}
	}
	if len(mp) == 0{
		return 1
	}
	sum :=  1
	f := []int{}
	siz := []int{}
	for k,v := range mp{
		sum += v
		f = append(f,dp[k])
		siz = append(siz,v)
	}
	dp[x] = max(dp[f[0]]+siz[1]-1, dp[f[1]]+siz[0]-1)
	return sum
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	cnt := 0
	for t > 0{
		cnt++
		t--
		var n int
		fmt.Fscan(in,&n)
		u = make([]int,4*n)
		v = make([]int, 4*n)
		nex = make([]int, 4*n)
		first = make([]int,4*n)
		dp = make([]int, 4*n)
		tot = 0
		var s int64 = 0
		for i:=1;i<=n-1;i++{
			var x, y int
			fmt.Fscan(in,&x,&y)
			s = 10*s + int64(x)
			s = 10*s + int64(y)
			add(x,y)
			add(y,x)
		}
		if cnt == 88{
			fmt.Fprintln(out,s)
		}
		search(1,-1)
		fmt.Fprintln(out,dp[1])
	}
}

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 abs(x int)int{
	if x<0{
		return -x
	}
	return x
}
var u []int
var v []int
var nex []int
var first []int
var dp []int
var tot int
func add(x, y int){
	tot++
	u[tot]=x
	v[tot]=y
	nex[tot]=first[x]
	first[x] = tot
}
func search(x, fa int) int{
	dp[x]=0
	mp := map[int]int{}
	for i:= first[x] ;i!=0;i=nex[i]{
		if v[i]!=fa{
			ret := search(v[i],x)
			mp[v[i]]=ret
		}
	}
	if len(mp) == 1{
		for _, v:= range mp{
			dp[x] = v-1
			return v+1
		}
	}
	if len(mp) == 0{
		return 1
	}
	sum :=  1
	f := []int{}
	siz := []int{}
	for k,v := range mp{
		sum += v
		f = append(f,dp[k])
		siz = append(siz,v)
	}
	dp[x] = max(dp[f[0]]+siz[1]-1, dp[f[1]]+siz[0]-1)
	return sum
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	cnt := 0
	for t > 0{
		cnt++
		t--
		var n int
		fmt.Fscan(in,&n)
		u = make([]int,4*n)
		v = make([]int, 4*n)
		nex = make([]int, 4*n)
		first = make([]int,4*n)
		dp = make([]int, 4*n)
		tot = 0
		var s int64 = 0
		for i:=1;i<=n-1;i++{
			var x, y int
			fmt.Fscan(in,&x,&y)
			s = 10*s + int64(x)
			s = 10*s + int64(y)
			add(x,y)
			add(y,x)
		}
		if cnt == 88{
			fmt.Fprintln(out,n)
		}
		search(1,-1)
		fmt.Fprintln(out,dp[1])
	}
}

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 abs(x int)int{
	if x<0{
		return -x
	}
	return x
}
var u []int
var v []int
var nex []int
var first []int
var dp []int
var tot int
func add(x, y int){
	tot++
	u[tot]=x
	v[tot]=y
	nex[tot]=first[x]
	first[x] = tot
}
func search(x, fa int) int{
	dp[x]=0
	mp := map[int]int{}
	for i:= first[x] ;i!=0;i=nex[i]{
		if v[i]!=fa{
			ret := search(v[i],x)
			mp[v[i]]=ret
		}
	}
	if len(mp) == 1{
		for _, v:= range mp{
			dp[x] = v-1
			return v+1
		}
	}
	if len(mp) == 0{
		return 1
	}
	sum :=  1
	f := []int{}
	siz := []int{}
	for k,v := range mp{
		sum += v
		f = append(f,dp[k])
		siz = append(siz,v)
	}
	dp[x] = max(dp[f[0]]+siz[1]-1, dp[f[1]]+siz[0]-1)
	return sum
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	cnt := 0
	for t > 0{
		cnt++
		t--
		var n int
		fmt.Fscan(in,&n)
		u = make([]int,4*n)
		v = make([]int, 4*n)
		nex = make([]int, 4*n)
		first = make([]int,4*n)
		dp = make([]int, 4*n)
		tot = 0
		s := 0
		for i:=1;i<=n-1;i++{
			var x, y int
			fmt.Fscan(in,&x,&y)
			s = 10*s + x
			s = 10*s + y
			add(x,y)
			add(y,x)
		}
		if cnt == 88{
			fmt.Fprintln(out,n)
		}
		search(1,-1)
		fmt.Fprintln(out,dp[1])
	}
}

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 abs(x int)int{
	if x<0{
		return -x
	}
	return x
}
var u []int
var v []int
var nex []int
var first []int
var dp []int
var tot int
func add(x, y int){
	tot++
	u[tot]=x
	v[tot]=y
	nex[tot]=first[x]
	first[x] = tot
}
func search(x, fa int) int{
	dp[x]=0
	mp := map[int]int{}
	for i:= first[x] ;i!=0;i=nex[i]{
		if v[i]!=fa{
			ret := search(v[i],x)
			mp[v[i]]=ret
		}
	}
	if len(mp) == 1{
		for _, v:= range mp{
			dp[x] = v-1
			return v+1
		}
	}
	if len(mp) == 0{
		return 1
	}
	sum :=  1
	f := []int{}
	siz := []int{}
	for k,v := range mp{
		sum += v
		f = append(f,dp[k])
		siz = append(siz,v)
	}
	dp[x] = max(dp[f[0]]+siz[1]-1, dp[f[1]]+siz[0]-1)
	return sum
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	cnt := 0
	for t > 0{
		cnt++
		t--
		var n int
		fmt.Fscan(in,&n)
		u = make([]int,4*n)
		v = make([]int, 4*n)
		nex = make([]int, 4*n)
		first = make([]int,4*n)
		dp = make([]int, 4*n)
		tot = 0
		s := 0
		for i:=1;i<=n-1;i++{
			var x, y int
			fmt.Fscan(in,&x,&y)
			s = 10*s + x
			s = 10*s + y
			add(x,y)
			add(y,x)
		}
		if cnt == 88{
			fmt.Fprintln(out,s)
		}
		search(1,-1)
		fmt.Fprintln(out,dp[1])
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)
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 abs(x int)int{
	if x<0{
		return -x
	}
	return x
}
var u []int
var v []int
var nex []int
var first []int
var dp []int
var tot int
func add(x, y int){
	tot++
	u[tot]=x
	v[tot]=y
	nex[tot]=first[x]
	first[x] = tot
}
func search(x, fa int) int{
	dp[x]=0
	mp := map[int]int{}
	for i:= first[x] ;i!=0;i=nex[i]{
		if v[i]!=fa{
			ret := search(v[i],x)
			mp[v[i]]=ret
		}
	}
	if len(mp) == 1{
		for _, v:= range mp{
			dp[x] = v-1
			return v+1
		}
	}
	if len(mp) == 0{
		return 1
	}
	sum :=  1
	f := []int{}
	siz := []int{}
	for k,v := range mp{
		sum += v
		f = append(f,dp[k])
		siz = append(siz,v)
	}
	dp[x] = max(dp[f[0]]+siz[1]-1, dp[f[1]]+siz[0]-1)
	return sum
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	cnt := 0
	for t > 0{
		cnt++
		t--
		var n int
		fmt.Fscan(in,&n)
		u = make([]int,4*n)
		v = make([]int, 4*n)
		nex = make([]int, 4*n)
		first = make([]int,4*n)
		dp = make([]int, 4*n)
		tot = 0
		s := []byte{}
		for i:=1;i<=n-1;i++{
			var x, y int
			fmt.Fscan(in,&x,&y)
			s = append(s,byte(x))
			s = append(s,byte(y))
			add(x,y)
			add(y,x)
		}
		if cnt == 88{
			yo, _ := strconv.Atoi(string(s))
			fmt.Fprintln(out,yo)
		}
		search(1,-1)
		fmt.Fprintln(out,dp[1])
	}
}

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 abs(x int)int{
	if x<0{
		return -x
	}
	return x
}
var u []int
var v []int
var nex []int
var first []int
var dp []int
var tot int
func add(x, y int){
	tot++
	u[tot]=x
	v[tot]=y
	nex[tot]=first[x]
	first[x] = tot
}
func search(x, fa int) int{
	dp[x]=0
	mp := map[int]int{}
	for i:= first[x] ;i!=0;i=nex[i]{
		if v[i]!=fa{
			ret := search(v[i],x)
			mp[v[i]]=ret
		}
	}
	if len(mp) == 1{
		for _, v:= range mp{
			dp[x] = v-1
			return v+1
		}
	}
	if len(mp) == 0{
		return 1
	}
	sum :=  1
	f := []int{}
	siz := []int{}
	for k,v := range mp{
		sum += v
		f = append(f,dp[k])
		siz = append(siz,v)
	}
	dp[x] = max(dp[f[0]]+siz[1]-1, dp[f[1]]+siz[0]-1)
	return sum
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	cnt := 0
	for t > 0{
		cnt++
		t--
		var n int
		fmt.Fscan(in,&n)
		u = make([]int,4*n)
		v = make([]int, 4*n)
		nex = make([]int, 4*n)
		first = make([]int,4*n)
		dp = make([]int, 4*n)
		tot = 0
		s := []byte{}
		for i:=1;i<=n-1;i++{
			var x, y int
			fmt.Fscan(in,&x,&y)
			s = append(s,byte(x))
			s = append(s,byte(y))
			add(x,y)
			add(y,x)
		}
		if cnt == 88{
			fmt.Fprintln(out,string(s))
		}
		search(1,-1)
		fmt.Fprintln(out,dp[1])
	}
}

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 abs(x int)int{
	if x<0{
		return -x
	}
	return x
}
var u []int
var v []int
var nex []int
var first []int
var dp []int
var tot int
func add(x, y int){
	tot++
	u[tot]=x
	v[tot]=y
	nex[tot]=first[x]
	first[x] = tot
}
func search(x, fa int) int{
	dp[x]=0
	mp := map[int]int{}
	for i:= first[x] ;i!=0;i=nex[i]{
		if v[i]!=fa{
			ret := search(v[i],x)
			mp[v[i]]=ret
		}
	}
	if len(mp) == 1{
		for _, v:= range mp{
			dp[x] = v-1
			return v+1
		}
	}
	if len(mp) == 0{
		return 1
	}
	sum :=  1
	f := []int{}
	siz := []int{}
	for k,v := range mp{
		sum += v
		f = append(f,dp[k])
		siz = append(siz,v)
	}
	dp[x] = max(dp[f[0]]+siz[1]-1, dp[f[1]]+siz[0]-1)
	return sum
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	cnt := 0
	for t > 0{
		cnt++
		t--
		var n int
		fmt.Fscan(in,&n)
		u = make([]int,4*n)
		v = make([]int, 4*n)
		nex = make([]int, 4*n)
		first = make([]int,4*n)
		dp = make([]int, 4*n)
		tot = 0
		s := []byte{}
		for i:=1;i<=n-1;i++{
			var x, y int
			fmt.Fscan(in,&x,&y)
			s = append(s,byte(x))
			s = append(s,byte(y))
			add(x,y)
			add(y,x)
		}
		if cnt == 88{
			fmt.Fprintln(out,string(s))
		}
		search(1,-1)
		fmt.Println(dp[2])
		fmt.Fprintln(out,dp[1])
	}
}

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 abs(x int)int{
	if x<0{
		return -x
	}
	return x
}
var u []int
var v []int
var nex []int
var first []int
var dp []int
var tot int
func add(x, y int){
	tot++
	u[tot]=x
	v[tot]=y
	nex[tot]=first[x]
	first[x] = tot
}
func search(x, fa int) int{
	dp[x]=0
	mp := map[int]int{}
	for i:= first[x] ;i!=0;i=nex[i]{
		if v[i]!=fa{
			ret := search(v[i],x)
			mp[v[i]]=ret
		}
	}
	if len(mp) == 1{
		for _, v:= range mp{
			dp[x] = v-1
			return v+1
		}
	}
	if len(mp) == 0{
		return 1
	}
	sum :=  1
	f := []int{}
	siz := []int{}
	for k,v := range mp{
		sum += v
		f = append(f,dp[k])
		siz = append(siz,v)
	}
	dp[x] = max(dp[f[0]]+siz[1]-1, dp[f[1]]+siz[0]-1)
	return sum
}
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)
		u = make([]int,4*n)
		v = make([]int, 4*n)
		nex = make([]int, 4*n)
		first = make([]int,4*n)
		dp = make([]int, 4*n)
		tot = 0
		for i:=1;i<=n-1;i++{
			var x, y int
			fmt.Fscan(in,&x,&y)
			add(x,y)
			add(y,x)
		}
		search(1,-1)
		fmt.Fprintln(out,dp[1])
	}
}

package main

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

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

	return y
}

type Graph struct {
	n     int
	edges [][]int
}

func (graph *Graph) Add(v int, u int) {
	graph.edges[v] = append(graph.edges[v], u)
	graph.edges[u] = append(graph.edges[u], v)
}

func (graph *Graph) Bfs() int {
	if len(graph.edges[0]) == 0 {
		return 0
	}
	if len(graph.edges[0]) == 1 {
		return graph.n - 2
	}
	var visited = make([]bool, graph.n)
	var children = make([]int, graph.n)
	graph.Dfs(visited, 0, children)
	visited = make([]bool, graph.n)
	visited[0] = true
	return Max(children[graph.edges[0][0]]+graph.Visit(visited, graph.edges[0][1], children), children[graph.edges[0][1]]+graph.Visit(visited, graph.edges[0][0], children))
}

func (graph *Graph) Visit(visited []bool, v int, children []int) int {
	visited[v] = true
	if len(graph.edges[v]) == 1 {
		return 0
	}
	if len(graph.edges[v]) == 2 {
		return Max(0, children[v]-1)
	}
	var first = 0
	if visited[graph.edges[v][0]] {
		first = 1
	}
	var second = 1
	if visited[graph.edges[v][0]] {
		second = 2
	} else {
		if visited[graph.edges[v][1]] {
			second = 2
		}
	}
	var leftEdges = len(graph.edges[graph.edges[v][first]])
	var rightEdges = len(graph.edges[graph.edges[v][second]])
	var leftChildren = children[graph.edges[v][first]]
	var rightChildren = children[graph.edges[v][second]]
	if leftEdges == 2 || rightEdges == 2 {
		return Max(0, leftChildren+rightChildren-1)
	}

	var max1 = children[graph.edges[v][second]] + graph.Visit(visited, graph.edges[v][first], children)
	var max2 = children[graph.edges[v][first]] + graph.Visit(visited, graph.edges[v][second], children)
	return Max(max1, max2)
}

func (graph *Graph) Dfs(visited []bool, v int, children []int) int {
	visited[v] = true
	var result = 0
	for u := 0; u < len(graph.edges[v]); u++ {
		if !visited[graph.edges[v][u]] {
			result += graph.Dfs(visited, graph.edges[v][u], children)
		}
	}

	children[v] = result

	return result + 1
}

func main() {
	//f, _ := os.Open("input.txt")
	var reader = bufio.NewReader(os.Stdin)
	//var reader = bufio.NewReader(f)
	var tests int
	fmt.Fscan(reader, &tests)
	for ; tests > 0; tests-- {
		var n int
		fmt.Fscan(reader, &n)
		var graph = Graph{n: n, edges: make([][]int, n)}
		for i := 0; i < n-1; i++ {
			var u, v int
			fmt.Fscan(reader, &u, &v)
			graph.Add(v-1, u-1)
		}
		fmt.Println(graph.Bfs())

	}
}

package main

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

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

	return y
}

type Graph struct {
	n     int
	edges [][]int
}

func (graph *Graph) Add(v int, u int) {
	graph.edges[v] = append(graph.edges[v], u)
	graph.edges[u] = append(graph.edges[u], v)
}

func (graph *Graph) Bfs() int {
	if len(graph.edges[0]) == 0 {
		return 0
	}
	if len(graph.edges[0]) == 1 {
		return graph.n - 2
	}
	var visited = make([]bool, graph.n)
	var children = make([]int, graph.n)
	graph.Dfs(visited, 0, children)
	visited = make([]bool, graph.n)
	visited[0] = true
	return Max(children[graph.edges[0][0]]+graph.Visit(visited, graph.edges[0][0], children), children[graph.edges[0][1]]+graph.Visit(visited, graph.edges[0][1], children))
}

func (graph *Graph) Visit(visited []bool, v int, children []int) int {
	visited[v] = true
	if len(graph.edges[v]) == 1 {
		return 0
	}
	if len(graph.edges[v]) == 2 {
		return Max(0, children[v]-1)
	}
	var first = 0
	if visited[graph.edges[v][0]] {
		first = 1
	}
	var second = 1
	if visited[graph.edges[v][0]] {
		second = 2
	} else {
		if visited[graph.edges[v][1]] {
			second = 2
		}
	}
	var leftEdges = len(graph.edges[graph.edges[v][first]])
	var rightEdges = len(graph.edges[graph.edges[v][second]])
	var leftChildren = children[graph.edges[v][first]]
	var rightChildren = children[graph.edges[v][second]]
	if leftEdges == 2 || rightEdges == 2 {
		return Max(0, leftChildren+rightChildren-1)
	}

	var max1 = children[graph.edges[v][second]] + graph.Visit(visited, graph.edges[v][first], children)
	var max2 = children[graph.edges[v][first]] + graph.Visit(visited, graph.edges[v][second], children)
	return Max(max1, max2)
}

func (graph *Graph) Dfs(visited []bool, v int, children []int) int {
	visited[v] = true
	var result = 0
	for u := 0; u < len(graph.edges[v]); u++ {
		if !visited[graph.edges[v][u]] {
			result += graph.Dfs(visited, graph.edges[v][u], children)
		}
	}

	children[v] = result

	return result + 1
}

func main() {
	//f, _ := os.Open("input.txt")
	var reader = bufio.NewReader(os.Stdin)
	var tests int
	fmt.Fscan(reader, &tests)
	for ; tests > 0; tests-- {
		var n int
		fmt.Fscan(reader, &n)
		var graph = Graph{n: n, edges: make([][]int, n)}
		for i := 0; i < n-1; i++ {
			var u, v int
			fmt.Fscan(reader, &u, &v)
			graph.Add(v-1, u-1)
		}
		fmt.Println(graph.Bfs())

	}
}

package main

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

type Tree struct {
	elem     int
	children int
	left     *Tree
	right    *Tree
}

func NewTree(elem int, left, right *Tree) *Tree {
	return &Tree{elem: elem, left: left, right: right}
}

func NewTreeFromEdges(edges [][]int, startNode int) *Tree {
	built := make([]bool, len(edges))
	built[startNode] = true
	var treeFromEdges func(edges [][]int, currelem int) *Tree
	treeFromEdges = func(edges [][]int, currelem int) *Tree {
		built[currelem] = true
		if len(edges[currelem]) == 1 {
			return NewTree(currelem, nil, nil)
		} else {
			slc := make([]int, 0) // len = 1 or len = 2
			for _, el := range edges[currelem] {
				if !built[el] {
					slc = append(slc, el)
				}
			}
			if len(slc) == 2 {
				return NewTree(currelem, treeFromEdges(edges, slc[0]), treeFromEdges(edges, slc[1]))
			} else {
				return NewTree(currelem, nil, treeFromEdges(edges, slc[0]))
			}
		}
	}
	return treeFromEdges(edges, startNode)
}

func (t *Tree) countChildren() int {
	if t == nil {
		return 0
	}
	if t.left == nil && t.right == nil {
		t.children = 0
	} else {
		if t.left != nil && t.right != nil {
			t.children = 2 + t.left.countChildren() + t.right.countChildren()
		} else {
			t.children = 1 + t.left.countChildren() + t.right.countChildren()
		}
	}
	return t.children
}

func getChildren(t *Tree) int {
	if t == nil {
		return 0
	} else {
		return t.children
	}
}

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

func saveFromInfestation(t *Tree) int {
	if t == nil {
		return 0
	} else {
		l := getChildren(t.left)
		r := getChildren(t.right)
		return max(saveFromInfestation(t.left)+r, saveFromInfestation(t.right)+l)
	}
}

func main() {
	scanner := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var ntests int
	fmt.Fscan(scanner, &ntests)
	for iter := 0; iter < ntests; iter++ {
		var n int
		fmt.Fscan(scanner, &n)
		graph := make([][]int, n)
		for i := 0; i < n; i++ {
			graph[i] = make([]int, 0)
		}
		for i := 1; i < n; i++ {
			var l, r int
			fmt.Fscan(scanner, &l, &r)
			l--
			r--
			graph[l] = append(graph[l], r)
			graph[r] = append(graph[r], l)
		}
		t := NewTreeFromEdges(graph, 0)
		t.countChildren()
		fmt.Fprintln(writer, saveFromInfestation(t))
	}
}


ZS the Coder and Chris the Baboon are travelling to Udayland! To get there, they have to get on the special IOI bus. The IOI bus has n rows of seats. There are 4 seats in each row, and the seats are separated into pairs by a walkway. When ZS and Chris came, some places in the bus was already occupied.ZS and Chris are good friends. They insist to get a pair of neighbouring empty seats. Two seats are considered neighbouring if they are in the same row and in the same pair. Given the configuration of the bus, can you help ZS and Chris determine where they should sit?

NoteNote that the following is an incorrect configuration for the first sample case because the seats must be in the same pair.O+|+XXO|XXOX|OOXX|OXOO|OOOO|XX

package main

import "fmt"

func main() {
    var n int
    var rows [1000]string

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

    // Input rows and determine open seats
    flag := false
    for i := 0; i < n; i++ {
        fmt.Scanf("%s\n", &rows[i])
        if (!flag && rows[i][0] == rows[i][1] && rows[i][0] == 'O') {
            rows[i] = "++" + rows[i][2:]
            flag = true
        } else if (!flag && rows[i][3] == rows[i][4] && rows[i][3] == 'O') {
            rows[i] = rows[i][:3] + "++"
            flag = true
        }
    }

    // Output
    if flag {
        fmt.Println("YES")
        for i := 0; i < n; i++ {
            fmt.Printf("%s\n", rows[i])
        }
    } else {
        fmt.Println("NO")
    }
}

package main

import (
	"fmt"
)

func main() {

	var n int
	fmt.Scan(&n)
	configurations := make([][] byte, n)
	for i:=0; i<n;i++ {
		configurations[i] = make([] byte, 5)
		fmt.Scan(&configurations[i])
	}
	found := false
	for _, configuration := range configurations {
		if !found && configuration[0] == 'O' && configuration[1] == 'O' {
			configuration[0] = '+'
			configuration[1] = '+'
			found = true
		} else if !found && configuration[3] == 'O' && configuration[4] == 'O' {
			configuration[3] = '+'
			configuration[4] = '+'
			found = true
		}
	}
	if !found {
		fmt.Println("NO")
		return
	} else {
		fmt.Println("YES")
		for _, configuration := range configurations {
			fmt.Printf("%s\n", configuration)
		}
	}

}

package main

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

func main() {
	var n int
	var line string
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &n)
	result := make([]string, 0)
	hasPair := false
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &line)
		if !hasPair {
			s := strings.Split(line, "|")
			if s[0] == "OO" {
				hasPair = true
				result = append(result, "++|"+s[1])
			} else if s[1] == "OO" {
				hasPair = true
				result = append(result, s[0]+"|++")
			} else {
				result = append(result, line)
			}
		} else {
			result = append(result, line)
		}
	}
	if hasPair {
		fmt.Println("YES")
		for i := 0; i < n; i++ {
			fmt.Println(result[i])
		}
	} else {
		fmt.Println("NO")
	}
}

// A. Bus to Udayland
/*
ZS the Coder and Chris the Baboon are travelling to Udayland! To get there,
they have to get on the special IOI bus. The IOI bus has n rows of seats.
There are 4 seats in each row, and the seats are separated into pairs by a
walkway. When ZS and Chris came, some places in the bus was already occupied.

ZS and Chris are good friends. They insist to get a pair of neighbouring
empty seats. Two seats are considered neighbouring if they are in the same row
and in the same pair. Given the configuration of the bus, can you help ZS and
Chris determine where they should sit?

Input
The first line of the input contains a single integer n (1 ≤ n ≤ 1000) —
the number of rows of seats in the bus.

Then, n lines follow. Each line contains exactly 5 characters,
the first two of them denote the first pair of seats in the row,
the third character denotes the walkway (it always equals '|') and the last
two of them denote the second pair of seats in the row.

Each character, except the walkway, equals to 'O' or to 'X'. 'O'
denotes an empty seat, 'X' denotes an occupied seat. See the sample
cases for more details.

Output
If it is possible for Chris and ZS to sit at neighbouring empty seats,
print "YES" (without quotes) in the first line. In the next n lines print
the bus configuration, where the characters in the pair of seats for Chris
and ZS is changed with characters '+'. Thus the configuration should differ
from the input one by exactly two charaters (they should be equal to 'O'
in the input and to '+' in the output).

If there is no pair of seats for Chris and ZS, print "NO" (without quotes)
in a single line.

If there are multiple solutions, you may print any of them.

Examples
Input
6
OO|OX
XO|XX
OX|OO
XX|OX
OO|OO
OO|XX

Output
YES
++|OX
XO|XX
OX|OO
XX|OX
OO|OO
OO|XX

Input
4
XO|OX
XO|XX
OX|OX
XX|OX

Output
NO

Input
5
XX|XX
XX|XX
XO|OX
XO|OO
OX|XO

Output
YES
XX|XX
XX|XX
XO|OX
XO|++
OX|XO

Note
Note that the following is an incorrect configuration for the first sample case
because the seats must be in the same pair.

O+|+X

XO|XX

OX|OO

XX|OX

OO|OO

OO|XX
*/
package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	input1, _ := reader.ReadString('\n')
	n, _ := strconv.Atoi(strings.TrimSpace(input1))

	out := ""
	found := false
	for ; n > 0; n-- {
		input2, _ := reader.ReadString('\n')
		arr := strings.Split(strings.TrimSpace(input2), "|")

		if !found && arr[0] == "OO" {
			found = true
			out += fmt.Sprintf("++|%v\n", arr[1])
		} else if !found && arr[1] == "OO" {
			found = true
			out += fmt.Sprintf("%v|++\n", arr[0])
		} else {
			out += fmt.Sprint(input2)
		}
	}

	if found {
		fmt.Println("YES")
		fmt.Print(out)
	} else {
		fmt.Println("NO")
	}
}

package main

import "fmt"
import "bufio"
import "os"

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

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

	seats := make([][]byte, n)
	found := false

	for i := 0; i < n; i++ {
		seats[i] = make([]byte, 5)
		fmt.Fscanf(buf, "%s\n", &seats[i])
		if !found {
			if (seats[i][0] == 'O') && (seats[i][1] == 'O') {
				found = true
				seats[i][0] = '+'
				seats[i][1] = '+'
			} else if (seats[i][3] == 'O') && (seats[i][4] == 'O') {
				found = true
				seats[i][3] = '+'
				seats[i][4] = '+'
			}
		}
	}

	if found {
		fmt.Println("YES")
		for i := 0; i < n; i++ {
			fmt.Printf("%s\n", seats[i])
		}
	} else {
		fmt.Println("NO")
	}

}

package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)

	seats := make([][]byte, n)
	found := false

	for i := 0; i < n; i++ {
		seats[i] = make([]byte, 5)
		fmt.Scan(&seats[i])
		if !found {
			if (seats[i][0] == 'O') && (seats[i][1] == 'O') {
				found = true
				seats[i][0] = '+'
				seats[i][1] = '+'
			} else if (seats[i][3] == 'O') && (seats[i][4] == 'O') {
				found = true
				seats[i][3] = '+'
				seats[i][4] = '+'
			}
		}
	}

	if found {
		fmt.Println("YES")
		for i := 0; i < n; i++ {
			fmt.Printf("%s\n", seats[i])
		}
	} else {
		fmt.Println("NO")
	}
}

package main

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

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

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

	seats := make([][]byte, n)
	found := false

	for i := 0; i < n; i++ {
		seats[i] = make([]byte, 5)
		fmt.Fscanf(buf, "%s\n", &seats[i])
		if !found {
			if (seats[i][0] == 'O') && (seats[i][1] == 'O') {
				found = true
				seats[i][0] = '+'
				seats[i][1] = '+'
			} else if (seats[i][3] == 'O') && (seats[i][4] == 'O') {
				found = true
				seats[i][3] = '+'
				seats[i][4] = '+'
			}
		}
	}

	if found {
		fmt.Println("YES")
		for i := 0; i < n; i++ {
			fmt.Printf("%s\n", seats[i])
		}
	} else {
		fmt.Println("NO")
	}

}

package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)

	pairs := make([]string, 0, 2*n)
	flgOK := false
	for range make([]struct{}, n) {
		var s string
		fmt.Scan(&s)

		for range make([]struct{}, 2) {
			if s[0] == '|' {
				s = s[1:]
			}
			pairs = append(pairs, s[:2])
			s = s[2:]

			i := len(pairs) - 1
			if !flgOK && pairs[i] == "OO" {
				pairs[i] = "++"
				flgOK = true
			}
		}
	}
	if !flgOK {
		fmt.Println("NO")
	} else {
		fmt.Println("YES")
		for i, v := range pairs {
			fmt.Print(v)
			fmt.Print([]string{"|", "\n"}[i%2])
		}
	}
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)

	seq := make([]string, n)
	flgOK := false
	for i := range seq {
		fmt.Scan(&seq[i])
		v := seq[i]

		if flgOK {
			continue
		} else if v[:2] == "OO" {
			seq[i] = "++" + v[2:]
		} else if v[3:] == "OO" {
			seq[i] = v[:3] + "++"
		} else {
			continue
		}
		flgOK = true
	}

	if !flgOK {
		fmt.Println("NO")
	} else {
		fmt.Println("YES")
		for _, v := range seq {
			fmt.Println(v)
		}
	}
}

package main

import "fmt"

func main() {
	var n, idx int
	var free bool

	fmt.Scanln(&n)

	buff := make([]string, n)

	for i := 0; i < n; i++ {
		var tmp string
		fmt.Scanln(&tmp)
		buff[i] = tmp
		if !free {
			free = (buff[i][0] == buff[i][1] && buff[i][0] == "O"[0]) || (buff[i][3] == buff[i][4] && buff[i][3] == "O"[0])

			if free {
				idx = i
			}
		}
	}

	if !free {
		fmt.Println("NO")
		return
	}

	// Output answer
	fmt.Println("YES")

	for i := 0; i < n; i++ {
		if i != idx {
			fmt.Println(buff[i])
		} else {
			if (buff[i][0] == buff[i][1] && buff[i][0] == "O"[0]) {
				fmt.Println("++" + buff[i][2:5])

			} else {
				fmt.Println(buff[i][0:3] + "++")
			}
		}
	}
}

package main

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

func main() {
	var n int
	var list []string
	fmt.Scanf("%d \n", &n)
	scanner := bufio.NewScanner(os.Stdin)
	result := "NO"
	for i := 0; i < n; i++ {
		scanner.Scan()
		temp := scanner.Text()
		if strings.Compare(result, "NO") == 0 && strings.Contains(temp, "OO") {
			temp = strings.Replace(temp, "OO", "++", 1)
			result = "YES"
		}
		list = append(list, temp)
	}
	fmt.Println(result)
	if strings.Compare(result, "YES") == 0 {
		for i := 0; i < n; i++ {
			fmt.Println(list[i])
		}
	}
}

package main

import (
	"fmt"
	"strings"
)

func main() {
	var n int
	b := []string{}
	ok := false
	fmt.Scan(&n)
	for i:=0; i<n; i++ {
		var x, y string
		fmt.Scan(&x)
		if (!ok) {
			y = strings.Replace(x, "OO", "++", 1)
			ok = x!=y
			x = y
		}
		b = append(b, x)
	}
	if ok {
		fmt.Println("YES")
		fmt.Println(strings.Join(b, "\n"))
	} else {
		fmt.Println("NO")
	}
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)

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

	ans := -1
	line := "++"
	for i := 0; i < n; i++ {
		rs := []rune(seats[i])
		if rs[0] == 'O' && rs[1] == 'O' {
			ans = i
			line += string(rs[2:5])
			break
		}
		if rs[3] == 'O' && rs[4] == 'O' {
			ans = i
			line = string(rs[0:3]) + line
			break
		}
	}

	if ans == -1 {
		fmt.Println("NO")
	} else {
		fmt.Println("YES")
		for i := 0; i < ans; i++ {
			fmt.Println(seats[i])
		}
		fmt.Println(line)
		for i := ans + 1; i < n; i++ {
			fmt.Println(seats[i])
		}
	}
}

package main

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

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

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

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

func main() {
	var T int
	scanf("%d \n", &T)
	var A []string
	for i:=0; i<T;i++ {
		var S string
		scanf("%s \n", &S)
		A = append(A, S)
	}

	found := false
	for i, v := range A {

		if strings.Contains(v, "OO") {
			found = true
			A[i] = strings.Replace(v, "OO", "++", 1)
			break
		}
	}

	if found {
		fmt.Println("YES")
		for _, v := range A {
			fmt.Println(v)
		}
	}else {
		fmt.Println("NO")
	}

}

package main

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

func main() {
	var n int
	fmt.Scan(&n)
	scanner := bufio.NewScanner(os.Stdin)
	var out string
	found := false
	for scanner.Scan() {
		s := scanner.Text()
		seats := strings.Split(s, "|")
		if len(seats) < 2 {
			continue
		}
		if !found && seats[0] == "OO" {
			found = true
			out += "++|"
		} else {
			out += seats[0] + "|"
		}
		if !found && seats[1] == "OO" {
			found = true
			out += "++\n"
		} else {
			out += seats[1] + "\n"
		}
	}
	if found {
		fmt.Print("YES\n")
		fmt.Print(out)
	} else {
		fmt.Print("NO")
	}
}

package main

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

func main() {
	sc := NewScanner()
	N := sc.NextInt()
	ans := make([]string, N)
	hit := false
	for i := 0; i < N; i++ {
		A := strings.Split(sc.NextLine(), "|")
		for j := 0; j < 2; j++ {
			if A[j] == "OO" && !hit {
				A[j] = "++"
				hit = true
			}
		}
		ans[i] = A[0] + "|" + A[1]
	}
	if !hit {
		fmt.Println("NO")
		return
	}
	fmt.Println("YES")
	for _, v := range ans {
		fmt.Println(v)
	}

}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r:rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			result = append(result, v)
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	result = append(result, v)

	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n int
	var str []byte
	fmt.Scan(&n)

	for ; n > 0; n-- {
		tmp, _ := reader.ReadBytes(byte('\r'))
		str = append(str, tmp...)
	}

	if strings.Index(string(str), "OO") != -1 {
		fmt.Print("YES")
		str = []byte(strings.Replace(string(str), "OO", "++", 1))
		fmt.Println(string(str))
	} else {
		fmt.Println("NO")
	}
}

package main

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

func main() {
	fsc := NewFastScanner()
	N := fsc.NextInt()
	ok := false
	var seats []string
	for i := 0; i < N; i++ {
		text := []byte(fsc.Next())
		if !ok {
			for j := 0; j < len(text)-1; j++ {
				if text[j] == 'O' && text[j+1] == 'O' {
					text[j] = '+'
					text[j+1] = '+'
					ok = true
					break
				}
			}
		}
		seats = append(seats, string(text))
	}
	if ok {
		fmt.Println("YES")
		for _, seat := range seats {
			fmt.Println(seat)
		}
	} else {
		fmt.Println("NO")
	}
}

//template
type FastScanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewFastScanner() *FastScanner {
	rdr := bufio.NewReaderSize(os.Stdin, 2028)
	return &FastScanner{r: rdr}
}

func (s *FastScanner) Next() string {
	s.Pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}

func (s *FastScanner) NextInt() int {
	val, err := strconv.Atoi(s.Next())
	if err != nil {
		panic(err)
	}
	return val
}
func (s *FastScanner) NextInt64() int64 {
	val, err := strconv.ParseInt(s.Next(), 10, 64)
	if err != nil {
		panic(err)
	}
	return val
}
func (s *FastScanner) Pre() {
	if s.p >= len(s.buf) {
		s.ReadLine()
		s.p = 0
	}
}

func (s *FastScanner) ReadLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, err := s.r.ReadLine()
		if err != nil {
			panic(err.Error())
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

import "fmt"

func main() {
    var n int
    var rows [1000]string

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

    // Input rows and determine open seats
    flag := false
    for i := 0; i < n; i++ {
        fmt.Scanf("%s", &rows[i])
        if (!flag && rows[i][0] == rows[i][1] && rows[i][0] == 'O') {
            rows[i] = "++" + rows[i][2:]
            flag = true
        } else if (!flag && rows[i][3] == rows[i][4] && rows[i][3] == 'O') {
            rows[i] = rows[i][:3] + "++"
            flag = true
        }
    }

    // Output
    if flag {
        fmt.Println("YES")
        for i := 0; i < n; i++ {
            fmt.Println(rows[i])
        }
    } else {
        fmt.Println("NO")
    }
}

package main

import "fmt"

func main() {
    var n int
    var rows [1000]string

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

    // Input rows and determine open seats
    flag := false
    for i := 0; i < n; i++ {
        fmt.Scanf("%s", &rows[i])
        if (!flag && rows[i][0] == rows[i][1] && rows[i][0] == 'O') {
            rows[i] = "++" + rows[i][2:]
            flag = true
        } else if (!flag && rows[i][3] == rows[i][4] && rows[i][3] == 'O') {
            rows[i] = rows[i][:3] + "++"
            flag = true
        }
    }

    // Output
    if flag {
        fmt.Println("YES")
        for i := 0; i < n; i++ {
            fmt.Printf("%s\n", rows[i])
        }
    } else {
        fmt.Println("NO")
    }
}

package main

import "fmt"

func main() {
    var n int
    var rows [1000]string

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

    // Input rows and determine open seats
    flag := false
    for i := 0; i < n; i++ {
        fmt.Scanf("%s", &rows[i])
        if (!flag && rows[i][0] == rows[i][1] && rows[i][0] == 'O') {
            rows[i] = "++" + rows[i][2:]
            flag = true
        } else if (!flag && rows[i][3] == rows[i][4] && rows[i][3] == 'O') {
            rows[i] = rows[i][:3] + "++"
            flag = true
        }
    }

    // Output
    if flag {
        fmt.Println("YES")
        for i := 0; i < n; i++ {
            fmt.Printf("%s", rows[i])
        }
    } else {
        fmt.Println("NO")
    }
}

package main

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

func main() {
	var n int
	var list []string
	fmt.Scanf("%d \n", &n)
	scanner := bufio.NewScanner(os.Stdin)
	result := "NO"
	for i := 0; i < n; i++ {
		scanner.Scan()
		temp := scanner.Text()
		if strings.Compare(result, "NO") == 0 && strings.Contains(temp, "OO") {
			temp = strings.Replace(temp, "OO", "++", -1)
			result = "YES"
		}
		list = append(list, temp)
	}
	fmt.Println(result)
	if strings.Compare(result, "YES") == 0 {
		for i := 0; i < n; i++ {
			fmt.Println(list[i])
		}
	}
}

package main

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

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

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

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

func main() {
	var T int
	scanf("%d \n", &T)
	var A []string
	for i:=0; i<T;i++ {
		var S string
		scanf("%s \n", &S)
		A = append(A, S)
	}

	found := false
	for i, v := range A {

		if strings.Contains(v, "OO") {
			found = true
			A[i] = strings.Replace(v, "OO", "++", 2)
			break
		}
	}

	if found {
		fmt.Println("YES")
		for _, v := range A {
			fmt.Println(v)
		}
	}else {
		fmt.Println("NO")
	}

}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n int
	var str []byte
	fmt.Scan(&n)
	for ; n > 0; n-- {
		tmp, _ := reader.ReadBytes(byte('\n'))
		str = append(str, tmp...)
	}

	if strings.Index(string(str), "OO") != -1 {
		fmt.Print("YES")
		str = []byte(strings.Replace(string(str), "OO", "++", 1))
		fmt.Println(string(str))
	} else {
		fmt.Println("NO")
	}
}


Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $$$2n$$$ jars of strawberry and blueberry jam.All the $$$2n$$$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $$$n$$$ jars to his left and $$$n$$$ jars to his right.For example, the basement might look like this:  Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right.Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same.For example, this might be the result:  He has eaten $$$1$$$ jar to his left and then $$$5$$$ jars to his right. There remained exactly $$$3$$$ full jars of both strawberry and blueberry jam. Jars are numbered from $$$1$$$ to $$$2n$$$ from left to right, so Karlsson initially stands between jars $$$n$$$ and $$$n+1$$$.What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left?Your program should answer $$$t$$$ independent test cases.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \dots, a_{2n}$$$ ($$$1 \le a_i \le 2$$$) — $$$a_i=1$$$ means that the $$$i$$$-th jar from the left is a strawberry jam jar and $$$a_i=2$$$ means that it is a blueberry jam jar. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case print the answer to it — the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left.

NoteThe picture from the statement describes the first test case.In the second test case the number of strawberry and blueberry jam jars is already equal.In the third test case Karlsson is required to eat all $$$6$$$ jars so that there remain $$$0$$$ jars of both jams.In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $$$1$$$ jar of both jams.

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := 0
	fmt.Fscanf(in, "%d\n", &t)
	for tt := 0; tt < t; tt++ {
		n := 0
		fmt.Fscanf(in, "%d\n", &n)
		a := make([]int, n)
		b := make([]int, n)
		r := 0
		bl := 0
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d", &a[i])
			if a[i] == 1 {
				r++
			} else {
				bl++
			}
		}
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d", &b[i])
			if b[i] == 1 {
				r++
			} else {
				bl++
			}
		}
		fmt.Fscanf(in, "\n")
		c := make([]int, n)
		for i := 0; i < n; i++ {
			c[i] = a[n-1-i]
		}
		a = c
		need := r - bl
		f1 := make(map[int]int)
		f1[0] = 0
		balance := 0
		for i := 0; i < n; i++ {
			if a[i] == 1 {
				balance++
			} else {
				balance--
			}
			if _, ok := f1[balance]; !ok {
				f1[balance] = i + 1
			}
		}
		balance = 0
		if need == 0 {
			fmt.Fprintf(out, "0\n")
			continue
		}
		//fmt.Println(f1)
		ans := 2 * n
		if _, ok := f1[need]; ok {
			ans = f1[need]
		}
		for i := 0; i < n; i++ {
			if b[i] == 1 {
				balance++
			} else {
				balance--
			}
			if _, ok := f1[need-balance]; ok {
				if f1[need-balance]+i+1 < ans {
					ans = f1[need-balance] + i + 1
					//fmt.Println(f1[need-balance], i+1)
				}
			}
		}
		fmt.Fprintf(out, "%d\n", ans)
	}
	return
}

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

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

package main

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

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

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

func init() {
	ReadString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	// r.Buffer(make([]byte, 1024), int(1e+11)) // for AtCoder
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

	return string(res)
}

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

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

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

	return string(res)
}

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

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

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

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

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

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

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

const MOD = 1000000000 + 7
const ALPHABET_NUM = 26
const INF_INT64 = math.MaxInt64
const INF_BIT60 = 1 << 60
const INF_INT32 = math.MaxInt32
const INF_BIT30 = 1 << 30

var t int
var n int
var A []int

func main() {
	t = ReadInt()

	for tc := 0; tc < t; tc++ {
		n = ReadInt()
		A = ReadIntSlice(2 * n)

		solve()
	}
}

func solve() {
	E := make([]int, len(A))
	for i := 0; i < len(A); i++ {
		if A[i] == 2 {
			E[i] = -1
		} else {
			E[i] = 1
		}
	}
	sum := Sum(E...)

	if sum == 0 {
		fmt.Println(0)
		return
	}

	if sum < 0 {
		for i := 0; i < len(E); i++ {
			E[i] = -E[i]
		}
	}
	sum = Sum(E...)

	R, L := []int{}, []int{}
	for i := n - 1; i >= 0; i-- {
		L = append(L, E[i])
	}
	for i := 1; i < n; i++ {
		L[i] += L[i-1]
	}
	for i := n; i < 2*n; i++ {
		R = append(R, E[i])
	}
	for i := 1; i < n; i++ {
		R[i] += R[i-1]
	}
	// PrintDebug("%v\n", L)
	// PrintDebug("%v\n", R)
	// PrintDebug("%d\n", sum)

	leftMemo, rightMemo := make(map[int]int), make(map[int]int)
	leftMemo[0], rightMemo[0] = -1, -1
	for i := 0; i < n; i++ {
		a := L[i]
		if _, ok := leftMemo[a]; ok {
			continue
		} else {
			leftMemo[a] = i
		}
	}
	for i := 0; i < n; i++ {
		a := R[i]
		if _, ok := rightMemo[a]; ok {
			continue
		} else {
			rightMemo[a] = i
		}
	}

	ans := INF_BIT30
	for lv := 0; lv <= sum; lv++ {
		rv := sum - lv
		lidx, lok := leftMemo[lv]
		ridx, rok := rightMemo[rv]
		if lok && rok {
			ChMin(&ans, lidx+ridx+2)
		}
	}
	for rv := 0; rv <= sum; rv++ {
		lv := sum - rv
		lidx, lok := leftMemo[lv]
		ridx, rok := rightMemo[rv]
		if lok && rok {
			ChMin(&ans, lidx+ridx+2)
		}
	}
	fmt.Println(ans)
}

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

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

	return s
}

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

/*
- まずは全探索を検討しましょう
- MODは最後にとりましたか？
- ループを抜けた後も処理が必要じゃありませんか？
- 和・積・あまりを求められたらint64が必要ではありませんか？
- いきなりオーバーフローはしていませんか？
	- MOD取る系はint64必須ですよ？
*/

/*
ASCII code

ASCII   10進数  ASCII   10進数  ASCII   10進数
!       33      "       34      #       35
$       36      %       37      &       38
'       39      (       40      )       41
*       42      +       43      ,       44
-       45      .       46      /       47
0       48      1       49      2       50
3       51      4       52      5       53
6       54      7       55      8       56
9       57      :       58      ;       59
<       60      =       61      >       62
?       63      @       64      A       65
B       66      C       67      D       68
E       69      F       70      G       71
H       72      I       73      J       74
K       75      L       76      M       77
N       78      O       79      P       80
Q       81      R       82      S       83
T       84      U       85      V       86
W       87      X       88      Y       89
Z       90      [       91      \       92
]       93      ^       94      _       95
`       96      a       97      b       98
c       99      d       100     e       101
f       102     g       103     h       104
i       105     j       106     k       107
l       108     m       109     n       110
o       111     p       112     q       113
r       114     s       115     t       116
u       117     v       118     w       119
x       120     y       121     z       122
{       123     |       124     }       125
~       126             127
*/

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

package main

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

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

	var t, n int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n)
		a := make([]int, 2*n)
		sum := 0
		for i := range a {
			Fscan(in, &a[i])
			if a[i] == 2 {
				a[i] = -1
			}
			sum += a[i]
		}
		if sum == 0 {
			Fprintln(out, 0)
			continue
		}
		ans := 2 * n
		l := map[int]int{0: 0}
		s := 0
		for i, v := range a[n:] {
			s += v
			if _, ok := l[s]; !ok {
				l[s] = i + 1
			}
			if s == sum && i+1 < ans {
				ans = i + 1
			}
		}
		for i := n - 1; i >= 0; i-- {
			sum -= a[i]
			if l, ok := l[sum]; ok && n-i+l < ans {
				ans = n - i + l
			}
		}
		Fprintln(out, ans)
	}
}

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

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

package main

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

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

func Solve(n int, a []int) int {
	left := make([]int, n+1)
	for i := 1; i < n+1; i++ {
		if a[i-1] == 1 {
			left[i] = 1 + left[i-1]
		} else {
			left[i] = -1 + left[i-1]
		}
	}
	right := make([]int, n+1)
	for i := 2*n - 1; i >= n; i-- {
		k := i - n
		if a[i] == 1 {
			right[k] = 1 + right[k+1]
		} else {
			right[k] = -1 + right[k+1]
		}
	}
	rightMap := make(map[int]int)
	for i := n; i >= 0; i-- {
		rightMap[right[i]] = i
	}
	minChanges := math.MaxInt32
	for i, elem := range left {
		leftCost := n - i
		rightCost, ok := rightMap[-elem]
		if ok {
			minChanges = min(leftCost+rightCost, minChanges)
		}
	}
	return minChanges
}

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

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

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := 0
	fmt.Fscanf(in, "%d\n", &t)
	for tt := 0; tt < t; tt++ {
		n := 0
		fmt.Fscanf(in, "%d\n", &n)
		a := make([]int, n)
		b := make([]int, n)
		r := 0
		bl := 0
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d", &a[i])
			if a[i] == 1 {
				r++
			} else {
				bl++
			}
		}
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d", &b[i])
			if b[i] == 1 {
				r++
			} else {
				bl++
			}
		}
		fmt.Fscanf(in, "\n")
		c := make([]int, n)
		for i := 0; i < n; i++ {
			c[i] = a[n-1-i]
		}
		a = c
		need := r - bl
		f1 := make(map[int]int)
		f1[0] = 0
		balance := 0
		for i := 0; i < n; i++ {
			if a[i] == 1 {
				balance++
			} else {
				balance--
			}
			if _, ok := f1[balance]; !ok {
				f1[balance] = i + 1
			}
		}
		balance = 0
		if need == 0 {
			fmt.Fprintf(out, "0\n")
			continue
		}
		//fmt.Println(f1)
		ans := 2 * n
		for i := 0; i < n; i++ {
			if b[i] == 1 {
				balance++
			} else {
				balance--
			}
			if _, ok := f1[need-balance]; ok {
				if f1[need-balance]+i+1 < ans {
					ans = f1[need-balance] + i + 1
					//fmt.Println(f1[need-balance], i+1)
				}
			}
		}
		fmt.Fprintf(out, "%d\n", ans)
	}
	return
}

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

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

package main

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

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

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

func init() {
	ReadString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	// r.Buffer(make([]byte, 1024), int(1e+11)) // for AtCoder
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

	return string(res)
}

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

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

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

	return string(res)
}

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

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

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

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

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

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

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

const MOD = 1000000000 + 7
const ALPHABET_NUM = 26
const INF_INT64 = math.MaxInt64
const INF_BIT60 = 1 << 60
const INF_INT32 = math.MaxInt32
const INF_BIT30 = 1 << 30

var t int
var n int
var A []int

func main() {
	t = ReadInt()

	for tc := 0; tc < t; tc++ {
		n = ReadInt()
		A = ReadIntSlice(2 * n)

		solve()
	}
}

func solve() {
	E := make([]int, len(A))
	for i := 0; i < len(A); i++ {
		if A[i] == 2 {
			E[i] = -1
		} else {
			E[i] = 1
		}
	}
	sum := Sum(E...)

	if sum == 0 {
		fmt.Println(0)
		return
	}

	R, L := []int{}, []int{}
	for i := n - 1; i >= 0; i-- {
		L = append(L, E[i])
	}
	for i := 1; i < n; i++ {
		L[i] += L[i-1]
	}
	for i := n; i < 2*n; i++ {
		R = append(R, E[i])
	}
	for i := 1; i < n; i++ {
		R[i] += R[i-1]
	}
	// PrintDebug("%v\n", L)
	// PrintDebug("%v\n", R)
	// PrintDebug("%d\n", sum)

	leftMemo, rightMemo := make(map[int]int), make(map[int]int)
	for i := 0; i < n; i++ {
		a := L[i]
		if _, ok := leftMemo[a]; ok {
			continue
		} else {
			leftMemo[a] = i
		}
	}
	for i := 0; i < n; i++ {
		a := R[i]
		if _, ok := rightMemo[a]; ok {
			continue
		} else {
			rightMemo[a] = i
		}
	}

	ans := INF_BIT30
	for lv := 0; lv <= sum; lv++ {
		rv := sum - lv
		lidx, lok := leftMemo[lv]
		ridx, rok := rightMemo[rv]
		if lok && rok {
			ChMin(&ans, lidx+ridx+2)
		}
	}
	for rv := 0; rv <= sum; rv++ {
		lv := sum - rv
		lidx, lok := leftMemo[lv]
		ridx, rok := rightMemo[rv]
		if lok && rok {
			ChMin(&ans, lidx+ridx+2)
		}
	}
	fmt.Println(ans)
}

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

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

	return s
}

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

/*
- まずは全探索を検討しましょう
- MODは最後にとりましたか？
- ループを抜けた後も処理が必要じゃありませんか？
- 和・積・あまりを求められたらint64が必要ではありませんか？
- いきなりオーバーフローはしていませんか？
	- MOD取る系はint64必須ですよ？
*/

/*
ASCII code

ASCII   10進数  ASCII   10進数  ASCII   10進数
!       33      "       34      #       35
$       36      %       37      &       38
'       39      (       40      )       41
*       42      +       43      ,       44
-       45      .       46      /       47
0       48      1       49      2       50
3       51      4       52      5       53
6       54      7       55      8       56
9       57      :       58      ;       59
<       60      =       61      >       62
?       63      @       64      A       65
B       66      C       67      D       68
E       69      F       70      G       71
H       72      I       73      J       74
K       75      L       76      M       77
N       78      O       79      P       80
Q       81      R       82      S       83
T       84      U       85      V       86
W       87      X       88      Y       89
Z       90      [       91      \       92
]       93      ^       94      _       95
`       96      a       97      b       98
c       99      d       100     e       101
f       102     g       103     h       104
i       105     j       106     k       107
l       108     m       109     n       110
o       111     p       112     q       113
r       114     s       115     t       116
u       117     v       118     w       119
x       120     y       121     z       122
{       123     |       124     }       125
~       126             127
*/

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

package main

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

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

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

func init() {
	ReadString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	// r.Buffer(make([]byte, 1024), int(1e+11)) // for AtCoder
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

	return string(res)
}

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

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

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

	return string(res)
}

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

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

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

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

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

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

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

const MOD = 1000000000 + 7
const ALPHABET_NUM = 26
const INF_INT64 = math.MaxInt64
const INF_BIT60 = 1 << 60
const INF_INT32 = math.MaxInt32
const INF_BIT30 = 1 << 30

var t int
var n int
var A []int

func main() {
	t = ReadInt()

	for tc := 0; tc < t; tc++ {
		n = ReadInt()
		A = ReadIntSlice(2 * n)

		solve()
	}
}

func solve() {
	E := make([]int, len(A))
	for i := 0; i < len(A); i++ {
		if A[i] == 2 {
			E[i] = -1
		} else {
			E[i] = 1
		}
	}
	sum := Sum(E...)

	if sum == 0 {
		fmt.Println(0)
		return
	}

	R, L := []int{}, []int{}
	for i := n - 1; i >= 0; i-- {
		L = append(L, E[i])
	}
	for i := 1; i < n; i++ {
		L[i] += L[i-1]
	}
	for i := n; i < 2*n; i++ {
		R = append(R, E[i])
	}
	for i := 1; i < n; i++ {
		R[i] += R[i-1]
	}
	// PrintDebug("%v\n", L)
	// PrintDebug("%v\n", R)
	// PrintDebug("%d\n", sum)

	leftMemo, rightMemo := make(map[int]int), make(map[int]int)
	for i := 0; i < n; i++ {
		a := L[i]
		if _, ok := leftMemo[a]; ok {
			continue
		} else {
			leftMemo[a] = i
		}
	}
	for i := 0; i < n; i++ {
		a := R[i]
		if _, ok := rightMemo[a]; ok {
			continue
		} else {
			rightMemo[a] = i
		}
	}

	ans := INF_BIT30
	for lv := 0; lv <= sum; lv++ {
		rv := sum - lv
		lidx, lok := leftMemo[lv]
		ridx, rok := rightMemo[rv]
		if lok && rok {
			ChMin(&ans, lidx+ridx+2)
		}
	}
	fmt.Println(ans)
}

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

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

	return s
}

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

/*
- まずは全探索を検討しましょう
- MODは最後にとりましたか？
- ループを抜けた後も処理が必要じゃありませんか？
- 和・積・あまりを求められたらint64が必要ではありませんか？
- いきなりオーバーフローはしていませんか？
	- MOD取る系はint64必須ですよ？
*/

/*
ASCII code

ASCII   10進数  ASCII   10進数  ASCII   10進数
!       33      "       34      #       35
$       36      %       37      &       38
'       39      (       40      )       41
*       42      +       43      ,       44
-       45      .       46      /       47
0       48      1       49      2       50
3       51      4       52      5       53
6       54      7       55      8       56
9       57      :       58      ;       59
<       60      =       61      >       62
?       63      @       64      A       65
B       66      C       67      D       68
E       69      F       70      G       71
H       72      I       73      J       74
K       75      L       76      M       77
N       78      O       79      P       80
Q       81      R       82      S       83
T       84      U       85      V       86
W       87      X       88      Y       89
Z       90      [       91      \       92
]       93      ^       94      _       95
`       96      a       97      b       98
c       99      d       100     e       101
f       102     g       103     h       104
i       105     j       106     k       107
l       108     m       109     n       110
o       111     p       112     q       113
r       114     s       115     t       116
u       117     v       118     w       119
x       120     y       121     z       122
{       123     |       124     }       125
~       126             127
*/

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

package main

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

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

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

func init() {
	ReadString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	// r.Buffer(make([]byte, 1024), int(1e+11)) // for AtCoder
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

	return string(res)
}

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

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

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

	return string(res)
}

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

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

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

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

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

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

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

const MOD = 1000000000 + 7
const ALPHABET_NUM = 26
const INF_INT64 = math.MaxInt64
const INF_BIT60 = 1 << 60
const INF_INT32 = math.MaxInt32
const INF_BIT30 = 1 << 30

var t int
var n int
var A []int

func main() {
	t = ReadInt()

	for tc := 0; tc < t; tc++ {
		n = ReadInt()
		A = ReadIntSlice(2 * n)

		solve()
	}
}

func solve() {
	E := make([]int, len(A))
	for i := 0; i < len(A); i++ {
		if A[i] == 2 {
			E[i] = -1
		} else {
			E[i] = 1
		}
	}
	sum := Sum(E...)

	if sum == 0 {
		fmt.Println(0)
		return
	}

	if sum < 0 {
		for i := 0; i < len(A); i++ {
			A[i] = -A[i]
		}
	}

	R, L := []int{}, []int{}
	for i := n - 1; i >= 0; i-- {
		L = append(L, E[i])
	}
	for i := 1; i < n; i++ {
		L[i] += L[i-1]
	}
	for i := n; i < 2*n; i++ {
		R = append(R, E[i])
	}
	for i := 1; i < n; i++ {
		R[i] += R[i-1]
	}
	// PrintDebug("%v\n", L)
	// PrintDebug("%v\n", R)
	// PrintDebug("%d\n", sum)

	leftMemo, rightMemo := make(map[int]int), make(map[int]int)
	leftMemo[0], rightMemo[0] = -1, -1
	for i := 0; i < n; i++ {
		a := L[i]
		if _, ok := leftMemo[a]; ok {
			continue
		} else {
			leftMemo[a] = i
		}
	}
	for i := 0; i < n; i++ {
		a := R[i]
		if _, ok := rightMemo[a]; ok {
			continue
		} else {
			rightMemo[a] = i
		}
	}

	ans := INF_BIT30
	for lv := 0; lv <= sum; lv++ {
		rv := sum - lv
		lidx, lok := leftMemo[lv]
		ridx, rok := rightMemo[rv]
		if lok && rok {
			ChMin(&ans, lidx+ridx+2)
		}
	}
	for rv := 0; rv <= sum; rv++ {
		lv := sum - rv
		lidx, lok := leftMemo[lv]
		ridx, rok := rightMemo[rv]
		if lok && rok {
			ChMin(&ans, lidx+ridx+2)
		}
	}
	fmt.Println(ans)
}

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

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

	return s
}

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

/*
- まずは全探索を検討しましょう
- MODは最後にとりましたか？
- ループを抜けた後も処理が必要じゃありませんか？
- 和・積・あまりを求められたらint64が必要ではありませんか？
- いきなりオーバーフローはしていませんか？
	- MOD取る系はint64必須ですよ？
*/

/*
ASCII code

ASCII   10進数  ASCII   10進数  ASCII   10進数
!       33      "       34      #       35
$       36      %       37      &       38
'       39      (       40      )       41
*       42      +       43      ,       44
-       45      .       46      /       47
0       48      1       49      2       50
3       51      4       52      5       53
6       54      7       55      8       56
9       57      :       58      ;       59
<       60      =       61      >       62
?       63      @       64      A       65
B       66      C       67      D       68
E       69      F       70      G       71
H       72      I       73      J       74
K       75      L       76      M       77
N       78      O       79      P       80
Q       81      R       82      S       83
T       84      U       85      V       86
W       87      X       88      Y       89
Z       90      [       91      \       92
]       93      ^       94      _       95
`       96      a       97      b       98
c       99      d       100     e       101
f       102     g       103     h       104
i       105     j       106     k       107
l       108     m       109     n       110
o       111     p       112     q       113
r       114     s       115     t       116
u       117     v       118     w       119
x       120     y       121     z       122
{       123     |       124     }       125
~       126             127
*/

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

package main

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

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

	var t, n int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n)
		a := make([]int, 2*n)
		sum := 0
		for i := range a {
			Fscan(in, &a[i])
			if a[i] == 2 {
				a[i] = -1
			}
			sum += a[i]
		}
		if sum == 0 {
			Fprintln(out, 0)
			continue
		}
		l := map[int]int{0: 0}
		s := 0
		for i, v := range a[n:] {
			s += v
			if _, ok := l[s]; !ok {
				l[s] = i + 1
			}
		}
		ans := 2 * n
		for i := n - 1; i >= 0; i-- {
			if l, ok := l[sum-a[i]]; ok && n-i+l < ans {
				ans = n - i + l
			}
		}
		Fprintln(out, ans)
	}
}

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

package main

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

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

	var t, n int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n)
		a := make([]int, 2*n)
		sum := 0
		for i := range a {
			Fscan(in, &a[i])
			if a[i] == 2 {
				a[i] = -1
			}
			sum += a[i]
		}
		if sum == 0 {
			Fprintln(out, 0)
			continue
		}
		l := map[int]int{0: 0}
		s := 0
		for i, v := range a[n:] {
			s += v
			if _, ok := l[s]; !ok {
				l[s] = i + 1
			}
		}
		ans := 2 * n
		for i := n - 1; i >= 0; i-- {
			sum -= a[i]
			if l, ok := l[sum]; ok && n-i+l < ans {
				ans = n - i + l
			}
		}
		Fprintln(out, ans)
	}
}

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