Consider 2n rows of the seats in a bus. n rows of the seats on the left and n rows of the seats on the right. Each row can be filled by two people. So the total capacity of the bus is 4n.Consider that m (m ≤ 4n) people occupy the seats in the bus. The passengers entering the bus are numbered from 1 to m (in the order of their entering the bus). The pattern of the seat occupation is as below:1-st row left window seat, 1-st row right window seat, 2-nd row left window seat, 2-nd row right window seat, ... , n-th row left window seat, n-th row right window seat.After occupying all the window seats (for m > 2n) the non-window seats are occupied:1-st row left non-window seat, 1-st row right non-window seat, ... , n-th row left non-window seat, n-th row right non-window seat.All the passengers go to a single final destination. In the final destination, the passengers get off in the given order.1-st row left non-window seat, 1-st row left window seat, 1-st row right non-window seat, 1-st row right window seat, ... , n-th row left non-window seat, n-th row left window seat, n-th row right non-window seat, n-th row right window seat.  The seating for n = 9 and m = 36. You are given the values n and m. Output m numbers from 1 to m, the order in which the passengers will get off the bus.

The only line contains two integers, n and m (1 ≤ n ≤ 100, 1 ≤ m ≤ 4n) — the number of pairs of rows and the number of passengers.

Print m distinct integers from 1 to m — the order in which the passengers will get off the bus.

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(), " "))
}


You are given a sequence $$$a$$$ consisting of $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, and an integer $$$x$$$. Your task is to make the sequence $$$a$$$ sorted (it is considered sorted if the condition $$$a_1 \le a_2 \le a_3 \le \dots \le a_n$$$ holds).To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $$$i$$$ such that $$$1 \le i \le n$$$ and $$$a_i > x$$$, and swap the values of $$$a_i$$$ and $$$x$$$.For example, if $$$a = [0, 2, 3, 5, 4]$$$, $$$x = 1$$$, the following sequence of operations is possible:  choose $$$i = 2$$$ (it is possible since $$$a_2 > x$$$), then $$$a = [0, 1, 3, 5, 4]$$$, $$$x = 2$$$;  choose $$$i = 3$$$ (it is possible since $$$a_3 > x$$$), then $$$a = [0, 1, 2, 5, 4]$$$, $$$x = 3$$$;  choose $$$i = 4$$$ (it is possible since $$$a_4 > x$$$), then $$$a = [0, 1, 2, 3, 4]$$$, $$$x = 5$$$. Calculate the minimum number of operations you have to perform so that $$$a$$$ becomes sorted, or report that it is impossible.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. Each test case consists of two lines. The first line contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 500$$$, $$$0 \le x \le 500$$$) — the number of elements in the sequence and the initial value of $$$x$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 \le a_i \le 500$$$). The sum of values of $$$n$$$ over all test cases in the input does not exceed $$$500$$$.

For each test case, print one integer — the minimum number of operations you have to perform to make $$$a$$$ sorted, or $$$-1$$$, if it is impossible.

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

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:]) {
				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
}


Polycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him — his keyboard will consist of only one row, where all $$$26$$$ lowercase Latin letters will be arranged in some order.Polycarp uses the same password $$$s$$$ on all websites where he is registered (it is bad, but he doesn't care). He wants to assemble a keyboard that will allow to type this password very easily. He doesn't like to move his fingers while typing the password, so, for each pair of adjacent characters in $$$s$$$, they should be adjacent on the keyboard. For example, if the password is abacaba, then the layout cabdefghi... is perfect, since characters a and c are adjacent on the keyboard, and a and b are adjacent on the keyboard. It is guaranteed that there are no two adjacent equal characters in $$$s$$$, so, for example, the password cannot be password (two characters s are adjacent).Can you help Polycarp with choosing the perfect layout of the keyboard, if it is possible?

The first line contains one integer $$$T$$$ ($$$1 \le T \le 1000$$$) — the number of test cases. Then $$$T$$$ lines follow, each containing one string $$$s$$$ ($$$1 \le |s| \le 200$$$) representing the test case. $$$s$$$ consists of lowercase Latin letters only. There are no two adjacent equal characters in $$$s$$$.

For each test case, do the following:   if it is impossible to assemble a perfect keyboard, print NO (in upper case, it matters in this problem);  otherwise, print YES (in upper case), and then a string consisting of $$$26$$$ lowercase Latin letters — the perfect layout. Each Latin letter should appear in this string exactly once. If there are multiple answers, print any of them.

YES
bacdefghijklmnopqrstuvwxyz
YES
edocabfghijklmnpqrstuvwxyz
NO
YES
xzytabcdefghijklmnopqrsuvw
NO

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"
)

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


