package main

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

func acc(op func(int, int) int, c ...int) int {
	res := c[0]
	for _, i := range c {
		res = op(res, i)
	}
	return res
}

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

func min(c ...int) int {
	return acc(func(a int, b int) int {
		if a > b {
			return b
		}
		return a
	}, c...)
}

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

func btoI(a bool) int {
	if a {
		return 1
	}
	return 0
}

type State struct {
	fa, dep int
}

type Record struct {
	node int
	State
}

type VDSU struct {
	state []State
	rcd   []Record
}

func (dsu *VDSU) initialize(n int) {
	dsu.state = make([]State, n+1)
	for i, _ := range dsu.state {
		dsu.state[i] = State{i, 0}
	}
}

func (dsu *VDSU) getTime() int { return len(dsu.rcd) }
func (dsu *VDSU) reserve(time int) {
	rcdlen := len(dsu.rcd)
	for rcdlen > time {
		rcdlen--
		dsu.state[dsu.rcd[rcdlen].node] = dsu.rcd[rcdlen].State
		dsu.rcd = dsu.rcd[:rcdlen]
	}
}
func (dsu *VDSU) getfa(v int) int {
	if dsu.state[v].fa == v {
		return v
	}
	return dsu.getfa(dsu.state[v].fa)
}
func (dsu *VDSU) merge(u, v int) bool {
	u, v = dsu.getfa(u), dsu.getfa(v)
	if u == v {
		return false
	}
	if dsu.state[u].dep > dsu.state[v].dep {
		u, v = v, u
	}
	dsu.rcd = append(dsu.rcd, Record{v, dsu.state[v]})
	dsu.rcd = append(dsu.rcd, Record{u, dsu.state[u]})
	dsu.state[u].fa = v
	dsu.state[v].dep = max(dsu.state[v].dep, dsu.state[u].dep+1)
	return true
}
func (dsu *VDSU) same(u, v int) bool {
	return dsu.getfa(u) == dsu.getfa(v)
}

type Edge struct {
	u, v int
}

var dsu VDSU

func tryMerge(l, r int, edges []Edge) bool {
	for i := l; i <= r; i++ {
		u, v := edges[i].u, edges[i].v
		dsu.merge(u<<1, v<<1|1)
		dsu.merge(u<<1|1, v<<1)
		if dsu.same(u<<1, u<<1|1) {
			return false
		}
	}
	return true
}

func dfs(left, right int, edges []Edge) bool {
	if left == right {
		u, v := edges[left].u, edges[left].v

		if dsu.same(u<<1, v<<1|1) || dsu.same(u<<1, v<<1) {
			dsu.merge(u<<1, 1)
		} else {
			dsu.merge(u<<1, 1)
			dsu.merge(v<<1, 1)
		}
		return true
	}
	mid := (left + right) >> 1
	t := dsu.getTime()
	flag := tryMerge(mid+1, right, edges)
	if flag && dfs(left, mid, edges) {
		return true
	}
	dsu.reserve(t)
	t = dsu.getTime()
	if tryMerge(left, mid, edges) && dfs(mid+1, right, edges) {
		return true
	}

	dsu.reserve(t)
	return false
}

func solve(inout io.ReadWriter) {
	scan := func(inp ...interface{}) {
		fmt.Fscan(inout, inp...)
	}
	printf := func(format string, outp ...interface{}) {
		fmt.Fprintf(inout, format, outp...)
	}
	var n, m int
	scan(&n, &m)

	dsu.initialize(n<<1 | 1)
	edges := make([]Edge, m)

	for i := 0; i < m; i++ {
		scan(&edges[i].u, &edges[i].v)
	}

	if dfs(0, m-1, edges) {
		printf("YES\n")
		ans := make([]rune, n+1)
		for i := 1; i <= n; i++ {
			if dsu.same(i<<1, 1) {
				ans[i-1] = '1'
			} else if dsu.same(i<<1|1, 1) {
				ans[i-1] = '0'
			} else {
				dsu.merge(i<<1, 1)
				ans[i-1] = '1'
			}
		}
		ans[n] = '\n'
		printf(string(ans))

	} else {
		printf("NO\n")
	}

}

func main() {

	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	inout := bufio.NewReadWriter(in, out)
	defer out.Flush()

	var T int = 1
	fmt.Fscan(inout, &T)
	for ; T > 0; T-- {
		solve(inout)
	}
}

package main

import (
	"bufio"
	"flag"
	"fmt"
	"io"
	"log"
	"math/big"
	"os"
	"runtime/pprof"
)

type Graph struct {
	g [][]int

	used   []int
	parity []int
}

func NewGraph(n int) *Graph {
	return &Graph{
		g:      make([][]int, n),
		used:   make([]int, n),
		parity: make([]int, n),
	}
}

func (g *Graph) clear() {
	for i := range g.used {
		g.used[i] = 0
	}
}

func (g *Graph) addEdge(u, v int) {
	g.g[u] = append(g.g[u], v)
	g.g[v] = append(g.g[v], u)
}

func (g *Graph) dfs(v int, p int, totalBad int, F *int, T *int) (int, int) {
	g.used[v] = 1
	if p != -1 {
		g.parity[v] = g.parity[p] ^ 1
	}

	bad, good := 0, 0

	for _, to := range g.g[v] {
		if to == p {
			continue
		}

		if g.used[to] == 2 {
			if g.parity[to] == g.parity[v] {
				bad -= 1
			} else {
				good -= 1
			}
		} else if g.used[to] == 1 {
			if g.parity[to] == g.parity[v] {
				bad += 1
			} else {
				good += 1
			}
		} else {
			nbad, ngood := g.dfs(to, v, totalBad, F, T)

			if nbad == totalBad && ngood == 0 {
				*F = v
				*T = to
			}

			bad += nbad
			good += ngood
		}

	}

	g.used[v] = 2

	return bad, good
}

func (g *Graph) dfs2(v int, p int, c int, F *int, T *int) (int, int) {
	g.used[v] = 1
	g.parity[v] = c

	bad, good := 0, 0

	for _, to := range g.g[v] {
		if to == p {
			continue
		}

		if g.used[to] == 2 {
			continue
		} else if g.used[to] == 1 {
			if g.parity[to] == g.parity[v] {
				*F = v
				*T = to

				bad += 1
			} else {
				good += 1
			}
		} else {
			nbad, ngood := g.dfs2(to, v, c^1, F, T)

			bad += nbad
			good += ngood
		}

	}

	g.used[v] = 2

	return bad, good
}

func solve() {
	n := NextInt()
	m := NextInt()

	g := NewGraph(n)

	for i := 0; i < m; i++ {
		u := NextInt() - 1
		v := NextInt() - 1

		g.addEdge(u, v)
	}

	F, T := -1, -1

	bad, _ := g.dfs2(0, -1, 0, &F, &T)
	g.clear()

	if bad == 0 {
		println("YES")
		for i := range g.parity {
			printf("%d", g.parity[i]^1)
		}
		printf("\n")
	} else if bad == 1 {
		g.used[T] = 2
		g.dfs2(F, T, 0, nil, nil)
		g.dfs2(T, F, 0, nil, nil)

		println("YES")
		for i := range g.parity {
			printf("%d", g.parity[i]^1)
		}
		printf("\n")
	} else {
		F, T = -1, -1
		g.dfs(0, -1, bad, &F, &T)

		if F == -1 {
			println("NO")
			return
		}
		g.clear()

		g.used[T] = 2
		g.dfs2(F, T, 0, nil, nil)
		g.dfs2(T, F, 0, nil, nil)

		println("YES")
		for i := range g.parity {
			printf("%d", g.parity[i]^1)
		}
		printf("\n")
	}

}

