Nash designed an interesting yet simple board game where a player is simply required to follow instructions written on the cell where the player currently stands. This board game is played on the $$$n\times n$$$ board. Rows and columns of this board are numbered from $$$1$$$ to $$$n$$$. The cell on the intersection of the $$$r$$$-th row and $$$c$$$-th column is denoted by $$$(r, c)$$$.Some cells on the board are called blocked zones. On each cell of the board, there is written one of the following $$$5$$$ characters  — $$$U$$$, $$$D$$$, $$$L$$$, $$$R$$$ or $$$X$$$  — instructions for the player. Suppose that the current cell is $$$(r, c)$$$. If the character is $$$R$$$, the player should move to the right cell $$$(r, c+1)$$$, for $$$L$$$ the player should move to the left cell $$$(r, c-1)$$$, for $$$U$$$ the player should move to the top cell $$$(r-1, c)$$$, for $$$D$$$ the player should move to the bottom cell $$$(r+1, c)$$$. Finally, if the character in the cell is $$$X$$$, then this cell is the blocked zone. The player should remain in this cell (the game for him isn't very interesting from now on).It is guaranteed that the characters are written in a way that the player will never have to step outside of the board, no matter at which cell he starts.As a player starts from a cell, he moves according to the character in the current cell. The player keeps moving until he lands in a blocked zone. It is also possible that the player will keep moving infinitely long.For every of the $$$n^2$$$ cells of the board Alice, your friend, wants to know, how will the game go, if the player starts in this cell. For each starting cell of the board, she writes down the cell that the player stops at, or that the player never stops at all. She gives you the information she has written: for each cell $$$(r, c)$$$ she wrote:   a pair ($$$x$$$,$$$y$$$), meaning if a player had started at $$$(r, c)$$$, he would end up at cell ($$$x$$$,$$$y$$$).  or a pair ($$$-1$$$,$$$-1$$$), meaning if a player had started at $$$(r, c)$$$, he would keep moving infinitely long and would never enter the blocked zone. It might be possible that Alice is trying to fool you and there's no possible grid that satisfies all the constraints Alice gave you. For the given information Alice provided you, you are required to decipher a possible board, or to determine that such a board doesn't exist. If there exist several different boards that satisfy the provided information, you can find any of them.

The first line of the input contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^{3}$$$)  — the side of the board. The $$$i$$$-th of the next $$$n$$$ lines of the input contains $$$2n$$$ integers $$$x_1, y_1, x_2, y_2, \dots, x_n, y_n$$$, where $$$(x_j, y_j)$$$ ($$$1 \leq x_j \leq n, 1 \leq y_j \leq n$$$, or $$$(x_j,y_j)=(-1,-1)$$$) is the pair written by Alice for the cell $$$(i, j)$$$.

If there doesn't exist a board satisfying the information that Alice gave you, print a single line containing INVALID.  Otherwise, in the first line print VALID. In the $$$i$$$-th of the next $$$n$$$ lines, print the string of $$$n$$$ characters, corresponding to the characters in the $$$i$$$-th row of the suitable board you found. Each character of a string can either be $$$U$$$, $$$D$$$, $$$L$$$, $$$R$$$ or $$$X$$$. If there exist several different boards that satisfy the provided information, you can find any of them.

NoteFor the sample test 1 :The given grid in output is a valid one.   If the player starts at $$$(1,1)$$$, he doesn't move any further following $$$X$$$ and stops there.  If the player starts at $$$(1,2)$$$, he moves to left following $$$L$$$ and stops at $$$(1,1)$$$.  If the player starts at $$$(2,1)$$$, he moves to right following $$$R$$$ and stops at $$$(2,2)$$$.  If the player starts at $$$(2,2)$$$, he doesn't move any further following $$$X$$$ and stops there. The simulation can be seen below :   For the sample test 2 : The given grid in output is a valid one, as a player starting at any cell other than the one at center $$$(2,2)$$$, keeps moving in an infinitely long cycle and never stops. Had he started at $$$(2,2)$$$, he wouldn't have moved further following instruction $$$X$$$ .The simulation can be seen below :

package main

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

var reader io.Reader
var scanner *bufio.Scanner

func Init(io io.Reader, max_token_size int) {
	reader = bufio.NewReader(io)
	scanner = bufio.NewScanner(reader)
	// max token size
	scanner.Split(bufio.ScanWords)
	scanner.Buffer([]byte{}, max_token_size)
}

func stderr(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
}

func die(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
	os.Exit(1)
}

func NextInt() int {
	bytes := NextBytes()
	i, err := Atoi(bytes)
	if err != nil {
		die(err)
	}
	return i
}
func NextInt64() int64 {
	bytes := NextBytes()
	i, err := Atoi64(bytes)
	if err != nil {
		die(err)
	}
	return i
}

func NextBytes() []byte {
	if !scanner.Scan() {
		die(scanner.Err())
	}
	bytes := scanner.Bytes()
	copied := make([]byte, len(bytes))
	copy(copied, bytes)
	return copied
}

func Atoi(b []byte) (int, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	n := 0
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func Atoi64(b []byte) (int64, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	var n int64
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int64(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func ExtendByteArray(a []byte, size int) []byte {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]byte, size-len(a))...)
	}
}

func ExtendIntArray(a []int, size int) []int {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]int, size-len(a))...)
	}
}

func reverseInts(a []int) {
	for l, r := 0, len(a)-1; l < r; l, r = l+1, r-1 {
		a[l], a[r] = a[r], a[l]
	}
}

func unique(a []int) int {
	newLen := 0
	n := len(a)
	for i := 0; i < n; i++ {
		if newLen == 0 || a[i] != a[newLen-1] {
			a[newLen] = a[i]
			newLen++
		}
	}
	return newLen
}

