package main

import (
	"fmt"
)

func main() {
	var input string

	fmt.Scan(&input)
	s := input + "b"

	for i := 0; i < (len(s) - 1); i++ {
		if i > 0 {
			fmt.Printf(" ")
		}

		if s[i] != s[i+1] {
			fmt.Printf("1")
		} else {
			fmt.Printf("0")
		}
	}
	fmt.Printf("\n")
}

package main

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

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

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

func Reverse(s string) string {
	r := []rune(s)
	for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
		r[i], r[j] = r[j], r[i]
	}
	return string(r)
}

var ans []int

func fundi(s string) string {
	if len(s) == 0 {
		return ""
	}

	if s[len(s)-1] == 'b' {
		ans = append(ans, 1)
		return "b" + Reverse(funli(s[:len(s)-1]))
	} else {
		ans = append(ans, 0)
		return fundi(s[:len(s)-1]) + "a"
	}
}

func funli(s string) string {
	if len(s) == 0 {
		return ""
	}

	if s[len(s)-1] == 'a' {
		ans = append(ans, 1)
		return "a" + Reverse(fundi(s[:len(s)-1]))
	} else {
		ans = append(ans, 0)
		return funli(s[:len(s)-1]) + "b"
	}
}

func main() {
	defer writer.Flush()

	var s string
	fmt.Scanln(&s)
	n := len(s)
	//fmt.Print(s)
	ans = make([]int, 0)
	funli(s)

	for i := 0; i < n; i++ {
		fmt.Print(strconv.Itoa(ans[n-i-1]) + " ")
	}

}

package main

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

func Init() {
}

func Solve(io *FastIO) {
	S := io.NextLine()

	N := len(S)

	ans := make([]int, N)
	abMode := true
	for i := 0; i+1 < N; i++ {
		if abMode {
			if S[i+1] == 'a' {
				ans[i] = 1
				abMode = false
			}
		} else {
			if S[i+1] == 'b' {
				ans[i] = 1
				abMode = true
			}
		}
	}
	if !abMode {
		ans[N-1] = 1
	}

	// fwd := make([]string, N)
	// fwdFrom := make([]int, N)
	// fwdRev := make([]int, N)
	// bwd := make([]string, N)
	// bwdFrom := make([]int, N)
	// bwdRev := make([]int, N)
	// for i := 1; i < N; i++ {
	// 	fwd[i] = "omg"
	// 	bwd[i] = "wtf"
	// }
	// fwd[0] = S[:1]
	// bwd[0] = S[:1]

	// // for i := 0; i + 1 < N; i++ {

	// // }

	// for i := 1; i < N; i++ {
	// 	// rf := reverse(fwd[i-1])
	// 	// rb := reverse(bwd[i-1])
	// 	c := S[i : i+1]
	// 	{
	// 		fwdF := fwd[i-1] + c
	// 		if strings.Compare(fwdF, fwd[i]) < 0 {
	// 			fwd[i] = fwdF
	// 		}
	// 		if strings.Compare(reverse(fwdF), reverse(bwd[i])) < 0 {
	// 			bwd[i] = fwdF
	// 		}
	// 	}
	// 	{
	// 		fwdR := reverse(fwd[i-1] + c)
	// 		if strings.Compare(fwdR, fwd[i]) < 0 {
	// 			fwd[i] = fwdR
	// 			fwdRev[i] = 1
	// 		}
	// 		if strings.Compare(reverse(fwdR), reverse(bwd[i])) < 0 {
	// 			bwd[i] = fwdR
	// 			bwdRev[i] = 1
	// 		}
	// 	}
	// 	{
	// 		bwdF := bwd[i-1] + c
	// 		if strings.Compare(bwdF, fwd[i]) < 0 {
	// 			fwd[i] = bwdF
	// 			fwdFrom[i] = 1
	// 		}
	// 		if strings.Compare(reverse(bwdF), reverse(bwd[i])) < 0 {
	// 			bwd[i] = bwdF
	// 			bwdFrom[i] = 1
	// 		}
	// 	}
	// 	{
	// 		bwdR := reverse(bwd[i-1] + c)
	// 		if strings.Compare(bwdR, fwd[i]) < 0 {
	// 			fwd[i] = bwdR
	// 			fwdFrom[i] = 1
	// 			fwdRev[i] = 1
	// 		}
	// 		if strings.Compare(reverse(bwdR), reverse(bwd[i])) < 0 {
	// 			bwd[i] = bwdR
	// 			bwdFrom[i] = 1
	// 			bwdRev[i] = 1
	// 		}
	// 	}

	// 	fmt.Printf("%d: fwd[i] = %s, bwd[i] = %s\n", i, fwd[i], bwd[i])

	// 	// for _, next := range []string{
	// 	// 	fwd[i-1] + c,
	// 	// 	bwd[i-1] + c,
	// 	// } {
	// 	// 	if strings.Compare(next, fwd[i]) < 0 {
	// 	// 		fwd[i] = next
	// 	// 	}
	// 	// 	r := reverse(next)
	// 	// 	if strings.Compare(r, reverse(bwd[i])) < 0 {
	// 	// 		bwd[i] = next
	// 	// 	}
	// 	// }
	// 	// for _, next := range []string{
	// 	// 	c + rf,
	// 	// 	c + rb,
	// 	// } {
	// 	// 	if strings.Compare(next, fwd[i]) < 0 {
	// 	// 		fwd[i] = next
	// 	// 		fwdRev = 1
	// 	// 	}
	// 	// 	r := reverse(next)
	// 	// 	if strings.Compare(r, reverse(bwd[i])) < 0 {
	// 	// 		bwd[i] = next
	// 	// 		bwdRev = 1
	// 	// 	}
	// 	// }
	// }

	// ans := make([]int, N)
	// which := 0
	// for i := N - 1; i >= 0; i-- {
	// 	if which == 0 {
	// 		ans[i] = fwdRev[i]
	// 		which = fwdFrom[i]
	// 	} else {
	// 		ans[i] = bwdRev[i]
	// 		which = bwdFrom[i]
	// 	}
	// }

	for _, v := range ans {
		io.Printf("%d ", v)
	}
	io.Println()
}

func reverse(S string) string {
	A := []rune(S)
	for i, j := 0, len(A)-1; i < j; i, j = i+1, j-1 {
		A[i], A[j] = A[j], A[i]
	}
	return string(A)
}

type FastIO struct {
	reader *bufio.Reader
	writer *bufio.Writer
}

func (io *FastIO) NextChar() byte {
	c, _ := io.reader.ReadByte()
	return c
}