func main() {
	defer writer.Flush()

	if *cpuprofile != "" {
		f, err := os.Create(*cpuprofile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	T := NextInt()

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

var reader *bufio.Reader
var writer *bufio.Writer

var inputFile = flag.String("input", "", "")
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")

func init() {
	flag.Parse()

	var i io.Reader
	if *inputFile != "" {
		i, _ = os.Open(*inputFile)
	} else {
		i = os.Stdin
	}
	reader = bufio.NewReaderSize(i, 1<<20)
	writer = bufio.NewWriterSize(os.Stdout, 1<<20)
}

func println(a ...interface{}) {
	_, _ = fmt.Fprintln(writer, a...)
}

func printf(format string, a ...interface{}) {
	_, _ = fmt.Fprintf(writer, format, a...)
}

func scanf(format string, a ...interface{}) {
	_, _ = fmt.Fscanf(reader, format, a...)
}

func scan(a interface{}) {
	_, _ = fmt.Fscan(reader, a)
}

func NextInt64() int64 {
	var res int64
	scan(&res)
	return res
}

func NextInt() int {
	var res int
	scan(&res)
	return res
}

func NextIntArray(n int) []int {
	res := make([]int, n)
	for i := range res {
		scan(&res[i])
	}
	return res
}

func NextInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := range res {
		scan(&res[i])
	}
	return res
}

func NextString() string {
	var res string
	scan(&res)
	return res
}

func NextBigInt() *big.Int {
	n := new(big.Int)
	n, _ = n.SetString(NextString(), 10)
	return n
}

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

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

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

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

package main

import (
	"bufio"
	"flag"
	"fmt"
	"io"
	"math/big"
	"os"
)

type DSU struct {
	ptrs []*int
	vals []int

	p      []int
	sz     []int
	parity []int

	isBipartite int
}

func NewDSU(n int) *DSU {
	res := DSU{
		ptrs:        nil,
		vals:        nil,
		p:           make([]int, n),
		sz:          make([]int, n),
		parity:      make([]int, n),
		isBipartite: 1,
	}

	for i := 0; i < n; i++ {
		res.p[i] = i
		res.sz[i] = i
	}

	return &res
}

func (d *DSU) get(v int) (int, int) {
	if d.p[v] != v {
		par, res := d.get(d.p[v])
		return d.parity[v] ^ par, res
	}

	return 0, v
}

func (d *DSU) set(ptr *int, val int) {
	d.ptrs = append(d.ptrs, ptr)
	d.vals = append(d.vals, *ptr)

	*ptr = val
}

func (d *DSU) revert(version int) {
	for len(d.ptrs) > version {
		nlen := len(d.ptrs) - 1
		*d.ptrs[nlen] = d.vals[nlen]
		d.ptrs, d.vals = d.ptrs[:nlen], d.vals[:nlen]
	}
}

func (d *DSU) unite(a, b int) {
	pa, a := d.get(a)
	pb, b := d.get(b)
	if a == b {
		if pa == pb {
			d.set(&d.isBipartite, 0)
		}
		return
	}

	if d.sz[a] > d.sz[b] {
		a, b = b, a
	}

	d.set(&d.p[a], b)
	d.set(&d.parity[a], pa^pb^1)
	d.set(&d.sz[b], d.sz[a]+d.sz[b])
}

type Query struct {
	tp int
	l  int
	r  int
	u  int
	v  int
}

func doProcess(dsu *DSU, queries []Query, left, right int, answer []int) {
	if right-left == 1 {
		version := len(dsu.ptrs)

		for _, q := range queries {
			if q.tp == 1 {
				if q.l <= left && q.r >= right {
					dsu.unite(q.u, q.v)
				}
			}
		}

		for _, q := range queries {
			if q.tp == 0 && q.l == left && dsu.isBipartite == 1 {
				if answer[0] == -1 {
					//println("OK", q.l)
					//for i := range dsu.p {
					//	parity, parent := dsu.get(i)
					//	println(i, parity, parent)
					//}
					for i := range dsu.p {
						p, _ := dsu.get(i)
						if p == 0 {
							answer[i] = 1
						} else {
							answer[i] = 0
						}
					}
				}
			}
		}

		dsu.revert(version)
		return
	}

	version := len(dsu.ptrs)
	var nqueries []Query

	for _, q := range queries {
		if q.tp == 1 && q.l <= left && q.r >= right {
			dsu.unite(q.u, q.v)
			continue
		}

		if q.tp == 0 && q.l >= left && q.r <= right {
			nqueries = append(nqueries, q)
		}

		if q.tp == 1 && q.l < right && q.r > left {
			nqueries = append(nqueries, q)
		}
	}

	mid := (left + right) / 2
	doProcess(dsu, nqueries, left, mid, answer)
	doProcess(dsu, nqueries, mid, right, answer)

	dsu.revert(version)
}

type Edge struct {
	u int
	v int
}

func solve() {
	n := NextInt()
	m := NextInt()

	dsu := NewDSU(n)

	edges := make([]Edge, m)
	for i := 0; i < m; i++ {
		edges[i].u = NextInt() - 1
		edges[i].v = NextInt() - 1
	}

	queries := make([]Query, 0, 2*m)
	for i := 0; i < m; i++ {
		queries = append(queries, Query{
			tp: 1,
			l:  0,
			r:  i,
			u:  edges[i].u,
			v:  edges[i].v,
		})

		queries = append(queries, Query{
			tp: 0,
			l:  i,
			r:  i + 1,
		})

		queries = append(queries, Query{
			tp: 1,
			l:  i + 1,
			r:  m,
			u:  edges[i].u,
			v:  edges[i].v,
		})
	}

	answer := make([]int, n)
	answer[0] = -1

	doProcess(dsu, queries, 0, m, answer)
	if answer[0] != -1 {
		println("YES")
		for i := range answer {
			printf("%d", answer[i])
		}
		println()
	} else {
		println("NO")
	}
}

func main() {
	defer writer.Flush()

	T := NextInt()

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

var reader *bufio.Reader
var writer *bufio.Writer

var inputFile = flag.String("input", "", "")

func init() {
	flag.Parse()
	var i io.Reader
	if *inputFile != "" {
		i, _ = os.Open(*inputFile)
	} else {
		i = os.Stdin
	}
	reader = bufio.NewReaderSize(i, 1<<20)
	writer = bufio.NewWriterSize(os.Stdout, 1<<20)
}

func println(a ...interface{}) {
	_, _ = fmt.Fprintln(writer, a...)
}

func printf(format string, a ...interface{}) {
	_, _ = fmt.Fprintf(writer, format, a...)
}

func scanf(format string, a ...interface{}) {
	_, _ = fmt.Fscanf(reader, format, a...)
}

func scan(a interface{}) {
	_, _ = fmt.Fscan(reader, a)
}

func NextInt64() int64 {
	var res int64
	scan(&res)
	return res
}

func NextInt() int {
	var res int
	scan(&res)
	return res
}

func NextIntArray(n int) []int {
	res := make([]int, n)
	for i := range res {
		scan(&res[i])
	}
	return res
}

func NextInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := range res {
		scan(&res[i])
	}
	return res
}

func NextString() string {
	var res string
	scan(&res)
	return res
}

func NextBigInt() *big.Int {
	n := new(big.Int)
	n, _ = n.SetString(NextString(), 10)
	return n
}

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

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

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

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

package main

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

func acc(op func(int, int) int, c ...int) int {
	res := c[0]
	for _, i := range c {
		res = op(res, i)
	}
	return res
}

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

func min(c ...int) int {
	return acc(func(a int, b int) int {
		if a > b {
			return b
		}
		return a
	}, c...)
}

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

func btoI(a bool) int {
	if a {
		return 1
	}
	return 0
}

type State struct {
	fa, dep int
}

type Record struct {
	node int
	State
}

type VDSU struct {
	state []State
	rcd   []Record
}

func (dsu *VDSU) initialize(n int) {
	dsu.state = make([]State, n+1)
	for i, _ := range dsu.state {
		dsu.state[i] = State{i, 0}
	}
}

func (dsu *VDSU) getTime() int { return len(dsu.rcd) }
func (dsu *VDSU) reserve(time int) {
	rcdlen := len(dsu.rcd)
	for rcdlen > time {
		rcdlen--
		dsu.state[dsu.rcd[rcdlen].node] = dsu.rcd[rcdlen].State
		dsu.rcd = dsu.rcd[:rcdlen]
	}
}
func (dsu *VDSU) getfa(v int) int {
	if dsu.state[v].fa == v {
		return v
	}
	return dsu.getfa(dsu.state[v].fa)
}
func (dsu *VDSU) merge(u, v int) bool {
	u, v = dsu.getfa(u), dsu.getfa(v)
	if u == v {
		return false
	}
	if dsu.state[u].dep > dsu.state[v].dep {
		u, v = v, u
	}
	dsu.rcd = append(dsu.rcd, Record{v, dsu.state[v]})
	dsu.rcd = append(dsu.rcd, Record{u, dsu.state[u]})
	dsu.state[u].fa = v
	dsu.state[v].dep = max(dsu.state[v].dep, dsu.state[u].dep+1)
	return true
}
func (dsu *VDSU) same(u, v int) bool {
	return dsu.getfa(u) == dsu.getfa(v)
}

type Edge struct {
	u, v int
}

var dsu VDSU

func tryMerge(l, r int, edges []Edge) bool {
	for i := l; i <= r; i++ {
		u, v := edges[i].u, edges[i].v
		dsu.merge(u<<1, v<<1|1)
		dsu.merge(u<<1|1, v<<1)
		if dsu.same(u<<1, u<<1|1) {
			return false
		}
	}
	return true
}

func dfs(left, right int, edges []Edge) bool {
	if left == right {
		u, v := edges[left].u, edges[left].v

		if dsu.same(u<<1, v<<1|1) || dsu.same(u<<1, v<<1) {
			dsu.merge(u<<1, 1)
		} else {
			dsu.merge(u<<1, 1)
			dsu.merge(v<<1, 1)
		}
		return true
	}
	mid := (left + right) >> 1
	t := dsu.getTime()
	flag := tryMerge(mid+1, right, edges)
	if flag && dfs(left, mid, edges) {
		return true
	}
	dsu.reserve(t)
	t = dsu.getTime()
	if tryMerge(left, mid, edges) && dfs(mid+1, right, edges) {
		return true
	}

	dsu.reserve(t)
	return false
}

func solve(inout io.ReadWriter) {
	scan := func(inp ...interface{}) {
		fmt.Fscan(inout, inp...)
	}
	printf := func(format string, outp ...interface{}) {
		fmt.Fprintf(inout, format, outp...)
	}
	var n, m int
	scan(&n, &m)

	dsu.initialize(n<<1 | 1)
	edges := make([]Edge, m)

	for i := 0; i < m; i++ {
		scan(&edges[i].u, &edges[i].v)
	}

	if dfs(0, m-1, edges) {
		printf("YES\n")
		ans := make([]rune, n+2)
		for i := 1; i <= n; i++ {
			if dsu.same(i<<1, 1) {
				ans[i-1] = '1'
			} else if dsu.same(i<<1|1, 1) {
				ans[i-1] = '0'
			} else {
				dsu.merge(i<<1, 1)
				ans[i-1] = '1'
			}
		}
		ans[n] = '\n'
		ans[n+1] = 0
		printf(string(ans))

	} else {
		printf("NO\n")
	}

}

func main() {

	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	inout := bufio.NewReadWriter(in, out)
	defer out.Flush()

	var T int = 1
	fmt.Fscan(inout, &T)
	for ; T > 0; T-- {
		solve(inout)
	}
}

package main

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

func acc(op func(int, int) int, c ...int) int {
	res := c[0]
	for _, i := range c {
		res = op(res, i)
	}
	return res
}

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

func min(c ...int) int {
	return acc(func(a int, b int) int {
		if a > b {
			return b
		}
		return a
	}, c...)
}

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

func btoI(a bool) int {
	if a {
		return 1
	}
	return 0
}

type State struct {
	fa, dep int
}

type Record struct {
	node int
	State
}

type VDSU struct {
	state []State
	rcd   []Record
}

func (dsu *VDSU) initialize(n int) {
	dsu.state = make([]State, n+1)
	for i, _ := range dsu.state {
		dsu.state[i] = State{i, 0}
	}
}

func (dsu *VDSU) getTime() int { return len(dsu.rcd) }
func (dsu *VDSU) reserve(time int) {
	rcdlen := len(dsu.rcd)
	for rcdlen > time {
		rcdlen--
		dsu.state[dsu.rcd[rcdlen].node] = dsu.rcd[rcdlen].State
		dsu.rcd = dsu.rcd[:rcdlen]
	}
}
func (dsu *VDSU) getfa(v int) int {
	if dsu.state[v].fa == v {
		return v
	}
	return dsu.getfa(dsu.state[v].fa)
}
func (dsu *VDSU) merge(u, v int) bool {
	u, v = dsu.getfa(u), dsu.getfa(v)
	if u == v {
		return false
	}
	if dsu.state[u].dep > dsu.state[v].dep {
		u, v = v, u
	}
	dsu.rcd = append(dsu.rcd, Record{v, dsu.state[v]})
	dsu.rcd = append(dsu.rcd, Record{u, dsu.state[u]})
	dsu.state[u].fa = v
	dsu.state[v].dep = max(dsu.state[v].dep, dsu.state[u].dep+1)
	return true
}
func (dsu *VDSU) same(u, v int) bool {
	return dsu.getfa(u) == dsu.getfa(v)
}

type Edge struct {
	u, v int
}

var dsu VDSU

func tryMerge(l, r int, edges []Edge) bool {
	for i := l; i <= r; i++ {
		u, v := edges[i].u, edges[i].v
		dsu.merge(u<<1, v<<1|1)
		dsu.merge(u<<1|1, v<<1)
		if dsu.same(u<<1, u<<1|1) {
			return false
		}
	}
	return true
}

func dfs(left, right int, edges []Edge) bool {
	if left == right {
		u, v := edges[left].u, edges[left].v

		if dsu.same(u<<1, v<<1|1) || dsu.same(u<<1, v<<1) {
			dsu.merge(u<<1, 1)
		} else {
			dsu.merge(u<<1, 1)
			dsu.merge(v<<1, 1)
		}
		return true
	}
	mid := (left + right) >> 1
	t := dsu.getTime()
	flag := tryMerge(mid+1, right, edges)
	if flag && dfs(left, mid, edges) {
		return true
	}
	dsu.reserve(t)
	t = dsu.getTime()
	if tryMerge(left, mid, edges) && dfs(mid+1, right, edges) {
		return true
	}

	dsu.reserve(t)
	return false
}

func solve(inout io.ReadWriter) {
	scan := func(inp ...interface{}) {
		fmt.Fscan(inout, inp...)
	}
	printf := func(format string, outp ...interface{}) {
		fmt.Fprintf(inout, format, outp...)
	}
	var n, m int
	scan(&n, &m)

	dsu.initialize(n<<1 | 1)
	edges := make([]Edge, m)

	for i := 0; i < m; i++ {
		scan(&edges[i].u, &edges[i].v)
	}

	if dfs(0, m-1, edges) {
		printf("YES\n")
		ans := make([]rune, n+2)
		for i := 1; i <= n; i++ {
			if dsu.same(i<<1, 1) {
				ans[i-1] = '1'
			} else if dsu.same(i<<1|1, 1) {
				ans[i-1] = '0'
			} else {
				dsu.merge(i<<1, 1)
				ans[i-1] = '1'
			}
		}
		ans[n] = '\n'
		ans[n+1] = 0
		printf(string(ans))

	} else {
		printf("NO")
	}

}

func main() {

	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	inout := bufio.NewReadWriter(in, out)
	defer out.Flush()

	var T int = 1
	fmt.Fscan(inout, &T)
	for ; T > 0; T-- {
		solve(inout)
	}
}

package main

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

func acc(op func(int, int) int, c ...int) int {
	res := c[0]
	for _, i := range c {
		res = op(res, i)
	}
	return res
}

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

func min(c ...int) int {
	return acc(func(a int, b int) int {
		if a > b {
			return b
		}
		return a
	}, c...)
}

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

func btoI(a bool) int {
	if a {
		return 1
	}
	return 0
}

type State struct {
	fa, dep int
}

type Record struct {
	node int
	State
}

type VDSU struct {
	state []State
	rcd   []Record
}

func (dsu *VDSU) initialize(n int) {
	dsu.state = make([]State, n+1)
	for i, _ := range dsu.state {
		dsu.state[i] = State{i, 0}
	}
}

func (dsu *VDSU) getTime() int { return len(dsu.rcd) }
func (dsu *VDSU) reserve(time int) {
	rcdlen := len(dsu.rcd)
	for rcdlen > time {
		rcdlen--
		dsu.state[dsu.rcd[rcdlen].node] = dsu.rcd[rcdlen].State
		dsu.rcd = dsu.rcd[:rcdlen]
	}
}
func (dsu *VDSU) getfa(v int) int {
	if dsu.state[v].fa == v {
		return v
	}
	return dsu.getfa(dsu.state[v].fa)
}
func (dsu *VDSU) merge(u, v int) bool {
	u, v = dsu.getfa(u), dsu.getfa(v)
	if u == v {
		return false
	}
	if dsu.state[u].dep > dsu.state[v].dep {
		u, v = v, u
	}
	dsu.rcd = append(dsu.rcd, Record{v, dsu.state[v]})
	dsu.rcd = append(dsu.rcd, Record{u, dsu.state[u]})
	dsu.state[u].fa = v
	dsu.state[v].dep = max(dsu.state[v].dep, dsu.state[u].dep+1)
	return true
}
func (dsu *VDSU) same(u, v int) bool {
	return dsu.getfa(u) == dsu.getfa(v)
}

type Edge struct {
	u, v int
}

var dsu VDSU

func tryMerge(l, r int, edges []Edge) bool {
	for i := l; i <= r; i++ {
		u, v := edges[i].u, edges[i].v
		dsu.merge(u<<1, v<<1|1)
		dsu.merge(u<<1|1, v<<1)
		if dsu.same(u<<1, u<<1|1) {
			return false
		}
	}
	return true
}

func dfs(left, right int, edges []Edge) bool {
	if left == right {
		u, v := edges[left].u, edges[left].v

		if dsu.same(u<<1, v<<1|1) || dsu.same(u<<1, v<<1) {
			dsu.merge(u<<1, 1)
		} else {
			dsu.merge(u<<1, 1)
			dsu.merge(v<<1, 1)
		}
		return true
	}
	mid := (left + right) >> 1
	t := dsu.getTime()
	flag := tryMerge(mid+1, right, edges)
	if flag && dfs(left, mid, edges) {
		return true
	}
	dsu.reserve(t)
	t = dsu.getTime()
	if tryMerge(left, mid, edges) && dfs(mid+1, right, edges) {
		return true
	}

	dsu.reserve(t)
	return false
}

func solve(inout io.ReadWriter) {
	scan := func(inp ...interface{}) {
		fmt.Fscan(inout, inp...)
	}
	printf := func(format string, outp ...interface{}) {
		fmt.Fprintf(inout, format, outp...)
	}
	var n, m int
	scan(&n, &m)

	dsu.initialize(n<<1 | 1)
	edges := make([]Edge, m)

	for i := 0; i < m; i++ {
		scan(&edges[i].u, &edges[i].v)
	}

	if dfs(0, m-1, edges) {
		printf("YES\n")
		ans := make([]rune, n+2)
		for i := 1; i <= n; i++ {
			if dsu.same(i<<1, 1) {
				ans[i-1] = '1'
			} else if dsu.same(i<<1|1, 1) {
				ans[i-1] = '0'
			} else {
				dsu.merge(i<<1, 1)
				ans[i-1] = '2'
			}
		}
		ans[n] = '\n'
		ans[n+1] = 0
		printf(string(ans))

	} else {
		printf("NO")
	}

}

func main() {

	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	inout := bufio.NewReadWriter(in, out)
	defer out.Flush()

	var T int = 1
	fmt.Fscan(inout, &T)
	for ; T > 0; T-- {
		solve(inout)
	}
}


package main

import "fmt"

func calculateTime(a, b, c, r int) int {
	if a > b {
		a, b = b, a
	}

	path := b - a

	cLeft := c - r
	cRight := c + r
	if cRight < a {
		return path
	}
	if cLeft <= a && a <= cRight {
		if path-cRight+a > 0 {
			return path - cRight + a
		}

	}
	if cLeft > a {
		if cRight < b {
			return path - 2*r
		}
		if cLeft <= b && b <= cRight {
			if path-b+cLeft > 0 {
				return path - b + cLeft
			}
		}
		if cLeft > b {
			return path
		}
	}
	return 0
}

func main() {
	var n int
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		var a, b, c, r int
		fmt.Scan(&a, &b, &c, &r)
		time := calculateTime(a, b, c, r)
		fmt.Println(time)
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := int64(1)
	fmt.Fscanf(in, "%d\n", &t)
	for test := int64(0); test < t ; test++ {
		a, b, c, r := 0, 0, 0, 0
		fmt.Fscanf(in, "%d %d %d %d\n", &a, &b, &c, &r)
		if a > b {
			a, b = b, a
		}
		lo := c - r
		hi := c + r
		ans := 0
		if lo >= b || hi <= a {
			ans = b - a
		} else {
			if lo > a {
				ans = lo - a
			}
			if hi < b {
				ans += b - hi
			}
		}
		fmt.Fprintf(out, "%d\n", ans)
	}
}

package main
import (
    "fmt"
)

func main() {
    var t int
    fmt.Scan(&t)
    for i := 0; i < t; i++ {
        var a, b, c, r int
        fmt.Scan(&a, &b, &c, &r)
        d := c + r
        c = c - r
        if a > b {
            a, b = b, a
        }
        if a == b {
            fmt.Println(0)
            continue
        }
        if d <= a || b <= c {
            fmt.Println(b-a)
            continue
        }
        ans := 0
        if a <= c && c <= b {
            ans += c - a
        }
        if a <= d && d <= b {
            ans += b - d
        }
        fmt.Println(ans)
    }
}

package main

import (
	"fmt"
)

func main() {
	var t int
	fmt.Scanf("%d\n", &t)
	for i := 0; i < t; i++ {
		var a, b, c, r int
		fmt.Scanf("%d %d %d %d\n", &a, &b, &c, &r)

		cl := c - r
		cr := c + r

		if a > b {
			a, b = b, a
		}

		if cr <= a {
			fmt.Println(b - a)
		} else if cl >= b {
			fmt.Println(b - a)
		} else if cl <= a && cr >= b {
			fmt.Println(0)
		} else if cl < a && cr > a {
			fmt.Println(b - cr)
		} else if cl < b && cr > b {
			fmt.Println(cl - a)
		} else {
			fmt.Println((cl - a) + (b - cr))
		}
	}
}

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 a, b, c, r int

func main() {
	t = ReadInt()

	for tc := 0; tc < t; tc++ {
		a, b, c, r = ReadInt4()

		solve()
	}
}

func solve() {
	if a > b {
		a, b = b, a
	}

	left, right := c-r, c+r

	if b <= left || a >= right {
		fmt.Println(b - a)
	} else if a <= left && right <= b {
		fmt.Println(b - a - (right - left))
	} else if (a <= right && right <= b) && !(a <= left && left <= b) {
		fmt.Println(Max(0, b-right))
	} else {
		fmt.Println(Max(0, left-a))
	}
}

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

/*
- まずは全探索を検討しましょう
- 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"
	"os"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := int64(1)
	fmt.Fscanf(in, "%d\n", &t)
	for tt := int64(0); tt < t; tt++ {
		a, b, c, r := 0, 0, 0, 0
		fmt.Fscanf(in, "%d %d %d %d\n", &a, &b, &c, &r)
		if a > b {
			a, b = b, a
		}
		small := c - r
		large := c + r
		ans := 0
		if small >= b || large <= a {
			ans = b - a
		} else {
			if small > a {
				ans = small - a
			}
			if b > large {
				ans += b - large
			}
		}
		fmt.Fprintf(out, "%d\n", ans)
	}
	return
}

func same(s string, ss string) bool {
	a := make([]int, 26)
	for i := 0; i < len(s); i++ {
		b := int(s[i] - 'a')
		a[b]++
	}
	for i := 0; i < len(ss); i++ {
		b := int(ss[i] - 'a')
		a[b]--
		if a[b] == -1 {
			return false
		}
	}
	for i := 0; i < 26; i++ {
		if a[i] != 0 {
			return false
		}
	}
	return true
}

package main

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

// -----------------------------------------------------------------------------

// IO helper functions

// Returns next token from input.  It must be initialized by SetInput()
// before the first call.
var nextToken func() ([]byte, error)
var nextLine func() ([]byte, error)

// Holds a buffer for output.  It must be initialized by SetOutput().
// All IO fucntions (read*() and [e]print*()) should write to OutputWriter
// instead of this.
var OutputBuffer *bufio.Writer

// Holds an io.Writer.  It must be initialized by SetOutput()
var OutputWriter io.Writer

// Set IO functions for interactive input/output.
func SetInteractive(w io.Writer, r io.Reader) {
	SetUnbefferedInput(r)
	OutputBuffer = nil
	OutputWriter = w
}

// Setup OutputBuffer and OutputWriter.
func SetOutput(w io.Writer) {
	OutputBuffer = bufio.NewWriter(w)
	OutputWriter = OutputBuffer
}

// Flushes OutputBuffer
func Flush() {
	if OutputBuffer != nil {
		OutputBuffer.Flush()
	}
}

// Returns true if c is a white space
func IsSpace(c byte) bool {
	switch c {
	case '\t', '\n', '\v', '\f', '\r', ' ':
		return true
	}
	return false
}

func IsNewLine(c byte) bool {
	switch c {
	case '\n', '\r':
		return true
	}
	return false
}

// Setup nextToken with input buffer.
func SetInput(r io.Reader) {
	buf := new(bytes.Buffer)
	var b []byte

	var i int
	rest := func() ([]byte, error) {
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		return b[j:i], nil
	}
	initial := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextToken = rest
		return rest()
	}
	nextToken = initial

	restLn := func() ([]byte, error) {
		for i < len(b) && IsNewLine(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsNewLine(b[i]) {
			i++
		}
		return b[j:i], nil
	}

	initialLn := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextLine = restLn
		return restLn()
	}
	nextLine = initialLn
}

// Setup nextToken without input buffer.
func SetUnbefferedInput(r io.Reader) {
	buf := bufio.NewReader(r)
	var b []byte

	var i int
	nextToken = func() ([]byte, error) {
		var err error
		if i == len(b) {
			b, err = buf.ReadBytes('\n')
			if err != nil {
				return nil, err
			}
			i = 0
			j := len(b) - 1
			for 0 <= j && IsSpace(b[j]) {
				j--
			}
			b = b[0 : j+1]
		}
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		if i == j {
			return nil, io.ErrUnexpectedEOF
		}
		return b[j:i], nil
	}
}

// -----------------------------------------------------------------------------

// IO functions

// Reads next token and return it as []byte
func readb() []byte {
	b, err := nextToken()
	if err != nil {
		panic(err)
	}
	return b
}

// Reads next token and return it as string
func reads() string {
	return string(readb())
}

// Read next line as []byte
func readbln() []byte {
	b, err := nextLine()
	if err != nil {
		panic(err)
	}
	return b
}

// Read next line as string
func readsln() string {
	return string(readbln())
}

// Reads next token and return it as int64
func readll() int64 {
	i, err := strconv.ParseInt(reads(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// Reads next token and return it as int
func readi() int {
	return int(readll())
}

// Reads next token and return it as float64
func readf() float64 {
	f, err := strconv.ParseFloat(reads(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// Write args to OutputWriter with the format f
func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(OutputWriter, f, args...)
}

// Write args to OutputWriter without format
func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(OutputWriter, args...)
}

// Write args to stderr with the format f
func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

// Write args to stderr without format
func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

// -----------------------------------------------------------------------------

// Simple math functions

const (
	// big prime
	INF  = 1000000007
	INF2 = 1000000009
	INF3 = 998244353
)

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

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

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

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

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

func absll(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

// egcd(a, b) returns d, x, y:
//   d is gcd(a,b)
//   x, y are  integers that satisfy ax + by = d
func egcd(a, b int) (int, int, int) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcd(b, a%b)
	return d, y, x - a/b*y
}

func egcdll(a, b int64) (int64, int64, int64) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcdll(b, a%b)
	return d, y, x - a/b*y
}

func gcd(a, b int) int {
	d, _, _ := egcd(a, b)
	return d
}

func gcdll(a, b int64) int64 {
	d, _, _ := egcdll(a, b)
	return d
}

// set up IO functions
func init() {
	// for non-interactive
	SetInput(os.Stdin)
	SetOutput(os.Stdout)

	// Enable below when interactive.  Its ok to leave above intact.
	// SetInteractive(os.Stdout, os.Stdin)
}

func main() {
	defer Flush()

	t := readi()
	for i := 0; i < t; i++ {
		a := readi()
		b := readi()
		c := readi()
		r := readi()
		println(solve(a, b, c, r))
	}
}

func solve(a, b, c, r int) int {
	if b < a {
		a, b = b, a
	}
	cl := c - r
	cr := c + r
	if cr <= a || b <= cl {
		return b - a
	}
	if a <= cl && cr <= b {
		return b - a - (cr - cl)
	}
	if cl < a && b < cr {
		return 0
	}
	if cl < a {
		return b - cr
	}
	if b < cr {
		return cl - a
	}
	return 0
}

package main

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

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

	testCount := 0
	fmt.Fscanf(in, "%d\n", &testCount)

	for i := 0; i < testCount; i++ {
		a, b, c, r := 0, 0, 0, 0
		fmt.Fscanf(in, "%d %d %d %d\n", &a, &b, &c, &r)
		if a > b {
			a, b = b, a
		}

		p := c - r
		q := c + r

		pr := maxInt(0, minInt(b, q)-maxInt(a, p))
		fmt.Println((b - a - pr))
	}
}

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

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

package main

import (
	"fmt"
)

func main() {

	testCount := 0
	fmt.Scanf("%d\n", &testCount)

	for i := 0; i < testCount; i++ {
		a, b, c, r := 0, 0, 0, 0
		fmt.Scanf("%d %d %d %d\n", &a, &b, &c, &r)
		if a > b {
			a, b = b, a
		}

		p := c - r
		q := c + r

		pr := maxInt(0, minInt(b, q)-maxInt(a, p))
		fmt.Println((b - a - pr))
	}
}

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

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

package main

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

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

    for i := 0; i < n; i++ {
        var a, b, c, r int
        fmt.Fscan(in, &a, &b, &c, &r)
        if a > b {
            a, b = b, a
        }
        x, y := c - r, c + r
        ans := 0
        if y <= a || x >= b {
            ans = b - a
        } else {
            if x < a {
                x = a
            }
            if y > b {
                y = b
            }
            ans = (b - a) - (y - x)
        }
        fmt.Fprintln(out, ans)
    }
}

package main

import "fmt"

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

    for i := 0; i < n; i++ {
        var a, b, c, r int
        fmt.Scan(&a, &b, &c, &r)
        if a > b {
            a, b = b, a
        }
        x, y := c - r, c + r
        ans := 0
        if y <= a || x >= b {
            ans = b - a
        } else {
            if x < a {
                x = a
            }
            if y > b {
                y = b
            }
            ans = (b - a) - (y - x)
        }
        fmt.Println(ans)
    }
}

package main

import "fmt"

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

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

type Segment struct {
	l int
	r int
}

func intersect(s1, s2 Segment) Segment {
	l := max(s1.l, s2.l)
	r := min(s1.r, s2.r)
	return Segment{l, r}
}

func solve() {
	var a, b, c, r int
	fmt.Scanf("%d %d %d %d\n", &a, &b, &c, &r)
	if a > b {
		a, b = b, a
	}
	s1 := Segment{a, b}
	s2 := Segment{c-r, c+r}
	s := intersect(s1, s2)
	len := s.r - s.l
	if len < 0 {
		len = 0
	}
	ans := b - a - len
	fmt.Println(ans)
}

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

package main

import "fmt"

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

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

type Segment struct {
	l int
	r int
}

func intersect(s1, s2 Segment) Segment {
	l := max(s1.l, s2.l)
	r := min(s1.r, s2.r)
	return Segment{l, r}
}

func solve() {
	var a, b, c, r int
	fmt.Scanf("%d %d %d %d\n", &a, &b, &c, &r)
	if a > b {
		a, b = b, a
	}
	s1 := Segment{a, b}
	s2 := Segment{c-r, c+r}
	s := intersect(s1, s2)
	len := max(s.r - s.l, 0)
	ans := b - a - len
	fmt.Println(ans)
}

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

package main

import (
	"fmt"
)

func main() {
	testCount := 0
	fmt.Scanf("%d\n", &testCount)

	for i := 0; i < testCount; i++ {
		a, b, c, r := 0, 0, 0, 0
		fmt.Scanf("%d %d %d %d\n", &a, &b, &c, &r)
		p := maxInt(a, c-r)
		q := minInt(b, c+r)

		timeUnavailable := (b - a) - (q - p)
		fmt.Println(timeUnavailable)
	}
}

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

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

package main

import (
	"fmt"
)

func main() {
	testCount := 0
	fmt.Scanf("%d\n", &testCount)

	for i := 0; i < testCount; i++ {
		a, b, c, r := 0, 0, 0, 0
		fmt.Scanf("%d %d %d %d\n", &a, &b, &c, &r)
		if a > b {
			tmp := a
			a = b
			b = tmp
		}

		p := c - r
		q := c + r

		if p <= a && q <= a {
			fmt.Println((b - a))
		} else if p < a && q > a && q < b {
			fmt.Println((b - q))
		} else if p < a && q > b {
			fmt.Println(0)
		} else if p > a && p < b && q > a && q < b {
			fmt.Println((b - a) - (q - p))
		} else if p > a && p < b && q > b {
			fmt.Println((b - a) - (b - p))
		} else if p >= b && q >= b {
			fmt.Println((b - a))
		} else {
			fmt.Println(0)
		}
	}
}

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

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


package main

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

var land [105]int

func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewScanner(_r)
	in.Split(bufio.ScanWords)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	read := func() (x int) {
		in.Scan()
		for _, b := range in.Bytes() {
			x = x*10 + int(b&15)
		}
		return
	}
	T := read()
	for T > 0 {
		T--
		n := read()
		l, r := 1, n
		for i := 1; i <= n; i++ {
			land[i] = read()
		}
		for l < n && land[l+1] == 1 {
			l++
		}
		for r-1 > l && land[r-1] == 1 {
			r--
		}
		Fprintln(out, r-l)
	}
}

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

package main

import "fmt"

func main() {
	var t int
	fmt.Scan(&t)
	for t > 0 {
		var n int
		fmt.Scan(&n)
		var arr = make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Scan(&arr[i])
		}
		first := 0
		last := 0
		for i := 0; i < n; i++ {
			if arr[i] == 0 {
				first = i
				break
			}
		}
		for i := 0; i < n; i++ {
			if arr[i] == 0 {
				last = i
			}
		}
		if first == 0 {
			fmt.Println(0)
		} else {
			fmt.Println((last + 1) - (first - 1))
		}
		t -= 1
	}
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1649A(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var T, n int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		land := make([]bool, n)
		for i := range land {
			Fscan(in, &land[i])
		}
		i, j := 1, n-2
		for ; i < n && land[i]; i++ {
		}
		for ; j > i && land[j]; j-- {
		}
		Fprintln(out, j-i+2)
	}
}

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

//nolint
package main

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

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

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }
func print(a ...interface{})            { fmt.Fprint(writer, a...) }
func println(a ...interface{})          { fmt.Fprintln(writer, a...) }
func scan(a ...interface{})             { fmt.Fscan(reader, a...) }

func main() {
	defer writer.Flush()

	Main()
}

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

//}

func Solve() {
	var n int
	scan(&n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		scan(&a[i])
	}
	l, r := 0, 0
	for l = 0; l+1 < n && a[l+1] == 1; l++ {
	}
	for r = n - 1; r-1 >= 0 && a[r-1] == 1; r-- {
	}
	if l >= r {
		println(0)
	} else {
		println(r - l)
	}
}

package main

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

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

  var t int
  for fmt.Fscan(reader, &t); t > 0; t-- {
		var n int
		fmt.Fscan(reader, &n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a[i])
		}
		l := 0
		r := n-1
		for ; l < n-1 && a[l+1] == 1; l++ {}
		for ; r > 0 && a[r-1] == 1; r-- {}
		ans := r-l
		if ans < 0 {
			ans = 0
		}
		fmt.Fprintf(writer, "%d\n", ans)
  }
}

package main

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

/*
2
4
1 1 0 1
6
1 0 1 0 0 1
---
2
5
*/

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

	a := make([]int, n)

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

	leftIdx := -1

	for i, num := range a {
		if num == 0 {
			leftIdx = i
			break
		}
	}

	rgtIdx := -1

	for i := n - 1; i >= 0; i-- {
		if a[i] == 0 {
			rgtIdx = i
			break
		}
	}

	if leftIdx > 0 {
		fmt.Fprintln(writer, rgtIdx-leftIdx+2)
	} else {
		fmt.Fprintln(writer, 0)
	}
}

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

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

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

const maxUint = ^uint(0)

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

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

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

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

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

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

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

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

	return nums[idx], idx
}

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

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

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

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

	return true
}

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

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

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

	num = abs(num)

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

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