func main() {
	inf := os.Stdin
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	// inf, err := os.Open("a.in")
	// if err != nil {
	// 	die(err)
	// }
	// out, err := os.Create("a.out")
	// if err != nil {
	// 	die(err)
	// }
	// defer out.Close()
	Init(inf, 1010)

	n := NextInt()

	x := make([][]int, n)
	y := make([][]int, n)
	a := make([][]byte, n)
	was := make([][]bool, n)
	q := make([][2]int, n*n)
	qt := 0
	qh := 0
	for i := 0; i < n; i++ {
		x[i] = make([]int, n)
		y[i] = make([]int, n)
		a[i] = make([]byte, n)
		was[i] = make([]bool, n)
		for j := 0; j < n; j++ {
			x[i][j] = NextInt() - 1
			y[i][j] = NextInt() - 1
			if x[i][j] == i && y[i][j] == j {
				q[qt][0] = i
				q[qt][1] = j
				a[i][j] = 'X'
				was[i][j] = true
				qt++
			}
		}
	}

	var step = []byte{
		'R',
		'L',
		'D',
		'U',
	}

	var ds = [][]int{
		{0, 1},
		{0, -1},
		{1, 0},
		{-1, 0},
	}

	for qh < qt {
		i := q[qh][0]
		j := q[qh][1]
		qh++

		for k := 0; k < 4; k++ {
			ni := i + ds[k][0]
			nj := j + ds[k][1]
			if ni < 0 || ni == n || nj < 0 || nj == n {
				continue
			}
			if was[ni][nj] {
				continue
			}
			if x[ni][nj] == x[i][j] && y[ni][nj] == y[i][j] {
				a[ni][nj] = step[k^1]
				was[ni][nj] = true
				q[qt][0] = ni
				q[qt][1] = nj
				qt++
			}
		}
	}

	// stderr("qt", qt, "n", n)
	placed := qt
	var dfs func(i, j int) bool
	dfs = func(i, j int) bool {
		was[i][j] = true
		for k := 0; k < 4; k++ {
			ni := i + ds[k][0]
			nj := j + ds[k][1]
			if ni < 0 || ni == n || nj < 0 || nj == n {
				continue
			}
			if x[ni][nj] != -2 {
				continue
			}
			if was[ni][nj] {
				placed++
				a[i][j] = step[k]
				return true
			}
			if dfs(ni, nj) {
				placed++
				a[i][j] = step[k]
				return true
			}
		}
		return false
	}

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if x[i][j] == -2 && !was[i][j] {
				// stderr("trying", i, j)
				if !dfs(i, j) {
					fmt.Fprintln(out, "INVALID")
					return
				}
			}
		}
	}

	// stderr("placed", placed)

	if placed != n*n {
		fmt.Fprintln(out, "INVALID")
		return
	}

	fmt.Fprintln(out, "VALID")
	for i := 0; i < n; i++ {
		fmt.Fprintln(out, string(a[i]))
	}

}

package main

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

var reader io.Reader
var scanner *bufio.Scanner

func Init(io io.Reader, max_token_size int) {
	reader = bufio.NewReader(io)
	scanner = bufio.NewScanner(reader)
	// max token size
	scanner.Split(bufio.ScanWords)
	scanner.Buffer([]byte{}, max_token_size)
}

func stderr(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
}

func die(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
	os.Exit(1)
}

func NextInt() int {
	bytes := NextBytes()
	i, err := Atoi(bytes)
	if err != nil {
		die(err)
	}
	return i
}
func NextInt64() int64 {
	bytes := NextBytes()
	i, err := Atoi64(bytes)
	if err != nil {
		die(err)
	}
	return i
}

func NextBytes() []byte {
	if !scanner.Scan() {
		die(scanner.Err())
	}
	bytes := scanner.Bytes()
	copied := make([]byte, len(bytes))
	copy(copied, bytes)
	return copied
}

func Atoi(b []byte) (int, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	n := 0
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func Atoi64(b []byte) (int64, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	var n int64
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int64(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func ExtendByteArray(a []byte, size int) []byte {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]byte, size-len(a))...)
	}
}

func ExtendIntArray(a []int, size int) []int {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]int, size-len(a))...)
	}
}

func reverseInts(a []int) {
	for l, r := 0, len(a)-1; l < r; l, r = l+1, r-1 {
		a[l], a[r] = a[r], a[l]
	}
}

func unique(a []int) int {
	newLen := 0
	n := len(a)
	for i := 0; i < n; i++ {
		if newLen == 0 || a[i] != a[newLen-1] {
			a[newLen] = a[i]
			newLen++
		}
	}
	return newLen
}

func main() {
	inf := os.Stdin
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	// inf, err := os.Open("a.in")
	// if err != nil {
	// 	die(err)
	// }
	// out, err := os.Create("a.out")
	// if err != nil {
	// 	die(err)
	// }
	// defer out.Close()
	Init(inf, 1000010)

	n := NextInt()

	x := make([][]int, n)
	y := make([][]int, n)
	a := make([][]byte, n)
	was := make([][]bool, n)
	q := make([][2]int, n*n)
	qt := 0
	qh := 0
	for i := 0; i < n; i++ {
		x[i] = make([]int, n)
		y[i] = make([]int, n)
		a[i] = make([]byte, n)
		was[i] = make([]bool, n)
		for j := 0; j < n; j++ {
			x[i][j] = NextInt() - 1
			y[i][j] = NextInt() - 1
			if x[i][j] == i && y[i][j] == j {
				q[qt][0] = i
				q[qt][1] = j
				a[i][j] = 'X'
				was[i][j] = true
				qt++
			}
		}
	}

	var step = []byte{
		'R',
		'L',
		'D',
		'U',
	}

	var ds = [][]int{
		{0, 1},
		{0, -1},
		{1, 0},
		{-1, 0},
	}

	for qh < qt {
		i := q[qh][0]
		j := q[qh][1]
		qh++

		for k := 0; k < 4; k++ {
			ni := i + ds[k][0]
			nj := j + ds[k][1]
			if ni < 0 || ni == n || nj < 0 || nj == n {
				continue
			}
			if was[ni][nj] {
				continue
			}
			if x[ni][nj] == x[i][j] && y[ni][nj] == y[i][j] {
				a[ni][nj] = step[k^1]
				was[ni][nj] = true
				q[qt][0] = ni
				q[qt][1] = nj
				qt++
			}
		}
	}

	// stderr("qt", qt, "n", n)
	placed := qt
	var dfs func(i, j int) bool
	dfs = func(i, j int) bool {
		was[i][j] = true
		for k := 0; k < 4; k++ {
			ni := i + ds[k][0]
			nj := j + ds[k][1]
			if ni < 0 || ni == n || nj < 0 || nj == n {
				continue
			}
			if x[ni][nj] != -2 {
				continue
			}
			if was[ni][nj] {
				placed++
				a[i][j] = step[k]
				return true
			}
			if dfs(ni, nj) {
				placed++
				a[i][j] = step[k]
				return true
			}
		}
		return false
	}

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if x[i][j] == -2 && !was[i][j] {
				// stderr("trying", i, j)
				if !dfs(i, j) {
					fmt.Fprintln(out, "INVALID")
					return
				}
			}
		}
	}

	// stderr("placed", placed)

	if placed != n*n {
		fmt.Fprintln(out, "INVALID")
		return
	}

	fmt.Fprintln(out, "VALID")
	for i := 0; i < n; i++ {
		fmt.Fprintln(out, string(a[i]))
	}

}