func (io *FastIO) NextInt() int {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := 1
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := 0
	for !IsSpaceChar(c) {
		res = (res * 10) + int(c-'0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextLong() int64 {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := int64(1)
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := int64(0)
	for !IsSpaceChar(c) {
		res = (res * 10) + int64(c-'0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextIntArray(size int) []int {
	return io.NextIntArrayOffset(size, 0)
}

func (io *FastIO) NextIntArrayOffset(size, offset int) []int {
	arr := make([]int, size+offset)
	for i := 0; i < size; i++ {
		arr[i+offset] = io.NextInt()
	}
	return arr
}

func (io *FastIO) NextLongArray(size int) []int64 {
	return io.NextLongArrayOffset(size, 0)
}

func (io *FastIO) NextLongArrayOffset(size, offset int) []int64 {
	arr := make([]int64, size+offset)
	for i := 0; i < size; i++ {
		arr[i+offset] = io.NextLong()
	}
	return arr
}

func (io *FastIO) NextString() string {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsSpaceChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) NextLine() string {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsLineBreakChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) Println(args ...interface{}) {
	io.writer.WriteString(fmt.Sprintln(args...))
}

func (io *FastIO) Printf(format string, args ...interface{}) {
	io.writer.WriteString(fmt.Sprintf(format, args...))
}

func (io *FastIO) FlushOutput() {
	io.writer.Flush()
}

func IsSpaceChar(c byte) bool {
	switch c {
	case 0, '\t', '\n', '\r', ' ':
		return true
	default:
		return false
	}
}

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

func main() {
	io := FastIO{reader: bufio.NewReader(os.Stdin), writer: bufio.NewWriter(os.Stdout)}
	Init()
	Solve(&io)
	io.FlushOutput()
}

package main

import (
	"fmt"
	"strings"
)

func main() {
	var s string
	fmt.Scan(&s)
	ans := make([]string, len(s))
	for i := range ans {
		ans[i] = "0"
	}

	i := 0
	for ; i < len(s); i++ {
		if s[i] == 'b' {
			break
		}
	}
	last := byte('b')
	for ; i < len(s); i++ {
		if s[i] == last {
			continue
		}
		ans[i-1] = "1"
		last = s[i]
	}
	if last == 'a' {
		ans[i-1] = "1"
	}
	fmt.Println(strings.Join(ans, " "))
}

package main

import "fmt"

func main() {
	var input string
	fmt.Scan(&input)

	word := []byte(input)
	N := len(word)
	doIt := make([]int, N)
	start, end := 0, N-1
	for scope := N - 1; scope > 0; scope-- {
		l, r := start, end
		isReversed := false
		if l > r {
			isReversed = true
			l, r = r, l
		}
		if word[l] == 'b' && word[r] == 'a' ||
			word[l] == 'b' && word[r] == 'b' && isReversed ||
			word[l] == 'a' && word[r] == 'a' && !isReversed {
			reverse(word, l, r)
			doIt[scope] = 1
			start, end = end, start
		}

		if start < end {
			end--
		} else {
			end++
		}
	}
	for _, x := range doIt {
		fmt.Printf("%d ", x)
	}

}

func reverse(word []byte, l, r int) {
	for ; l < r; l, r = l+1, r-1 {
		word[l], word[r] = word[r], word[l]
	}
}

package main

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

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

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

func Reverse(s string) string {
	r := []rune(s)
	for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
		r[i], r[j] = r[j], r[i]
	}
	return string(r)
}

var ans []int

func fundi(s string) string {
	if len(s) == 0 {
		return ""
	}

	if s[len(s)-1] == 'b' {
		ans = append(ans, 1)
		return "b" + Reverse(funli(s[:len(s)-1]))
	} else {
		ans = append(ans, 0)
		return funli(s[:len(s)-1]) + "a"
	}
}

func funli(s string) string {
	if len(s) == 0 {
		return ""
	}

	if s[len(s)-1] == 'a' {
		ans = append(ans, 1)
		return "a" + Reverse(fundi(s[:len(s)-1]))
	} else {
		ans = append(ans, 0)
		return funli(s[:len(s)-1]) + "b"
	}
}

func main() {
	defer writer.Flush()

	var s string
	fmt.Scanln(&s)
	n := len(s)
	//fmt.Print(s)
	ans = make([]int, 0)
	funli(s)

	for i := 0; i < n; i++ {
		fmt.Print(strconv.Itoa(ans[n-i-1]) + " ")
	}

}

package main

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

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

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

func Reverse(s string) string {
	r := []rune(s)
	for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
		r[i], r[j] = r[j], r[i]
	}
	return string(r)
}

var ans []int

func fun(s string) string {
	if len(s) == 0 {
		return ""
	}

	ss := fun(s[:len(s)-1])
	rs := string(s[len(s)-1]) + Reverse(ss)
	if rs < s {
		ans = append(ans, 1)
		return rs
	} else {
		ans = append(ans, 0)
		return s
	}

}

func main() {
	defer writer.Flush()

	var s string
	fmt.Scanln(&s)
	n := len(s)
	//fmt.Print(s)
	ans = make([]int, 0)
	fun(s)

	for i := 0; i < n; i++ {
		fmt.Print(strconv.Itoa(ans[i]) + " ")
	}

}

package main

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

func Init() {
}

func Solve(io *FastIO) {
	S := io.NextLine()

	N := len(S)
	fwd := make([]string, N)
	fwdFrom := make([]int, N)
	fwdRev := make([]int, N)
	bwd := make([]string, N)
	bwdFrom := make([]int, N)
	bwdRev := make([]int, N)
	for i := 1; i < N; i++ {
		fwd[i] = "omg"
		bwd[i] = "wtf"
	}
	fwd[0] = S[:1]
	bwd[0] = S[:1]
	for i := 1; i < N; i++ {
		// rf := reverse(fwd[i-1])
		// rb := reverse(bwd[i-1])
		c := S[i : i+1]

		fwdF := fwd[i-1] + c
		if strings.Compare(fwdF, fwd[i]) < 0 {
			fwd[i] = fwdF
		}
		if strings.Compare(reverse(fwdF), reverse(bwd[i])) < 0 {
			bwd[i] = fwdF
		}

		fwdR := c + fwd[i-1]
		if strings.Compare(fwdR, fwd[i]) < 0 {
			fwd[i] = fwdR
			fwdRev[i] = 1
		}
		if strings.Compare(reverse(fwdR), reverse(bwd[i])) < 0 {
			bwd[i] = fwdR
			bwdRev[i] = 1
		}

		bwdF := bwd[i-1] + c
		if strings.Compare(bwdF, fwd[i]) < 0 {
			fwd[i] = bwdF
			fwdFrom[i] = 1
		}
		if strings.Compare(reverse(bwdF), reverse(bwd[i])) < 0 {
			bwd[i] = bwdF
			bwdFrom[i] = 1
		}

		bwdR := c + bwd[i-1]
		if strings.Compare(bwdR, fwd[i]) < 0 {
			fwd[i] = bwdR
			fwdFrom[i] = 1
			fwdRev[i] = 1
		}
		if strings.Compare(reverse(bwdR), reverse(bwd[i])) < 0 {
			bwd[i] = bwdR
			bwdFrom[i] = 1
			bwdRev[i] = 1
		}

		// for _, next := range []string{
		// 	fwd[i-1] + c,
		// 	bwd[i-1] + c,
		// } {
		// 	if strings.Compare(next, fwd[i]) < 0 {
		// 		fwd[i] = next
		// 	}
		// 	r := reverse(next)
		// 	if strings.Compare(r, reverse(bwd[i])) < 0 {
		// 		bwd[i] = next
		// 	}
		// }
		// for _, next := range []string{
		// 	c + rf,
		// 	c + rb,
		// } {
		// 	if strings.Compare(next, fwd[i]) < 0 {
		// 		fwd[i] = next
		// 		fwdRev = 1
		// 	}
		// 	r := reverse(next)
		// 	if strings.Compare(r, reverse(bwd[i])) < 0 {
		// 		bwd[i] = next
		// 		bwdRev = 1
		// 	}
		// }
	}

	ans := make([]int, N)
	which := 0
	for i := N - 1; i >= 0; i-- {
		if which == 0 {
			ans[i] = fwdRev[i]
			which = fwdFrom[i]
		} else {
			ans[i] = bwdRev[i]
			which = bwdFrom[i]
		}
	}

	for _, v := range ans {
		io.Printf("%d ", v)
	}
	io.Println()
}

func reverse(S string) string {
	A := []rune(S)
	for i, j := 0, len(A)-1; i < j; i, j = i+1, j-1 {
		A[i], A[j] = A[j], A[i]
	}
	return string(A)
}

type FastIO struct {
	reader *bufio.Reader
	writer *bufio.Writer
}

func (io *FastIO) NextChar() byte {
	c, _ := io.reader.ReadByte()
	return c
}

func (io *FastIO) NextInt() int {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := 1
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := 0
	for !IsSpaceChar(c) {
		res = (res * 10) + int(c-'0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextLong() int64 {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := int64(1)
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := int64(0)
	for !IsSpaceChar(c) {
		res = (res * 10) + int64(c-'0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextIntArray(size int) []int {
	return io.NextIntArrayOffset(size, 0)
}

func (io *FastIO) NextIntArrayOffset(size, offset int) []int {
	arr := make([]int, size+offset)
	for i := 0; i < size; i++ {
		arr[i+offset] = io.NextInt()
	}
	return arr
}

func (io *FastIO) NextLongArray(size int) []int64 {
	return io.NextLongArrayOffset(size, 0)
}

func (io *FastIO) NextLongArrayOffset(size, offset int) []int64 {
	arr := make([]int64, size+offset)
	for i := 0; i < size; i++ {
		arr[i+offset] = io.NextLong()
	}
	return arr
}

func (io *FastIO) NextString() string {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsSpaceChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) NextLine() string {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsLineBreakChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) Println(args ...interface{}) {
	io.writer.WriteString(fmt.Sprintln(args...))
}

func (io *FastIO) Printf(format string, args ...interface{}) {
	io.writer.WriteString(fmt.Sprintf(format, args...))
}

func (io *FastIO) FlushOutput() {
	io.writer.Flush()
}

func IsSpaceChar(c byte) bool {
	switch c {
	case 0, '\t', '\n', '\r', ' ':
		return true
	default:
		return false
	}
}

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

func main() {
	io := FastIO{reader: bufio.NewReader(os.Stdin), writer: bufio.NewWriter(os.Stdout)}
	Init()
	Solve(&io)
	io.FlushOutput()
}

package main

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

func Init() {
}

func Solve(io *FastIO) {
	S := io.NextLine()

	N := len(S)
	fwd := make([]string, N)
	fwdFrom := make([]int, N)
	fwdRev := make([]int, N)
	bwd := make([]string, N)
	bwdFrom := make([]int, N)
	bwdRev := make([]int, N)
	for i := 1; i < N; i++ {
		fwd[i] = "omg"
		bwd[i] = "wtf"
	}
	fwd[0] = S[:1]
	bwd[0] = S[:1]

	// for i := 0; i + 1 < N; i++ {

	// }

	for i := 1; i < N; i++ {
		// rf := reverse(fwd[i-1])
		// rb := reverse(bwd[i-1])
		c := S[i : i+1]
		{
			fwdF := fwd[i-1] + c
			if strings.Compare(fwdF, fwd[i]) < 0 {
				fwd[i] = fwdF
			}
			if strings.Compare(reverse(fwdF), reverse(bwd[i])) < 0 {
				bwd[i] = fwdF
			}
		}
		{
			fwdR := reverse(fwd[i-1] + c)
			if strings.Compare(fwdR, fwd[i]) < 0 {
				fwd[i] = fwdR
				fwdRev[i] = 1
			}
			if strings.Compare(reverse(fwdR), reverse(bwd[i])) < 0 {
				bwd[i] = fwdR
				bwdRev[i] = 1
			}
		}
		{
			bwdF := bwd[i-1] + c
			if strings.Compare(bwdF, fwd[i]) < 0 {
				fwd[i] = bwdF
				fwdFrom[i] = 1
			}
			if strings.Compare(reverse(bwdF), reverse(bwd[i])) < 0 {
				bwd[i] = bwdF
				bwdFrom[i] = 1
			}
		}
		{
			bwdR := reverse(bwd[i-1] + c)
			if strings.Compare(bwdR, fwd[i]) < 0 {
				fwd[i] = bwdR
				fwdFrom[i] = 1
				fwdRev[i] = 1
			}
			if strings.Compare(reverse(bwdR), reverse(bwd[i])) < 0 {
				bwd[i] = bwdR
				bwdFrom[i] = 1
				bwdRev[i] = 1
			}
		}

		// fmt.Printf("%d: fwd[i] = %s, bwd[i] = %s\n", i, fwd[i], bwd[i])

		// for _, next := range []string{
		// 	fwd[i-1] + c,
		// 	bwd[i-1] + c,
		// } {
		// 	if strings.Compare(next, fwd[i]) < 0 {
		// 		fwd[i] = next
		// 	}
		// 	r := reverse(next)
		// 	if strings.Compare(r, reverse(bwd[i])) < 0 {
		// 		bwd[i] = next
		// 	}
		// }
		// for _, next := range []string{
		// 	c + rf,
		// 	c + rb,
		// } {
		// 	if strings.Compare(next, fwd[i]) < 0 {
		// 		fwd[i] = next
		// 		fwdRev = 1
		// 	}
		// 	r := reverse(next)
		// 	if strings.Compare(r, reverse(bwd[i])) < 0 {
		// 		bwd[i] = next
		// 		bwdRev = 1
		// 	}
		// }
	}

	ans := make([]int, N)
	which := 0
	for i := N - 1; i >= 0; i-- {
		if which == 0 {
			ans[i] = fwdRev[i]
			which = fwdFrom[i]
		} else {
			ans[i] = bwdRev[i]
			which = bwdFrom[i]
		}
	}

	for _, v := range ans {
		io.Printf("%d ", v)
	}
	io.Println()
}

func reverse(S string) string {
	A := []rune(S)
	for i, j := 0, len(A)-1; i < j; i, j = i+1, j-1 {
		A[i], A[j] = A[j], A[i]
	}
	return string(A)
}

type FastIO struct {
	reader *bufio.Reader
	writer *bufio.Writer
}

func (io *FastIO) NextChar() byte {
	c, _ := io.reader.ReadByte()
	return c
}

func (io *FastIO) NextInt() int {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := 1
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := 0
	for !IsSpaceChar(c) {
		res = (res * 10) + int(c-'0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextLong() int64 {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := int64(1)
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := int64(0)
	for !IsSpaceChar(c) {
		res = (res * 10) + int64(c-'0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextIntArray(size int) []int {
	return io.NextIntArrayOffset(size, 0)
}

func (io *FastIO) NextIntArrayOffset(size, offset int) []int {
	arr := make([]int, size+offset)
	for i := 0; i < size; i++ {
		arr[i+offset] = io.NextInt()
	}
	return arr
}

func (io *FastIO) NextLongArray(size int) []int64 {
	return io.NextLongArrayOffset(size, 0)
}

func (io *FastIO) NextLongArrayOffset(size, offset int) []int64 {
	arr := make([]int64, size+offset)
	for i := 0; i < size; i++ {
		arr[i+offset] = io.NextLong()
	}
	return arr
}

func (io *FastIO) NextString() string {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsSpaceChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) NextLine() string {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsLineBreakChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) Println(args ...interface{}) {
	io.writer.WriteString(fmt.Sprintln(args...))
}

func (io *FastIO) Printf(format string, args ...interface{}) {
	io.writer.WriteString(fmt.Sprintf(format, args...))
}

func (io *FastIO) FlushOutput() {
	io.writer.Flush()
}

func IsSpaceChar(c byte) bool {
	switch c {
	case 0, '\t', '\n', '\r', ' ':
		return true
	default:
		return false
	}
}

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

func main() {
	io := FastIO{reader: bufio.NewReader(os.Stdin), writer: bufio.NewWriter(os.Stdout)}
	Init()
	Solve(&io)
	io.FlushOutput()
}


package main

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

const (
	MAX = 1e6+10;
)

func solve(n int, a []int)(bool, int, []int, []int){
	d := make([]int, MAX)
	s := make([]int, n)
	t := make([]int, n)

	for i:=1; i<2*n; i++{
		//初始化
		for j:=1; j<2*n; j++{
			d[a[j]]++
		}

		cMax := a[0]
		cx := a[0]+a[i]
		d[a[i]]--

		k := 0
		s[k] = a[0]
		t[k] = a[i]
		k++

		for j:=1; j<2*n; j++{
			if d[a[j]] >0{
				d[a[j]]--
				if d[cMax-a[j]] > 0 {
					d[a[j]]--
					d[cMax-a[j]]--

					s[k] = a[j]
					t[k] = cMax - a[j]

					cMax = a[j]
					k++
				}else{
					break
				}
			}
		}

		if k == n{
			return true, cx, s, t
		}

		//恢复
		for j:=0; j<2*n; j++{
			d[a[j]] = 0
		}
	}

	return false, -1, nil, nil
}

func main(){
	var t, n int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &t)

	for (t > 0) {
		t--
		fmt.Fscan(in, &n)
		a := make([]int, 2*n)
		for i := 0; i < 2*n; i++ {
			fmt.Fscan(in, &a[i])
		}
		sort.Sort(sort.Reverse(sort.IntSlice(a)))

		if ok, x, s, t := solve(n, a); ok {
			fmt.Printf("%s\n%d\n", "YES", x)
			for i := 0; i < n; i++ {
				fmt.Printf("%d %d\n", t[i], s[i])
			}
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	t := 0
	fmt.Scan(&t)

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

		a := make([]int, 2*n)
		for i := 0; i < len(a); i++ {
			fmt.Scan(&a[i])
		}

		solve(a)
	}
}

type pair [2]int

// MaxInt returns maximum of received int numbers
func MaxInt(nums ...int) int {
	cand := nums[0]
	for _, num := range nums {
		if cand < num {
			cand = num
		}
	}
	return cand
}

func solve(a []int) {
	var (
		need int
		cnt  = make(map[int]int)
		res  = make([]pair, 0)
	)

	update := func(a, b int) {
		cnt[a]--
		cnt[b]--
		need--
		res = append(res, pair{a, b})
	}

	prepare := func() {
		need = len(a) / 2
		res = res[:0]
		for _, x := range a {
			cnt[x] = 0
		}
		for _, x := range a {
			cnt[x]++
		}
	}

	sort.Slice(a, func(i, j int) bool { return a[i] > a[j] })
	for _, x := range a {
		cnt[x]++
	}

	for i := 1; i < len(a); i++ {
		prepare()
		res = append(res, pair{a[0] + a[i], 0})
		update(a[0], a[i])
		for i, sum := 0, a[0]; true; {
			for i < len(a) && cnt[a[i]] == 0 {
				i++
			}
			if i == len(a) {
				break
			}
			if a[i] == sum-a[i] && cnt[a[i]] <= 1 {
				break
			}
			if cnt[a[i]] == 0 || cnt[sum-a[i]] == 0 {
				break
			}
			update(a[i], sum-a[i])
			sum = MaxInt(a[i], sum-a[i])
		}

		if need == 0 {
			fmt.Println("YES")
			fmt.Println(res[0][0])
			for i := 1; i < len(res); i++ {
				fmt.Printf("%v %v\n", res[i][0], res[i][1])
			}
			return
		}
	}

	fmt.Println("NO")
}

package main

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

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

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

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

func sint64() int64 {
	scanner.Scan()
	bb := scanner.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 suint64() uint64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func sstring() string {
	scanner.Scan()
	return scanner.Text()
}

func sbytes() []byte {
	scanner.Scan()
	return scanner.Bytes()
}

type Solver struct {
	a     []int64
	cache map[int64]int64
	n     int64
}

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		a := make([]int64, n*2)
		for i := range a {
			a[i] = sint64()
		}
		res := (&Solver{a, map[int64]int64{}, n}).Solve()
		if res == nil {
			printf("NO\n")
		} else {
			printf("YES\n")
			printf("%d\n", res[0][0]+res[0][1])
			for _, r := range res {
				printf("%d %d\n", r[0], r[1])
			}
		}
	}
}

func (s *Solver) Solve() [][2]int64 {
	sort.Slice(s.a, func(il, ir int) bool {
		return s.a[il] < s.a[ir]
	})
	for i := range s.a {
		s.cache[s.a[i]]++
	}
	for i := 0; i < len(s.a)-1; i++ {
		res := s.trySolve(i)
		if res != nil {
			return res
		}
	}
	return nil
}

func (s *Solver) trySolve(i int) [][2]int64 {
	l := s.a[i]
	r := s.a[len(s.a)-1]
	tmp := [][2]int64{{l, r}}
	count := map[int64]int64{}
	count[l]++
	count[r]++
	for i := len(s.a) - 2; i >= 0; i-- {
		curr := s.a[i]
		if count[curr] == s.cache[curr] {
			continue
		}
		match := tmp[len(tmp)-1][1] - curr
		count[curr]++
		count[match]++
		if count[match] > s.cache[match] {
			return nil
		}
		tmp = append(tmp, [2]int64{match, curr})
		if int64(len(tmp)) == s.n {
			return tmp
		}
	}
	return tmp
}

package main

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

func main() {
	var t int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in,&t)
	out := os.Stdout
	for u:=1;u<=t;u++{
		var n int
		fmt.Fscan(in,&n)
		a := make([]int,2*n)
		for i:=1;i<=2*n;i++{
			fmt.Fscan(in,&a[i-1])
		}
		st := make([]int,2*n)
		copy(st,a)
		sort.Ints(st)
		ans := []int{}
		flag := false
		for i:=0;i<2*n-1;i++ {
			mindex := len(st) - 1
			mp := map[int]int{}
			for j:=0;j<2*n;j++{
				mp[st[j]]++
			}
			mp[st[i]]--
			mp[st[mindex]]--
			ans = []int{}
			ans = append(ans,st[i],st[mindex])
			for j:=1;j<=n-1;j++{
				lstma := st[mindex]
				mindex--
				for mp[st[mindex]]==0{
					mindex--
				}
				nexm := st[mindex]
				if mp[lstma-nexm]==0||(lstma-nexm == nexm && mp[nexm]<=1){
					break
				}else{
					ans = append(ans,nexm,lstma-nexm)
					mp[nexm]--
					mp[lstma-nexm]--

				}
			}
			if len(ans)==2*n{
				flag = true
				break
			}
		}
		if flag{
			fmt.Fprintln(out,"YES")
			fmt.Fprintln(out,ans[0]+ans[1])
			for i:=0;i<2*n;i+=2{
				fmt.Println(ans[i],ans[i+1])
			}
		}else{
			fmt.Fprintln(out,"NO")
		}
	}
}

package main

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

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

func main() {
	defer out.Flush()

	var t int
	fmt.Fscan(in, &t)
	for t > 0 {
		t--
		solve()
	}
}

func solve() {
	var n int
	fmt.Fscan(in, &n)
	n <<= 1
	a := make([]int, n)
	cnt := make(map[int]int)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &a[i])
		cnt[a[i]]++
	}
	sort.Ints(a)
	for i := 0; i < n-1; i++ {
		if solve1(a, cnt, a[i]) {
			return
		}
	}
	fmt.Fprintln(out, "NO")
}

func solve1(a []int, cnt map[int]int, pp int) bool {
	res := make([][]int, 0)
	n := len(a)
	cc := make(map[int]int)
	cc[a[n-1]]++
	cc[pp]++
	i := n - 2
	x := a[n-1]
	res = append(res, []int{x, pp})
	sum := x + pp
	for i > 0 {
		if cnt[a[i]] <= cc[a[i]] {
			i--
			continue
		}
		cc[a[i]]++
		if cnt[x-a[i]] <= cc[x-a[i]] {
			return false
		}
		res = append(res, []int{a[i], x - a[i]})
		cc[x-a[i]]++
		x = a[i]
		i--
	}
	fmt.Fprintln(out, "YES")
	fmt.Fprintln(out, sum)
	for _, r := range res {
		fmt.Fprintln(out, r[0], r[1])
	}
	return true
}

package main

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

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

	var T, m, v int
O:
	for Fscan(in, &T); T > 0; T-- {
		cnt := map[int]int{}
		Fscan(in, &m)
		for i := 0; i < 2*m; i++ {
			Fscan(in, &v)
			cnt[v]++
		}
		n := len(cnt)
		a := make([]int, 0, n)
		for k := range cnt {
			a = append(a, k)
		}
		sort.Ints(a)
		X := a[n-1]
		cnt[X]--
	o:
		for _, v := range a {
			if cnt[v] == 0 {
				continue
			}
			c := make(map[int]int, n)
			for k, v := range cnt {
				c[k] = v
			}
			ans := append(make([]interface{}, 0, 2*m), X, v)
			c[v]--
			x := X
			for j := n - 1; j >= 0; j-- {
				v := a[j]
				if c[v] == 0 {
					continue
				}
				c[v]--
				if c[x-v] == 0 {
					continue o
				}
				c[x-v]--
				if c[v] > 0 {
					continue o
				}
				ans = append(ans, v, x-v)
				x = v
			}
			Fprintln(out, "YES")
			Fprintln(out, X+v)
			Fprintln(out, ans...)
			continue O
		}
		Fprintln(out, "NO")
	}
}

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

package main

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

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

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

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

func sint64() int64 {
	scanner.Scan()
	bb := scanner.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 suint64() uint64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func sstring() string {
	scanner.Scan()
	return scanner.Text()
}

func sbytes() []byte {
	scanner.Scan()
	return scanner.Bytes()
}

type Solver struct {
	a     []int64
	cache map[int64]int64
	n     int64
}

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		a := make([]int64, n*2)
		for i := range a {
			a[i] = sint64()
		}
		res := (&Solver{a, map[int64]int64{}, n}).Solve()
		if res == nil {
			printf("NO\n")
		} else {
			printf("YES\n")
			printf("%d\n", res[0][0]+res[0][1])
			for _, r := range res {
				printf("%d %d\n", r[0], r[1])
			}
		}
	}
}

func (s *Solver) Solve() [][2]int64 {
	sort.Slice(s.a, func(il, ir int) bool {
		return s.a[il] < s.a[ir]
	})
	for i := range s.a {
		s.cache[s.a[i]]++
	}
	for i := 0; i < len(s.a)-1; i++ {
		res := s.trySolve(i)
		if res != nil {
			return res
		}
	}
	return nil
}

func (s *Solver) trySolve(i int) [][2]int64 {
	l := s.a[i]
	r := s.a[len(s.a)-1]
	tmp := [][2]int64{{l, r}}
	count := map[int64]int64{}
	count[l]++
	count[r]++
	for i := len(s.a) - 2; i >= 0; i-- {
		curr := s.a[i]
		if count[curr] == s.cache[curr] {
			continue
		}
		match := tmp[len(tmp)-1][1] - curr
		count[curr]++
		count[match]++
		if count[match] > s.cache[match] {
			return nil
		}
		tmp = append(tmp, [2]int64{match, curr})
		if int64(len(tmp)) == s.n {
			return tmp
		}
	}
	return nil
}


package main

import (
	"fmt"
)

func shift(b []byte, n int) []byte {
	ret := make([]byte, 0, len(b))
	for i := len(b) - 1; i > len(b) - 1 - n; i -- {
		ret = append(ret, b[i])
	}
	ret = append(ret, b[:(len(b) - n)]...)
	return ret
}

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

	bs := []byte(s)

	mid := n / 2
	flag := true
	ans := make([]int, 0, 6100)
	for i := 0;i < n;i ++ {
		if t[mid] == bs[i] {
			bs = shift(bs, n - i - 1)
			if n - i - 1 != 0 {
				ans = append(ans, n - i - 1)
			}
			bs = shift(bs, 1)
			ans = append(ans, 1)
			flag = false
			break
		}
	}
	if flag {
		fmt.Printf("-1")
		return
	}
	left, right, reversed := mid, mid, false

	for left != 0 || right != n - 1 {
		var target byte = '\n'
		if !reversed && left != 0 {
			target = t[left - 1]
		} else if reversed && right != n - 1 {
			target = t[right + 1]
		}
		if target != '\n' {
			flag := true
			for i := right - left + 1;i < n;i ++ {
				if target == bs[i] {
					if n - i != 0 {
						bs = shift(bs, n - i)
						ans = append(ans, n - i)
					}
					if i - right + left - 1 != 0 {
						bs = shift(bs, i - right + left - 1)
						ans = append(ans, i - right + left - 1)
					}
					bs = shift(bs, n)
					ans = append(ans, n)
					if reversed {
						right ++
					} else {
						left --
					}
					reversed = !reversed
					flag = false
					break
				}
			}
			if flag {
				fmt.Printf("-1")
				return
			}
		} else {
			if n - right + left - 1 != 0 {
				bs = shift(bs, n - right + left - 1)
				ans = append(ans, n - right + left - 1)
			}
			bs = shift(bs, n)
			ans = append(ans, n - right + left - 1, n)
			reversed = !reversed
		}
	}
	if reversed {
		bs = shift(bs, n)
		ans = append(ans, n)
	}
	fmt.Printf("%d\n", len(ans))
	for i := 0;i < len(ans);i ++ {
		fmt.Printf("%d ", ans[i])
	}
}

package main

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

var in = bufio.NewScanner(os.Stdin)

func readInt() int {
	if !in.Scan() {
		panic(in.Err())
	}
	res := 0
	for _, b := range in.Bytes() {
		res = res*10 + int(b-'0')
	}
	return res
}

func readString() string {
	if !in.Scan() {
		panic(in.Err())
	}
	return in.Text()
}

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

	n := readInt()
	s := readString()
	t := readString()
	var cntS, cntT [256]int
	for _, c := range s {
		cntS[c]++
	}
	for _, c := range t {
		cntT[c]++
	}
	if cntS != cntT {
		fmt.Println(-1)
		return
	}

	buf := []byte(s)
	buf2 := make([]byte, n)
	var ans []int
	for done := 0; done < n; done++ {
		pos := 0
		for buf[pos] != t[done] {
			pos++
		}
		ans = append(ans, n-1-pos, 1, n)
		for i := 0; i < pos; i++ {
			buf2[i] = buf[pos-1-i]
		}
		for i := pos; i < n-1; i++ {
			buf2[i] = buf[i+1]
		}
		buf2[n-1] = buf[pos]
		buf, buf2 = buf2, buf
	}
	fmt.Fprintln(out, len(ans))
	for i := range ans {
		if i+1 == len(ans) {
			fmt.Fprintln(out, ans[i])
		} else {
			fmt.Fprint(out, ans[i], " ")
		}
	}
}


package main

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

func solve() {
    n, m, _ := readInt(), readInt(), readInt()
    a, c := make([][]int, n), readIntArray(n)
    for i := 0; i < m; i++ {
        l, r := readInt()-1, readInt()-1
        a[l], a[r] = append(a[l], r), append(a[r], l)
    }
    was, ans := make([]bool, n), 0
    for s := range c {
        if was[s] {
            continue
        }
        was[s] = true
        cnts, mx, mxC := make(map[int]int), -1, 0
        for q := []int{s}; len(q) > 0; {
            cur := q[len(q)-1]
            q = q[:len(q)-1]
            for _, v := range a[cur] {
                if !was[v] {
                    q, was[v] = append(q, v), true
                }
            }
            col := c[cur]
            cnts[col]++
            if cnt := cnts[col]; cnt > mx {
                mx, mxC = cnt, col
            }
        }
        for k, v := range cnts {
            if k != mxC {
                ans += v
            }
        }
    }
    fmt.Println(ans)
}

// Helpers

var scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)

func main() {
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// Math

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

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

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

// IO

func readString() string {
	scanner.Scan()
	return scanner.Text()
}

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

// Sort

type Ints64 []int64

func (a Ints64) Len() int           { return len(a) }
func (a Ints64) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a Ints64) Less(i, j int) bool { return a[i] < a[j] }

/* Sort tempalte
func (a ) Len() int           { return len(a) }
func (a ) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a ) Less(i, j int) bool { return  }
*/

package main

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

var bufin *bufio.Reader
var bufout *bufio.Writer

var n, m, k, E int
var c, adj, next, last, cols []int
var vst []bool

func add(u int, v int) {
	adj[E] = v
	next[E] = last[u]
	last[u] = E
	E++
}

func dfs(u int) {
	vst[u] = true
	cols = append(cols, c[u])
	for i := last[u]; i != -1; i = next[i] {
		v := adj[i]
		if !vst[v] {
			dfs(v)
		}
	}
}

func repaint(u int) int {
	cols = make([]int, 0)
	dfs(u)

	sort.Ints(cols)

	most := 0
	nc := len(cols)

	for i := 0; i < nc; i++ {
		j := i
		for (j < nc) && (cols[j] == cols[i]) {
			j++
		}
		if most < j-i {
			most = j - i
		}
		i = j - 1
	}

	return nc - most
}

func main() {
	bufin = bufio.NewReader(os.Stdin)
	bufout = bufio.NewWriter(os.Stdout)
	defer bufout.Flush()

	fmt.Fscanf(bufin, "%d %d %d\n", &n, &m, &k)
	c = make([]int, n+1)
	for i := 1; i <= n; i++ {
		fmt.Fscanf(bufin, "%d\n", &c[i])
	}

	adj = make([]int, 2*m+1)
	next = make([]int, 2*m+1)
	last = make([]int, n+1)
	vst = make([]bool, n+1)

	E = 0
	for i := 0; i <= n; i++ {
		last[i] = -1
		vst[i] = false
	}

	var u, v int
	for i := 0; i < m; i++ {
		fmt.Fscanf(bufin, "%d %d\n", &u, &v)
		add(u, v)
		add(v, u)
	}

	res := 0
	for u := 1; u <= n; u++ {
		if !vst[u] {
			res += repaint(u)
		}
	}

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


package main

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

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

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

	n, m := nextInt(), nextInt()

	for i := 0; i < n; i++ {
		printSeat(i*2+1+n*2, m)
		printSeat(i*2+1, m)
		printSeat(i*2+2+n*2, m)
		printSeat(i*2+2, m)
	}

	println()
}

func printSeat(n, m int) {
	if n <= m {
		print(n, " ")
	}
}

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 (
    "os"
    "bufio"
    "fmt"
)

var stdin *bufio.Reader

func init() {
    stdin = bufio.NewReader(os.Stdin)
}

func main() {

    n, m := 0, 0

    fmt.Fscanf(stdin, "%d%d\n", &n, &m)

    for i := 0; i < n; i++ {
        if 2*n + 2*i + 1 <= m {
            fmt.Printf("%d ", 2*n + 2*i + 1)
        }

        if 2*i + 1 <= m {
            fmt.Printf("%d ", 2*i + 1)
        }

        if 2*n + 2*i + 2 <= m {
            fmt.Printf("%d ", 2*n + 2*i + 2)
        }

        if 2*i + 2 <= m {
            fmt.Printf("%d ", 2*i + 2)
        }
    }
}

package main

import (
	"fmt"
	"strings"
)

type Bus struct {
	rows, passengers int
}

func (b Bus) Order() []string {
	result := make([]string, 0, b.passengers)

	for row := 0; row < b.rows; row++ {
		for _, passenger := range []int{
			2*b.rows + 2*row + 1, // left non-window
			2*row + 1,            // left window
			2*b.rows + 2*row + 2, // right non-window
			2*row + 2,            // right window
		} {
			if passenger <= b.passengers {
				result = append(result, fmt.Sprintf("%d", passenger))
			}
		}
	}
	return result
}

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

	fmt.Println(strings.Join(Bus{rows: n, passengers: m}.Order(), " "))
}