package main

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

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

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

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

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

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

/**
Run solve the problem CF1649A
Date: 11/10/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1649/A
Problem: CF1649A
**/
func (in *CF1649A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			arr[i] = in.NextInt()
		}
		ans := 0

		last := n - 1
		for ; last >= 0; last-- {
			if arr[last] == 1 {
				continue
			} else {
				break
			}
		}
		last++
		ini := 0

		for ; ini <= last; ini++ {
			if arr[ini] == 1 {
				continue
			} else {
				break
			}
		}
		ini--

		start := ini
		zero := false
		for i := ini; i <= last; i++ {
			if arr[i] == 0 {
				zero = true
			}
			if zero && arr[i] == 1 {
				ans += i - start
				start = i
			}
		}

		fmt.Println(ans)

	}

}

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

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

package main

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

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

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

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

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

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

/**
Run solve the problem CF1649A
Date: 11/10/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1649/A
Problem: CF1649A
**/
func (in *CF1649A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			arr[i] = in.NextInt()
		}
		ans := 0

		last := n - 1
		for ; last >= 0; last-- {
			if arr[last] == 1 {
				continue
			} else {
				break
			}
		}
		last++

		start := 0
		zero := false
		for i := 1; i <= last; i++ {
			if arr[i] == 0 {
				zero = true
			}
			if zero && arr[i] == 1 {
				ans += i - start
				start = i
			}
		}

		fmt.Println(ans)

	}

}

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

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