package main

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

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

	var n, x, y int
	Fscan(in, &n)
	ends := make([][][2]int, n)
	for i := range ends {
		ends[i] = make([][2]int, n)
		for j := range ends[i] {
			Fscan(in, &x, &y)
			if x > 0 {
				ends[i][j] = [2]int{x - 1, y - 1}
			} else {
				ends[i][j] = [2]int{-1, -1}
			}
		}
	}

	ans := make([][]byte, n)
	for i := range ans {
		ans[i] = make([]byte, n)
	}
	valid := func(x, y int) bool { return x >= 0 && x < n && y >= 0 && y < n }
	dir4 := [4][2]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}
	const dirMark = "LURD"
	var tar [2]int
	var makePath func(x, y int)
	makePath = func(x, y int) {
		for i, d := range dir4 {
			if xx, yy := x+d[0], y+d[1]; valid(xx, yy) && ans[xx][yy] == 0 && ends[xx][yy] == tar {
				ans[xx][yy] = dirMark[i]
				makePath(xx, yy)
			}
		}
	}
	for i, row := range ends {
		for j, ed := range row {
			if ans[i][j] > 0 {
				continue
			}
			if x, y := ed[0], ed[1]; x == -1 {
				tar = [2]int{-1, -1}
				if valid(i, j+1) && ends[i][j+1] == tar {
					ans[i][j] = 'R'
					ans[i][j+1] = 'L'
					makePath(i, j)
					makePath(i, j+1)
				} else if valid(i+1, j) && ends[i+1][j] == tar {
					ans[i][j] = 'D'
					ans[i+1][j] = 'U'
					makePath(i, j)
					makePath(i+1, j)
				} else {
					Fprint(out, "INVALID")
					return
				}
			} else {
				if ans[x][y] > 0 || ends[x][y] != ed {
					Fprint(out, "INVALID")
					return
				}
				ans[x][y] = 'X'
				tar = ed
				makePath(x, y)
				if ans[i][j] == 0 {
					Fprint(out, "INVALID")
					return
				}
			}
		}
	}
	Fprintln(out, "VALID")
	for _, row := range ans {
		Fprintf(out, "%s\n", row)
	}
}

// 1 1 1 1 0 0 4 4 0 0 1 7 1 7
// 1 1 1 1 0 0 4 4 0 0 1 7 1 7
// 0 0 0 0 0 0 4 4 0 0 0 0 0 0
// 0 0 4 4 4 4 4 4 4 4 4 4 0 0
// 0 0 0 0 0 0 4 4 0 0 0 0 0 0
// 7 1 0 0 0 0 4 4 0 0 7 7 7 7
// 7 1 0 0 0 0 0 0 0 0 7 7 7 7

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

package main

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

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

	var n, x, y int
	Fscan(in, &n)
	ends := make([][][2]int, n)
	for i := range ends {
		ends[i] = make([][2]int, n)
		for j := range ends[i] {
			Fscan(in, &x, &y)
			if x > 0 {
				ends[i][j] = [2]int{x - 1, y - 1}
			} else {
				ends[i][j] = [2]int{-1, -1}
			}
		}
	}

	ans := make([][]byte, n)
	for i := range ans {
		ans[i] = make([]byte, n)
	}
	valid := func(x, y int) bool { return x >= 0 && x < n && y >= 0 && y < n }
	dir4 := [4][2]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}
	const dirMark = "LURD"
	var tar [2]int
	var makePath func(x, y int)
	makePath = func(x, y int) {
		for i, d := range dir4 {
			if xx, yy := x+d[0], y+d[1]; valid(xx, yy) && ans[xx][yy] == 0 && ends[xx][yy] == tar {
				ans[xx][yy] = dirMark[i]
				makePath(xx, yy)
			}
		}
	}
	for i, row := range ends {
		for j, ed := range row {
			if ans[i][j] > 0 {
				continue
			}
			if x, y := ed[0], ed[1]; x == -1 {
				tar = [2]int{-1, -1}
				if valid(i, j+1) && ends[i][j+1] == tar {
					ans[i][j] = 'R'
					ans[i][j+1] = 'L'
					makePath(i, j)
					makePath(i, j+1)
				} else if valid(i+1, j) && ends[i+1][j] == tar {
					ans[i][j] = 'D'
					ans[i+1][j] = 'U'
					makePath(i, j)
					makePath(i+1, j)
				} else {
					Fprint(out, "INVALID")
					return
				}
			} else {
				if ans[x][y] > 0 || ends[x][y] != ed {
					Fprint(out, "INVALID")
					return
				}
				ans[x][y] = 'X'
				tar = ed
				makePath(x, y)
				if ans[i][j] == 0 {
					Fprint(out, "INVALID")
					return
				}
			}
		}
	}
	Fprintln(out, "VALID")
	for _, row := range ans {
		Fprintf(out, "%s\n", row)
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1316D(_r io.Reader, _w io.Writer) {
	in := bufio.NewScanner(_r)
	in.Split(bufio.ScanWords)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	read := func() (x int) {
		in.Scan()
		raw := in.Bytes()
		if raw[0] == '-' {
			return -1
		}
		for _, b := range raw {
			x = x*10 + int(b-'0')
		}
		return
	}

	n := read()
	ends := make([][][2]int, n)
	for i := range ends {
		ends[i] = make([][2]int, n)
		for j := range ends[i] {
			if x, y := read(), read(); x > 0 {
				ends[i][j] = [2]int{x - 1, y - 1}
			} else {
				ends[i][j] = [2]int{-1, -1}
			}
		}
	}

	ans := make([][]byte, n)
	for i := range ans {
		ans[i] = make([]byte, n)
	}
	valid := func(x, y int) bool { return x >= 0 && x < n && y >= 0 && y < n }
	dir4 := [4][2]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}
	const dirMark = "LURD"
	var tar [2]int
	var makePath func(x, y int)
	makePath = func(x, y int) {
		for i, d := range dir4 {
			if xx, yy := x+d[0], y+d[1]; valid(xx, yy) && ans[xx][yy] == 0 && ends[xx][yy] == tar {
				ans[xx][yy] = dirMark[i]
				makePath(xx, yy)
			}
		}
	}
	for i, row := range ends {
		for j, ed := range row {
			if ans[i][j] > 0 {
				continue
			}
			if x, y := ed[0], ed[1]; x == -1 {
				tar = [2]int{-1, -1}
				if valid(i, j+1) && ends[i][j+1] == tar {
					ans[i][j] = 'R'
					ans[i][j+1] = 'L'
					makePath(i, j)
					makePath(i, j+1)
				} else if valid(i+1, j) && ends[i+1][j] == tar {
					ans[i][j] = 'D'
					ans[i+1][j] = 'U'
					makePath(i, j)
					makePath(i+1, j)
				} else {
					Fprint(out, "INVALID")
					return
				}
			} else {
				if ans[x][y] > 0 || ends[x][y] != ed {
					Fprint(out, "INVALID")
					return
				}
				ans[x][y] = 'X'
				tar = ed
				makePath(x, y)
				if ans[i][j] == 0 {
					Fprint(out, "INVALID")
					return
				}
			}
		}
	}
	Fprintln(out, "VALID")
	for _, row := range ans {
		Fprintf(out, "%s\n", row)
	}
}

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