Theofanis started playing the new online game called "Among them". However, he always plays with Cypriot players, and they all have the same name: "Andreas" (the most common name in Cyprus).In each game, Theofanis plays with $$$n$$$ other players. Since they all have the same name, they are numbered from $$$1$$$ to $$$n$$$.The players write $$$m$$$ comments in the chat. A comment has the structure of "$$$i$$$ $$$j$$$ $$$c$$$" where $$$i$$$ and $$$j$$$ are two distinct integers and $$$c$$$ is a string ($$$1 \le i, j \le n$$$; $$$i \neq j$$$; $$$c$$$ is either imposter or crewmate). The comment means that player $$$i$$$ said that player $$$j$$$ has the role $$$c$$$.An imposter always lies, and a crewmate always tells the truth. Help Theofanis find the maximum possible number of imposters among all the other Cypriot players, or determine that the comments contradict each other (see the notes for further explanation).Note that each player has exactly one role: either imposter or crewmate.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Description of each test case follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$0 \le m \le 5 \cdot 10^5$$$) — the number of players except Theofanis and the number of comments. Each of the next $$$m$$$ lines contains a comment made by the players of the structure "$$$i$$$ $$$j$$$ $$$c$$$" where $$$i$$$ and $$$j$$$ are two distinct integers and $$$c$$$ is a string ($$$1 \le i, j \le n$$$; $$$i \neq j$$$; $$$c$$$ is either imposter or crewmate). There can be multiple comments for the same pair of $$$(i, j)$$$. It is guaranteed that the sum of all $$$n$$$ does not exceed $$$2 \cdot 10^5$$$ and the sum of all $$$m$$$ does not exceed $$$5 \cdot 10^5$$$.

For each test case, print one integer — the maximum possible number of imposters. If the comments contradict each other, print $$$-1$$$.

5
3 2
1 2 imposter
2 3 crewmate
5 4
1 3 crewmate
2 5 crewmate
2 4 imposter
3 4 imposter
2 2
1 2 imposter
2 1 crewmate
3 5
1 2 imposter
1 2 imposter
3 2 crewmate
3 2 crewmate
1 3 imposter
5 0

NoteIn the first test case, imposters can be Andreas $$$2$$$ and $$$3$$$.In the second test case, imposters can be Andreas $$$1$$$, $$$2$$$, $$$3$$$ and $$$5$$$.In the third test case, comments contradict each other. This is because player $$$1$$$ says that player $$$2$$$ is an imposter, and player $$$2$$$ says that player $$$1$$$ is a crewmate. If player $$$1$$$ is a crewmate, then he must be telling the truth, so player $$$2$$$ must be an imposter. But if player $$$2$$$ is an imposter then he must be lying, so player $$$1$$$ can't be a crewmate. Contradiction.

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


Nowadays the one-way traffic is introduced all over the world in order to improve driving safety and reduce traffic jams. The government of Berland decided to keep up with new trends. Formerly all n cities of Berland were connected by n two-way roads in the ring, i. e. each city was connected directly to exactly two other cities, and from each city it was possible to get to any other city. Government of Berland introduced one-way traffic on all n roads, but it soon became clear that it's impossible to get from some of the cities to some others. Now for each road is known in which direction the traffic is directed at it, and the cost of redirecting the traffic. What is the smallest amount of money the government should spend on the redirecting of roads so that from every city you can get to any other?

The first line contains integer n (3 ≤ n ≤ 100) — amount of cities (and roads) in Berland. Next n lines contain description of roads. Each road is described by three integers ai, bi, ci (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ ci ≤ 100) — road is directed from city ai to city bi, redirecting the traffic costs ci.

Output single integer — the smallest amount of money the government should spend on the redirecting of roads so that from every city you can get to any other.

package main