package main

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

func isSorted(a []int) bool {
	for i := 1; i < len(a); i++ {
		if a[i] < a[i-1] {
			return false
		}
	}
	return true
}

func solve(a []int, x int) int {
	n := len(a)

	if isSorted(a) {
		return 0
	}

	cnt := 0
	if a[0] > x {
		a[0], x = x, a[0]
		cnt++
	}
	for i := 1; i < n; i++ {
		if isSorted(a[i-1:]) {
			break
		}
		if a[i] > x {
			a[i], x = x, a[i]
			cnt++
		}
		if a[i] < a[i-1] {
			return -1
		}
	}
	return cnt
}

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

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

	for ; t > 0; t-- {
		fmt.Fscan(in, &n, &x)
		a := make([]int, n)
		for i := range a {
			fmt.Fscan(in, &a[i])
		}

		fmt.Println(solve(a, x))
	}
}

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

package main

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

var (
	t int

	n, x int
	A    []int
)

func main() {
	defer stdout.Flush()

	t = readi()
	for tc := 0; tc < t; tc++ {
		n, x = readi2()
		A = readis(n)

		solve()
	}
}

func solve() {
	res := 0

	if ok(A) {
		printf("%d\n", res)
		return
	}

	for i := 0; i < n; i++ {
		if A[i] > x {
			A[i], x = x, A[i]

			res++
			if ok(A[:i]) && ok(A[i:]) {
				printf("%d\n", res)
				return
			}

			// break
		}
	}

	printf("-1\n")
}