package main

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

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

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

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

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

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

/**
Run solve the problem CF1649A
Date: 11/10/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1649/A
Problem: CF1649A
**/
func (in *CF1649A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		space := 0
		ans := 0
		val := in.NextInt()
		for i := 1; i < n; i++ {
			val = in.NextInt()
			if val == 1 {
				if space > 0 {
					ans += space + 1
				}

				space = 0
			} else {
				space++
			}
		}
		fmt.Println(ans)

	}

}

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

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

package main

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

//var land [105]int

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

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

package main

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

//var land [105]int

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

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

package main

import "fmt"

func main() {
	var t int
	fmt.Scan(&t)
	for t > 0 {
		var n int
		fmt.Scan(&n)
		ans := 0
		for i := 0; i < n; i++ {
			var x int
			fmt.Scan(&x)
			c := 0
			if x == 0 {
				for x == 0 && i < n {
					fmt.Scan(&x)
					i++
					c++
				}
				c++
			}
			ans += c
		}
		fmt.Println(ans)
		t -= 1
	}
}


package main

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

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

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

// func min3(a, b, c int) int {
// 	return min(min(a, b), c)
// }

// func maxSquare(a int) int {
// 	tmp := int(math.Sqrt(float64(a)))
// 	if tmp*tmp == a {
// 		return tmp * tmp
// 	} else {
// 		return (tmp + 1) * (tmp + 1)
// 	}
// }

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var niter int
	fmt.Fscan(in, &niter)
	for iiter := 0; iiter < niter; iiter++ {
		var n, m int
		fmt.Fscan(in, &n, &m)
		table := make([][]rune, n)
		for i := 0; i < n; i++ {
			table[i] = make([]rune, m)
		}
		for i := 0; i < n*m; i++ {
			//fmt.Fscan(in, &table[i/m][i%m])
			b, _, _ := in.ReadRune()
			for b != '0' && b != '1' {
				b, _, _ = in.ReadRune()
			}
			table[i/m][i%m] = b
		}
		ones := 0
		zeroes := 0
		for i := 0; i < n*m; i++ {
			if table[i/m][i%m] == '1' {
				ones++
			} else {
				zeroes++
			}
		}
		mode := 0
		if zeroes > 0 {
			mode = 1
		}
		// check for 2 horizontal, vertical, diagonal (x2)
		for i := 0; i < n-1; i++ {
			for j := 0; j < m; j++ {
				if table[i][j] == table[i+1][j] && table[i][j] == '0' {
					mode = 2
				}
			}
		}
		for i := 0; i < n; i++ {
			for j := 0; j < m-1; j++ {
				if table[i][j] == table[i][j+1] && table[i][j] == '0' {
					mode = 2
				}
			}
		}
		// diag x2
		for i := 0; i < n-1; i++ {
			for j := 0; j < m-1; j++ {
				if table[i][j] == table[i+1][j+1] && table[i][j] == '0' {
					mode = 2
				}
			}
		}
		for i := 1; i < n; i++ {
			for j := 0; j < m-1; j++ {
				if table[i][j] == table[i-1][j+1] && table[i][j] == '0' {
					mode = 2
				}
			}
		}
		if mode == 0 {
			fmt.Fprintln(out, ones-2)
		}
		if mode == 1 {
			fmt.Fprintln(out, ones-1)
		}
		if mode == 2 {
			fmt.Fprintln(out, ones)
		}
	}
}