import (
	"fmt"
	"io/ioutil"
	"bytes"
	"os"
)
type Cost struct {
	Next, Cost int
}
var edge0 []*Cost
var edge1 []*Cost

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)

	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	edge0 := make([]*Cost, n, n)
	edge1 := make([]*Cost, n, n)
	for i := 0;i < n;i ++ {
		var a,b,c int
		fmt.Fscanf(reader, "%d %d %d\n", &a, &b, &c)
		a --
		b --
		if edge0[a] == nil {
			edge0[a] = &Cost {Next: b, Cost: 0}
		} else {
			edge1[a] = &Cost {Next: b, Cost: 0}
		}
		if edge0[b] == nil {
			edge0[b] = &Cost {Next: a, Cost: c}
		} else {
			edge1[b] = &Cost {Next: a, Cost: c}
		}

	}
	prev, cur := 0, edge0[0].Next
	cost0 := edge0[0].Cost
	for cur != 0 {
		if prev != edge0[cur].Next {
			cost0 += edge0[cur].Cost
			prev = cur
			cur = edge0[cur].Next
		} else {
			cost0 += edge1[cur].Cost
			prev = cur
			cur = edge1[cur].Next
		}
	}
	prev, cur = 0, edge1[0].Next
	cost1 := edge1[0].Cost
	for cur != 0 {
		if prev != edge0[cur].Next {
			cost1 += edge0[cur].Cost
			prev = cur
			cur = edge0[cur].Next
		} else {
			cost1 += edge1[cur].Cost
			prev = cur
			cur = edge1[cur].Next
		}
	}
	if cost0 < cost1 {
		fmt.Printf("%d", cost0)
	} else {
		fmt.Printf("%d", cost1)
	}
}

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

var graph = map[int]map[int]int{}

func augment(u, v, c int) {
  _, found := graph[u]
  if !found {
    graph[u] = map[int]int{}
  }
  graph[u][v] = c
}

func traverse(start, u, v int) int {
  cost := graph[u][v]
  if v == start {
    return cost
  }
  neighbors := graph[v]
  for w := range neighbors {
    if w != u {
      return cost + traverse(start, v, w)
    }
  }
  return cost
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  n := getI()
  for i := 0; i < n; i++ {
    u, v, c := getI(), getI(), getI()
    augment(u, v, 0)
    augment(v, u, c)
  }
  neighbors := graph[1]
  result := -1
  for v := range neighbors {
    cost := traverse(1, 1, v)
    if result == -1 || cost < result {
      result = cost
    }
  }
  writer.WriteString(fmt.Sprintf("%d\n", result))
}

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  s := getS()
  n := len(s)
  max := 0
  seen := map[string]bool{}
  for i := 0; i < n; i++ {
    for j := i+1; j <= n; j++ {
      if seen[s[i:j]] && j-i > max {
        max = j-i
      }
      seen[s[i:j]] = true
    }
  }
  writer.WriteString(fmt.Sprintf("%d\n", max))
}


You are given string s. Let's call word any largest sequence of consecutive symbols without symbols ',' (comma) and ';' (semicolon). For example, there are four words in string "aba,123;1a;0": "aba", "123", "1a", "0". A word can be empty: for example, the string s=";;" contains three empty words separated by ';'.You should find all words in the given string that are nonnegative INTEGER numbers without leading zeroes and build by them new string a. String a should contain all words that are numbers separating them by ',' (the order of numbers should remain the same as in the string s). By all other words you should build string b in the same way (the order of numbers should remain the same as in the string s).Here strings "101", "0" are INTEGER numbers, but "01" and "1.0" are not.For example, for the string aba,123;1a;0 the string a would be equal to "123,0" and string b would be equal to "aba,1a".

The only line of input contains the string s (1 ≤ |s| ≤ 105). The string contains only symbols '.' (ASCII 46), ',' (ASCII 44), ';' (ASCII 59), digits, lowercase and uppercase latin letters.

Print the string a to the first line and string b to the second line. Each string should be surrounded by quotes (ASCII 34). If there are no words that are numbers print dash (ASCII 45) on the first line. If all words are numbers print dash on the second line.

NoteIn the second example the string s contains five words: "1", "", "01", "a0", "".

package main

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

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

func Scanf(args ...interface{}) {
	fmt.Fscanln(in, args...)
}

func outs(ints []string) {
	f := bufio.NewWriter(os.Stdout)
	defer f.Flush()
	if li := len(ints); li > 0 {
		f.WriteString("\"")
		for i, _ := range ints {
			f.WriteString(ints[i])
			if i != li-1 {
				f.WriteString(",")
			}
		}
		f.WriteString("\"\n")
	} else {
		f.WriteString("-\n")
	}
}