func ok(B []int) bool {
	if len(B) == 1 {
		return true
	}

	for i := 0; i < len(B)-1; i++ {
		if B[i] > B[i+1] {
			return false
		}
	}

	return true
}

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

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

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

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

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

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

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

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

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

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

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

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

	return s
}

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

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

	if e == 0 {
		return 1
	}

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

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

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

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

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

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

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

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

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

func newReadString(ior io.Reader, sf bufio.SplitFunc) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	r.Split(sf)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return string(res)
}

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

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

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

	return string(res)
}

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

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

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

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

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

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

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

	return res
}

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

package main

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

var (
	t int

	n, x int
	A    []int
)

func main() {
	defer stdout.Flush()

	t = readi()
	for tc := 0; tc < t; tc++ {
		n, x = readi2()
		A = readis(n)

		solve()
	}
}

func solve() {
	res := 0

	if ok(A) {
		printf("%d\n", res)
		return
	}

	for i := 0; i < n; i++ {
		if A[i] > x {
			A[i], x = x, A[i]

			res++
			if ok(A[i:]) {
				printf("%d\n", res)
				return
			}

			// break
		}
	}

	printf("-1\n")
}

func ok(B []int) bool {
	if len(B) == 1 {
		return true
	}

	for i := 0; i < len(B)-1; i++ {
		if B[i] > B[i+1] {
			return false
		}
	}

	return true
}

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

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

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

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

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

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

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

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

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

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

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

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

	return s
}

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

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

	if e == 0 {
		return 1
	}

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

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

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

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

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

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

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

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

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