package main

import (
	"fmt"
	"math"
)

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

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

func solve(m, n int, arr [][]int, nOfOne int) int {
	if nOfOne == 0 {
		return 0
	}
	minn := math.MaxInt
	for i := 0; i < n-1; i++ {
		for j := 0; j < m-1; j++ {
			tmp := arr[i][j] + arr[i+1][j] + arr[i][j+1] + arr[i+1][j+1]
			if tmp == 0 {
				continue
			}
			minn = min(minn, max(1, tmp-1))
		}
	}
	// fmt.Println(minn)
	return 1 + nOfOne - minn
}

func main() {
	t, n, m := 0, 0, 0
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		fmt.Scan(&n, &m)
		input := make([][]int, n)
		for j := 0; j < n; j++ {
			input[j] = make([]int, m)
		}
		numOfOne := 0
		for j := 0; j < n; j++ {
			tmp := ""
			fmt.Scan(&tmp)
			for k, v := range tmp {
				if string(v) == "1" {
					input[j][k] = 1
					numOfOne++
				} else {
					input[j][k] = 0
				}
			}
		}
		fmt.Println(solve(m, n, input, numOfOne))
	}
}

	package main

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

	var in *bufio.Reader = bufio.NewReader(os.Stdin)

	func main() {
		var t int
		fmt.Fscanf(in, "%d\n", &t)
		for t > 0 {
			solve()
			t--
		}
	}
	func solve() {
		var n, m int
		fmt.Fscanf(in, "%d %d\n", &n, &m)
		a := make([][]int, n)
		// 构造矩阵
		for i := range a {
			a[i] = make([]int, m)
		}
		zero := 0
		for i := 0; i < n; i++ {
			var s string
			fmt.Fscanln(in, &s)
			for j := range s {
				a[i][j] = int(s[j] - '0')
				if a[i][j] == 0 {
					zero++
				}
			}
		}
		one := n*m - zero
		if one == 0 {
			fmt.Println(0)
		} else if zero == 0 {
			fmt.Println(1 + one - 3)
		} else {
			flag := false

		out:
			for i := 0; i < n; i++ {
				for j := 0; j < m; j++ {
					if a[i][j] == 0 {
						if (i-1 >= 0 && a[i-1][j] == 0) ||
							(j-1 >= 0 && a[i][j-1] == 0) ||
							(i+1 < n && a[i+1][j] == 0) ||
							(j+1 < m && a[i][j+1] == 0) {
							flag = true
							break out
						}
					}
					if a[i][j] == 1 {
						if (i-1 >= 0 && a[i-1][j] == 0 && (j-1 >= 0 && a[i][j-1] == 0 || j+1 < m && a[i][j+1] == 0)) ||
							(i+1 < n && a[i+1][j] == 0 && (j-1 >= 0 && a[i][j-1] == 0 || j+1 < m && a[i][j+1] == 0)) {
							flag = true
							break out
						}
					}
				}
			}
			if flag {
				fmt.Println(one)
			} else {
				fmt.Println(1 + one - 2)
			}
		}
	}

package main

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

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

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

		aa := make([]string, m)
		var c int
		for i := 0; i < m; i++ {
			fmt.Fscan(in, &aa[i])
			for _, ch := range aa[i] {
				if ch == '1' {
					c++
				}
			}
		}

		// fmt.Println(aa)
		val := func(ch byte) int {
			return int(ch - '0')
		}
		d := 5
		for i := 0; i < m-1; i++ {
			for j := 0; j < n-1; j++ {
				d = min(d, val(aa[i][j])+val(aa[i+1][j])+val(aa[i][j+1])+val(aa[i+1][j+1]))
			}
		}
		c -= d
		c += min(2, d)
		fmt.Fprintln(out, c)
	}
}

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	cases := readInt(input)
	for asdf := 0; asdf < cases; asdf++ {
		tmp := readArrInt(input)
		n := tmp[0]
		m := tmp[1]
		lines := [][]rune{}
		for i := 0; i < n; i++ {
			line := readLine(input)
			lines = append(lines, []rune(line))
		}
		fmt.Println(solve(n, m, lines))
	}
}

func solve(n, m int, arr [][]rune) int {
	ones := 0
	zeros := 0
	coupled_zeros := false
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if arr[i][j] == '1' {
				ones++
			} else {
				if coupled_zeros == false {
					sub := 0
					for k := max(0, i-1); k <= min(i+1, n-1); k++ {
						for l := max(0, j-1); l <= min(j+1, m-1); l++ {
							if arr[k][l] == '0' {
								sub++
							}
						}
					}
					if sub > 1 {
						coupled_zeros = true
					}
				}
				zeros++
			}
		}
	}
	if zeros == 0 {
		return ones - 2
	} else if coupled_zeros {
		return ones
	} else {
		return ones - 1
	}
}

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

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

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

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

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

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

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

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

package main

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

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

func slove() {
	var n, m int
	fmt.Fscan(rr, &n, &m)
	g := make([]string, n)
	sum := 0
	for i := range g {
		fmt.Fscan(rr, &g[i])
		for j := range g[i] {
			if g[i][j] == '1' {
				sum++
			}
		}
	}
	flag := false
	for i := range g {
		for j:=0;j<m-1;j++ {
			if g[i][j] == g[i][j+1] && g[i][j] == '0' {
				flag = true
			}
		}
	}
	for i:=0;i<n-1;i++ {
		for j:=0;j<m-1;j++ {
			if g[i][j] == g[i+1][j+1] && g[i][j] == '0' {
				flag = true
			}
		}
	}
	for i:=0;i<n-1;i++ {
		for j:=1;j<m;j++ {
			if g[i][j] == g[i+1][j-1] && g[i][j] == '0' {
				flag = true
			}
		}
	}
	for j := range g[0] {
		for i:=0;i<n-1;i++ {
			if g[i][j] == g[i+1][j] && g[i][j] == '0' {
				flag = true
			}
		}
	}
	has0 := sum != n*m
	if flag {
		fmt.Fprintln(ww, sum)
	} else if has0 {
		fmt.Fprintln(ww, sum-1)
	} else {
		fmt.Fprintln(ww, sum-2)
	}
}

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

package main

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

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

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

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

func do(n, m int, a []string) int {
	c0, c1 := 0, 0
	for _, s := range a {
		for _, c := range s {
			if c == '0' {
				c0++
			} else {
				c1++
			}
		}
	}
	pat := [][]string{
		{"0*", "*0"},
		{"*0", "0*"},
		{"00"},
		{"0", "0"},
	}
	for _, v := range pat {
		ok := search(n, m, a, v)
		if ok {
			return c1
		}
	}
	if c0 > 0 {
		return c1 - 1
	}
	return c1 - 2
}

func search(n, m int, a []string, v []string) bool {
	for i := 0; i+len(v)-1 < n; i++ {
		for j := 0; j+len(v[0])-1 < m; j++ {
			ok := true
			for x := 0; x < len(v); x++ {
				for y := 0; y < len(v[x]); y++ {
					if v[x][y] != '*' && v[x][y] != a[i+x][j+y] {
						ok = false
					}
				}
			}
			if ok {
				return true
			}
		}
	}
	return false
}

package main

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

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

	var t int
	Fscan(in, &t)
	for t > 0 {
		t--
		var n, m int
		Fscan(in, &n, &m)
		a := make([]string, n)
		for i := 0; i < n; i++ {
			Fscan(in, &a[i])
		}
		cnt := 0
		flag1 := false
		flag23 := false
		for i := 0; i < n; i++ {
			for j := 0; j < m; j++ {
				if a[i][j] == '1' {
					cnt++
				}
				x, y := i, j
				cnt0 := 0
				if x - 1 >= 0 && y - 1 >= 0 {
					cnt0 = 0
					if a[x - 1][y - 1] == '0' {
						cnt0++
					}
					if a[x - 1][y] == '0' {
						cnt0++
					}
					if a[x][y - 1] == '0' {
						cnt0++
					}
					if a[x][y] == '0' {
						cnt0++
					}
					if cnt0 >= 2 {
						flag23 = true
					} else if cnt0 == 1 {
						flag1 = true
					}
				}
				if x - 1 >= 0 && y + 1 < m {
					cnt0 = 0
					if a[x - 1][y + 1] == '0' {
						cnt0++
					}
					if a[x - 1][y] == '0' {
						cnt0++
					}
					if a[x][y + 1] == '0' {
						cnt0++
					}
					if a[x][y] == '0' {
						cnt0++
					}
					if cnt0 >= 2 {
						flag23 = true
					} else if cnt0 == 1 {
						flag1 = true
					}
				}
				if x + 1 < n && y - 1 >= 0 {
					cnt0 = 0
					if a[x + 1][y - 1] == '0' {
						cnt0++
					}
					if a[x + 1][y] == '0' {
						cnt0++
					}
					if a[x][y - 1] == '0' {
						cnt0++
					}
					if a[x][y] == '0' {
						cnt0++
					}
					if cnt0 >= 2 {
						flag23 = true
					} else if cnt0 == 1 {
						flag1 = true
					}
				}
				if x + 1 < n && y + 1 < m {
					cnt0 = 0
					if a[x + 1][y + 1] == '0' {
						cnt0++
					}
					if a[x + 1][y] == '0' {
						cnt0++
					}
					if a[x][y + 1] == '0' {
						cnt0++
					}
					if a[x][y] == '0' {
						cnt0++
					}
					if cnt0 >= 2 {
						flag23 = true
					} else if cnt0 == 1 {
						flag1 = true
					}
				}
			}
		}
		if flag23 {
			Fprintln(out, cnt)
		} else if flag1 {
			Fprintln(out, cnt - 1)
		} else {
			Fprintln(out, cnt - 2)
		}
	}
}

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