func main() {
	var s string
	Scanf(&s)
	l := len(s)
	strs := make([]string, 0, 10000)
	ints := make([]string, 0, 10000)
	for i := 0; i < l; {
		isInt := true
		start := i
		for i < l && s[i] != ',' && s[i] != ';' {
			if s[i] < '0' || s[i] > '9' {
				isInt = false
			}
			i++
		}
		wl := i - start

		var apStr string
		if start == i {
			apStr = string(s[start:i])
			isInt = false
		} else {
			if s[start] == '0' && wl > 1 {
				isInt = false
			}
			apStr = string(s[start:i])
		}

		if isInt {
			ints = append(ints, apStr)
		} else {
			strs = append(strs, apStr)
		}
		if i == l-1 && (s[i] == ',' || s[i] == ';') {
			strs = append(strs, "")
		}
		i++
	}

	outs(ints)
	outs(strs)
}

package main

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

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

func Scanf(args ...interface{}) {
	fmt.Fscanln(in, args...)
}

func outs(ints []string) {
	if li := len(ints); li > 0 {
		fmt.Print("\"")
		for i, _ := range ints {
			fmt.Print(ints[i])
			if i != li-1 {
				fmt.Print(",")
			}
		}
		fmt.Print("\"\n")
	} else {
		fmt.Println("-")
	}

}

func main() {
	var s string
	Scanf(&s)
	l := len(s)
	strs := make([]string, 0, 10000)
	ints := make([]string, 0, 10000)
	for i := 0; i < l; {
		isInt := true
		start := i
		for i < l && s[i] != ',' && s[i] != ';' {
			if s[i] < '0' || s[i] > '9' {
				isInt = false
			}
			i++
		}
		wl := i - start

		var apStr string
		if start == i {
			apStr = string(s[start:i])
			isInt = false
		} else {
			if s[start] == '0' && wl > 1 {
				isInt = false
			}
			apStr = string(s[start:i])
		}

		if isInt {
			ints = append(ints, apStr)
		} else {
			strs = append(strs, apStr)
		}
		if i == l-1 && (s[i] == ',' || s[i] == ';') {
			strs = append(strs, "")
		}
		i++
	}

	outs(ints)
	outs(strs)
}

package main

import "fmt"

func main() {
	var s, a, b string
	fmt.Scan(&s)
	a, b = short(s)
	printResult(a)
	printResult(b)
}

func short(s string) (string, string) {
	var a, b, tmp string
	for _, r := range s {
		if r == ';' || r == ',' {
			if isInteger(tmp) {
				a = a + tmp + ","
			} else {
				b = b + tmp + ","
			}
			tmp = ""
		} else {
			tmp = tmp + string(r)
		}
	}
	if isInteger(tmp) {
		a = a + tmp + ","
	} else {
		b = b + tmp + ","
	}
	return a, b

}

func isInteger(w string) bool {
	if len(w) == 0 {
		return false
	}
	if len(w) > 1 && w[0] == '0' {
		return false
	}
	for _, r := range w {
		if byte(r) < 48 || byte(r) > 57 {
			return false
		}
	}
	return true
}

func printResult(a string) {
	if len(a) == 0 {
		a = "-"
	} else {
		a = `"` + a[:len(a)-1] + `"`
	}
	fmt.Println(a)
}

func split(s string) []string {
	var res []string
	var tmp string
	for _, r := range s {
		if r == ';' || r == ',' {
			res = append(res, tmp)
			tmp = ""
		} else {
			tmp = tmp + string(r)
		}
	}
	res = append(res, tmp)
	return res
}

package main

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

func isInt(s string) bool {
	if len(s) == 0 {
		return false
	}
	if (len(s) > 1 && s[0] == 48) || s[0] == 45 {
		return false
	}

	for idx := range s {
		if s[idx] < 48 || s[idx] > 57 {
			return false
		}
	}
	return true
}