package main

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

var reader io.Reader
var scanner *bufio.Scanner

func Init(io io.Reader, max_token_size int) {
	reader = bufio.NewReader(io)
	scanner = bufio.NewScanner(reader)
	// max token size
	scanner.Split(bufio.ScanWords)
	scanner.Buffer([]byte{}, max_token_size)
}

func stderr(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
}

func die(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
	os.Exit(1)
}

func NextInt() int {
	bytes := NextBytes()
	i, err := Atoi(bytes)
	if err != nil {
		die(err)
	}
	return i
}
func NextInt64() int64 {
	bytes := NextBytes()
	i, err := Atoi64(bytes)
	if err != nil {
		die(err)
	}
	return i
}

func NextBytes() []byte {
	if !scanner.Scan() {
		die(scanner.Err())
	}
	bytes := scanner.Bytes()
	copied := make([]byte, len(bytes))
	copy(copied, bytes)
	return copied
}

func Atoi(b []byte) (int, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	n := 0
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func Atoi64(b []byte) (int64, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	var n int64
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int64(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func ExtendByteArray(a []byte, size int) []byte {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]byte, size-len(a))...)
	}
}

func ExtendIntArray(a []int, size int) []int {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]int, size-len(a))...)
	}
}

func reverseInts(a []int) {
	for l, r := 0, len(a)-1; l < r; l, r = l+1, r-1 {
		a[l], a[r] = a[r], a[l]
	}
}

func unique(a []int) int {
	newLen := 0
	n := len(a)
	for i := 0; i < n; i++ {
		if newLen == 0 || a[i] != a[newLen-1] {
			a[newLen] = a[i]
			newLen++
		}
	}
	return newLen
}

func main() {
	inf := os.Stdin
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	// inf, err := os.Open("a.in")
	// if err != nil {
	// 	die(err)
	// }
	// out, err := os.Create("a.out")
	// if err != nil {
	// 	die(err)
	// }
	// defer out.Close()
	Init(inf, 1000010)

	n := NextInt()

	x := make([][]int, n)
	y := make([][]int, n)
	a := make([][]byte, n)
	was := make([][]bool, n)
	q := make([][2]int, n*n)
	qt := 0
	qh := 0
	for i := 0; i < n; i++ {
		x[i] = make([]int, n)
		y[i] = make([]int, n)
		a[i] = make([]byte, n)
		was[i] = make([]bool, n)
		for j := 0; j < n; j++ {
			x[i][j] = NextInt() - 1
			y[i][j] = NextInt() - 1
			if x[i][j] == i && y[i][j] == j {
				q[qt][0] = i
				q[qt][1] = j
				a[i][j] = 'X'
				was[i][j] = true
				qt++
			}
		}
	}

	var step = []byte{
		'R',
		'L',
		'D',
		'U',
	}

	var ds = [][]int{
		{0, 1},
		{0, -1},
		{1, 0},
		{1, -1},
	}

	for qh < qt {
		i := q[qh][0]
		j := q[qh][1]
		qh++

		for k := 0; k < 4; k++ {
			ni := i + ds[k][0]
			nj := j + ds[k][1]
			if ni < 0 || ni == n || nj < 0 || nj == n {
				continue
			}
			if was[ni][nj] {
				continue
			}
			if x[ni][nj] == x[i][j] {
				a[ni][nj] = step[k^1]
				was[ni][nj] = true
				q[qt][0] = ni
				q[qt][1] = nj
				qt++
			}
		}
	}

	// stderr("qt", qt, "n", n)
	placed := qt
	var dfs func(i, j int) bool
	dfs = func(i, j int) bool {
		was[i][j] = true
		for k := 0; k < 4; k++ {
			ni := i + ds[k][0]
			nj := j + ds[k][1]
			if ni < 0 || ni == n || nj < 0 || nj == n {
				continue
			}
			if x[ni][nj] != -2 {
				continue
			}
			if was[ni][nj] {
				placed++
				a[i][j] = step[k]
				return true
			}
			if dfs(ni, nj) {
				placed++
				a[i][j] = step[k]
				return true
			}
		}
		return false
	}

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if x[i][j] == -2 && !was[i][j] {
				// stderr("trying", i, j)
				if !dfs(i, j) {
					fmt.Fprintln(out, "INVALID")
					return
				}
			}
		}
	}

	// stderr("placed", placed)

	if placed != n*n {
		fmt.Fprintln(out, "INVALID")
		return
	}

	fmt.Fprintln(out, "VALID")
	for i := 0; i < n; i++ {
		fmt.Fprintln(out, string(a[i]))
	}

}