package main

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

var in *bufio.Reader = bufio.NewReader(os.Stdin)

func main() {
	var t int
	fmt.Fscanf(in, "%d\n", &t)
	for t > 0 {
		solve()
		t--
	}
}
func solve() {
	var n, m int
	fmt.Fscanf(in, "%d %d\n", &n, &m)
	a := make([][]int, n)
	// 构造矩阵
	for i := range a {
		a[i] = make([]int, m)
	}
	zero := 0
	for i := 0; i < n; i++ {
		var s string
		fmt.Fscanln(in, &s)
		for j := range s {
			a[i][j] = int(s[j] - '0')
			if a[i][j] == 0 {
				zero++
			}
		}
	}
	one := n*m - zero
	if one == 0 {
		fmt.Println(0)
	} else if zero == 0 {
		fmt.Println(1 + one - 3)
	} else {
		flag := false

	out:
		for i := 0; i < n-1; i++ {
			for j := 0; j < m-1; j++ {
				if a[i][j] == 0 && (a[i+1][j] == 0 || a[i][j+1] == 0) {
					flag = true
					break out
				}
			}
		}
		if flag {
			fmt.Println(one)
		} else {
			fmt.Println(1 + one - 2)
		}
	}
}

package main

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

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

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

		aa := make([]string, m)
		var c int
		for i := 0; i < m; i++ {
			fmt.Fscan(in, &aa[i])
			for _, ch := range aa[i] {
				if ch == '1' {
					c++
				}
			}
		}

		// fmt.Println(aa)
		val := func(ch byte) int {
			return int(ch - '0')
		}
		d := 5
		d = min(d, val(aa[0][0])+val(aa[0][1])+val(aa[1][0])+val(aa[1][1]))
		d = min(d, val(aa[m-2][0])+val(aa[m-2][1])+val(aa[m-1][0])+val(aa[m-1][1]))
		d = min(d, val(aa[0][n-2])+val(aa[0][n-1])+val(aa[1][n-2])+val(aa[1][n-1]))
		d = min(d, val(aa[m-2][n-2])+val(aa[m-2][n-1])+val(aa[m-1][n-2])+val(aa[m-1][n-1]))

		c -= d
		c += min(2, d)
		fmt.Fprintln(out, c)
	}
}

package main

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

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

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

// func min3(a, b, c int) int {
// 	return min(min(a, b), c)
// }

// func maxSquare(a int) int {
// 	tmp := int(math.Sqrt(float64(a)))
// 	if tmp*tmp == a {
// 		return tmp * tmp
// 	} else {
// 		return (tmp + 1) * (tmp + 1)
// 	}
// }

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var niter int
	fmt.Fscan(in, &niter)
	for iiter := 0; iiter < niter; iiter++ {
		var n, m int
		fmt.Fscan(in, &n, &m)
		table := make([][]rune, n)
		for i := 0; i < n; i++ {
			table[i] = make([]rune, m)
		}
		for i := 0; i < n*m; i++ {
			//fmt.Fscan(in, &table[i/m][i%m])
			b, _, _ := in.ReadRune()
			if b == '\n' {
				b, _, _ = in.ReadRune()
			}
			table[i/m][i%m] = b
		}
		ones := 0
		zeroes := 0
		for i := 0; i < n*m; i++ {
			if table[i/m][i%m] == '1' {
				ones++
			} else {
				zeroes++
			}
		}
		mode := 0
		if zeroes > 0 {
			mode = 1
		}
		// check for 2 horizontal, vertical, diagonal (x2)
		for i := 0; i < n-1; i++ {
			for j := 0; j < m; j++ {
				if table[i][j] == table[i+1][j] && table[i][j] == '0' {
					mode = 2
				}
			}
		}
		for i := 0; i < n; i++ {
			for j := 0; j < m-1; j++ {
				if table[i][j] == table[i][j+1] && table[i][j] == '0' {
					mode = 2
				}
			}
		}
		// diag x2
		for i := 0; i < n-1; i++ {
			for j := 0; j < m-1; j++ {
				if table[i][j] == table[i+1][j+1] && table[i][j] == '0' {
					mode = 2
				}
			}
		}
		for i := 1; i < n; i++ {
			for j := 0; j < m-1; j++ {
				if table[i][j] == table[i-1][j+1] && table[i][j] == '0' {
					mode = 2
				}
			}
		}
		if mode == 0 {
			fmt.Fprintln(out, ones-2)
		}
		if mode == 1 {
			fmt.Fprintln(out, ones-1)
		}
		if mode == 2 {
			fmt.Fprintln(out, ones)
		}
	}
}

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	cases := readInt(input)
	for asdf := 0; asdf < cases; asdf++ {
		tmp := readArrInt(input)
		n := tmp[0]
		m := tmp[1]
		lines := [][]rune{}
		for i := 0; i < n; i++ {
			line := readLine(input)
			lines = append(lines, []rune(line))
		}
		fmt.Println(solve(n, m, lines))
	}
}

func solve(n, m int, arr [][]rune) int {
	ones := 0
	zeros := 0
	coupled_zeros := false
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if arr[i][j] == '1' {
				ones++
			} else {
				if coupled_zeros == false {
					sub := 0
					for k := max(0, i-1); k < min(i+1, n); k++ {
						for l := max(0, j-1); l < min(j+1, m); l++ {
							if arr[k][l] == '0' {
								sub++
							}
						}
					}
					if sub > 1 {
						coupled_zeros = true
					}
				}
				zeros++
			}
		}
	}
	if zeros == 0 {
		return ones - 2
	} else if coupled_zeros {
		return ones
	} else {
		return ones - 1
	}
}

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

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

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

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

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

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

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

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

package main

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

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

func slove() {
	var n, m int
	fmt.Fscan(rr, &n, &m)
	g := make([]string, n)
	sum := 0
	for i := range g {
		fmt.Fscan(rr, &g[i])
		for j := range g[i] {
			if g[i][j] == '1' {
				sum++
			}
		}
	}
	flag := false
	for i := range g {
		for j:=0;j<m-1;j++ {
			if g[i][j] == g[i][j+1] && g[i][j] == '0' {
				flag = true
			}
		}
	}
	for j := range g[0] {
		for i:=0;i<n-1;i++ {
			if g[i][j] == g[i+1][j] && g[i][j] == '0' {
				flag = true
			}
		}
	}
	has0 := sum != n*m
	if flag {
		fmt.Fprintln(ww, sum)
	} else if has0 {
		fmt.Fprintln(ww, sum-1)
	} else {
		fmt.Fprintln(ww, sum-2)
	}
}

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


package main

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

type BufferedWriter interface {
	Printf(format string, a ...interface{})
	Flush()
}

type writerImpl struct {
	*bufio.Writer
}

func NewBufferedWriter(writer io.Writer) BufferedWriter {
	return &writerImpl{Writer: bufio.NewWriter(writer)}
}

func (impl *writerImpl) Printf(f string, a ...interface{}) {
	fmt.Fprintf(impl.Writer, f, a...)
}

func (impl *writerImpl) Flush() {
	impl.Writer.Flush()
}

type WordScanner interface {
	NextInt() int
	NextInt64() int64
	NextString() string
}

type wordScannerImpl struct {
	*bufio.Scanner
}

func NewWordScanner(reader io.Reader) WordScanner {
	s := bufio.NewScanner(reader)
	s.Split(bufio.ScanWords)
	// adjust the following size as needed
	// 2 << 17 is good enough to scan strings of len 2*10^5
	// it could go as high as 2 << 26 before cf judge breaks
	size := 2 << 20
	buf := make([]byte, size)
	s.Buffer(buf, size)
	return &wordScannerImpl{Scanner: s}
}

func (impl *wordScannerImpl) NextInt() int {
	impl.Scan()
	bb := impl.Bytes()
	i := 0
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int(b - '0')
	}
	return i
}

func (impl *wordScannerImpl) NextInt64() int64 {
	impl.Scan()
	bb := impl.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func (impl *wordScannerImpl) NextString() string {
	impl.Scan()
	return impl.Text()
}

var reader = NewWordScanner(os.Stdin)
var writer = NewBufferedWriter(os.Stdout)

func main() {
	defer writer.Flush()
	_, k := reader.NextInt64(), reader.NextInt64()
	s := reader.NextString()
	res := []rune{}
	for _, r := range s {
		if k == 0 {
			res = append(res, r)
			continue
		}
		if int64('z' - r) >= k {
			res = append(res, r + rune(k))
			k = 0
			continue
		}
		if int64(r - 'a') >= k {
			res = append(res, r - rune(k))
			k = 0
			continue
		}
		if r - 'a' > 'z' - r {
			res = append(res, 'a')
			k -= int64(r - 'a')
		} else {
			res = append(res, 'z')
			k -= int64('z' - r)
		}
	}
	if k > 0 {
		writer.Printf("-1\n")
	} else {
		writer.Printf("%s\n", string(res))
	}
}

package main

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

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

//Wrong
func main() {
	defer writer.Flush()

	s, _ := reader.ReadString('\n')
	scanner = bufio.NewScanner(strings.NewReader(s))
	scanner.Split(bufio.ScanWords)

	n, k := nextInt(), nextInt()

	s, _ = reader.ReadString('\n')
	s = strings.TrimSpace(s)
	t := make([]uint8, n)

	for i := range t {
		if k == 0 {
			t[i] = s[i]
			continue
		}

		l, r := int(s[i]-'a'), int('z'-s[i])
		// println(l, r, k)
		if k >= l && k >= r {
			if l > r {
				t[i] = 'a'
				k -= l
			} else {
				t[i] = 'z'
				k -= r
			}
		} else {
			if k < r {
				t[i] = s[i] + uint8(k)
			} else if k < l {
				t[i] = s[i] - uint8(k)
			}
			k = 0
		}
	}

	if k > 0 {
		println(-1)
	} else {
		for i := range t {
			printf("%c", t[i])
		}
		println()
	}
}

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

func nextInt64() int64 {
	n, _ := strconv.ParseInt(next(), 0, 64)
	return n
}

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{})               { fmt.Fprintln(writer, a...) }
func print(a ...interface{})                 { fmt.Fprint(writer, a...) }
func printf(format string, a ...interface{}) { fmt.Fprintf(writer, format, a...) }

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	line1, _ := in.ReadString('\n')
	var n, k int
	fmt.Sscanf(line1, "%d%d", &n, &k)
	s, _ := in.ReadString('\n')
	for len(s) > 0 && (s[len(s)-1] == '\n' || s[len(s)-1] == '\r') {
		s = s[:len(s)-1]
	}
	s2 := make([]byte, len(s))
	for i := 0; i < len(s); i++ {
		l := s[i] - 'a'
		r := 'z' - s[i]
		d := min(k, max(int(l), int(r)))
		k -= d
		if l > r {
			d = -d
		}
		s2[i] = s[i] + byte(d)
	}
	if k == 0 {
		out.Write(s2)
	} else {
		out.WriteString("-1")
	}
	out.WriteString("\n")
}

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

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

package main

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

type BufferedWriter interface {
	Printf(format string, a ...interface{})
	Flush()
}