func main() {
	//var in strings.Builder
	//byteList := []byte{44, 46, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
	//	80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113,
	//	114, 115, 116, 117, 118, 119, 120, 121, 122}
	//for i := 0; i < int(math.Pow(10, 5)); i++ {
	//	idx := rand.Intn(len(byteList))
	//	in.WriteByte(byteList[idx])
	//}
	//outF, err := os.Create("input.txt")
	//if err != nil {
	//	panic(err)
	//}
	//outF.WriteString(in.String())
	//outF.Close()
	//
	//f, err := os.Open("input.txt")
	//if err != nil {
	//	panic(err)
	//}
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]byte, int(math.Pow(10, 6))), int(math.Pow(10,6)))

	var ints strings.Builder
	var others strings.Builder
	var intCount int
	var othersCount int
	var curr strings.Builder

	scanner.Scan()
	s := scanner.Text()

	for idx := range s {
		if s[idx] == 44 || s[idx] == 59 {
			currStr := curr.String()
			if isInt(currStr) {
				intCount += 1
				ints.WriteString(currStr)
				ints.WriteByte(44)
			} else {
				othersCount += 1
				others.WriteString(currStr)
				others.WriteByte(44)
			}
			curr.Reset()
		} else {
			curr.WriteByte(s[idx])
		}
	}
	currStr := curr.String()
	if isInt(currStr) {
		intCount += 1
		ints.WriteString(currStr)
		ints.WriteByte(44)
	} else {
		othersCount += 1
		others.WriteString(currStr)
		others.WriteByte(44)
	}

	intsStr := ints.String()
	if len(intsStr) > 0 {
		intsStr = intsStr[:len(intsStr)-1]
	}

	othersStr := others.String()
	if len(othersStr) > 0 {
		othersStr = othersStr[:len(othersStr)-1]
	}

	if intCount == 0 {
		intsStr = "-"
	} else {
		intsStr = "\"" + intsStr + "\""
	}

	if othersCount == 0 {
		othersStr = "-"
	} else {
		othersStr = "\"" + othersStr + "\""
	}

	fmt.Println(intsStr)
	fmt.Println(othersStr)
}

package main

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

func isNumber(str *string) bool{
	if len(*str) == 0{
		return false
	}
	if len(*str) == 1 && (*str)[0] == '0'{
		return true
	}
	if (*str)[0] == '0'{
		return false
	}
	for _, x:= range (*str){
		if x < '0' || x > '9'{
			return false
		}
	}
	return true
}

const(
	n = int(1e5 + 5)
)
func filter(input chan *string, intOut chan *string, strOut chan *string){
	for str := range input{
		if isNumber(str){
			intOut <- str
		} else {
			strOut <- str
		}
	}
	close(intOut)
	close(strOut)
}

func main(){
	rs := bufio.NewReader(os.Stdin)
	ws := bufio.NewWriter(os.Stdout)

	input, _ := rs.ReadString('\n')
	input = strings.TrimSpace(input)

	// parsed := strings.FieldsFunc(input, func(a rune) bool {return a == ';' || a == ','})
	parsed1 := strings.Split(input, ",")
	parsed	:= make([]string, 0, n)

	for i := range parsed1{
		if len(parsed1[i]) > 0{
			parsed2 := strings.Split(parsed1[i], ";")
			parsed = append(parsed, parsed2...)
		} else {
			parsed = append(parsed, parsed1[i])
		}
	}

	ints := make(chan *string)
	strs := make(chan *string)
	inputs := make(chan *string)

	go filter(inputs, ints, strs)

	go func (){
		for i := range parsed{
			inputs <- &parsed[i]
		}
		close(inputs)
	}()

	intString := make([]byte, 0, n)
	strString := make([]byte, 0, n)

	for {
		select {
		case x, ok := <-ints:
			if !ok {
				ints = nil
			} else {
				intString = append(intString, ',')
				intString = append(intString, []byte(*x)...)
			}

		case x, ok := <-strs:
			if !ok {
				strs = nil
			} else {
				strString = append(strString, ',')
				strString = append(strString, []byte(*x)...)
			}
		}

		if ints == nil && strs == nil {
			break
		}
	}

	if len(intString) > 0{
		ws.WriteString("\"" + string(intString[1:]) + "\"\n")
	} else {
		ws.WriteString("-\n")
	}

	if len(strString) > 0{
		ws.WriteString("\"" + string(strString[1:]) + "\"\n")
	} else {
		ws.WriteString("-\n")
	}

	ws.Flush()
}

package main

import (
	"fmt"
)

func main() {
	var s string
	fmt.Scan(&s)
	parse(s)
}

func parse(s string) {
	words := split(s)

	once := false
	for _, w := range words {
		if isNumber(w) {
			if once {
				fmt.Print(",")
			} else {
				fmt.Print(`"`)
			}
			fmt.Print(w)
			once = true
		}
	}
	if !once {
		fmt.Println("-")
	} else {
		fmt.Println(`"`)
	}

	once = false
	for _, w := range words {
		if !isNumber(w) {
			if once {
				fmt.Print(",")
			} else {
				fmt.Print(`"`)
			}
			fmt.Print(w)
			once = true
		}
	}
	if !once {
		fmt.Println("-")
	} else {
		fmt.Println(`"`)
	}
}