func newReadString(ior io.Reader, sf bufio.SplitFunc) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	r.Split(sf)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return string(res)
}

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

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

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

	return string(res)
}

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

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

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

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

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

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

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

	return res
}


package main

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

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

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

func main() {
  defer writer.Flush()
  var t int64
  scanf("%d\n", &t)
  for i := int64(0); i < t; i++ {
    var s string
    scanf("%s\n", &s)
    solve(s)
  }
}

func solve(s string) {
  visited := [26]bool{}
  res := ""
  cur := 0
  for i := range s {
    b := s[i]
    if !visited[b-'a'] {
      visited[b-'a'] = true
      if cur == 0 {
        res = string(b) + res
      } else if cur == len(res)-1 {
        cur++
        res += string(b)
      } else {
        printf("NO\n")
        return
      }
    } else {
      if cur > 0 && res[cur-1] == b {
        cur--
      } else if cur < len(res)-1 && res[cur+1] == b {
        cur++
      } else {
        printf("NO\n")
        return
      }
    }
  }
  for i := range visited {
    if !visited[i] {
      res += string(byte(i) + 'a')
    }
  }
  printf("YES\n%s\n", res)
}

package main

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

const (
	mod = int64(1e9 + 7)
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := 1
	fmt.Fscanf(in, "%d\n", &t)
	for tt := 0; tt < t; tt++ {
		s := ""
		fmt.Fscanf(in, "%s\n", &s)
		used := make([]bool, 26)
		// for i := 0; i < len(s); i++ {
		// 	used[s[i]-'a'] = true
		// }
		//fmt.Println(used)
		ans := []byte{}
		id := 0
		ans = append(ans, s[0])
		used[s[0]-'a'] = true
		hasAns := true
		for i := 1; i < len(s); i++ {
			//fmt.Println(string(ans), id)
			// Can map
			if id > 0 && ans[id-1] == s[i] {
				id--
			} else if id < len(ans)-1 && ans[id+1] == s[i] {
				id++
			} else if id == len(ans)-1 && !used[s[i]-'a'] {
				ans = append(ans, s[i])
				id++
			} else if id == 0 && !used[s[i]-'a'] {
				ans = append([]byte{s[i]}, ans...)
			} else {
				hasAns = false
				break
			}
			used[s[i]-'a'] = true
		}
		if hasAns {
			for i := 0; i < 26; i++ {
				if !used[i] {
					ans = append(ans, byte(i+'a'))
				}
			}
			fmt.Fprintf(out, "YES\n")
			fmt.Fprintf(out, "%s\n", string(ans))
		} else {
			fmt.Fprintf(out, "NO\n")
		}
	}
	return
}

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

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

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	numStr := scanner.Text()
	num, _ := strconv.Atoi(numStr)
	for i := 0; i < num; i++ {
		scanner.Scan()
		str := scanner.Text()
		m := make(map[uint8][]uint8)
		result := str[0:1]
		flag := true
		letters := make(map[uint8]struct{})
		for c:=uint8('a');c <= 'z';c++ {
			if c!=str[0]{
				letters[c] = struct{}{}
			}
		}
		for i := 1; i < len(str); i++ {
			lastLetter := str[i-1]
			currentL := str[i]
			if list, ok := m[currentL]; !ok { // 1. 存在切pair <i, i+1> exist
				if result[0] == lastLetter {
					result = string(currentL) + result
				} else if result[len(result)-1] == lastLetter {
					result = result + string(currentL)
				} else {
					// fmt.Println(result, lastLetter, currentL, m, result[0])
					flag = false
					break
				}
				m[currentL] = []uint8{lastLetter}
				val := m[lastLetter]
				val = append(val, currentL)
				m[lastLetter] = val
				delete(letters, currentL)
			} else {
				exist := false
				for _, c := range list {
					if c == lastLetter {
						exist = true
					}
				}
				if !exist {
					flag = false
					break
				}
			}
		}
		if !flag {
			// fmt.Println(result)
			fmt.Println("NO")
			continue
		}
		for key, _ := range letters {
			result += string(key)
		}
		fmt.Println("YES")
		fmt.Println(result)
	}
}

/**
codedoca
c o a
o c d
d o e
e d
a c
 */

package main

import "fmt"

func main() {
	var t int
	fmt.Scan(&t)
	for ; t > 0; t-- {
		var pass string
		var place [60] int
		var key [60] byte
		for i := 0; i < 60; i++ {
			place[i] = -1
			key[i] = '.'
		}
		fmt.Scan(&pass)
		var j int = 26
		var cou = true
		for i := len(pass) - 1; i >= 0 && cou; i-- {
			if(key[j] == '.') {
				key[j] = pass[i]
				place[pass[i]-'a'] = i
			} else if(key[j-1] == pass[i]) {
				j--
			} else if(key[j+1] == pass[i]) {
				j++
			} else if(place[pass[i] - 'a'] != -1) {
				cou = false
			} else if(key[j+1] == '.') {
				j++
				place[pass[i] - 'a'] = j
				key[j] = pass[i]
			} else if(key[j-1] == '.') {
				j--
				place[pass[i] - 'a'] = j
				key[j] = pass[i]
			} else {
				cou = false
			}
		}
		if cou {
			fmt.Println("YES")
			for i := 0; i < 60; i++ {
				if key[i] != '.' {
					fmt.Printf("%c", key[i])
				}
			}
			for i := 0; i < 26; i++ {
				if place[i] == -1 {
					fmt.Printf("%c", byte('a' + i))
				}
			}
			fmt.Println()
		} else {
			fmt.Println("NO")
		}
	}
}

/*
5
ababa
codedoca
abcda
zxzytyz
abcdefghijklmnopqrstuvwxyza
 */

package main

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

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

	solve := func() (ans []byte) {
		var s []byte
		Fscan(in, &s)
		for i := range s {
			s[i] -= 'a'
		}

		used := [26]bool{}
		ans = make([]byte, 0, 26)
		if len(s) > 1 {
			g := make([]map[byte]int, 26)
			for i := range g {
				g[i] = map[byte]int{}
			}
			for i := 0; i < len(s)-1; i++ {
				v, w := s[i], s[i+1]
				g[v][w]++
				g[w][v]++
			}

			st := -1
			for v, e := range g {
				if len(e) == 1 {
					st = v
				} else if len(e) > 2 {
					return nil
				}
			}
			if st == -1 {
				return nil
			}
			var f func(byte)
			f = func(v byte) {
				used[v] = true
				ans = append(ans, 'a'+v)
				for w := range g[v] {
					if !used[w] {
						f(w)
					}
				}
			}
			f(byte(st))
		}
		for i, u := range used {
			if !u {
				ans = append(ans, byte('a'+i))
			}
		}
		return
	}

	var t int
	for Fscan(in, &t); t > 0; t-- {
		if ans := solve(); ans != nil {
			Fprintf(out, "YES\n%s\n", ans)
		} else {
			Fprintln(out, "NO")
		}
	}
}

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

package main

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

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

	solve := func() (ans []byte) {
		var s []byte
		Fscan(in, &s)

		ans = make([]byte, 26)
		for i := range ans {
			ans[i] = byte('a' + i)
		}

		if len(s) == 1 {
			return
		}

		nb := [26]map[byte]bool{}
		for i := range nb {
			nb[i] = map[byte]bool{}
		}
		for i := range s {
			s[i]-='a'

		}
		for i, b := range s {
			if i == 0 {
				nb[b][s[i+1]]=true
			} else if i == len(s)-1 {
				nb[b][s[i-1]]=true
			} else {
				nb[b][s[i+1]]=true
				nb[b][s[i-1]]=true
			}
		}

		for _, mp := range nb {
			if len(mp) > 2 {
				return nil
			}
		}

		g := make([]map[byte]bool, 26)
		for i := range g {
			g[i]= map[byte]bool{}
		}
		for v, lin := range nb {
			if len(lin) > 0 {
				for w := range lin {
					g[v][w]=true
					g[w][byte(v)]=true
				}
			}
		}
		cnt1 := 0
		cnt2 :=0
		for _, e := range g {
			if len(e) == 1 {
				cnt1++
			} else if len(e) ==2 {
				cnt2++
			} else if len(e) > 2 {
				return nil
			}
		}
		if cnt1 != 2 {
			return nil
		}

		//toArr := func(mp map[byte]bool) (res[]byte){
		//	for b := range mp {
		//		res = append(res, b)
		//	}
		//	return
		//}

		used := [26]bool{}
		ans = make([]byte,0,26)
		for b, e := range g {
			if len(e) == 1 {
				//a :=toArr(e)

				vis := make([]bool, 26)
				var f func(byte)
				f = func(v byte) {
					vis[v] = true
					ans = append(ans, 'a'+v)
					for w := range g[v] {
						if !vis[w] {
							f(w)
						}
					}
				}
				f(byte(b))

				//ans = append(ans, byte('a'+b))
				//a :=toArr(e)
				//next := a[0]
				//for len(g[next]) ==2 {
				//	a = toArr(g[next])
				//	//if a[0] ==
				//}
				//ans = append(ans, 'a'+next)
				break
			}
		}
		for _, v := range ans {
			used[v-'a']=true
		}
		for i, b := range used {
			if !b {
				ans = append(ans, byte(i+'a'))
			}
		}

		return
	}

	var t int
	Fscan(in, &t)
	for _case := 1; _case <= t; _case++ {
		ans := solve()
		if len(ans) == 0 {
			Fprintln(out, "NO")
		} else {
			Fprintln(out, "YES")
			Fprintf(out, "%s\n", ans)
		}
	}
}

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