package main

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

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

	var n, x, y int
	Fscan(in, &n)
	ends := make([][][2]int, n)
	for i := range ends {
		ends[i] = make([][2]int, n)
		for j := range ends[i] {
			Fscan(in, &x, &y)
			if x > 0 {
				ends[i][j] = [2]int{x - 1, y - 1}
			} else {
				ends[i][j] = [2]int{-1, -1}
			}
		}
	}

	ans := make([][]byte, n)
	for i := range ans {
		ans[i] = make([]byte, n)
	}
	valid := func(x, y int) bool { return x >= 0 && x < n && y >= 0 && y < n }
	dir4 := [4][2]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}
	const dirMark = "LURD"
	var tar [2]int
	var makePath func(x, y int)
	makePath = func(x, y int) {
		for i, d := range dir4 {
			if xx, yy := x+d[0], y+d[1]; valid(xx, yy) && ans[xx][yy] == 0 && ends[xx][yy] == tar {
				ans[xx][yy] = dirMark[i]
				makePath(xx, yy)
			}
		}
	}
	for i, row := range ends {
		for j, ed := range row {
			if ans[i][j] > 0 {
				continue
			}
			if x, y := ed[0], ed[1]; x == -1 {
				tar = [2]int{-1, -1}
				if valid(i, j+1) {
					ans[i][j] = 'R'
					ans[i][j+1] = 'L'
					makePath(i, j)
					makePath(i, j+1)
				} else if valid(i+1, j) {
					ans[i][j] = 'D'
					ans[i+1][j] = 'U'
					makePath(i, j)
					makePath(i+1, j)
				} else {
					Fprint(out, "INVALID")
					return
				}
			} else {
				if ends[x][y] != [2]int{x, y} {
					Fprint(out, "INVALID")
					return
				}
				ans[x][y] = 'X'
				tar = ed
				makePath(x, y)
				if ans[i][j] == 0 {
					Fprint(out, "INVALID")
					return
				}
			}
		}
	}
	Fprintln(out, "VALID")
	for _, row := range ans {
		Fprintf(out, "%s\n", row)
	}
}

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

package main

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

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

	var n, x, y int
	Fscan(in, &n)
	ends := make([][][2]int, n)
	for i := range ends {
		ends[i] = make([][2]int, n)
		for j := range ends[i] {
			Fscan(in, &x, &y)
			if x > 0 {
				ends[i][j] = [2]int{x - 1, y - 1}
			} else {
				ends[i][j] = [2]int{-1, -1}
			}
		}
	}

	ans := make([][]byte, n)
	for i := range ans {
		ans[i] = make([]byte, n)
	}
	valid := func(x, y int) bool { return x >= 0 && x < n && y >= 0 && y < n }
	dir4 := [4][2]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}
	const dirMark = "LURD"
	var tar [2]int
	var makePath func(x, y int)
	makePath = func(x, y int) {
		for i, d := range dir4 {
			if xx, yy := x+d[0], y+d[1]; valid(xx, yy) && ans[xx][yy] == 0 && ends[xx][yy] == tar {
				ans[xx][yy] = dirMark[i]
				makePath(xx, yy)
			}
		}
	}
	for i, row := range ends {
		for j, ed := range row {
			if ans[i][j] > 0 {
				continue
			}
			if x, y := ed[0], ed[1]; x == -1 {
				tar = [2]int{-1, -1}
				if valid(i, j+1) {
					ans[i][j] = 'R'
					ans[i][j+1] = 'L'
					makePath(i, j)
					makePath(i, j+1)
				} else if valid(i+1, j) {
					ans[i][j] = 'D'
					ans[i+1][j] = 'U'
					makePath(i, j)
					makePath(i+1, j)
				} else {
					Fprint(out, "INVALID")
					return
				}
			} else {
				if ans[x][y] > 0 || ends[x][y] != ed {
					Fprint(out, "INVALID")
					return
				}
				ans[x][y] = 'X'
				tar = ed
				makePath(x, y)
				if ans[i][j] == 0 {
					Fprint(out, "INVALID")
					return
				}
			}
		}
	}
	Fprintln(out, "VALID")
	for _, row := range ans {
		Fprintf(out, "%s\n", row)
	}
}

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

package main

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

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

	var n, x, y int
	Fscan(in, &n)
	ends := make([][][2]int, n)
	for i := range ends {
		ends[i] = make([][2]int, n)
		for j := range ends[i] {
			Fscan(in, &x, &y)
			if x > 0 {
				ends[i][j] = [2]int{x - 1, y - 1}
			} else {
				ends[i][j] = [2]int{-1, -1}
			}
		}
	}

	ans := make([][]byte, n)
	for i := range ans {
		ans[i] = make([]byte, n)
	}
	valid := func(x, y int) bool { return x >= 0 && x < n && y >= 0 && y < n }
	dir4 := [4][2]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}
	const dirMark = "LURD"
	var tar [2]int
	var makePath func(x, y int)
	makePath = func(x, y int) {
		for i, d := range dir4 {
			if xx, yy := x+d[0], y+d[1]; valid(xx, yy) && ans[xx][yy] == 0 && ends[xx][yy] == tar {
				ans[xx][yy] = dirMark[i]
				makePath(xx, yy)
			}
		}
	}
	for i, row := range ends {
		for j, ed := range row {
			if ans[i][j] > 0 {
				continue
			}
			if x, y := ed[0], ed[1]; x == -1 {
				tar = [2]int{-1, -1}
				if valid(i, j+1) {
					ans[i][j] = 'R'
					ans[i][j+1] = 'L'
					makePath(i, j)
					makePath(i, j+1)
				} else if valid(i+1, j) {
					ans[i][j] = 'D'
					ans[i+1][j] = 'U'
					makePath(i, j)
					makePath(i+1, j)
				} else {
					Fprint(out, "INVALID")
					return
				}
			} else {
				if ans[x][y] > 0 {
					Fprint(out, "INVALID")
					return
				}
				ans[x][y] = 'X'
				tar = ed
				makePath(x, y)
				if ans[i][j] == 0 {
					Fprint(out, "INVALID")
					return
				}
			}
		}
	}
	Fprintln(out, "VALID")
	for _, row := range ans {
		Fprintf(out, "%s\n", row)
	}
}

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