func split(s string) []string {
	var result []string

	last := 0
	for i, r := range s {
		if r == ';' || r == ',' {
			result = append(result, s[last:i])
			last = i + 1
		}
	}
	result = append(result, s[last:])
	return result
}

func isNumber(w string) bool {
	if len(w) < 1 {
		return false
	}
	bad := false

	for i, r := range w {
		switch r {
		case '0':
			if bad {
				return false
			}
			if i == 0 {
				bad = true
			}
		case '1', '2', '3', '4', '5', '6', '7', '8', '9':
			if bad {
				return false
			}
			continue
		default:
			return false
		}
	}
	return true
}

package main

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

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

func Scanf(args ...interface{}) {
	fmt.Fscanln(in, args...)
}

func outs(ints []string) {
	f := bufio.NewWriter(os.Stdout)
	defer f.Flush()
	if li := len(ints); li > 0 {
		f.WriteString("\"")
		for i, _ := range ints {
			f.WriteString(ints[i])
			if i != li-1 {
				f.WriteString(",")
			}
		}
		f.WriteString("\"\n")
	} else {
		f.WriteString("-")
	}
}

func main() {
	var s string
	Scanf(&s)
	l := len(s)
	strs := make([]string, 0, 10000)
	ints := make([]string, 0, 10000)
	for i := 0; i < l; {
		isInt := true
		start := i
		for i < l && s[i] != ',' && s[i] != ';' {
			if s[i] < '0' || s[i] > '9' {
				isInt = false
			}
			i++
		}
		wl := i - start

		var apStr string
		if start == i {
			apStr = string(s[start:i])
			isInt = false
		} else {
			if s[start] == '0' && wl > 1 {
				isInt = false
			}
			apStr = string(s[start:i])
		}

		if isInt {
			ints = append(ints, apStr)
		} else {
			strs = append(strs, apStr)
		}
		if i == l-1 && (s[i] == ',' || s[i] == ';') {
			strs = append(strs, "")
		}
		i++
	}

	outs(ints)
	outs(strs)
}

package main

import (
	"fmt"
	"strconv"
)

func main() {
	var s, a, b string
	fmt.Scan(&s)
	ws := split(s)
	for _, w := range ws {
		if i, err := strconv.Atoi(w); err != nil || i < 0 || (i != 0 && w[0] == '0') {
			b = b + w + ","
		} else {
			a = a + w + ","
		}
	}

	if len(a) == 0 {
		a = "-"
	} else {
		a = a[:len(a)-1]
	}

	if len(b) == 0 {
		b = "-"
	} else {
		b = b[:len(b)-1]
	}
	fmt.Println("\"" + a + "\"")
	fmt.Println("\"" + b + "\"")
}

func split(s string) []string {
	var res []string
	var tmp string
	for _, r := range s {
		if r == ';' || r == ',' {
			res = append(res, tmp)
			tmp = ""
		} else {
			tmp = tmp + string(r)
		}
	}
	res = append(res, tmp)
	return res
}

package main

import (
	"fmt"
	"strconv"
)

func main() {
	var s, a, b string
	fmt.Scan(&s)
	ws := split(s)
	for _, w := range ws {
		if isInteger(w) {
			a = a + w + ","
		} else {
			b = b + w + ","
		}
	}
	printResult(a)
	printResult(b)
}

func isInteger(w string) bool {
	if len(w) == 0 {
		return false
	}
	if len(w) > 0 && (w[0] == '0' || w[0] == '-') {
		return false
	}
	for _, r := range w {
		if _, err := strconv.Atoi(string(r)); err != nil {
			return false
		}
	}
	return true
}

func printResult(a string) {
	if len(a) == 0 {
		a = "-"
	} else {
		a = `"` + a[:len(a)-1] + `"`
	}
	fmt.Println(a)
}

func split(s string) []string {
	var res []string
	var tmp string
	for _, r := range s {
		if r == ';' || r == ',' {
			res = append(res, tmp)
			tmp = ""
		} else {
			tmp = tmp + string(r)
		}
	}
	res = append(res, tmp)
	return res
}

package main

import (
	"fmt"
	"strconv"
)