package main

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

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

	solve := func() (ans []byte) {
		var s []byte
		Fscan(in, &s)
		used := [26]bool{}
		if len(s) > 1 {
			for i := range s {
				s[i] -= 'a'
			}
			g := make([]map[byte]int, 26)
			for i := range g {
				g[i] = map[byte]int{}
			}
			for i := 0; i < len(s)-1; i++ {
				v, w := s[i], s[i+1]
				g[v][w]++
				g[w][v]++
			}
			st := -1
			for v, e := range g {
				if len(e) == 1 {
					st = v
				} else if len(e) > 2 {
					return
				}
			}
			if st == -1 {
				return
			}
			var f func(byte)
			f = func(v byte) {
				used[v] = true
				ans = append(ans, 'a'+v)
				for w := range g[v] {
					if !used[w] {
						f(w)
					}
				}
			}
			f(byte(st))
		}
		for i, u := range used {
			if !u {
				ans = append(ans, byte('a'+i))
			}
		}
		return
	}

	var t int
	for Fscan(in, &t); t > 0; t-- {
		if ans := solve(); ans != nil {
			Fprintf(out, "YES\n%s\n", ans)
		} else {
			Fprintln(out, "NO")
		}
	}
}

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

package main

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

func main() {
	defer Flush()

	t := readi()
	for i := 0; i < t; i++ {
		s := readb()
		ans, ok := solve(s)
		if ok {
			println("YES")
			println(string(ans))
		} else {
			println("NO")
		}
	}
}

func solve(s []byte) ([]byte, bool) {
	n := len(s)
	a := make([]int, n)
	for i := range a {
		a[i] = int(s[i] - 'a')
	}
	G := make([][]bool, 26)
	for i := range G {
		G[i] = make([]bool, 26)
	}
	for i := 0; i+1 < n; i++ {
		x := a[i]
		y := a[i+1]
		G[x][y] = true
		G[y][x] = true
	}
	start := -1
	zeros := 0
	for i := range G {
		var count int
		for j := range G[i] {
			if G[i][j] {
				count++
			}
		}
		if count == 0 {
			zeros++
		} else if count == 1 {
			start = i
		} else if 2 < count {
			return nil, false
		}
	}
	if zeros == 26 {
		return []byte("abcdefghijklmnopqrstuvwxyz"), true
	}
	if start == -1 {
		return nil, false
	}
	used := make([]bool, 26)
	b := make([]byte, 0, 26)
	var dfs func(u int)
	dfs = func(u int) {
		b = append(b, byte(u+'a'))
		used[u] = true
		for v, t := range G[u] {
			if !t {
				continue
			}
			if used[v] {
				continue
			}
			dfs(v)
		}
	}
	dfs(start)
	for i, t := range used {
		if t {
			continue
		}
		b = append(b, byte(i+'a'))
	}
	return b, true
}

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

// 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[:len(b):len(b)]
}

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

// Read next line as []byte.  Trailing '\n' will not be included.
// See also comments on readb()
func readbln() []byte {
	b, err := nextLine()
	if err != nil {
		panic(err)
	}
	return b[:len(b):len(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...)
}

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

// Utilities

func sumSlice(a []int) int {
	var res int
	for _, v := range a {
		res += v
	}
	return res
}

func sumSlicell(a []int64) int64 {
	var res int64
	for _, v := range a {
		res += v
	}
	return res
}

func readInts(N int) (int, []int) {
	if N == 0 {
		N = readi()
	}
	a := make([]int, N)
	for i := range a {
		a[i] = readi()
	}
	return N, a
}

func readIntsll(N int) (int, []int64) {
	if N == 0 {
		N = readi()
	}
	a := make([]int64, N)
	for i := range a {
		a[i] = readll()
	}
	return N, a
}

// reverse slice in place
func reverse(a []int) {
	for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 {
		a[i], a[j] = a[j], a[i]
	}
}

// areverse: allocating reverse
func areverse(a []int) []int {
	b := make([]int, len(a))
	for i := range a {
		b[i] = a[len(a)-i-1]
	}
	return b
}

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

// 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 minf(a, b float64) float64 {
	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 maxf(a, b float64) float64 {
	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
}

func absf(a float64) float64 {
	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)
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	numStr := scanner.Text()
	num, _ := strconv.Atoi(numStr)
	for i := 0; i < num; i++ {
		scanner.Scan()
		str := scanner.Text()
		m := make(map[uint8][]uint8)
		result := str[0:1]
		flag := true
		letters := make(map[uint8]struct{})
		for c:=uint8('a');c <= 'z';c++ {
			if c!=str[0]{
				letters[c] = struct{}{}
			}
		}
		for i := 1; i < len(str); i++ {
			lastLetter := str[i-1]
			currentL := str[i]
			if list, ok := m[currentL]; !ok { // 1. 存在切pair <i, i+1> exist
				if result[0] == lastLetter {
					result = string(currentL) + result
				} else if result[len(result)-1] == lastLetter {
					result = result + string(currentL)
				} else {
					fmt.Println(result, lastLetter, currentL, m, result[0])
					flag = false
					break
				}
				m[currentL] = []uint8{lastLetter}
				val := m[lastLetter]
				val = append(val, currentL)
				m[lastLetter] = val
				delete(letters, currentL)
			} else {
				exist := false
				for _, c := range list {
					if c == lastLetter {
						exist = true
					}
				}
				if !exist {
					flag = false
					break
				}
			}
		}
		if !flag {
			// fmt.Println(result)
			fmt.Println("NO")
			continue
		}
		for key, _ := range letters {
			result += string(key)
		}
		fmt.Println("YES")
		fmt.Println(result)
	}
}

/**
codedoca
c o a
o c d
d o e
e d
a c
 */

package main

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

func main() {
	defer Flush()

	t := readi()
	for i := 0; i < t; i++ {
		s := readb()
		ans, ok := solve(s)
		if ok {
			println("YES")
			println(string(ans))
		} else {
			println("NO")
		}
	}
}

func solve(s []byte) ([]byte, bool) {
	n := len(s)
	a := make([]int, n)
	for i := range a {
		a[i] = int(s[i] - 'a')
	}
	G := make([][]bool, 26)
	for i := range G {
		G[i] = make([]bool, 26)
	}
	for i := 0; i+1 < n; i++ {
		x := a[i]
		y := a[i+1]
		G[x][y] = true
		G[y][x] = true
	}
	start := -1
	for i := range G {
		var count int
		for j := range G[i] {
			if G[i][j] {
				count++
			}
		}
		if count == 1 {
			start = i
		} else if 2 < count {
			return nil, false
		}
	}
	if start == -1 {
		return nil, false
	}
	used := make([]bool, 26)
	b := make([]byte, 0, 26)
	var dfs func(u int)
	dfs = func(u int) {
		b = append(b, byte(u+'a'))
		used[u] = true
		for v, t := range G[u] {
			if !t {
				continue
			}
			if used[v] {
				continue
			}
			dfs(v)
		}
	}
	dfs(start)
	for i, t := range used {
		if t {
			continue
		}
		b = append(b, byte(i+'a'))
	}
	return b, true
}

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

// 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[:len(b):len(b)]
}

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

// Read next line as []byte.  Trailing '\n' will not be included.
// See also comments on readb()
func readbln() []byte {
	b, err := nextLine()
	if err != nil {
		panic(err)
	}
	return b[:len(b):len(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...)
}

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

// Utilities

func sumSlice(a []int) int {
	var res int
	for _, v := range a {
		res += v
	}
	return res
}

func sumSlicell(a []int64) int64 {
	var res int64
	for _, v := range a {
		res += v
	}
	return res
}

func readInts(N int) (int, []int) {
	if N == 0 {
		N = readi()
	}
	a := make([]int, N)
	for i := range a {
		a[i] = readi()
	}
	return N, a
}

func readIntsll(N int) (int, []int64) {
	if N == 0 {
		N = readi()
	}
	a := make([]int64, N)
	for i := range a {
		a[i] = readll()
	}
	return N, a
}

// reverse slice in place
func reverse(a []int) {
	for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 {
		a[i], a[j] = a[j], a[i]
	}
}

// areverse: allocating reverse
func areverse(a []int) []int {
	b := make([]int, len(a))
	for i := range a {
		b[i] = a[len(a)-i-1]
	}
	return b
}

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

// 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 minf(a, b float64) float64 {
	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 maxf(a, b float64) float64 {
	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
}

func absf(a float64) float64 {
	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)
}


package main

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

var N, M int

const MOD int64 = 1000000007
const IMP = "imposter"
const CREW = "crewmate"

const IMPOSTER = 1
const CREWMATE = 2

var v []int

type comment struct {
	i, j int
	c string
}

var gr [][]*edge

var co []*comment

var seen []bool

var crew []int