package main

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

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

	var n, x, y int
	Fscan(in, &n)
	ends := make([][][2]int, n)
	for i := range ends {
		ends[i] = make([][2]int, n)
		for j := range ends[i] {
			Fscan(in, &x, &y)
			if x > 0 {
				ends[i][j] = [2]int{x - 1, y - 1}
			} else {
				ends[i][j] = [2]int{-1, -1}
			}
		}
	}

	ans := make([][]byte, n)
	for i := range ans {
		ans[i] = make([]byte, n)
	}
	valid := func(x, y int) bool { return x >= 0 && x < n && y >= 0 && y < n }
	dir4 := [4][2]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}
	const dirMark = "LURD"
	var makeLoop func(x, y int)
	makeLoop = func(x, y int) {
		for i, d := range dir4 {
			if xx, yy := x+d[0], y+d[1]; valid(xx, yy) && ans[xx][yy] == 0 && ends[xx][yy][0] == -1 {
				ans[xx][yy] = dirMark[i]
				makeLoop(xx, yy)
			}
		}
	}
	var tar [2]int
	var makePath func(x, y int)
	makePath = func(x, y int) {
		for i, d := range dir4 {
			if xx, yy := x+d[0], y+d[1]; valid(xx, yy) && ans[xx][yy] == 0 && ends[xx][yy] == tar {
				ans[xx][yy] = dirMark[i]
				makePath(xx, yy)
			}
		}
	}
	for i, row := range ends {
		for j, ed := range row {
			if ans[i][j] > 0 {
				continue
			}
			if x, y := ed[0], ed[1]; x == -1 {
				if valid(i, j+1) {
					ans[i][j] = 'R'
					ans[i][j+1] = 'L'
					makeLoop(i, j)
					makeLoop(i, j+1)
				} else if valid(i+1, j) {
					ans[i][j] = 'D'
					ans[i+1][j] = 'U'
					makeLoop(i, j)
					makeLoop(i+1, j)
				} else {
					Fprint(out, "INVALID")
					return
				}
			} else {
				ans[x][y] = 'X'
				tar = ed
				makePath(x, y)
				if ans[i][j] == 0 {
					Fprint(out, "INVALID")
					return
				}
			}
		}
	}
	Fprintln(out, "VALID")
	for _, row := range ans {
		Fprintf(out, "%s\n", row)
	}
}

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


Polycarp bought a new expensive painting and decided to show it to his $$$n$$$ friends. He hung it in his room. $$$n$$$ of his friends entered and exited there one by one. At one moment there was no more than one person in the room. In other words, the first friend entered and left first, then the second, and so on.It is known that at the beginning (before visiting friends) a picture hung in the room. At the end (after the $$$n$$$-th friend) it turned out that it disappeared. At what exact moment it disappeared — there is no information.Polycarp asked his friends one by one. He asked each one if there was a picture when he entered the room. Each friend answered one of three:  no (response encoded with 0);  yes (response encoded as 1);  can't remember (response is encoded with ?). Everyone except the thief either doesn't remember or told the truth. The thief can say anything (any of the three options).Polycarp cannot understand who the thief is. He asks you to find out the number of those who can be considered a thief according to the answers.

The first number $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The following is a description of test cases. The first line of each test case contains one string $$$s$$$ (length does not exceed $$$2 \cdot 10^5$$$) — a description of the friends' answers, where $$$s_i$$$ indicates the answer of the $$$i$$$-th friend. Each character in the string is either 0 or 1 or ?. The given regularity is described in the actual situation. In particular, on the basis of answers, at least one friend can be suspected of stealing a painting. It is guaranteed that the sum of string lengths over the entire input data set does not exceed $$$2 \cdot 10^5$$$.

Output one positive (strictly more zero) number – the number of people who could steal the picture based on the data shown.

NoteIn the first case, the answer is $$$1$$$ since we had exactly $$$1$$$ friend.The second case is similar to the first.In the third case, the suspects are the third and fourth friends (we count from one). It can be shown that no one else could be the thief.In the fourth case, we know absolutely nothing, so we suspect everyone.

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	T, s := 0, ""
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &s)
		i, j := LastIndex(s, "1"), Index(s, "0")
		if i < 0 {i = 0}
		if j < 0 {j = len(s) - 1}
		Println(j-i+1)
	}
}

package main

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

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

	T, s := 0, ""
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &s)
		i, j := strings.LastIndex(s, "1"), strings.Index(s, "0")
		if i < 0 {
			i = 0
		}
		if j < 0 {
			j = len(s) - 1
		}
		Fprintln(out, j-i+1)
	}
}

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

package main

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

/*
7
0
1
00
10
11
?
??
---
1
1
1
2
1
1
2
======
6
?????
111
11000
110?0?0
111??00?0
11??0
---
5
1
2
2
4
4
========
2
??0??
??0
---
3
3
++======
5
1???00
11???00
1???
1???0
11???00
---
5
5
4
5
5

*/

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

	if len(s) == 1 || s[0] == '0' {
		fmt.Fprintln(writer, 1)
		return
	}

	onlyQuestionMarks := true

	first0Idx := len(s)

	for i, r := range s {
		if r == '0' && first0Idx == len(s) {
			first0Idx = i
		}

		if r == '0' || r == '1' {
			onlyQuestionMarks = false
		}
	}

	if onlyQuestionMarks {
		fmt.Fprintln(writer, len(s))
		return
	}

	i := first0Idx - 1
	for ; i >= 0; i-- {
		if s[i] == '1' {
			break
		}
	}

	cnt := first0Idx - i
	if first0Idx != len(s) {
		cnt++
	}
	if i < 0 {
		cnt--
	}

	fmt.Fprintln(writer, cnt)
}

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

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

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