func main() {
	var s, a, b string
	fmt.Scan(&s)
	ws := split(s)
	for _, w := range ws {
		if i, err := strconv.Atoi(w); err != nil || i < 0 || (i != 0 && w[0] == '0') {
			b = b + w + ","
		} else {
			a = a + w + ","
		}
	}

	if len(a) == 0 {
		a = "-"
	} else {
		a = `"` + a[:len(a)-1] + `"`
	}

	if len(b) == 0 {
		b = "-"
	} else {
		b = `"` + b[:len(b)-1] + `"`
	}
	fmt.Println(a)
	fmt.Println(b)
}

func split(s string) []string {
	var res []string
	var tmp string
	for _, r := range s {
		if r == ';' || r == ',' {
			res = append(res, tmp)
			tmp = ""
		} else {
			tmp = tmp + string(r)
		}
	}
	res = append(res, tmp)
	return res
}

package main

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

func isInt(s string) bool {
	if len(s) == 0 {
		return false
	}
	if (len(s) > 1 && s[0] == 48) || s[0] == 45 {
		return false
	}

	for idx := range s {
		if s[idx] < 48 || s[idx] > 57 {
			return false
		}
	}
	return true
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)

	scanner.Scan()
	s := scanner.Text()

	var ints strings.Builder
	var others strings.Builder
	var curr strings.Builder
	for idx := range s {
		if s[idx] == 44 || s[idx] == 59 {
			currStr := curr.String()
			if isInt(currStr) {
				ints.WriteString(currStr)
				ints.WriteByte(44)
			} else {
				others.WriteString(currStr)
				others.WriteByte(44)
			}
			curr.Reset()
		} else {
			curr.WriteByte(s[idx])
		}
	}
	currStr := curr.String()
	if isInt(currStr) {
		ints.WriteString(currStr)
		ints.WriteByte(44)
	} else {
		others.WriteString(currStr)
		others.WriteByte(44)
	}

	intsStr := ints.String()
	if len(intsStr) > 0 {
		intsStr = intsStr[:len(intsStr)-1]
	}

	othersStr := others.String()
	if len(othersStr) > 0 {
		othersStr = othersStr[:len(othersStr)-1]
	}

	if len(intsStr) == 0 {
		intsStr = "-"
	} else {
		intsStr = "\"" + intsStr + "\""
	}

	if len(othersStr) == 0 {
		othersStr = "-"
	} else {
		othersStr = "\"" + othersStr + "\""
	}

	fmt.Println(intsStr)
	fmt.Println(othersStr)

}

package main

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

func isInt(s string) bool {
	if len(s) == 0 {
		return false
	}
	if (len(s) > 1 && s[0] == 48) || s[0] == 45 {
		return false
	}

	for idx := range s {
		if s[idx] < 48 || s[idx] > 57 {
			return false
		}
	}
	return true
}

func main() {
	//var in strings.Builder
	//byteList := []byte{44, 46, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
	//	80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113,
	//	114, 115, 116, 117, 118, 119, 120, 121, 122}
	//for i := 0; i < int(math.Pow(10, 5)); i++ {
	//	idx := rand.Intn(len(byteList))
	//	in.WriteByte(byteList[idx])
	//}
	//outF, err := os.Create("input.txt")
	//if err != nil {
	//	panic(err)
	//}
	//outF.WriteString(in.String())
	//outF.Close()
	//
	//f, err := os.Open("input.txt")
	//if err != nil {
	//	panic(err)
	//}
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]byte, int(math.Pow(10, 6))), int(math.Pow(10,6)))

	var ints strings.Builder
	var others strings.Builder
	var curr strings.Builder

	scanner.Scan()
	s := scanner.Text()

	fmt.Println(s)
	for idx := range s {
		if s[idx] == 44 || s[idx] == 59 {
			currStr := curr.String()
			if isInt(currStr) {
				ints.WriteString(currStr)
				ints.WriteByte(44)
			} else {
				others.WriteString(currStr)
				others.WriteByte(44)
			}
			curr.Reset()
		} else {
			curr.WriteByte(s[idx])
		}
	}
	currStr := curr.String()
	if isInt(currStr) {
		ints.WriteString(currStr)
		ints.WriteByte(44)
	} else {
		others.WriteString(currStr)
		others.WriteByte(44)
	}

	intsStr := ints.String()
	if len(intsStr) > 0 {
		intsStr = intsStr[:len(intsStr)-1]
	}

	othersStr := others.String()
	if len(othersStr) > 0 {
		othersStr = othersStr[:len(othersStr)-1]
	}

	if len(intsStr) == 0 {
		intsStr = "-"
	} else {
		intsStr = "\"" + intsStr + "\""
	}

	if len(othersStr) == 0 {
		othersStr = "-"
	} else {
		othersStr = "\"" + othersStr + "\""
	}

	fmt.Println(intsStr)
	fmt.Println(othersStr)
}