type edge struct {
	from , to int
	crew bool
}

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)

	for i := 0; i < t; i++ {
		fmt.Fscan(r, &N, &M)

		co = make([]*comment, M)

		for k := 0; k < M; k++ {
			var i, j int
			var c string
			fmt.Fscan(r, &i, &j, &c)
			co[k] = &comment{i, j, c}
		}

		ans := solve()

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

func solve() int {
	//1
	gr = make([][]*edge, N + 1)

	// 0 - outgoing
	// 1 - incoming

	for i := 0; i < M; i++ {
		com := co[i]

		from := com.i
		to := com.j
		var v bool
		if com.c == IMP {
			v = false
		} else {
			v = true
		}

		gr[from] = append(gr[from], &edge{from, to, v})
		gr[to] = append(gr[to], &edge{from, to, v})
	}

	var global [2][]bool
	for i := 0; i < 2; i++ {
		global[i] = make([]bool, N)
	}

	//const IMPOSTER = 1
	//const CREWMATE = 2

	var components [3][]int

	for i := 1; i <= 2; i++ {
		crew = make([]int, N + 1)

		seen = make([]bool, N + 1)

		for j := 1; j <= N; j++ {
			if seen[j] {
				continue
			}

			s := dfs(j, -1, i)

			components[i] = append(components[i], s)
		}
	}

	var sum int

	for i := 0; i < len(components[1]); i++ {
		s1 := components[1][i]
		s2 := components[2][i]

		if s1 == -1  && s2 == -1 {
			return -1
		}

		sum += max(s1, s2)
	}

	return sum
}

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

func dfs(v int, parent int, mustbe int) int  {
	if seen[v] {
		role := crew[v]

		if role != mustbe {
			return -1
		}

		return 0
	}

	crew[v] = mustbe
	seen[v] = true

	edges := gr[v]
	var sum int

	wasBad := false

	for i := 0; i < len(edges); i++ {
		e := edges[i]

		if e.from == parent || e.to == parent {
			continue
		}

		from := e.from
		to := e.to
		isCrew := e.crew

		var a int

		if mustbe == IMPOSTER {
			if to == v {
				if isCrew {
					a = dfs(from, v, IMPOSTER)

				} else {
					a = dfs(from, v, CREWMATE)
				}

			} else {
				if isCrew {
					a = dfs(to, v, IMPOSTER)

				} else {
					a = dfs(to, v, CREWMATE)
				}
			}

		} else {
			if to == v {
				if isCrew {
					a = dfs(from, v, CREWMATE)
				} else {
					a = dfs(from, v, IMPOSTER)
				}

			} else {
				if isCrew {
					a = dfs(to, v, CREWMATE)
				} else {
					a = dfs(to, v, IMPOSTER)
				}
			}
		}

		if a == -1 {
			wasBad = true
			sum = -1

		} else {
			sum += a
		}
	}

	if wasBad {
		return -1
	}

	if mustbe == IMPOSTER {
		sum++
	}

	return sum
}

package main

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

type Edge struct {
	from, to int
	etype    int
}

func main() {
	in := bufio.NewReader(os.Stdin)
	//out := bufio.NewWriter(os.Stdout)
	var t int
	var n, m int
	fmt.Fscanf(in, "%d\n", &t)
	for i := 0; i < t; i++ {

		fmt.Fscanf(in, "%d %d\n", &n, &m)
		edges := make(map[int][]Edge)
		str := make([]byte, 20)
		for j := 0; j < m; j++ {
			var from, to int
			fmt.Fscanf(in, "%d %d %s\n", &from, &to, &str)
			newEdge := Edge{
				from: from,
				to:   to,
			}
			if string(str) == "imposter" {
				newEdge.etype = 0
			} else {
				newEdge.etype = 1
			}
			if _, ok := edges[from]; !ok {
				edges[from] = make([]Edge, 0)
			}
			edges[from] = append(edges[from], newEdge)

			if _, ok := edges[to]; !ok {
				edges[to] = make([]Edge, 0)
			}
			edges[to] = append(edges[to], Edge{from:to,to:from,etype:newEdge.etype})

		}
		ans := 0
		color := make([]int, n+1)
		for j:=0;j <= n;j++{
			color[j] = 0
		}
		contradict := false
		for j := 1; contradict == false && j <= n; j++ {
			if color[j] != 0 {
				continue
			}
			visitCnt := 0
			aCnt := 0
			sf, stail := 0, 0
			stack := make([]int, 0)
			stack = append(stack, j)
			stail++
			visitCnt++
			color[j] = 1
			aCnt++
			for contradict == false && sf < stail {
				cur := stack[sf]
				tedges := edges[cur]
				for _, item := range tedges {
					toExpectedColor := color[cur]
					if item.etype == 0 {
						if toExpectedColor == 1 {
							toExpectedColor = 2
						} else {
							toExpectedColor = 1
						}
					}
					if color[item.to] != 0 {
						if color[item.to] != toExpectedColor {
						//	fmt.Printf("== cur=%d item.to=%d color[item.to]=%d toExpectedColor=%d\n",cur,item.to,color[item.to],toExpectedColor)
							contradict = true
							break
						}
					} else {
						stack = append(stack, item.to)
						stail++
						visitCnt++
						color[item.to] = toExpectedColor
						if toExpectedColor == 1 {
							aCnt++
						}
					}
				}
				sf++
			}
			if aCnt > visitCnt-aCnt {
				ans = ans + aCnt
			} else {
				ans = ans + visitCnt - aCnt
			}

		}
		if contradict {
			fmt.Printf("-1\n")
		} else {
			fmt.Printf("%d\n", ans)
		}

	}

}
/*
33
2 2
1 2 imposter
1 2 crewmate

2 2
1 2 imposter
1 2 imposter

2 2
1 2 crewmate
1 2 crewmate

3 3
1 2 imposter
2 3 imposter
3 1 imposter

4 4
1 2 imposter
2 4 crewmate
3 4 imposter
3 1 imposter

4 4
1 2 imposter
2 4 imposter
3 4 imposter
3 1 imposter
*/

package main

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

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

	var T, n, m, v, w int
	var s string
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &m)
		fa := make([]int, n)
		for i := range fa {
			fa[i] = i
		}
		dis := make([]uint8, n)
		var f func(int) int
		f = func(x int) int {
			if fa[x] != x {
				ffx := f(fa[x])
				dis[x] ^= dis[fa[x]]
				fa[x] = ffx
			}
			return fa[x]
		}
		merge := func(x, y int, d uint8) bool {
			if fx, fy := f(x), f(y); fx != fy {
				dis[fx] = d ^ dis[y] ^ dis[x]
				fa[fx] = fy
				return true
			}
			f(x)
			f(y)
			return dis[x]^dis[y] == d
		}
		ok := true
		for ; m > 0; m-- {
			Fscan(in, &v, &w, &s)
			if ok && !merge(v-1, w-1, s[1]&1) {
				ok = false
			}
		}
		if !ok {
			Fprintln(out, -1)
			continue
		}
		cnt := make([][2]int, n)
		for i := range dis {
			cnt[f(i)][dis[i]]++
		}
		ans := 0
		for _, p := range cnt {
			if p[0] > 0 || p[1] > 0 {
				ans += max(p[0], p[1])
			}
		}
		Fprintln(out, ans)
	}
}

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

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

package main

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

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

	var T, n, m, v, w int
	var s string
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &m)
		fa := make([]int, n)
		for i := range fa {
			fa[i] = i
		}
		dis := make([]int, n)
		var find func(int) int
		find = func(x int) int {
			if fa[x] != x {
				ffx := find(fa[x])
				dis[x] ^= dis[fa[x]]
				fa[x] = ffx
			}
			return fa[x]
		}
		delta := func(x, y int) int {
			find(x)
			find(y)
			return dis[x] ^ dis[y]
		}
		merge := func(from, to, d int) bool {
			if fFrom, fTo := find(from), find(to); fFrom != fTo {
				dis[fFrom] = d ^ dis[to] ^ dis[from]
				fa[fFrom] = fTo
				return true
			}
			return delta(from, to) == d
		}
		ok := true
		for ; m > 0; m-- {
			Fscan(in, &v, &w, &s)
			if ok && !merge(v-1, w-1, int(s[1]&1)) {
				ok = false
			}
		}
		if !ok {
			Fprintln(out, -1)
			continue
		}
		cnt := make([][2]int, n)
		for i := range dis {
			cnt[find(i)][dis[i]]++
		}
		ans := 0
		for _, p := range cnt {
			if p[0] > 0 || p[1] > 0 {
				ans += max(p[0], p[1])
			}
		}
		Fprintln(out, ans)
	}
}

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

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

package main