const maxUint = ^uint(0)

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

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

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

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

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

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

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

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

	return minNumIdx
}

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

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

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func sum(nums ...int) (sum int) {
	for _, num := range nums {
		sum += num
	}
	return
}

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

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

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

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

	return true
}

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

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

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

	num = abs(num)

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

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

//goland:noinspection GoUnusedFunction
func factorize(num int64) []int64 {
	factors := make([]int64, 0)

	for ftr := int64(2); ftr*ftr <= num; ftr++ {
		for num%ftr == 0 {
			num /= ftr
			factors = append(factors, ftr)
		}
	}

	if num > 1 {
		factors = append(factors, num)
	}

	return factors
}

package main

import (
	"fmt"
)

func main() {
	var t int
	fmt.Scanln(&t)

	for i := 0; i < t; i++ {
		var s string
		fmt.Scanln(&s)

		fmt.Println(Soln(s))
	}
}

const (
	No  = '0'
	Yes = '1'
	CR  = '?'
)

func Soln(s string) int {
	n := len(s)

	fNo, lYes := -1, -1

	for i := 0; i < n; i++ {
		if s[i] == No {
			fNo = i
			break
		}
	}

	for i := n - 1; i >= 0; i-- {
		if s[i] == Yes {
			lYes = i
			break
		}
	}

	if fNo == -1 && lYes == -1 {
		return n
	}

	if fNo == -1 {
		return n - lYes
	}

	if lYes == -1 {
		return fNo + 1
	}

	return fNo - lYes + 1
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)
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 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++ {
		s := gs(); n := len(s)
		l1 := -1; f0 := -1
		// Can't have 101
		for i,c := range(s) { if c == '1' { l1 = i } else if c == '0' && f0 == -1 { f0 = i } }
		ans := 0
		if l1 == -1 && f0 == -1 {
			ans = n
		} else if l1 == -1 {
			ans = f0+1
		} else if f0 == -1 {
			ans = n - l1
		} else {
			ans = f0-l1+1
		}
		fmt.Fprintln(wrtr,ans)
	}
}

package main

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

var wr = bufio.NewWriter(os.Stdout)
var rd = bufio.NewScanner(os.Stdin)

func nextInt() int {
	rd.Scan()
	if n, err := strconv.Atoi(rd.Text()); err == nil {
		return n
	}
	return 0
}
func Max(a,b int)int{
	if a>b {
		return a
	}else{
		return b
	}
}
func main() {
	defer wr.Flush()
	rd.Buffer(make([]byte, 0), 1000000009)
	rd.Split(bufio.ScanWords)
	T := nextInt()
	for Case := 0; Case < T; Case++ {
		solve()
	}
}
func solve() {
	rd.Scan()
	s:=rd.Text()
	cnt1:=0
	for _,val:=range s{
		if val=='1'{
			cnt1++
		}
	}
	cnt0:=0
	res:=0
	for _,val:=range s{
		if val=='1'{
			cnt1--
		}
		if cnt1==0 && cnt0==0{
			res++
		}
		if(val=='0'){
			cnt0++
		}
	}
	fmt.Fprint(wr,res,"\n")
}

package main

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

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

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

		var x, y int
		for _, ch := range s {
			if ch == '0' {
				x++
			} else if ch == '1' {
				y++
			}
		}

		var tot int
		var a, b int
		for _, ch := range s {
			if ch == '1' {
				b++
				y--
				if y == 0 { // last 1 is a suspect
					tot++
				}
			} else if ch == '0' {
				a++
				x--
				if y == 0 && a == 1 { // first 0 is a suspect
					tot++
				}
			} else {
				if y == 0 && a == 0 {
					tot++
				}
			}
		}
		fmt.Fprintln(out, tot)
	}
}

package main

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

func main() {
	var (
		t int
	)

	reader := bufio.NewReader(os.Stdin)
	fmt.Scanf("%d\n", &t)

	for t > 0 {
		t--

		s, _ := reader.ReadString('\n')
		if s[len(s)-1] == 10 {
			s = s[:len(s)-1]
		}
		if s[len(s)-1] == 13 {
			s = s[:len(s)-1]
		}

		if len(s) == 1 {
  			fmt.Println(1)
  			continue
 		}

 		lindex := len(s) - 1
		rindex := 0
 		for i := 0; i < len(s); i++ {
  			if string(s[i]) == "0" {
   				lindex = i
   				break
  			}
		}

 		for i := len(s) - 1; i >= 0; i-- {
			if string(s[i]) == "1" {
				rindex = i
				break
			}
		}
                fmt.Println(math.Abs(float64(lindex-rindex)+1))
	}
}

package main

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

type Int int64

const (
	INF = 3e5 + 10
	L0  = Int(0)
	L1  = Int(1)
)

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

	var t int
	Fscan(in, &t)

	for tt := 0; tt < t; tt++ {
		var s string
		Fscan(in, &s)

		f1 := 0
		for i, ch := range s {
			if ch == '1' {
				f1 = i
			}

		}

		l0 := len(s) - 1
		for i := len(s) - 1; i >= 0; i-- {
			if s[i] == '0' {
				l0 = i
			}
		}

		ans := (l0 - f1) + 1

		Fprintln(out, ans)

	}

}