type writerImpl struct {
	*bufio.Writer
}

func NewBufferedWriter(writer io.Writer) BufferedWriter {
	return &writerImpl{Writer: bufio.NewWriter(writer)}
}

func (impl *writerImpl) Printf(f string, a ...interface{}) {
	fmt.Fprintf(impl.Writer, f, a...)
}

func (impl *writerImpl) Flush() {
	impl.Writer.Flush()
}

type WordScanner interface {
	NextInt() int
	NextInt64() int64
	NextString() string
}

type wordScannerImpl struct {
	*bufio.Scanner
}

func NewWordScanner(reader io.Reader) WordScanner {
	s := bufio.NewScanner(reader)
	s.Split(bufio.ScanWords)
	// adjust the following size as needed
	// 2 << 17 is good enough to scan strings of len 2*10^5
	// it could go as high as 2 << 26 before cf judge breaks
	size := 2 << 20
	buf := make([]byte, size)
	s.Buffer(buf, size)
	return &wordScannerImpl{Scanner: s}
}

func (impl *wordScannerImpl) NextInt() int {
	impl.Scan()
	bb := impl.Bytes()
	i := 0
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int(b - '0')
	}
	return i
}

func (impl *wordScannerImpl) NextInt64() int64 {
	impl.Scan()
	bb := impl.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func (impl *wordScannerImpl) NextString() string {
	impl.Scan()
	return impl.Text()
}

var reader = NewWordScanner(os.Stdin)
var writer = NewBufferedWriter(os.Stdout)

func main() {
	defer writer.Flush()
	_, k := reader.NextInt64(), reader.NextInt64()
	s := reader.NextString()
	res := []rune{}
	for _, r := range s {
		if k == 0 {
			res = append(res, r)
			continue
		}
		if int64('z' - r) > k {
			res = append(res, r + rune(k))
			k = 0
			continue
		}
		if int64(r - 'a') > k {
			res = append(res, r - rune(k))
			k = 0
			continue
		}
		if r - 'a' > 'z' - r {
			res = append(res, 'a')
			k -= int64(r - 'a')
		} else {
			res = append(res, 'z')
			k -= int64('z' - r)
		}
	}
	if k > 0 {
		writer.Printf("-1\n")
	} else {
		writer.Printf("%s\n", string(res))
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	line1, _ := in.ReadString('\n')
	var n, k int
	fmt.Sscanf(line1, "%d%d", &n, &k)
	s, _ := in.ReadString('\n')
	s = s[:len(s)-1]
	s2 := make([]byte, len(s))
	for i := 0; i < len(s); i++ {
		l := s[i] - 'a'
		r := 'z' - s[i]
		d := min(k, max(int(l), int(r)))
		k -= d
		if l > r {
			d = -d
		}
		s2[i] = s[i] + byte(d)
	}
	if k == 0 {
		out.Write(s2)
	} else {
		out.WriteString("-1")
	}
	out.WriteString("\n")
}

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

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


package main
import(
    "fmt"
    "bufio"
    "os"
    "strconv"
    // "sort"
    // "strings"
    // "reflect"
)

type Reader struct{
    scanner *bufio.Scanner
}

func (read *Reader) init(){
    if read.scanner == nil {
        read.scanner = bufio.NewScanner(os.Stdin)
    }
}

func (read *Reader) setWord(){
    read.scanner.Split(bufio.ScanWords)
}

func (read *Reader) setByte(){
    read.scanner.Split(bufio.ScanBytes)
}

func (read *Reader) setLine(){
    read.scanner.Split(bufio.ScanLines)
}

func (read *Reader) read() string{
    read.scanner.Scan()
    return read.scanner.Text()
}

func (read *Reader) readInt() int{
    res,_ := strconv.Atoi(read.read())
    return res
}

func (read *Reader) readByte(length int) string{
    bt := make([]byte, length+2)
    for i := 0; i < length; i++ {
        bt[i] = read.read()[0]
    }

    return string(bt)
}

var read Reader

var writer *bufio.Writer

var cost [9]int
var mem [1000006]int

func f(v int) int{
    if(v <= 0){
        return 0
    }
    if mem[v] > -1{
        return mem[v]
    }
    mx := 0
    for i := 0; i < 9; i++{
        if cost[i] <= v {
            cur := 1 + f(v-cost[i])
            if mx < cur{
                mx = cur
            }
        }
    }
    mem[v] = mx
    return mx
}

func prt(v, len int){
    if len == 0{
        writer.Flush()
        return
    }

    for i := 8; i >= 0; i--{
        if cost[i] <= v{
            if len == 1+f(v-cost[i]){
                fmt.Fprint(writer, i+1)
                prt(v-cost[i], len-1)
                return
            }
        }
    }
}

func main(){

    writer = bufio.NewWriter(os.Stdout)
    read.init()
    read.setWord()

    for i := 0; i <= 1000002; i++{
        mem[i] = -1
    }

    v := read.readInt()
    mn := 1000000
    for i := 0; i < 9; i++ {
        cost[i] = read.readInt()
        if cost[i] < mn {
            mn = cost[i]
        }
    }
    mxLen := v / mn
    // fmt.Println("bal", mxLen, v, f(v))
    if mxLen == 0{
        fmt.Print(-1)
    } else{
        prt(v, mxLen)
    }

}

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	v, min := 0, int(1e9)
	a := [10]int{}
	Scan(&v)
	for i := 1; i <= 9; i++ {
		Scan(&a[i])
		if a[i] < min {
			min = a[i]
		}
	}

	l := v / min
	if l == 0 {
		Fprint(out, -1)
		return
	}
	for ; l >= 0; l-- {
		for i := 9; i > 0; i-- {
			if c := a[i]; c <= v && (v-c)/min == l {
				v -= c
				Fprint(out, i)
				break
			}
		}
	}
}

// 349B
package main

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

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func main() {
	var l, n int
	var a [10]int
	s := 10000000
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	fmt.Scan(&n)
	for i := 1; i < 10; i++ {
		fmt.Fscan(in, &a[i])
		s = min(s, a[i])
	}
	l = n / s
	if l == 0 {
		fmt.Println(-1)
	}
	for ; l >= 0; l-- {
		for i := 9; i > 0; i-- {
			if (n-a[i])/s == l && n >= a[i] {
				fmt.Fprint(out, i)
				n -= a[i]
				break
			}
		}
	}
	out.Flush()
}

package main

import "os"
import "bufio"
import "strconv"

func main(){
	var v int
	var a [10]int
	r := bufio.NewScanner(os.Stdin)
	r.Split(bufio.ScanWords)
	w := bufio.NewWriter(os.Stdout)
	r.Scan()
	v, _ = strconv.Atoi(r.Text())
	min := 100000
	m := 0
	for i:=1; i<=9; i++ {
		r.Scan()
		a[i], _ = strconv.Atoi(r.Text())
		if a[i] <= min {
			min = a[i]
			m = i
		}
	}
	size := v/min
	v -= min * size
	if size == 0 {
		w.WriteString("-1\n")
		w.Flush()
		return
	}
	for j:=0; j<size; j++ {
		for k:=9; k>=m; k-- {
			if v >= a[k]-a[m] {
				w.WriteString(strconv.Itoa(k))
				v -= a[k]-a[m]
				break
			}
		}
	}
	w.WriteString("\n")
	w.Flush()
}

// 349B
package main

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

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func main() {
	var l, n int
	var a [10]int
	s := 10000000
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	fmt.Scan(&n)
	for i := 1; i < 10; i++ {
		fmt.Fscan(in, &a[i])
		s = min(s, a[i])
	}
	l = n / s
	if l == 0 {
		fmt.Println(-1)
	}
	for ; l >= 0; l-- {
		for i := 9; i > 0; i-- {
			if (n-a[i])/s == l && n >= a[i] {
				fmt.Fprint(out, i)
				n -= a[i]
				break
			}
		}
	}
}


package main

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

func main() {
	reader := bufio.NewReaderSize(os.Stdin, 300000)
	n, s := 0, ""
	fmt.Fscanf(reader, "%d\n%s\n", &n, &s)
	for i := 0; i < (len(s) - 1); i++ {
		if s[i] > s[i+1] {
			fmt.Println(s[:i] + s[i+1:])
			os.Exit(0)
		}
	}
	fmt.Println(s[:len(s)-1])
}

package main

import (
	"fmt"
	"os"
)

func main() {
	var n int
	fmt.Fscan(os.Stdin, &n)
	var s string
	fmt.Fscan(os.Stdin, &s)
	for i := 1; i < len(s); i++ {
		if s[i] < s[i-1] {
			fmt.Printf("%s%s\n", s[:i-1], s[i:])
			return
		}
	}
	fmt.Println(s[:len(s)-1])
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n := 0
	fmt.Fscanf(in, "%d\n", &n)
	s := ""
	fmt.Fscanf(in, "%s\n", &s)
	if s[0] > s[1] {
		fmt.Fprintf(out, "%s\n", s[1:])
		return
	}
	for i := 1; i < n; i++ {
		if s[i] < s[i-1] {
			fmt.Fprintf(out, "%s\n", s[:i-1]+s[i:])
			return
		}
	}
	fmt.Fprintf(out, "%s\n", s[:n-1])
	return
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	var s string
	fmt.Scan(&s)
	idx := 0
	for i, c := range s {
		if i == len(s)-1 || (i <= len(s)-2 && rune(s[i+1]) < c) {
			idx = i
			break
		}
	}
	fmt.Println(s[:idx] + s[idx+1:])
}


package main

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

func main() {
    var s [50]int64
    var n,i,sum,u,o int64
    sum=0
    fmt.Scan(&n)
    input:= bufio.NewScanner(os.Stdin)
    input.Split(bufio.ScanWords)

    for i=0;i<n;i++ {
    	input.Scan()
	bb,_:=strconv.ParseInt(input.Text(),0,64)
	u=bb
	o=0
	for u!=1 {
		if u%2!=0 {
			o++
		}
		u/=2
	}
	s[o]++
    }
    for i=0;i<50;i++ {
    	sum+=s[i]*(s[i]-1)/2
    }
    fmt.Println(sum)
}

package main

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

func scanInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	x, _ := strconv.Atoi(scanner.Text())
	return x
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	n := scanInt(scanner)
	counts := make(map[int]int64)
	for i := 0; i < n; i++ {
		x := scanInt(scanner)
		f := 1
		for x != 1 {
			if x%2 == 1 {
				x--
				f++
			}
			x /= 2
		}
		counts[f]++
	}
	total := int64(0)
	for _, count := range counts {
		total += (count*count - count) / 2
	}
	writer.WriteString(fmt.Sprintf("%d\n", total))
}

package main

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

func scanInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	x, _ := strconv.Atoi(scanner.Text())
	return x
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	n := scanInt(scanner)
	counts := make(map[int]int64)
	for i := 0; i < n; i++ {
		x := scanInt(scanner)
		f := 1
		for x != 1 {
			f += (x&1)
			x /= 2
		}
		counts[f]++
	}
	total := int64(0)
	for _, count := range counts {
		total += (count*count - count) / 2
	}
	writer.WriteString(fmt.Sprintf("%d\n", total))
}


package main

import "fmt"

func main() {
    var n int
    fmt.Scan(&n)
    pos, plus := 0, 1
    for i := 1; i < n; i++ {
        pos = (pos+plus)%n
        fmt.Print(pos+1)
        if i != (n-1) { fmt.Print(" ") } else { fmt.Println() }
        plus++
    }
}

package main
import (
	"fmt"
)

func main() {
	var input, x int
	x=1
	fmt.Scan(&input)
	for i:=1; i<input; i++ {
		x+=i
		if x>input {
			if x%input==0 {
				fmt.Println(input)
			} else {
				fmt.Println(x%input)
			}
		} else {
			fmt.Println(x)
		}
	}
}

// 46A
package main

import (
	"fmt"
)