import(
	"bufio"
	"fmt"
	"os"
)
const N int =4e5+5
var n,m int
var friend,unfriend,size [N]int
var input *bufio.Reader
func findRoot(u int) int {
	if friend[u]==u{
		return u
	}
	friend[u]=findRoot(friend[u])
	return friend[u]
}
func nhap(){
	fmt.Fscanf(input,"%d %d\n",&n,&m)
	for i:=1;i<=n;i++{
		friend[i]=i
		size[i]=1
		unfriend[i]=i+n
	}
	for i:=n+1;i<=2*n;i++{
		friend[i]=i
		size[i]=0
		unfriend[i]=i-n
	}
}
func union(u,v int){
	if(u==v){return}
	friend[v]=u
	size[u]+=size[v]
}
func lam(){
	var u,v,uu,uv int
	var s string
	var er bool=false
	for i:=1;i<=m;i++{
		fmt.Fscanf(input,"%d %d %s\n",&u,&v,&s)
		u=findRoot(u)
		v=findRoot(v)
		uu=findRoot(unfriend[u])
		uv=findRoot(unfriend[v])
		if(s[0]=='c'){
			if u==uv||v==uu{
				er=true
				continue
			}
			union(u,v)
			union(uu,uv)
		}else{
			if u==v||uv==uu{
				er=true
				continue
			}
			union(u,uv)
			union(uu,v)
		}
	}
	if(er==true){
		fmt.Println(-1)
	}else{
		ans:=0
		for i:=1;i<=2*n;i++{
			u=findRoot(i)
			uu=findRoot(unfriend[u])
			if(size[u]>size[uu]){
				ans=ans+size[u]
			}else{
				ans=ans+size[uu]
			}
			size[u]=0
			size[uu]=0
		}
		fmt.Println(ans)
	}
}
func main() {
	input=bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscanf(input,"%d\n",&t)
	for t>0{
		t--
		nhap()
		lam()
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)
var u []int
var v []int
var w []int
var first[]int
var nex[]int
var cnt int
var vis[]int
var ti int
var tc int
func add(x, y, q int){
	cnt++
	u[cnt]=x
	v[cnt]=y
	w[cnt]=q
	nex[cnt]=first[x]
	first[x]=cnt
}
func max(x, y int)int{
	if x>y{
		return x
	}
	return y
}
var contra bool
func search(x int, t int){
	if contra {
		return
	}
	if t==2{
		ti++
	}else{
		tc++
	}
	vis[x]=t
	for i:=first[x];i!=0;i=nex[i]{
		if vis[v[i]]!=0{
			if t==2{
				if w[i]==1||w[i]==-1{
					if vis[v[i]]==1{
						contra = true
					}
				}
				if w[i]==2||w[i]==-2{
					if vis[v[i]]==2{
						contra = true
					}
				}
			}
			if t==1{
				if w[i]==1||w[i]==-1{
					if vis[v[i]]==2{
						contra = true
					}
				}
				if w[i]==2||w[i]==-2{
					if vis[v[i]]==1{
						contra = true
					}
				}
			}
		}
		if vis[v[i]]==0{
			if t==2{
				if w[i]==1||w[i]==-1{
					search(v[i],2)
				}
				if w[i]==2||w[i]==-2{
					search(v[i],1)
				}
			}
			if t==1{
				if w[i]==1||w[i]==-1{
					search(v[i],1)
				}
				if w[i]==2||w[i]==-2{
					search(v[i],2)
				}
			}
		}
	}
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := os.Stdout
	var t int
	fmt.Fscan(in,&t)
	for c:=1;c<=t;c++{
		var n, m int
		fmt.Fscan(in,&n,&m)
		u = make([]int,m*5+15)
		v = make([]int,m*5+15)
		w = make([]int,m*5+15)
		first = make([]int,n*2+5)
		nex = make([]int, m*5+15)
		vis = make([]int,n*2+5)
		cnt = 0
		contra = false
		for i:=1;i<=m;i++{
			var x, y int
			var s string
			fmt.Fscan(in,&x,&y,&s)
			if s=="imposter"{
				add(x,y,2)
				add(y,x,-2)
			}else{
				add(x,y,1)
				add(y,x,-1)
			}
		}
		ans:=0
		for i:=1;i<=n;i++{
			if vis[i]==0{
				ti = 0
				tc = 0
				search(i,1)
				ans += max(ti,tc)
				if contra{
					break
				}
			}
		}
		if contra{
			ans = -1
		}
		fmt.Fprintln(out,ans)
	}
}

package main

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

type edge struct {
	v     int
	value bool
}

func main() {
	r := bufio.NewReaderSize(os.Stdin, 1024*1024)

	var T int
	fmt.Fscanf(r, "%d\n", &T)
	for t := 0; t < T; t++ {
		var n, m int
		fmt.Fscanf(r, "%d %d\n", &n, &m)

		in := make([][]edge, n+1)
		out := make([][]edge, n+1)

		for i := 0; i < m; i++ {
			var u, v int
			var s string
			fmt.Fscanf(r, "%d %d %s\n", &u, &v, &s)

			in[v] = append(in[v], edge{u, s == "crewmate"})
			out[u] = append(out[u], edge{v, s == "crewmate"})
		}

		seen := make([]int, n+1)
		val := make([]bool, n+1)
		res := 0

		var count func(u int, value bool, mark int) int
		count = func(u int, value bool, mark int) int {
			if seen[u] == mark {
				if value != val[u] {
					return -1
				}
				return 0
			}

			seen[u] = mark
			val[u] = value
			res := 0
			if !value {
				res++
			}

			for _, e := range in[u] {
				if value == e.value {
					a := count(e.v, true, mark)
					if a == -1 {
						return -1
					}
					res += a
				} else {
					a := count(e.v, false, mark)
					if a == -1 {
						return -1
					}
					res += a
				}
			}
			for _, e := range out[u] {
				if value {
					a := count(e.v, e.value, mark)
					if a == -1 {
						return -1
					}
					res += a
				} else {
					a := count(e.v, !e.value, mark)
					if a == -1 {
						return -1
					}
					res += a
				}
			}

			return res
		}

		for i := 1; i <= n; i++ {
			if seen[i] == 0 {
				x := count(i, true, 1)
				y := count(i, false, 2)

				if x == -1 && y == -1 {
					res = -1
					break
				}
				if x == -1 {
					res += y
				} else if y == -1 {
					res += x
				} else if x > y {
					res += x
				} else {
					res += y
				}
			}
		}

		fmt.Println(res)
	}
}

package main

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

type Edge struct {
	from, to int
	etype    int
}

func main() {
	in := bufio.NewReader(os.Stdin)
	//out := bufio.NewWriter(os.Stdout)
	var t int
	var n, m int
	fmt.Fscanf(in, "%d\n", &t)
	for i := 0; i < t; i++ {

		fmt.Fscanf(in, "%d %d\n", &n, &m)
		edges := make(map[int][]Edge)
		str := make([]byte, 20)
		for j := 0; j < m; j++ {
			var from, to int
			fmt.Fscanf(in, "%d %d %s\n", &from, &to, &str)
			newEdge := Edge{
				from: from,
				to:   to,
			}
			if string(str) == "imposter" {
				newEdge.etype = 0
			} else {
				newEdge.etype = 1
			}
			if _, ok := edges[from]; !ok {
				edges[from] = make([]Edge, 0)
			}
			edges[from] = append(edges[from], newEdge)
		}
		ans := 0
		color := make([]int, n+1)
		for j:=0;j <= n;j++{
			color[j] = 0
		}
		contradict := false
		for j := 1; contradict == false && j <= n; j++ {
			if color[j] != 0 {
				continue
			}
			visitCnt := 0
			aCnt := 0
			sf, stail := 0, 0
			stack := make([]int, 0)
			stack = append(stack, j)
			stail++
			visitCnt++
			color[j] = 1
			aCnt++
			for contradict == false && sf < stail {
				cur := stack[sf]
				tedges := edges[cur]
				for _, item := range tedges {
					toExpectedColor := color[cur]
					if item.etype == 0 {
						if toExpectedColor == 1 {
							toExpectedColor = 2
						} else {
							toExpectedColor = 1
						}
					}
					if color[item.to] != 0 {
						if color[item.to] != toExpectedColor {
							contradict = true
							break
						}
					} else {
						stack = append(stack, item.to)
						stail++
						visitCnt++
						color[item.to] = toExpectedColor
						if toExpectedColor == 1 {
							aCnt++
						}
					}
				}
				sf++
			}
			if aCnt > visitCnt-aCnt {
				ans = ans + aCnt
			} else {
				ans = ans + visitCnt - aCnt
			}

		}
		if contradict {
			fmt.Printf("-1\n")
		} else {
			fmt.Printf("%d\n", ans)
		}

	}

}
/*
33
2 2
1 2 imposter
1 2 crewmate

2 2
1 2 imposter
1 2 imposter

2 2
1 2 crewmate
1 2 crewmate
*/

package main

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

type Edge struct {
	from, to int
	etype    int
}

func main() {
	in := bufio.NewReader(os.Stdin)
	//out := bufio.NewWriter(os.Stdout)
	var t int
	var n, m int
	fmt.Fscanf(in, "%d\n", &t)
	for i := 0; i < t; i++ {

		fmt.Fscanf(in, "%d %d\n", &n, &m)
		edges := make(map[int][]Edge, n)
		str := make([]byte, 20)
		for j := 0; j < m; j++ {
			var from, to int
			fmt.Fscanf(in, "%d %d %s\n", &from, &to, &str)
			newEdge := Edge{
				from: from,
				to:   to,
			}
			if string(str) == "imposter" {
				newEdge.etype = 0
			} else {
				newEdge.etype = 1
			}
			if _, ok := edges[from]; !ok {
				edges[from] = make([]Edge, 0)
			}
			edges[from] = append(edges[from], newEdge)
		}
		ans := 0
		color := make([]int, n+1)
		contradict := false
		for j := 1; contradict == false && j <= n; j++ {
			if color[j] != 0 {
				continue
			}
			visitCnt := 0
			aCnt := 0
			sf, stail := 0, 0
			stack := make([]int, 0)
			stack = append(stack, j)
			stail++
			visitCnt++
			color[j] = 1
			aCnt++
			for contradict == false && sf < stail {
				cur := stack[sf]
				tedges := edges[cur]
				for _, item := range tedges {
					toExpectedColor := color[cur]
					if item.etype == 0 {
						if toExpectedColor == 1 {
							toExpectedColor = 2
						} else {
							toExpectedColor = 1
						}
					}
					if color[item.to] != 0 {
						if color[item.to] != toExpectedColor {
							contradict = true
							break
						}
					} else {
						stack = append(stack, item.to)
						stail++
						visitCnt++
						color[item.to] = toExpectedColor
						if toExpectedColor == 1 {
							aCnt++
						}
					}
				}
				sf++
			}
			if aCnt > visitCnt-aCnt {
				ans = ans + aCnt
			} else {
				ans = visitCnt - aCnt
			}

		}
		if contradict {
			fmt.Printf("-1\n")
		} else {
			fmt.Printf("%d\n", ans)
		}

	}

}

package main

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

var N, M int

const MOD int64 = 1000000007
const IMP = "imposter"
const CREW = "crewmate"

const IMPOSTER = 1
const CREWMATE = 2

var v []int

type comment struct {
	i, j int
	c string
}

var gr [][]*edge

var co []*comment

var seen []bool

var crew []int

type edge struct {
	from , to int
	crew bool
}

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)

	for i := 0; i < t; i++ {
		fmt.Fscan(r, &N, &M)

		co = make([]*comment, M)

		for k := 0; k < M; k++ {
			var i, j int
			var c string
			fmt.Fscan(r, &i, &j, &c)
			co[k] = &comment{i, j, c}
		}

		ans := solve()

		fmt.Printf(" Case #%d: %d", i + 1, ans)
		fmt.Println()
	}
}

func solve() int {
	//1
	gr = make([][]*edge, N + 1)

	// 0 - outgoing
	// 1 - incoming

	for i := 0; i < M; i++ {
		com := co[i]

		from := com.i
		to := com.j
		var v bool
		if com.c == IMP {
			v = false
		} else {
			v = true
		}

		gr[from] = append(gr[from], &edge{from, to, v})
		gr[to] = append(gr[to], &edge{from, to, v})
	}

	var global [2][]bool
	for i := 0; i < 2; i++ {
		global[i] = make([]bool, N)
	}

	//const IMPOSTER = 1
	//const CREWMATE = 2

	var components [3][]int

	for i := 1; i <= 2; i++ {
		crew = make([]int, N + 1)

		seen = make([]bool, N + 1)

		for j := 1; j <= N; j++ {
			if seen[j] {
				continue
			}

			s := dfs(j, -1, i)

			components[i] = append(components[i], s)
		}
	}

	var sum int

	for i := 0; i < len(components[1]); i++ {
		s1 := components[1][i]
		s2 := components[2][i]

		if s1 == -1  && s2 == -1 {
			return -1
		}

		sum += max(s1, s2)
	}

	return sum
}

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

func dfs(v int, parent int, mustbe int) int  {
	if seen[v] {
		role := crew[v]

		if role != mustbe {
			return -1
		}

		return 0
	}

	crew[v] = mustbe
	seen[v] = true

	edges := gr[v]
	var sum int

	wasBad := false

	for i := 0; i < len(edges); i++ {
		e := edges[i]

		if e.from == parent || e.to == parent {
			continue
		}

		from := e.from
		to := e.to
		isCrew := e.crew

		var a int

		if mustbe == IMPOSTER {
			if to == v {
				if isCrew {
					a = dfs(from, v, IMPOSTER)

				} else {
					a = dfs(from, v, CREWMATE)
				}

			} else {
				if isCrew {
					a = dfs(to, v, IMPOSTER)

				} else {
					a = dfs(to, v, CREWMATE)
				}
			}

		} else {
			if to == v {
				if isCrew {
					a = dfs(from, v, CREWMATE)
				} else {
					a = dfs(from, v, IMPOSTER)
				}

			} else {
				if isCrew {
					a = dfs(to, v, CREWMATE)
				} else {
					a = dfs(to, v, IMPOSTER)
				}
			}
		}

		if a == -1 {
			wasBad = true
			sum = -1

		} else {
			sum += a
		}
	}

	if wasBad {
		return -1
	}

	if mustbe == IMPOSTER {
		sum++
	}

	return sum
}