func MakeGrid(h, w int) [][]Int {
	index := make([][]Int, h)
	data := make([]Int, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

func Abs(x Int) Int {
	if x > 0 {
		return x
	}
	return -x
}

func Min(nums ...Int) Int {
	return nums[MinIdx(nums...)]
}

func MinIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num < nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

func Max(nums ...Int) Int {
	return nums[MaxIdx(nums...)]
}

func MaxIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

var (
	mod  Int
	fmod func(Int) Int = doNothing
)

// SetMod sets the mod. It must be called first.
func SetMod(newMod Int) {
	mod = newMod
	fmod = dynamicMod
}

func dynamicMod(m Int) Int {
	m %= mod
	if m < 0 {
		return m + mod
	}
	return m
}

func doNothing(m Int) Int {
	return m
}

// Mod returns m % mod.
func (m Int) Mod() Int {
	return fmod(m)
}

// Inv returns modular multiplicative inverse
func (m Int) Inv() Int {
	return m.Pow(Int(0).Sub(2))
}

// Pow returns m^n
func (m Int) Pow(n Int) Int {
	p := Int(1)
	for n > 0 {
		if n&1 == 1 {
			p.MulAs(m)
		}
		m.MulAs(m)
		n >>= 1
	}
	return p
}

// Add returns m+x
func (m Int) Add(x Int) Int {
	return (m + x).Mod()
}

// Sub returns m-x
func (m Int) Sub(x Int) Int {
	return (m - x).Mod()
}

// Mul returns m*x
func (m Int) Mul(x Int) Int {
	return (m * x).Mod()
}

// Div returns m/x
func (m Int) Div(x Int) Int {
	return m.Mul(x.Inv())
}

// AddAs assigns *m + x to *m and returns m
func (m *Int) AddAs(x Int) *Int {
	*m = m.Add(x)
	return m
}

// SubAs assigns *m - x to *m and returns m
func (m *Int) SubAs(x Int) *Int {
	*m = m.Sub(x)
	return m
}

// MulAs assigns *m * x to *m and returns m
func (m *Int) MulAs(x Int) *Int {
	*m = m.Mul(x)
	return m
}

// DivAs assigns *m / x to *m and returns m
func (m *Int) DivAs(x Int) *Int {
	*m = m.Div(x)
	return m
}

func (m *Int) MaxAs(x ...Int) *Int {
	*m = Max(append(x, *m)...)
	return m
}

func (m *Int) MinAs(x ...Int) *Int {
	*m = Min(append(x, *m)...)
	return m
}

package main

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

func main() {
	r, w := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer w.Flush()
	var n int
	Fscan(r, &n)
	t := 0
	var s string
	for t < n {
		t++
		Fscan(r, &s)
		if s[0] == '0'{
			Fprintln(w, 1)
			continue
		}
		left := 0
		right := len(s) - 1
		for i := range s{
			if s[i] == '1'{
				left = i
			}else if s[i] == '0' && right == len(s) - 1{
				right = i
			}
		}
		Fprintln(w, right - left + 1)
	}
}

package main

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

func main_div3_787_C() {
	_in, _out := os.Stdin, os.Stdout
	in, out := bufio.NewReader(_in), bufio.NewWriter(_out)
	defer out.Flush()
	var cas int = 1
	Fscan(in, &cas)
	var sol func()
	// Coding...
	sol = func() {
		var s string
		Fscan(in, &s)
		s = "1" + s + "0"
		i := len(s) - 1
		for i >= 0 && s[i] != '1' {
			i--
		}
		j := i
		for j < len(s) && s[j] != '0' {
			j++
		}
		ans := j - i + 1
		if i == 0 {
			ans--
		}
		if j == len(s)-1 {
			ans--
		}
		Fprintln(out, ans)
	}
	for i := 0; i < cas; i++ {
		sol()
	}
}

func main() {
	main_div3_787_C()
}

package main

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

var r = bufio.NewReader(os.Stdin)
var w = bufio.NewWriter(os.Stdout)

func slove() {
	defer w.Flush()
	var s string
	fmt.Fscan(r, &s)
	first_0, last_1 := -1, -1
	for i := range s {
		if s[i] == '0' && first_0 == -1 {
			first_0 = i
		} else if s[i] == '1' && first_0 != -1 {
			fmt.Fprintln(w, "1")
			return
		} else if s[i] == '1' {
			last_1 = i
		}
	}
	if first_0 != -1 && last_1 != -1 {
		fmt.Fprintln(w, first_0 - last_1 + 1)
	} else if first_0 == -1 && last_1 == -1 {
		fmt.Fprintln(w, len(s))
	} else if first_0 == -1 {
		fmt.Fprintln(w, len(s) - last_1)
	} else {
		fmt.Fprintln(w, first_0 + 1)
	}

}

func main() {
	var n int
	fmt.Fscan(r, &n)
	for i:=1;i<=n;i++ {
		slove()
	}
}

package main

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

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

	for ii := 0; ii < t; ii++ {
		var s string
		fmt.Fscan(in, &s)
		o, z := 0, len(s)-1
		for i := 0; i < len(s); i++ {
			if z == len(s)-1 && s[i] == '0' {
				z = i
			}
			if s[i] == '1' {
				o = i
			}
		}
		fmt.Fprintln(out, z-o+1)
	}
}

package main

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

/*
7
0
1
00
10
11
?
??
---
1
1
1
1
1
1
2
======
6
?????
111
11000
110?0?0
111??00?0
11??0
---
5
1
2
2
4
4
========
1
??0??
---
3
*/

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

	if len(s) == 1 || s[0] == '0' {
		fmt.Fprintln(writer, 1)
		return
	}

	allQuestions := true

	first0Idx := len(s)

	for i, r := range s {
		if r == '0' && first0Idx == len(s) {
			first0Idx = i
		}

		if r == '0' || r == '1' {
			allQuestions = false
		}
	}

	if allQuestions {
		fmt.Fprintln(writer, len(s))
		return
	}

	i := first0Idx - 1
	for ; i >= 0; i-- {
		if s[i] == '1' {
			break
		}
	}

	cnt := first0Idx - i
	if first0Idx != len(s) {
		cnt++
	}
	if i <= 0 {
		cnt--
	}

	fmt.Fprintln(writer, cnt)
}

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

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

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

const maxUint = ^uint(0)

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

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

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

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

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

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

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

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

	return minNumIdx
}

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

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

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func sum(nums ...int) (sum int) {
	for _, num := range nums {
		sum += num
	}
	return
}

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

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

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

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

	return true
}

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

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

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

	num = abs(num)

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

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

//goland:noinspection GoUnusedFunction
func factorize(num int64) []int64 {
	factors := make([]int64, 0)

	for ftr := int64(2); ftr*ftr <= num; ftr++ {
		for num%ftr == 0 {
			num /= ftr
			factors = append(factors, ftr)
		}
	}

	if num > 1 {
		factors = append(factors, num)
	}

	return factors
}