package main

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

func isInt(s string) bool {
	if len(s) == 0 {
		return false
	}
	if (len(s) > 1 && s[0] == 48) || s[0] == 45 {
		return false
	}

	for idx := range s {
		if s[idx] < 48 || s[idx] > 57 {
			return false
		}
	}
	return true
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)

	var ints strings.Builder
	var others strings.Builder
	var curr strings.Builder

	for scanner.Scan() {
		s := scanner.Text()

		for idx := range s {
			if s[idx] == 44 || s[idx] == 59 {
				currStr := curr.String()
				if isInt(currStr) {
					ints.WriteString(currStr)
					ints.WriteByte(44)
				} else {
					others.WriteString(currStr)
					others.WriteByte(44)
				}
				curr.Reset()
			} else {
				curr.WriteByte(s[idx])
			}
		}
		currStr := curr.String()
		if isInt(currStr) {
			ints.WriteString(currStr)
			ints.WriteByte(44)
		} else {
			others.WriteString(currStr)
			others.WriteByte(44)
		}

		intsStr := ints.String()
		if len(intsStr) > 0 {
			intsStr = intsStr[:len(intsStr)-1]
		}

		othersStr := others.String()
		if len(othersStr) > 0 {
			othersStr = othersStr[:len(othersStr)-1]
		}

		if len(intsStr) == 0 {
			intsStr = "-"
		} else {
			intsStr = "\"" + intsStr + "\""
		}

		if len(othersStr) == 0 {
			othersStr = "-"
		} else {
			othersStr = "\"" + othersStr + "\""
		}

		fmt.Println(intsStr)
		fmt.Println(othersStr)
	}
}

package main

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

func isInt(s string) bool {
	if len(s) == 0 {
		return false
	}
	if (len(s) > 1 && s[0] == 48) || s[0] == 45 {
		return false
	}

	for idx := range s {
		if s[idx] < 48 || s[idx] > 57 {
			return false
		}
	}
	return true
}

func main() {
	//var in strings.Builder
	//byteList := []byte{44, 46, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
	//	80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113,
	//	114, 115, 116, 117, 118, 119, 120, 121, 122}
	//for i := 0; i < int(math.Pow(10, 5)); i++ {
	//	idx := rand.Intn(len(byteList))
	//	in.WriteByte(byteList[idx])
	//}
	//outF, err := os.Create("input.txt")
	//if err != nil {
	//	panic(err)
	//}
	//outF.WriteString(in.String())
	//outF.Close()
	//
	//f, err := os.Open("input.txt")
	//if err != nil {
	//	panic(err)
	//}
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]byte, int(math.Pow(10, 6))), int(math.Pow(10,6)))

	var ints strings.Builder
	var others strings.Builder
	var curr strings.Builder

	scanner.Scan()
	s := scanner.Text()

	for idx := range s {
		if s[idx] == 44 || s[idx] == 59 {
			currStr := curr.String()
			if isInt(currStr) {
				ints.WriteString(currStr)
				ints.WriteByte(44)
			} else {
				others.WriteString(currStr)
				others.WriteByte(44)
			}
			curr.Reset()
		} else {
			curr.WriteByte(s[idx])
		}
	}
	currStr := curr.String()
	if isInt(currStr) {
		ints.WriteString(currStr)
		ints.WriteByte(44)
	} else {
		others.WriteString(currStr)
		others.WriteByte(44)
	}

	intsStr := ints.String()
	if len(intsStr) > 0 {
		intsStr = intsStr[:len(intsStr)-1]
	}

	othersStr := others.String()
	if len(othersStr) > 0 {
		othersStr = othersStr[:len(othersStr)-1]
	}

	if len(intsStr) == 0 {
		intsStr = "-"
	} else {
		intsStr = "\"" + intsStr + "\""
	}

	if len(othersStr) == 0 {
		othersStr = "-"
	} else {
		othersStr = "\"" + othersStr + "\""
	}

	fmt.Println(intsStr)
	fmt.Println(othersStr)
}