func main() {
	var n int
	now := 0
	fmt.Scan(&n)
	for i := 1; i < n; i++ {
		now += i
		now %= n
		fmt.Print(now+1, " ")
	}
}

// 46A-mic_rizal
package main

import (
    "fmt"
)

func main() {
    var n int
    fmt.Scan(&n)
    cou := 1
    for i := 1; i < n; i++ {
        cou = cou + i
        if cou > n {
            cou = cou - n
        }
        fmt.Print(cou)
        fmt.Print(" ")
    }
    fmt.Println()
}

package main
import (
	"fmt"
)

func main() {
	var input, x int
	x=1
	fmt.Scan(&input)
	for i:=1; i<input; i++ {
		x+=i
		if x>input {
			fmt.Println(x%input)
		} else {
			fmt.Println(x)
		}
	}
}


package main

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

// import bufio, os, strconv
var sc *bufio.Scanner

func scInit() {
	sc = bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
}

func scanOne() {
	if !sc.Scan() {
		panic("could not scan the reader")
	}
}

func nextInt() int {
	scanOne()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func nextLong() int64 {
	scanOne()
	i, e := strconv.ParseInt(sc.Text(), 10, 64)
	if e != nil {
		panic(e)
	}
	return i
}

func next() string {
	scanOne()
	return sc.Text()
}

func main() {
	scInit()
	n := nextInt()
	mf, mb := make(map[int]int), make(map[int]int)
	for i := 0; i < n; i++ {
		x, y := nextInt(), nextInt()
		mf[x] = y
		mb[y] = x
	}
	z := make([]int, n)
	for p, i := 0, 1; i < n; i += 2 {
		z[i] = mf[p]
		p = z[i]
	}
	for p, i := 0, n-2; i >= 0; i -= 2 {
		z[i] = mb[p]
		p = z[i]
	}
	if n%2 == 1 {
		v := make(map[int]bool)
		for k := range mb {
			v[k] = true
		}
		var head int
		for k := range mf {
			if !v[k] {
				head = k
				break
			}
		}
		for p, i := head, 0; i < n; i += 2 {
			z[i] = p
			p = mf[p]
		}
	}
	for i := 0; i < n; i++ {
		fmt.Printf("%d", z[i])
		if i < n-1 {
			fmt.Print(" ")
		}
	}
	fmt.Println()
}

package main

import (
    "fmt"
    "os"
    "bufio"
)
type node struct {
    s, x,c int

}
const MAXN = int(1e6 + 10)

var  data [MAXN]node
var ans ,mark  [MAXN]int

func main(){

   reader := bufio.NewReader(os.Stdin)
   var n,x,y int
   fmt.Fscan(reader,&n)
   for i := 1; i<= n; i ++ {
      var a,b int
      fmt.Fscan(reader,&a,&b)
      data[a].x = b
      data[a].c = 1
      data[b].s = a
      data[b].c = 2
      mark[a] ++
      mark[b] ++
      if 0 ==  b {
        x = a
      }
   }
   for i := 1; i< MAXN;i ++{
        if 1 == mark[i]{
            if 2 == data[i].c {
                y = i
            }

        }

   }
   idex1 := n
   idex2 := n - 1
   for i := 1;i <= n; i ++ {
       if idex1 >= 1 {
           ans[idex1] = y

       }
       if idex2 >= 1 {
           ans[idex2] = x
       }
       idex1 -= 2
       idex2 -= 2
       y = data[y].s
       x = data[x].s

   }
   for i := 1;i <=  n; i++{
       fmt.Printf("%d ",ans[i])
   }
   fmt.Println()

}

/*

3
0 2
1 3
2 0
*/

package main

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

var scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
var _ = fmt.Println

func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	forwardPos := make(map[int]int)
	backwardPos := make(map[int]int)

	N := ReadInt32()
	position := make([]int, N)
	for i := 0; i < N; i += 1 {
		x, y := ReadInt32(), ReadInt32()
		forwardPos[x] = y
		backwardPos[y] = x
	}

	startId := 0
	startPos := 1
	for {
		if backId, ok := forwardPos[startId]; ok && startPos < N {
			position[startPos] = backId
			startId = backId
			startPos += 2
		} else {
			break
		}
	}
	for fId := range forwardPos {
		if _, ok := backwardPos[fId]; !ok {
			startId = fId
			break
		}
	}
	position[0] = startId
	startPos = 2
	for {
		if backId, ok := forwardPos[startId]; ok && startPos < N {
			position[startPos] = backId
			startId = backId
			startPos += 2
		} else {
			break
		}
	}
	PrintInts(position...)
}

func ReadInt32() int {
	scanner.Scan()
	ans, _ := strconv.Atoi(scanner.Text())
	return ans
}

func PrintInts(ints ...int) {
	for _, value := range ints {
		writer.WriteString(strconv.Itoa(value))
		writer.WriteByte(' ')
	}
}

package main

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

func main() {
	m1 := make(map[int]int)
	m2 := make(map[int]int)
	var n int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n,_ = strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		scanner.Scan()
		f,_ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		s,_ := strconv.Atoi(scanner.Text())
		m1[f] = s
		m2[s] = f
	}
	arr := make([]int,n)
	h := 0
	for i := 0; i*2+1 < n; i++ {
		arr[i*2+1] = m1[h]
		h = arr[i*2+1]
	}

	//find the head
	deg := make(map[int]int)
	for k,v := range m1{
		deg[k]++
		deg[v]--
	}

	for k,v := range deg{
		if v == 1{
			h = k
			break
		}
	}
	arr[0] = h

	for i:=1; i*2 < n; i++  {
		arr[2*i] = m1[h]
		h = arr[2*i]
	}

	for i := 0; i < n; i++ {
		fmt.Printf("%d ",arr[i])
	}
}

package main

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

const (
	SIZE = 1000005
)

var (
	n      int
	a      []int
	b      []int
	ids    map[int]struct{}
	m      map[int]int
	first  int
	second int
	last   int
	result int
)

func in() {
	read(&n)
	a = make([]int, n)
	b = make([]int, n)
	m = make(map[int]int)
	ids = make(map[int]struct{})
	for i := 0; i < n; i++ {
		read(&a[i], &b[i])
		m[a[i]] = b[i]
		if a[i] == 0 {
			second = b[i]
		}
		if a[i] != 0 {
			ids[a[i]] = struct{}{}
		}
		if b[i] != 0 {
			ids[b[i]] = struct{}{}
		}
	}
}

func out() {
	defer output.Flush()
}

func run() {
	var count [SIZE]int
	for _, v := range a {
		count[v] = 1
	}
	for _, v := range b {
		count[v] += 2
	}
	for k := range ids {
		if count[k] == 1 {
			first = k
		} else if count[k] == 2 {
			last = k
		}
	}

	for first != last {
		printf("%d ", first)
		first, second = second, m[first]
	}
	println(last)
}

/** main **/
func main() {
	in()
	run()
	out()
}

// Input & Output
var (
	input  = bufio.NewReader(os.Stdin)
	output = bufio.NewWriter(os.Stdout)
)

func read(a ...interface{}) {
	_, err := fmt.Fscan(input, a...)
	if err != nil {
		println(err)
	}
}

func println(a ...interface{}) {
	fmt.Fprintln(output, a...)
}

func printf(format string, a ...interface{}) {
	fmt.Fprintf(output, format, a...)
}

package main

import (
    "fmt"
    "os"
    "bufio"
)
type node struct {
    s, x int

}
const MAXN = int(1e6 + 10)

var  data [MAXN]node
var ans [MAXN]int

func main(){

   reader := bufio.NewReader(os.Stdin)
   var n, last ,first int
   fmt.Fscan(reader,&n)
   for i := 1; i<= n; i ++ {
      var a,b int
      fmt.Fscan(reader,&a,&b)
      data[a].x = b
      data[b].s = a
      if 0 ==  b {
        first = a
      }else if 0 == a {

          last = b
      }
   }
   now := 2

   for i := 1;i <= n; i ++ {
        ans[now] = last
        last = data[last].x
        now += 2
        if now > n {
            break
        }

   }
   now = n - 1
   for i := 1;i <=   n; i ++ {

      ans[now] = first
     first = data[first].s
     now -= 2
    if now <= 0{
        break
      }
   }
   for i := 1;i <=  n; i++{
       fmt.Printf("%d ",ans[i])
   }
   fmt.Println()

}

package main

import (
    "fmt"
    "os"
    "bufio"
)
type node struct {
    s, x int

}
const MAXN = int(1e6 + 10)

var  data [MAXN]node
var ans [MAXN]int

func main(){

   reader := bufio.NewReader(os.Stdin)
   var n, last ,first int
   fmt.Fscan(reader,&n)
   for i := 1; i<= n; i ++ {
      var a,b int
      fmt.Fscan(reader,&a,&b)
      data[a].x = b
      data[b].s = a
      if 0 ==  b {
        first = a
      }else if 0 == a {

          last = b
      }
      if 3 == n{
        if 0 != a && 0 != b{
            ans[1] = a
            ans[3] = b
        }else  {
            ans[2] = b
        }

      }
   }
   now := 2

   for i := 1;i <= n; i ++ {
        ans[now] = last
        last = data[last].x
        now += 2
        if now > n {
            break
        }

   }
   now = n - 1
   for i := 1;i <=   n; i ++ {

      ans[now] = first
     first = data[first].s
     now -= 2
    if now <= 0{
        break
      }
   }

   for i := 1;i <=  n; i++{
       fmt.Printf("%d ",ans[i])
   }
   fmt.Println()

}

/*

3
0 2
1 3
2 0
*/

package main

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

var scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
var _ = fmt.Println

func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	forwardPos := make(map[int]int)
	backwardPos := make(map[int]int)

	N := ReadInt32()
	position := make([]int, N)
	for i := 0; i < N; i += 1 {
		x, y := ReadInt32(), ReadInt32()
		forwardPos[x] = y
		backwardPos[y] = x
	}

	startId := 0
	startPos := -1
	for {
		if backId, ok := forwardPos[startId]; ok && startPos < N {
			position[startPos+2] = backId
			startId = backId
			startPos += 2
		} else {
			break
		}
	}
	startId = 0
	startPos = N
	for {
		if frontId, ok := backwardPos[startId]; ok && startPos >= 0 {
			position[startPos-2] = frontId
			startId = frontId
			startPos -= 2
		} else {
			break
		}
	}
	fmt.Println(position)
}

func ReadInt32() int {
	scanner.Scan()
	ans, _ := strconv.Atoi(scanner.Text())
	return ans
}

func PrintInts(ints ...int) {
	for _, value := range ints {
		writer.WriteString(strconv.Itoa(value))
		writer.WriteByte(' ')
	}
}

package main

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

// import bufio, os, strconv
var sc *bufio.Scanner

func scInit() {
	sc = bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
}

func scanOne() {
	if !sc.Scan() {
		panic("could not scan the reader")
	}
}

func nextInt() int {
	scanOne()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func nextLong() int64 {
	scanOne()
	i, e := strconv.ParseInt(sc.Text(), 10, 64)
	if e != nil {
		panic(e)
	}
	return i
}

func next() string {
	scanOne()
	return sc.Text()
}

func main() {
	scInit()
	n := nextInt()
	mf, mb := make(map[int]int), make(map[int]int)
	for i := 0; i < n; i++ {
		x, y := nextInt(), nextInt()
		mf[x] = y
		mb[y] = x
	}
	z := make([]int, n)
	for p, i := 0, 1; i < n; i += 2 {
		z[i] = mf[p]
		p = z[i]
	}
	for p, i := 0, n-2; i >= 0; i -= 2 {
		z[i] = mb[p]
		p = z[i]
	}
	for i := 0; i < n; i++ {
		fmt.Printf("%d", z[i])
		if i < n-1 {
			fmt.Print(" ")
		}
	}
	fmt.Println()
}