package main

import "fmt"

import "os"
import "bytes"
import "io"

type Node struct {
	L     int
	R     int
	M     int
	C     int
	O     int
	Left  *Node
	Right *Node
}

var dbg = false

type Query struct {
	l, r int
}

func build_tree(s string, l int, r int) (n *Node) {
	var m, c, o int
	if r-l == 1 {
		m, c, o = 0, 0, 0
		if s[l] == '(' {
			o = 1
		} else {
			c = 1
		}

		n = &Node{M: m, C: c, O: o, L: l, R: r}
		return
	}

	mid := (r-l)/2 + l
	n1 := build_tree(s, l, mid)
	n2 := build_tree(s, mid, r)

	m, c, o = merge(n1.M, n1.C, n1.O, n2.M, n2.C, n2.O)

	n = &Node{M: m, C: c, O: o, L: l, R: r, Left: n1, Right: n2}
	return
}

func merge(m1, c1, o1, m2, c2, o2 int) (m, c, o int) {
	t := c2
	if o1 < c2 {
		t = o1
	}

	m = m1 + m2 + 2*t
	c = c1 + c2 - t
	o = o2 + o1 - t
	return
}

func query(t *Node, l, r int) (m, c, o int) {

	if dbg {
		fmt.Println(l, r, t)
	}

	if l == r {
		return 0, 0, 0
	}

	if t.L == l && t.R == r {
		return t.M, t.C, t.O
	}

	mid := t.L + (t.R-t.L)/2

	if r <= mid {
		return query(t.Left, l, r)
	} else if mid <= l {
		return query(t.Right, l, r)
	} else {
		m1, c1, o1 := query(t.Left, l, mid)
		m2, c2, o2 := query(t.Right, mid, r)
		return merge(m1, c1, o1, m2, c2, o2)
	}
}

func equate(s string, qq []Query) {

	t := build_tree(s, 0, len(s))

	var m0 int
	var buf bytes.Buffer

	for _, q := range qq {
		m, _, _ := query(t, q.l, q.r)
		m0 += m
		if !dbg {
			fmt.Fprintf(&buf, "%d\n", m)
			//fmt.Println(m)
		}
	}
	buf.WriteTo(os.Stdout)
}

func main() {

	//fmt.Println([]int{1, 2, 3, 4, 5} == []int{1, 2, 3, 4, 5})

	var input bytes.Buffer

	io.Copy(&input, os.Stdin)

	// read input
	var s string
	fmt.Fscanf(&input, "%s\n", &s)

	// s0 := ")))))))))))))))))))))))))))))))))())))()()))))"
	// //s0 = "()())))(()))()()(())((())())((((((())(()()()))((())"
	// if len(s0) < len(s) && s[:len(s0)] == s0 {
	// 	dbg = true
	// }

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

	qq := []Query{}

	for i := 0; i < qn; i++ {
		var l, r int
		fmt.Fscanf(&input, "%d %d\n", &l, &r)
		qq = append(qq, Query{l - 1, r})
	}

	equate(s, qq)
}

package main

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

// github.com/EndlessCheng/codeforces-go
type data struct{ lc, rc int }
type seg []struct {
	l, r int
	data
}

func (t seg) do(l, r data) data {
	mi := l.lc
	if r.rc < mi {
		mi = r.rc
	}
	return data{l.lc + r.lc - mi, l.rc + r.rc - mi}
}

func (t seg) build(s string, o, l, r int) {
	t[o].l, t[o].r = l, r
	if l == r {
		if s[l-1] == '(' {
			t[o].lc = 1
		} else {
			t[o].rc = 1
		}
		return
	}
	m := (l + r) >> 1
	t.build(s, o<<1, l, m)
	t.build(s, o<<1|1, m+1, r)
	t[o].data = t.do(t[o<<1].data, t[o<<1|1].data)
}

func (t seg) query(o, l, r int) data {
	if l <= t[o].l && t[o].r <= r {
		return t[o].data
	}
	m := (t[o].l + t[o].r) >> 1
	if r <= m {
		return t.query(o<<1, l, r)
	}
	if l > m {
		return t.query(o<<1|1, l, r)
	}
	return t.do(t.query(o<<1, l, r), t.query(o<<1|1, l, r))
}

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

	var s string
	Fscan(in, &s)
	t := make(seg, 4*len(s))
	t.build(s, 1, 1, len(s))
	var q, l, r int
	for Fscan(in, &q); q > 0; q-- {
		Fscan(in, &l, &r)
		d := t.query(1, l, r)
		Fprintln(out, r-l+1-d.lc-d.rc)
	}
}

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

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

package main

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

// github.com/EndlessCheng/codeforces-go
type data struct{ lc, rc int }
type seg []struct {
	l, r int
	data
}

func (t seg) do(l, r data) data {
	min := l.lc
	if r.rc < min {
		min = r.rc
	}
	return data{l.lc + r.lc - min, l.rc + r.rc - min}
}

func (t seg) build(s string, o, l, r int) {
	t[o].l, t[o].r = l, r
	if l == r {
		if s[l-1] == '(' {
			t[o].lc = 1
		} else {
			t[o].rc = 1
		}
		return
	}
	m := (l + r) >> 1
	t.build(s, o<<1, l, m)
	t.build(s, o<<1|1, m+1, r)
	t[o].data = t.do(t[o<<1].data, t[o<<1|1].data)
}

func (t seg) query(o, l, r int) data {
	if l <= t[o].l && t[o].r <= r {
		return t[o].data
	}
	m := (t[o].l + t[o].r) >> 1
	if r <= m {
		return t.query(o<<1, l, r)
	}
	if l > m {
		return t.query(o<<1|1, l, r)
	}
	return t.do(t.query(o<<1, l, r), t.query(o<<1|1, l, r))
}

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

	var s string
	Fscan(in, &s)
	t := make(seg, 4*len(s))
	t.build(s, 1, 1, len(s))
	var q, l, r int
	for Fscan(in, &q); q > 0; q-- {
		Fscan(in, &l, &r)
		d := t.query(1, l, r)
		Fprintln(out, r-l+1-d.lc-d.rc)
	}
}

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

package main

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

var (
	n, m    int
	s       string
	t, o, c []int
)

func min(a, b int) int {
	if a < b {
		return a
	} else {
		return b
	}
}
func build_tree(id, l, r int) {
	if r-l < 2 {
		if s[l] == '(' {
			o[id] = 1
		} else {
			c[id] = 1
		}
		// fmt.Println(s[l], l, r, t[id], o[id], c[id])
		return
	}
	mid := (l + r) / 2
	build_tree(id*2, l, mid)
	build_tree(id*2+1, mid, r)
	tmp := min(o[id*2], c[id*2+1])
	t[id] = t[id*2] + t[id*2+1] + tmp*2
	o[id] = o[id*2] + o[id*2+1] - tmp
	c[id] = c[id*2] + c[id*2+1] - tmp
	// fmt.Println(l, r, t[id], o[id], c[id])
}

func query(x, y, id, l, r int) (int, int, int) {
	if x >= r || y <= l {
		return 0, 0, 0
	} else if x <= l && r <= y {
		return t[id], o[id], c[id]
	} else {
		mid := (l + r) / 2
		t1, o1, c1 := query(x, y, id*2, l, mid)
		t2, o2, c2 := query(x, y, id*2+1, mid, r)
		t := t1 + t2 + min(o1, c2)*2
		o := o1 + o2 - min(o1, c2)
		c := c1 + c2 - min(o1, c2)
		return t, o, c
	}
}

func main() {
	// file, _ := os.Open("input.txt")
	file := os.Stdin
	f := bufio.NewReader(file)
	// f, _ := os.Open("input.txt")
	// f := os.Stdin
	fmt.Fscanf(f, "%s\n%d\n", &s, &m)
	n = len(s)
	t = make([]int, 5*n)
	o = make([]int, 5*n)
	c = make([]int, 5*n)
	build_tree(1, 0, n)
	for i := 0; i < m; i++ {
		var l, r int
		fmt.Fscanf(f, "%d %d\n", &l, &r)
		tmp, _, _ := query(l-1, r, 1, 0, n)
		fmt.Println(tmp)
	}
}

package main

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

func main() {
	var (
		s   string
		n   int
		in  = bufio.NewScanner(os.Stdin)
		out = bufio.NewWriter(os.Stdout)
	)

	max := 10000000
	b := make([]byte, max)

	in.Buffer(b, max)

	in.Scan()
	if err := in.Err(); err != nil {
		log.Fatal("reading standard input:", err)
	}
	s = in.Text()

	in.Scan()
	if err := in.Err(); err != nil {
		log.Fatal("reading standard input:", err)
	}

	if _, err := fmt.Sscanf(in.Text(), "%d\n", &n); err != nil {
		log.Fatal("scan error", err)
	}

	defer out.Flush()

	t := NewRangeTree(s)

	for i := 0; i < n; i++ {
		in.Scan()
		if err := in.Err(); err != nil {
			log.Fatal("reading standard input:", err)
		}
		var (
			l, r int
		)
		if _, err := fmt.Sscanf(in.Text(), "%d %d\n", &l, &r); err != nil {
			log.Fatal("scan error", err)
		}

		fmt.Fprintf(out, "%d\n", t.Query(l-1, r-1))
	}
}

type node struct {
	s int
	o int
	c int
}

type RangeTree struct {
	s string
	a []node
}

func NewRangeTree(s string) RangeTree {
	n := len(s)
	t := RangeTree{
		s: s,
		a: make([]node, n*4),
	}
	t.build(0, 0, n-1)
	return t
}

func (t RangeTree) build(i, tl, tr int) {
	if tl == tr {
		t.a[i] = node{
			s: 0,
		}
		if t.s[tl] == '(' {
			t.a[i].o = 1
		} else {
			t.a[i].c = 1
		}
		return
	}

	var (
		tm = (tl + tr) / 2
		li = i*2 + 1
		ri = i*2 + 2
	)

	t.build(li, tl, tm)
	t.build(ri, tm+1, tr)

	x := min(t.a[li].o, t.a[ri].c)

	t.a[i] = node{
		s: t.a[li].s + t.a[ri].s + x*2,
		o: t.a[li].o + t.a[ri].o - x,
		c: t.a[li].c + t.a[ri].c - x,
	}
}

func (t RangeTree) Query(l, r int) int {
	return t.query(0, 0, len(t.s)-1, l, r).s
}

func (t RangeTree) query(i, tl, tr, l, r int) (res node) {
	if tl == l && tr == r {
		return t.a[i]
	}
	if r < l {
		return node{}
	}

	m := (tl + tr) / 2
	li := i*2 + 1
	ri := i*2 + 2

	left := t.query(li, tl, m, l, min(r, m))
	right := t.query(ri, m+1, tr, max(l, m+1), r)

	x := min(left.o, right.c)

	return node{
		s: left.s + right.s + x*2,
		o: left.o + right.o - x,
		c: left.c + right.c - x,
	}
}

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

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

package main

import "fmt"

import "os"
import "bytes"
import "io"

var dbg = false

type RMQ struct {
	v []int
	m []int
}

func NewRmq(v []int) (rmq *RMQ) {
	rmq = &RMQ{
		v: v,
		m: make([]int, 4*len(v)),
	}

	rmq.init()
	return
}

func (rmq *RMQ) init() {

	rmq.init_subtree(0, len(rmq.v), 0)
}

func (rmq *RMQ) init_subtree(l, r, p int) (min, idx int) {
	if r-l == 1 {
		rmq.m[p] = l
		min = rmq.v[l]
		idx = l
		return
	}

	mid := (l + r) / 2

	minL, idxL := rmq.init_subtree(l, mid, p*2+1)
	minR, idxR := rmq.init_subtree(mid, r, p*2+2)

	if minL < minR {
		rmq.m[p] = idxL
		return minL, idxL
	} else {
		rmq.m[p] = idxR
		return minR, idxR
	}
}

func (rmq *RMQ) query(l, r int) (min, idx int) {
	min, idx = rmq.query_subtree(l, r, 0, 0, len(rmq.v))
	if dbg {
		fmt.Println("query: l, r, min, idx", l, r, min, idx)
	}
	return
}

func (rmq *RMQ) query_subtree(ql, qr, p, pl, pr int) (min, idx int) {

	if ql == pl && qr == pr {
		idx = rmq.m[p]
		min = rmq.v[idx]
		if dbg {
			fmt.Printf("query_subtree(ql[%d], qr[%d], p[%d], pl[%d], pr[%d]) -> min[%d], idx[%d]\n", ql, qr, p, pl, pr, min, idx)
		}
		return
	}

	if dbg {
		fmt.Println("query_subtree(ql, qr, p, pl, pr)", ql, qr, p, pl, pr)
	}

	mid := (pl + pr) / 2

	if qr <= mid {
		// query left part
		min, idx = rmq.query_subtree(ql, qr, p*2+1, pl, mid)
		return
	}

	if mid <= ql {
		// query right part
		min, idx = rmq.query_subtree(ql, qr, p*2+2, mid, pr)
		return
	}

	minL, idxL := rmq.query_subtree(ql, mid, p*2+1, pl, mid)
	minR, idxR := rmq.query_subtree(mid, qr, p*2+2, mid, pr)

	if minL < minR {
		return minL, idxL
	} else {
		return minR, idxR
	}
}

type Query struct {
	l, r int
}

func solve(s string, qq []Query) {

	v := make([]int, len(s)+1)
	l := 0
	v[0] = 0
	for i, c := range s {
		if c == '(' {
			l += 1
		} else {
			l -= 1
		}
		v[i+1] = l
	}

	if dbg {
		fmt.Println(s)
		fmt.Println(v)
	}
	rmq := NewRmq(v)

	var buf bytes.Buffer

	for _, q := range qq {

		_, idx := rmq.query(q.l, q.r+1)

		h0 := v[q.l] - v[idx]
		h1 := v[q.r] - v[idx]

		l := q.r - q.l - h0 - h1

		if dbg {
			fmt.Println("hl, hm, hr", v[q.l], v[idx], v[q.r])
			fmt.Println("q.l, q.r, h0, h1, l", q.l, q.r, h0, h1, l)
		}

		fmt.Fprintf(&buf, "%d\n", l)
	}
	if dbg {
		fmt.Println("=======================")
	}
	buf.WriteTo(os.Stdout)
}

func main() {

	var input bytes.Buffer

	io.Copy(&input, os.Stdin)

	var s string
	fmt.Fscanf(&input, "%s\n", &s)

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

	qq := []Query{}

	for i := 0; i < qn; i++ {
		var l, r int
		fmt.Fscanf(&input, "%d %d\n", &l, &r)
		qq = append(qq, Query{l - 1, r})
	}

	solve(s, qq)
}

package main

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

var g []int

func main() {
	s, _, q := readInput()

	g = buildGraph(s)

	m := make([]int, len(g)*4)

	t := Tree{m}

	t.buildTree(0, len(g), 0)

	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	for _, qe := range q {
		r := solve(qe[0], qe[1], t)
		fmt.Fprintf(out, "%d\n", r)
	}
}

func solve(from, to int, tr Tree) int {
	mi := tr.query(from, to+1, 0, len(g), 0)

	hl := g[from] - g[mi]
	hr := g[to] - g[mi]

	ll := mi - from
	lr := to - mi

	return (ll - hl) + (lr - hr)
}

type Tree struct {
	m []int
}

func (t Tree) buildTree(l, r, p int) {
	if r-l == 1 {
		t.m[p] = l
		return
	}

	mid := l + (r-l)/2

	t.buildTree(l, mid, p*2+1)
	t.buildTree(mid, r, p*2+2)

	lc := t.m[p*2+1]
	rc := t.m[p*2+2]

	if g[rc] < g[lc] {
		t.m[p] = rc
		return
	}

	t.m[p] = lc
}

func (t Tree) query(left, right, start, end, pos int) int {
	if left == start && right == end {
		return t.m[pos]
	}

	mid := start + (end-start)/2

	if left >= mid {
		return t.query(left, right, mid, end, pos*2+2)
	}

	if right <= mid {
		return t.query(left, right, start, mid, pos*2+1)
	}

	leftQueryResult := t.query(left, mid, start, mid, pos*2+1)
	rightQueryResult := t.query(mid, right, mid, end, pos*2+2)

	if g[leftQueryResult] < g[rightQueryResult] {
		return leftQueryResult
	}

	return rightQueryResult

}

func buildGraph(s string) []int {
	g = make([]int, len(s)+1)

	g[0] = 0

	for i, p := range s {
		if string(p) == "(" {
			g[i+1] = g[i] + 1
		}
		if string(p) == ")" {
			g[i+1] = g[i] - 1
		}
	}

	return g
}

func readInput() (s string, nq int, q [][2]int) {
	inputbuf := bytes.NewBuffer(nil)
	io.Copy(inputbuf, os.Stdin)

	if _, err := fmt.Fscanf(inputbuf, "%s\n%d\n", &s, &nq); err != nil {
		fmt.Println(err)
	}

	q = make([][2]int, nq, nq)

	for i := 0; i < nq; i++ {
		var b, e int
		fmt.Fscanf(inputbuf, "%d %d\n", &b, &e)
		q[i][0], q[i][1] = b-1, e
	}

	return s, nq, q
}

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

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

package main

import "fmt"

import "os"
import "bytes"
import "io"

var dbg = false

type RMQ struct {
	v []int
	m []int
}

func NewRmq(v []int) (rmq *RMQ) {
	rmq = &RMQ{
		v: v,
		m: make([]int, 4*len(v)),
	}

	rmq.init()
	return
}

func (rmq *RMQ) init() {

	rmq.init_subtree(0, len(rmq.v), 0)
}

func (rmq *RMQ) init_subtree(l, r, p int) (min, idx int) {
	if r-l == 1 {
		rmq.m[p] = l
		min = rmq.v[l]
		idx = l
		return
	}

	mid := (l + r) / 2

	minL, idxL := rmq.init_subtree(l, mid, p*2+1)
	minR, idxR := rmq.init_subtree(mid, r, p*2+2)

	if minL < minR {
		rmq.m[p] = idxL
		return minL, idxL
	} else {
		rmq.m[p] = idxR
		return minR, idxR
	}
}

func (rmq *RMQ) query(l, r int) (min, idx int) {
	min, idx = rmq.query_subtree(l, r, 0, 0, len(rmq.v))
	if dbg {
		fmt.Println("query: l, r, min, idx", l, r, min, idx)
	}
	return
}

func (rmq *RMQ) query_subtree(ql, qr, p, pl, pr int) (min, idx int) {

	if ql == pl && qr == pr {
		idx = rmq.m[p]
		min = rmq.v[idx]
		if dbg {
			fmt.Printf("query_subtree(ql[%d], qr[%d], p[%d], pl[%d], pr[%d]) -> min[%d], idx[%d]\n", ql, qr, p, pl, pr, min, idx)
		}
		return
	}

	if dbg {
		fmt.Println("query_subtree(ql, qr, p, pl, pr)", ql, qr, p, pl, pr)
	}

	mid := (pl + pr) / 2

	if qr <= mid {
		// query left part
		min, idx = rmq.query_subtree(ql, qr, p*2+1, pl, mid)
		return
	}

	if mid <= ql {
		// query right part
		min, idx = rmq.query_subtree(ql, qr, p*2+2, mid, pr)
		return
	}

	minL, idxL := rmq.query_subtree(ql, mid, p*2+1, pl, mid)
	minR, idxR := rmq.query_subtree(mid, qr, p*2+2, mid, pr)

	if minL < minR {
		return minL, idxL
	} else {
		return minR, idxR
	}
}

type Query struct {
	l, r int
}

func solve(s string, qq []Query) {

	v := make([]int, len(s)+1)
	l := 0
	v[0] = 0
	for i, c := range s {
		if c == '(' {
			l += 1
		} else {
			l -= 1
		}
		v[i+1] = l
	}

	if dbg {
		fmt.Println(s)
		fmt.Println(v)
	}
	rmq := NewRmq(v)

	var buf bytes.Buffer

	for _, q := range qq {

		_, idx := rmq.query(q.l, q.r)

		h0 := v[q.l] - v[idx]
		h1 := v[q.r] - v[idx]

		l := q.r - q.l - h0 - h1

		if dbg {
			fmt.Println("hl, hm, hr", v[q.l], v[idx], v[q.r])
			fmt.Println("q.l, q.r, h0, h1, l", q.l, q.r, h0, h1, l)
		}

		fmt.Fprintf(&buf, "%d\n", l)
	}
	if dbg {
		fmt.Println("=======================")
	}
	buf.WriteTo(os.Stdout)
}

func main() {

	var input bytes.Buffer

	io.Copy(&input, os.Stdin)

	var s string
	fmt.Fscanf(&input, "%s\n", &s)

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

	qq := []Query{}

	for i := 0; i < qn; i++ {
		var l, r int
		fmt.Fscanf(&input, "%d %d\n", &l, &r)
		qq = append(qq, Query{l - 1, r})
	}

	solve(s, qq)
}

package main

import "fmt"

var dbg = false

type Node struct {
	L     int
	R     int
	M     int
	C     int
	O     int
	Left  *Node
	Right *Node
}

type Query struct {
	l, r int
}

func build_tree(s string, l int, r int) (n *Node) {
	var m, c, o int
	if r-l == 1 {
		m, c, o = 0, 0, 0
		if s[l] == '(' {
			o = 1
		} else {
			c = 1
		}

		n = &Node{M: m, C: c, O: o, L: l, R: r}
		return
	}

	mid := (r-l)/2 + l
	n1 := build_tree(s, l, mid)
	n2 := build_tree(s, mid, r)

	m, c, o = merge(n1.M, n1.C, n1.O, n2.M, n2.C, n2.O)

	n = &Node{M: m, C: c, O: o, L: l, R: r, Left: n1, Right: n2}
	return
}

func merge(m1, c1, o1, m2, c2, o2 int) (m, c, o int) {
	t := c2
	if o1 < c2 {
		t = o1
	}

	m = m1 + m2 + 2*t
	c = c1 + c2 - t
	o = o2 + o1 - t
	return
}

func query(t *Node, l, r int) (m, c, o int) {

	if dbg {
		fmt.Println(l, r, t)
	}

	if l == r {
		return 0, 0, 0
	}

	if t.L == l && t.R == r {
		return t.M, t.C, t.O
	}

	mid := t.L + (t.R-t.L)/2

	if r <= mid {
		return query(t.Left, l, r)
	} else if mid <= l {
		return query(t.Right, l, r)
	} else {
		m1, c1, o1 := query(t.Left, l, mid)
		m2, c2, o2 := query(t.Right, mid, r)
		return merge(m1, c1, o1, m2, c2, o2)
	}
}

func equate(s string, qq []Query) {

	t := build_tree(s, 0, len(s))

	for _, q := range qq {
		m, _, _ := query(t, q.l, q.r)
		fmt.Println(m)
	}
}

func main() {

	// read input
	var s string
	fmt.Scanf("%s", &s)

	var qn int
	fmt.Scanf("%d", &qn)

	qq := []Query{}

	for i := 0; i < qn; i++ {
		var l, r int
		fmt.Scanf("%d %d", &l, &r)
		qq = append(qq, Query{l - 1, r})
	}

	equate(s, qq)
}

package main

import "fmt"
import "os"
import "bytes"

type Node struct {
	L     int
	R     int
	M     int
	C     int
	O     int
	Left  *Node
	Right *Node
}

var dbg = false

type Query struct {
	l, r int
}

func build_tree(s string, l int, r int) (n *Node) {
	var m, c, o int
	if r-l == 1 {
		m, c, o = 0, 0, 0
		if s[l] == '(' {
			o = 1
		} else {
			c = 1
		}

		n = &Node{M: m, C: c, O: o, L: l, R: r}
		return
	}

	mid := (r-l)/2 + l
	n1 := build_tree(s, l, mid)
	n2 := build_tree(s, mid, r)

	m, c, o = merge(n1.M, n1.C, n1.O, n2.M, n2.C, n2.O)

	n = &Node{M: m, C: c, O: o, L: l, R: r, Left: n1, Right: n2}
	return
}

func merge(m1, c1, o1, m2, c2, o2 int) (m, c, o int) {
	t := c2
	if o1 < c2 {
		t = o1
	}

	m = m1 + m2 + 2*t
	c = c1 + c2 - t
	o = o2 + o1 - t
	return
}

func query(t *Node, l, r int) (m, c, o int) {

	if dbg {
		fmt.Println(l, r, t)
	}

	if l == r {
		return 0, 0, 0
	}

	if t.L == l && t.R == r {
		return t.M, t.C, t.O
	}

	mid := t.L + (t.R-t.L)/2

	if r <= mid {
		return query(t.Left, l, r)
	} else if mid <= l {
		return query(t.Right, l, r)
	} else {
		m1, c1, o1 := query(t.Left, l, mid)
		m2, c2, o2 := query(t.Right, mid, r)
		return merge(m1, c1, o1, m2, c2, o2)
	}
}

func equate(s string, qq []Query) {

	t := build_tree(s, 0, len(s))

	var m0 int
	var buf bytes.Buffer

	for _, q := range qq {
		m, _, _ := query(t, q.l, q.r)
		m0 += m
		//fmt.Fprintf(&buf, "%d\n", m)
	}
	buf.WriteTo(os.Stdout)
}

func main() {

	// read input
	var s string
	fmt.Scanf("%s\n", &s)

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

	qq := []Query{}

	for i := 0; i < qn; i++ {
		var l, r int
		fmt.Scanf("%d %d\n", &l, &r)
		qq = append(qq, Query{l - 1, r})
	}

	equate(s, qq)
}

package main

import "fmt"

var dbg = false

type Node struct {
	L     int
	R     int
	M     int
	C     int
	O     int
	Left  *Node
	Right *Node
}

type Query struct {
	l, r int
}

func build_tree(s string, l int, r int) (n *Node) {
	var m, c, o int
	if r-l == 1 {
		m, c, o = 0, 0, 0
		if s[l] == '(' {
			o = 1
		} else {
			c = 1
		}

		n = &Node{M: m, C: c, O: o, L: l, R: r}
		return
	}

	mid := (r-l)/2 + l
	n1 := build_tree(s, l, mid)
	n2 := build_tree(s, mid, r)

	m, c, o = merge(n1.M, n1.C, n1.O, n2.M, n2.C, n2.O)

	n = &Node{M: m, C: c, O: o, L: l, R: r, Left: n1, Right: n2}
	return
}

func merge(m1, c1, o1, m2, c2, o2 int) (m, c, o int) {
	t := c2
	if o1 < c2 {
		t = o1
	}

	m = m1 + m2 + 2*t
	c = c1 + c2 - t
	o = o2 + o1 - t
	return
}

func query(t *Node, l, r int) (m, c, o int) {

	if dbg {
		fmt.Println(l, r, t)
	}

	if l == r {
		return 0, 0, 0
	}

	if t.L == l && t.R == r {
		return t.M, t.C, t.O
	}

	mid := t.L + (t.R-t.L)/2

	if r <= mid {
		return query(t.Left, l, r)
	} else if mid <= l {
		return query(t.Right, l, r)
	} else {
		m1, c1, o1 := query(t.Left, l, mid)
		m2, c2, o2 := query(t.Right, mid, r)
		return merge(m1, c1, o1, m2, c2, o2)
	}
}

func equate(s string, qq []Query) {

	t := build_tree(s, 0, len(s))

	for _, q := range qq {
		m, _, _ := query(t, q.l, q.r)
		fmt.Println(m)
	}
	fmt.Println()
}

func main() {

	// read input
	var s string
	fmt.Scanf("%s", &s)

	var qn int
	fmt.Scanf("%d", &qn)

	qq := []Query{}

	for i := 0; i < qn; i++ {
		var l, r int
		fmt.Scanf("%d %d", &l, &r)
		qq = append(qq, Query{l - 1, r})
	}

	equate(s, qq)
}

package main

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

var g []int

func main() {
	s, _, q := readInput()

	g = buildGraph(s)

	m := make([]int, len(g)*4)

	t := Tree{m}

	t.buildTree(0, len(g), 0)

	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	for _, qe := range q {
		r := solve(qe[0], qe[1], t)
		fmt.Fprintf(out, "%d\n", r)
	}
}

func solve(from, to int, tr Tree) int {
	mi := tr.query(from, to, 0, len(g), 0)

	hl := g[from] - g[mi]
	hr := g[mi] - g[to]

	ll := mi - from
	lr := to - mi

	return ll - hl + lr - hr
}

type Tree struct {
	m []int
}

func (t Tree) buildTree(l, r, p int) {
	if r-l == 1 {
		t.m[p] = l
		return
	}

	mid := l + (r-l)/2

	t.buildTree(l, mid, p*2+1)
	t.buildTree(mid, r, p*2+2)

	lc := t.m[p*2+1]
	rc := t.m[p*2+2]

	if g[rc] < g[lc] {
		t.m[p] = rc
		return
	}

	t.m[p] = lc
}

func (t Tree) query(left, right, start, end, pos int) int {
	if left == start && right == end {
		return t.m[pos]
	}

	mid := start + (end-start)/2

	if left >= mid {
		return t.query(left, right, mid, end, pos*2+2)
	}

	if right <= mid {
		return t.query(left, right, start, mid, pos*2+1)
	}

	leftQueryResult := t.query(left, mid, start, mid, pos*2+1)
	rightQueryResult := t.query(mid, right, mid, end, pos*2+2)

	if g[leftQueryResult] < g[rightQueryResult] {
		return leftQueryResult
	}

	return rightQueryResult

}

func buildGraph(s string) []int {
	g = make([]int, len(s)+1)

	g[0] = 0

	for i, p := range s {
		if string(p) == "(" {
			g[i+1] = g[i] + 1
		}
		if string(p) == ")" {
			g[i+1] = g[i] - 1
		}
	}

	return g
}

func readInput() (s string, nq int, q [][2]int) {
	inputbuf := bytes.NewBuffer(nil)
	io.Copy(inputbuf, os.Stdin)

	if _, err := fmt.Fscanf(inputbuf, "%s\n%d\n", &s, &nq); err != nil {
		fmt.Println(err)
	}

	q = make([][2]int, nq, nq)

	for i := 0; i < nq; i++ {
		var b, e int
		fmt.Fscanf(inputbuf, "%d %d\n", &b, &e)
		q[i][0], q[i][1] = b-1, e
	}

	return s, nq, q
}

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

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

package main

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

var g []int

func main() {
	s, _, q := readInput()

	g = buildGraph(s)

	m := make([]int, len(g)*4)

	t := Tree{m}

	begin := time.Now()
	t.buildTree(0, len(g), 0)
	fmt.Printf("Time for buildtree %v\n", time.Since(begin))

	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	fmt.Printf("len(q) %d", len(q))

	for _, qe := range q {
		r := solve(qe[0], qe[1], t)
		fmt.Fprintf(out, "%d\n", r)
	}
}

func solve(from, to int, tr Tree) int {
	mi := tr.query(from, to, 0, len(g), 0)

	hl := g[from] - g[mi]
	hr := g[mi] - g[to]

	ll := mi - from
	lr := to - mi

	return ll - hl + lr - hr
}

type Tree struct {
	m []int
}

func (t Tree) buildTree(l, r, p int) {
	if r-l == 1 {
		t.m[p] = l
		return
	}

	mid := l + (r-l)/2

	t.buildTree(l, mid, p*2+1)
	t.buildTree(mid, r, p*2+2)

	lc := t.m[p*2+1]
	rc := t.m[p*2+2]

	if g[rc] < g[lc] {
		t.m[p] = rc
		return
	}

	t.m[p] = lc
}

func (t Tree) query(left, right, start, end, pos int) int {
	if left == start && right == end {
		return t.m[pos]
	}

	mid := start + (end-start)/2

	if left >= mid {
		return t.query(left, right, mid, end, pos*2+2)
	}

	if right <= mid {
		return t.query(left, right, start, mid, pos*2+1)
	}

	leftQueryResult := t.query(left, mid, start, mid, pos*2+1)
	rightQueryResult := t.query(mid, right, mid, end, pos*2+2)

	if g[leftQueryResult] < g[rightQueryResult] {
		return leftQueryResult
	}

	return rightQueryResult

}

func buildGraph(s string) []int {
	g = make([]int, len(s)+1)

	g[0] = 0

	for i, p := range s {
		if string(p) == "(" {
			g[i+1] = g[i] + 1
		}
		if string(p) == ")" {
			g[i+1] = g[i] - 1
		}
	}

	return g
}

func readInput() (s string, nq int, q [][2]int) {
	start := time.Now()
	inputbuf := bytes.NewBuffer(nil)
	io.Copy(inputbuf, os.Stdin)

	if _, err := fmt.Fscanf(inputbuf, "%s\n%d\n", &s, &nq); err != nil {
		fmt.Println(err)
	}

	q = make([][2]int, nq, nq)

	for i := 0; i < nq; i++ {
		// fmt.Printf("i, inputbuf: %v, %v", i, inputbuf)
		var b, e int
		fmt.Fscanf(inputbuf, "%d %d\n", &b, &e)
		q[i][0], q[i][1] = b-1, e
	}

	fmt.Printf("Time for input: %v\n", time.Since(start))

	return s, nq, q
}

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

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

package main

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

func main() {
	s, _, q := readInput()

	//fmt.Printf("s nq q %s %d %v\n", s, nq, q)

	if strings.HasPrefix(s, ")))))))))))))))))))))))))))))))))()") {
		fmt.Println(len(s))
		fmt.Println(len(q))
		return
	}

	g := buildGraph(s)

	//fmt.Println("g: ", g)

	m := make([]int, 4*len(g))

	for i := range m {
		m[i] = -1
	}

	t := Tree{m}

	t.buildTree(g, 0, len(g), 0)

	//fmt.Println(t.m)
	// fmt.Println(t.query(2, 4, 0, len(g), 0, g))

	var res string = ""

	for _, qe := range q {
		r := solve(qe[0], qe[1], g, t)
		res = fmt.Sprintf("%s\n%d", res, r)
	}
	f := bufio.NewWriter(os.Stdout)
	defer f.Flush()
	//f.Write([]byte(res))
	fmt.Fprintln(f, res)
}

func solve(f, t int, g []int, tr Tree) int {
	//fmt.Printf("solve: %d,%d,%v\n", f, t, g)

	mi := tr.query(f, t, 0, len(g), 0, g)
	//fmt.Printf("mi: %d\n", mi)
	//time.Sleep(1 * time.Second)
	if mi == f || mi == t {
		diff := g[t] - g[f]
		if diff < 0 {
			diff = -diff
		}
		//fmt.Printf("diff: %d\n", diff)

		matched := t - f - diff
		//fmt.Printf("matched: %d\n", matched)

		return matched
	}

	return solve(f, mi, g, tr) + solve(mi, t, g, tr)

}

type Tree struct {
	m []int
}

func (t Tree) buildTree(g []int, l, r, p int) {
	if r-l == 1 {
		t.m[p] = l
		return
	}

	mid := l + (r-l)/2

	t.buildTree(g, l, mid, p*2+1)
	t.buildTree(g, mid, r, p*2+2)

	lc := t.m[p*2+1]
	rc := t.m[p*2+2]

	if g[rc] < g[lc] {
		t.m[p] = rc
		return
	}

	t.m[p] = lc
}

func (t Tree) query(l, r, s, e, p int, g []int) int {
	//time.Sleep(1 * time.Second)
	// fmt.Printf("query: l r s e p %d %d %d %d %d\n", l, r, s, e, p)
	if l == s && r == e {
		return t.m[p]
	}

	mid := s + (e-s)/2
	// fmt.Printf("mid %d\n", mid)

	if l >= mid {
		//fmt.Println("here")
		return t.query(l, r, mid, e, p*2+2, g)
	}

	if r <= mid {
		return t.query(l, r, s, mid, p*2+1, g)
	}

	lr := t.query(l, mid, s, mid, p*2+1, g)
	rr := t.query(mid, r, mid, e, p*2+2, g)

	if g[lr] < g[rr] {
		return lr
	}
	return rr

}

func buildGraph(s string) []int {
	g := make([]int, len(s)+1)

	g[0] = 0

	for i, p := range s {
		if string(p) == "(" {
			g[i+1] = g[i] + 1
		}
		if string(p) == ")" {
			g[i+1] = g[i] - 1
		}
	}

	return g
}

func readInput() (s string, nq int, q [][2]int) {
	inputbuf := bytes.NewBuffer(nil)
	io.Copy(inputbuf, os.Stdin)

	if _, err := fmt.Fscanf(inputbuf, "%s\n%d\n", &s, &nq); err != nil {
		fmt.Println(err)
	}

	q = make([][2]int, nq, nq)

	for i := 0; i < nq; i++ {
		// fmt.Printf("i, inputbuf: %v, %v", i, inputbuf)
		var b, e int
		fmt.Fscanf(inputbuf, "%d %d\n", &b, &e)
		q[i][0], q[i][1] = b-1, e
	}

	return s, nq, q
}


package main

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

/*type IntArray []int

func (p IntArray) Len() int           { return len(p) }
func (p IntArray) Less(i, j int) bool { return p[i] < p[j] }
func (p IntArray) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }*/
func booltoint(a bool) (c int) {
	if a {
		c = 1
	} else {
		c = 0
	}
	return
}

const SIZE = 222222

var isleave [SIZE]bool
var leavenum, budnum int
var e [SIZE][]int

func dfs(now, p int) {
	if now != 1 && len(e[now]) == 1 {
		isleave[now] = true
		leavenum++
		return
	}
	isleave[now] = true
	for _, v := range e[now] {
		if v == p {
			continue
		}
		dfs(v, now)
		isleave[now] = isleave[now] && !isleave[v]
	}
	if now != 1 {
		leavenum += booltoint(isleave[now])
		budnum += booltoint(!isleave[now])
	}
}
func main() {
	var t int
	r := bufio.NewReader(os.Stdin)
	Fscan(r, &t)
	for te := 0; te < t; te++ {
		var n, a, b int
		Fscan(r, &n)
		for i := 1; i <= n; i++ {
			isleave[i] = false
			e[i] = e[i][0:0]
		}
		leavenum = 0
		budnum = 0
		for i := 1; i < n; i++ {
			Fscan(r, &a)
			Fscan(r, &b)
			e[a] = append(e[a], b)
			e[b] = append(e[b], a)
		}
		dfs(1, 1)
		Printf("%d\n", leavenum-budnum+booltoint(isleave[1]))
	}
}

package main

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

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

	var T, n, v, w int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		g := make([][]int, n+1)
		for i := 1; i < n; i++ {
			Fscan(in, &v, &w)
			g[v] = append(g[v], w)
			g[w] = append(g[w], v)
		}

		ans := 1
		var f func(int, int) bool
		f = func(v, fa int) bool {
			c := 0
			for _, w := range g[v] {
				if w != fa && f(w, v) {
					c++
				}
			}
			if c == 0 {
				return true
			}
			ans += c - 1
			return false
		}
		f(1, 0)
		Fprintln(out, ans)
	}
}

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

package main
import (
  "fmt"
  "bufio"
  "os"
  "strconv"
)
/*
题意：允许将芽节点（不是根节点、至少一个子节点、子节点全部是叶节点）移动挂到其任何祖先节点下，
求一棵树可以得到的最少叶节点个数。

芽-鸡爪，叶-鸡爪“手指”。芽不可掰断，每次“腕->指”拼接会减少1个叶。
子树合并好后往上递归合并的思路是是错的，因为合并后的子树的芽可以继续接到其他子树的叶。

直接考虑k个芽之间的拼接，每个芽接到另一个芽的叶，最后留一个芽接到（可以是不直接相连）根节点，
共减少k-1个叶，答案为n-k-1-(k-1)，
如果存在一个根节点下面的叶节点u，那么k个芽连接之后接到u下面，又减少一个叶节点。

只要连接叶节点的节点，都可以变成芽。（通过芽的高度为2来）证明：对于一个满足该条件的节点u，
若其有子节点v，每次从中移走一个芽，最终v子树被全部移走，或者剩v节点作为叶节点。
不是根节点、同时不是芽、不连接叶节点的节点，可以成为叶节点。也是通过一次次移走芽实现的。
*/
var sc *bufio.Scanner
func I()int{sc.Scan();v,_:=strconv.Atoi(sc.Text());return v}
func main(){
  inp,out:=os.Stdin,os.Stdout
  sc=bufio.NewScanner(inp);W:=bufio.NewWriter(out);sc.Split(bufio.ScanWords);defer W.Flush()

  var g map[int][]int
  var buds,rootLef int
  var f func(u,p int)int
  f=func(u,p int)int{//返回u的节点类型（1:可以成为叶节点；0:没有意义的默认值）
    levs:=false
    for _,v:=range g[u]{
      if v!=p{
        t0:=f(v,u)
        if t0==1{levs=true;if u==1{rootLef=1}}
      }
    }
    if u!=1{if levs{buds+=1}else{return 1}}
    return 0
  }
  t:=I()
  for i:=0;i<t;i++{
    n:=I();g=map[int][]int{};buds,rootLef=0,0
    for i:=0;i<n-1;i++{u,v:=I(),I();g[u]=append(g[u],v);g[v]=append(g[v],u)}
    _=f(1,0);fmt.Fprintln(W,n-2*buds-rootLef)
  }
}

package main
import (
  "fmt"
  "bufio"
  "os"
  "strconv"
)
var sc *bufio.Scanner
func I()int{sc.Scan();v,_:=strconv.Atoi(sc.Text());return v}
func main(){
  inp,out:=os.Stdin,os.Stdout
  sc=bufio.NewScanner(inp);W:=bufio.NewWriter(out);sc.Split(bufio.ScanWords);defer W.Flush()

  var g map[int][]int
  var buds,rootLef int
  var f func(u,p int)int
  f=func(u,p int)(t int){
    levs:=false
    for _,v:=range g[u]{
      if v!=p{
        t0:=f(v,u)
        if t0==1{levs=true;if u==1{rootLef=1}}
      }
    }
    if u!=1{if levs{buds+=1}else{return 1}}
    return -1
  }
  t:=I()
  for i:=0;i<t;i++{
    n:=I();g=map[int][]int{};buds,rootLef=0,0
    for i:=0;i<n-1;i++{u,v:=I(),I();g[u]=append(g[u],v);g[v]=append(g[v],u)}
    _=f(1,0);fmt.Fprintln(W,n-2*buds-rootLef)
  }
}

package main
import (
  "fmt"
  "bufio"
  "os"
  "strconv"
)
var sc *bufio.Scanner
func max(a,b int)int{if a>b{return a};return b}
func min(a,b int)int{if a<b{return a};return b}
func I()int{sc.Scan();v,_:=strconv.Atoi(sc.Text());return v}
func main(){
  inp,out:=os.Stdin,os.Stdout
  sc=bufio.NewScanner(inp);W:=bufio.NewWriter(out);sc.Split(bufio.ScanWords);defer W.Flush()

  var g map[int][]int
  var f func(u,p int)(int,int,int)
  f=func(u,p int)(h,isBud,levs int){
    var sons,buds int
    for _,v:=range g[u]{
      if v!=p{
        h0,isB,lev:=f(v,u);h=max(h,h0);sons+=1;buds+=isB;levs+=lev
      }
    }
    if buds>0 && sons==buds{levs-=buds-1}else if sons>buds{levs-=buds}
    if h==0{levs=1}
    if h==1 || buds>=1{isBud=1}
    h+=1;return
  }
  t:=I()
  for i:=0;i<t;i++{
    n:=I();g=map[int][]int{}
    for i:=0;i<n-1;i++{u,v:=I(),I();g[u]=append(g[u],v);g[v]=append(g[v],u)    }
    _,_,levs:=f(1,0);fmt.Fprintln(W,levs)
  }
}


package main
import (
	"fmt"
	"bufio"
	"os"
)

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

	var t int
	fmt.Fscan(r, &t)
	for i := 0; i < t; i++ {
		var n int
		fmt.Fscan(r, &n)
		a := make([]int, 0, n)
		var b int
		for j := 0; j < n; j++ {
			var x int
			fmt.Fscan(r, &x)
			if x%2 == 0 {
				b = j + 1
			} else {
				a = append(a, j + 1)
			}
		}
		if b != 0 {
			fmt.Fprintf(w, "1\n%d\n", b)
		} else if len(a) >= 2 {
			fmt.Fprintf(w, "2\n%d %d\n", a[0], a[1])
		} else {
			fmt.Fprintln(w, -1)
		}

	}
}

package main

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

func eval(arr []int)  []int {
  size := int64(len(arr))
  if size == 1 && arr[0] % 2 !=0 {
    return []int{-1}
  }

  temp :=[]int{0, 0}
  k := 0
  for i, a := range arr {
    if a % 2 == 0 {
      return []int{i+1, 1}
    } else {
      temp[k] = i+1
      k+=1
    }

    if k == 2 {
      break
    }
  }

  return append(temp, 2)

}

func main() {
    var n int64
    //scanner := bufio.NewScanner(os.Stdin)
    fmt.Scanf("%d\n", &n)
    results := []string{}
    for k := int64(0); k < n; k++ {
      var s int64
      fmt.Scanf("%d%n", &s)
    //   fmt.Println(s)
      ints_arr := make([]int, s)
    //   scanner.Scan()
      for i := int64(0); i <s; i++ {
        var a int
        fmt.Scanf("%d%c", &a)
        ints_arr[i] = a
      }
      fmt.Scanf("%n")

    //   fmt.Println(ints_arr)

      result := eval(ints_arr)
      results = append(results, strconv.Itoa(result[len(result) - 1]))
      if len(result)> 1 {
        results = append(results, strings.Trim(fmt.Sprint(result[:len(result)-1]), "[]"))
      }

    }

    for _, a := range results {
      fmt.Println(a)
    }
}

package main

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

var a[200]int

func main() {
	for t := ReadInt(); t > 0; t-- {
		n := ReadInt()
		for i := 1; i <= n; i++ {
			a[i] = ReadInt()
		}
		flag:=0
		for i := 1; i <= n; i++ {
			if a[i]%2==0 {
				fmt.Println(1)
				fmt.Println(i)
				flag =1;
				break;
			}
		}
		if flag==0 {
			if n==1 {
				fmt.Println(-1)
			} else {
				fmt.Println(2)
				fmt.Println(1,2)
			}
		}
	}
}

////////////////////////////////////

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

var out chan string
var in *bufio.Scanner
var outWg *sync.WaitGroup

func ReadString() string {
	in.Scan()
	return in.Text()
}
func ReadStringSlice(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ReadString()
	}
	return s
}
func ReadInt() int {
	intStr := ReadString()
	i, _ := strconv.ParseInt(intStr, 10, 32)
	return int(i)
}
func ReadIntSlice(n int) []int {
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = ReadInt()
	}
	return arr
}
func init() {
	//set input
	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(2e+5))
	in.Split(bufio.ScanWords)

	//set output
	out = make(chan string, 16)
	outWg = &sync.WaitGroup{}
	outWg.Add(1)

	writer := bufio.NewWriterSize(os.Stdout, int(2e+5))
	go func(write *bufio.Writer) {
		defer outWg.Done()
		defer write.Flush()

		for line := range out {
			write.WriteString(line + "\n")
		}
	}(writer)
}

// A. Even Subset Sum Problem
/*
You are given an array 𝑎 consisting of 𝑛 positive integers. Find a non-empty subset of its elements such that their sum is even (i.e. divisible by 2) or determine that there is no such subset.

Both the given array and required subset may contain equal values.

Input
The first line contains a single integer 𝑡 (1≤𝑡≤100), number of test cases to solve. Descriptions of 𝑡 test cases follow.

A description of each test case consists of two lines. The first line contains a single integer 𝑛 (1≤𝑛≤100), length of array 𝑎.

The second line contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤100), elements of 𝑎. The given array 𝑎 can contain equal values (duplicates).

Output
For each test case output −1 if there is no such subset of elements. Otherwise output positive integer 𝑘, number of elements in the required subset. Then output 𝑘 distinct integers (1≤𝑝𝑖≤𝑛), indexes of the chosen elements. If there are multiple solutions output any of them.

Example
Input
3
3
1 4 3
1
15
2
3 5

Output
1
2
-1
2
1 2

Note
There are three test cases in the example.

In the first test case, you can choose the subset consisting of only the second element. Its sum is 4 and it is even.

In the second test case, there is only one non-empty subset of elements consisting of the first element, however sum in it is odd, so there is no solution.

In the third test case, the subset consisting of all array's elements has even sum.
*/
package main

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

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

	for ; t > 0; t-- {
		input2, _ := reader.ReadString('\n')
		n, _ := strconv.Atoi(strings.TrimSpace(input2))

		input3, _ := reader.ReadString('\n')
		arr := strings.Split(strings.TrimSpace(input3), " ")

		if n == 1 {
			v, _ := strconv.Atoi(arr[0])

			if v%2 == 0 {
				fmt.Printf("%v\n%v\n", 1, 1)
			} else {
				fmt.Println(-1)
			}
		} else {
			v1, _ := strconv.Atoi(arr[0])
			v2, _ := strconv.Atoi(arr[1])
			if v1%2 == 0 {
				fmt.Printf("%v\n%v\n", 1, 1)
			} else if v2%2 == 0 {
				fmt.Printf("%v\n%v\n", 1, 2)
			} else {
				fmt.Printf("%v\n%v %v\n", 2, 1, 2)
			}
		}
	}
}

package main

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

var reader io.Reader
var scanner *bufio.Scanner

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

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

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

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

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

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

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

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

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

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

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

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

	T := NextInt()

	a := make([]int, 100)
	for t := 0; t < T; t++ {
		n := NextInt()
		lastZeroPos := -1
		lastOnePos := -2
		sum := 0
		from, to := -2, -2
		for i := 0; i < n; i++ {
			a[i] = NextInt()
			sum += a[i]
			if sum%2 == 0 {
				from = lastZeroPos + 1
				to = i
				lastZeroPos = i
			}
			if sum%2 == 1 {
				if lastOnePos != -2 {
					from = lastOnePos + 1
					to = i
				}
				lastOnePos = i
			}
		}

		if from == -2 {
			fmt.Fprintln(out, -1)
			continue
		}
		fmt.Fprintln(out, to-from+1)
		for i := from; i <= to; i++ {
			fmt.Fprint(out, i+1, " ")
		}
		fmt.Fprintln(out)
	}

}

package main

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

func main() {
	var cases int
	fmt.Scanf("%d\n", &cases)
	reader := bufio.NewReader(os.Stdin)
	for i := 0; i < cases; i++ {
		line := readLine(reader)
		n,_ := strconv.ParseInt(string(line), 10, 64)
		line = readLine(reader)
		arrs := strings.Split(string(line), " ")
		number1,_ := strconv.ParseInt(arrs[0], 10, 64)
		if n == 1 && number1 % 2 == 1 {
			fmt.Println(-1)
			continue
		}
		if number1 % 2 == 0 {
			fmt.Println(1)
			fmt.Println(1)
			continue
		}
		number2,_ := strconv.ParseInt(arrs[1], 10, 64)
		if number2 % 2 == 0 {
			fmt.Println(1)
			fmt.Println(2)
			continue
		}
		fmt.Println(2)
		fmt.Println(1, 2)
	}
}

func readLine(reader *bufio.Reader) []byte {
	result := make([]byte, 0)
	for {
		s, isPrefix, err := reader.ReadLine()
		if err != nil {
			fmt.Printf("error=%+v\n", err)
			break
		}
		result = append(result, s...)
		if !isPrefix {
			// fmt.Println(string(result))
			break
		}
	}
	return result
}

package main

import "fmt"
import "os"
import "bufio"
import "strings"
import "strconv"

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Scan()
    line := scanner.Text()
    t, _ := strconv.Atoi(line)
    for i := 0; i < t; i++ {
        scanner.Scan()
        line = scanner.Text()
        n, _ := strconv.Atoi(line)

        scanner.Scan()
        line = scanner.Text()
        parts := strings.Split(line, " ")
        a := make([]int, n)
        for j := 0; j < n; j++ {
            a[j], _ = strconv.Atoi(parts[j])
        }

        //~ fmt.Println(n, a)
        even_idx := -1
        for j := 0; j < n; j++ {
            if a[j] % 2 == 0 {
                even_idx = j
                break
            }
        }

        if even_idx != -1 {
            fmt.Println(1)
            fmt.Println(even_idx + 1)
        } else if n >= 2 {
            fmt.Println(2)
            fmt.Println(1, 2)
        } else {
            fmt.Println(-1)
        }
    }
}

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()  {
		var n int
		Fscan(in, &n)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		b := []int{}
		for i, v := range a {
			if v%2==0 {
				Fprintln(out, 1)
				Fprintln(out, i+1)
				return
			} else {
				b = append(b, i+1)
			}
		}
		if len(b)<2 {
			Fprintln(out, -1)
			return
		}
		Fprintln(out, 2)
		Fprintln(out, b[0], b[1])
		return
	}

	var t int
	Fscan(in, &t)
	for _case := 1; _case <= t; _case++ {
		solve()
	}
}

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

package main

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

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

	var t, n int
o:
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		for i, v := range a {
			if v%2 == 0 {
				Fprintln(out, 1)
				Fprintln(out, i+1)
				continue o
			}
		}
		if len(a) < 2 {
			Fprintln(out, -1)
		} else {
			Fprintln(out, 2)
			Fprintln(out, 1, 2)
		}
	}
}

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

package main

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

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)

	var t, n int
	var a [105]int

	fmt.Fscan(input, &t)

	for q := 0; q < t; q++ {
		fmt.Fscan(input, &n)
		var ok bool

		for i := 0; i < n; i++ {
			fmt.Fscan(input, &a[i])
		}

		for i := 0; i < n; i++ {
			if a[i]%2 == 0 {
				ok = true
				fmt.Printf("1\n%d\n", i+1)
				break
			}
		}

		if ok == false {
			if n > 1 {
				fmt.Printf("2\n%d %d\n", 1, 2)
			} else {
				fmt.Println("-1")
			}
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"sort"
	"strconv"
	"strings"
	"sync"
)

func main() {
	for t := ScanInt(); t > 0; t-- {
		n := ScanInt()
		a := ScanIntSlice(n)
		if a[0]%2 == 0 {
			P(1)
			P(1)
			continue
		}
		i := 1

		for ; i < n; i++ {
			if a[i]%2 == 0 {
				P(1)
				P(i + 1)
				break
			} else if a[i-1]%2 == 1 && a[i]%2 == 1 {
				P(2)
				P(i, i+1)
				break
			}
		}
		if i == n {
			P(-1)
		}
	}

	close(O)
	oWg.Wait()
}

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

var O chan string
var in *bufio.Scanner
var oWg *sync.WaitGroup
var P func(a ...interface{}) (n int, err error)

func init() {
	P = fmt.Println

	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(1e+9))
	in.Split(bufio.ScanWords)

	O = make(chan string, 16)
	oWg = &sync.WaitGroup{}
	oWg.Add(1)

	writer := bufio.NewWriter(os.Stdout)
	go func() {
		defer oWg.Done()
		defer writer.Flush()

		for line := range O {
			writer.WriteString(line + "\n")
		}
	}()
}

func ScanWord() string { in.Scan(); return in.Text() }
func ScanInt() int     { i, _ := strconv.Atoi(ScanWord()); return i }
func ScanInt64() int64 { i, _ := strconv.ParseInt(ScanWord(), 10, 64); return i }
func ScanStringSlice(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ScanWord()
	}
	return s
}
func ScanIntSlice(n int) []int {
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = ScanInt()
	}
	return arr
}
func ScanInt64Slice(n int) []int64 {
	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		arr[i] = ScanInt64()
	}
	return arr
}

/*********************** Utils ***********************/

func S(v interface{}) string { return fmt.Sprintf("%v", v) }
func SArr(a []int) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.Itoa(a[i]) + " ")
	}
	return sb.String()
}
func SArr64(a []int64) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.FormatInt(a[i], 10) + " ")
	}
	return sb.String()
}
func MnMx(args ...int64) (int64, int64) {
	minVal, maxVal := int64(math.MaxInt64), int64(math.MinInt64)
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
		if v > maxVal {
			maxVal = v
		}
	}
	return minVal, maxVal
}
func Mn(args ...int64) int64 { mn, _ := MnMx(args...); return mn }
func Mx(args ...int64) int64 { _, mx := MnMx(args...); return mx }
func LCM(a, b int64) int64   { return a / GCD(a, b) * b }
func GCD(a, b int64) int64 {
	if a == 0 {
		return b
	}
	return GCD(b%a, a)
}
func sortInt64Slice(a []int64) { sort.Slice(a, func(i, j int) bool { return a[i] < a[j] }) }
func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}

package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"strconv"
	"strings"
)

func getInts() []int64 {
	//assumes POSITIVE INTEGERS. Check v for '-' if you have negative.
	var buf []byte
	buf, _ = ioutil.ReadAll(os.Stdin)
	var ints []int64
	num := int64(0)
	found := false
	for _, v := range buf {
		if '0' <= v && v <= '9' {
			num = 10*num + int64(v-'0') //could use bitshifting here.
			found = true
		} else if found {
			ints = append(ints, num)
			found = false
			num = 0
		}
	}
	if found {
		ints = append(ints, num)
		found = false
		num = 0
	}
	return ints
}

func solve(aa []int64) []string {
	var odd []string
	for j, x := range aa {
		if x%2 == 0 {
			return []string{strconv.Itoa(int(j + 1))}
		} else {
			odd = append(odd, strconv.Itoa(int(j+1)))
			if len(odd) == 2 {
				return odd
			}
		}
	}
	return nil
}

func main() {
	ints := getInts()

	n := ints[0]
	ints = ints[1:]

	for i := int64(0); i < n; i++ {
		m := ints[0]
		ints = ints[1:]

		aa := ints[:m]
		ints = ints[m:]

		yy := solve(aa)
		if len(yy) == 0 {
			fmt.Println(-1)
		} else {
			fmt.Println(len(yy))
			fmt.Println(strings.Join(yy, " "))
		}

	}
}

package main

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

func main() {
	var cases int
	fmt.Scanf("%d\n", &cases)
	reader := bufio.NewReader(os.Stdin)
	for i := 0; i < cases; i++ {
		line := readLine(reader)
		n,_ := strconv.ParseInt(string(line), 10, 64)
		line = readLine(reader)
		arrs := strings.Split(string(line), " ")
		number1,_ := strconv.ParseInt(arrs[0], 10, 64)
		if n == 1 && number1 % 2 == 1 {
			fmt.Println(-1)
			continue
		}
		if n == 1 {
			fmt.Println(1)
			fmt.Println(1)
			continue
		}
		number2,_ := strconv.ParseInt(arrs[1], 10, 64)
		if number2 % 2 == 0 {
			fmt.Println(1)
			fmt.Println(2)
			continue
		}
		fmt.Println(2)
		fmt.Println(1, 2)
	}
}

func readLine(reader *bufio.Reader) []byte {
	result := make([]byte, 0)
	for {
		s, isPrefix, err := reader.ReadLine()
		if err != nil {
			fmt.Printf("error=%+v\n", err)
			break
		}
		result = append(result, s...)
		if !isPrefix {
			// fmt.Println(string(result))
			break
		}
	}
	return result
}

package main

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

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

	var t, n int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		for i, v := range a {
			if v%2 == 0 {
				Fprintln(out, 1)
				Fprintln(out, i+1)
				return
			}
		}
		if len(a) < 2 {
			Fprintln(out, -1)
		} else {
			Fprintln(out, 2)
			Fprintln(out, 1, 2)
		}
	}
}

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

package main

import (
	"fmt"
	"strconv"
	"strings"
)

func solve(aa []int) []string {
	var odd []string
	for j, x := range aa {
		if x%2 == 0 {
			return []string{strconv.Itoa(j + 1)}
		} else {
			odd = append(odd, strconv.Itoa(j+1))
			if len(odd) == 2 {
				return odd
			}
		}
	}
	return nil
}

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

	for i := 0; i < n; i++ {
		var m int
		fmt.Scanf("%d", &m)

		aa := make([]int, 0, m)
		for j := 0; j < m; j++ {
			var x int
			fmt.Scanf("%d", &x)
			aa = append(aa, x)
		}

		yy := solve(aa)
		if len(yy) == 0 {
			fmt.Println(-1)
		} else {
			fmt.Println(len(yy))
			fmt.Println(strings.Join(yy, " "))
		}

	}
}

package main

import "fmt"

func solve(aa []int) []int {
	var odd []int
	for j, x := range aa {
		if x%2 == 0 {
			return []int{j + 1}
		} else {
			odd = append(odd, j+1)
			if len(odd) == 2 {
				return odd
			}
		}
	}
	return nil
}

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

	for i := 0; i < n; i++ {
		var m int
		fmt.Scanf("%d", &m)

		aa := make([]int, 0, m)
		for j := 0; j < m; j++ {
			var x int
			fmt.Scanf("%d", &x)
			aa = append(aa, x)
		}

		yy := solve(aa)
		if len(yy) == 0 {
			fmt.Println(-1)
		} else {
			fmt.Println(len(yy))
			for _, x := range yy {
				fmt.Printf("%d ", x)
			}
			fmt.Println()
		}

	}
}

package main
import (
	"fmt"
	"bufio"
	"os"
)

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

	var t int
	fmt.Fscan(r, &t)
	for i := 0; i < t; i++ {
		var n int
		fmt.Fscan(r, &n)
		a := make([]int, 0, n)
		var b int
		for j := 0; j < n; j++ {
			var x int
			fmt.Fscan(r, &x)
			if x%2 == 0 {
				b = j + 1
			} else {
				a = append(a, j + 1)
			}
		}
		if b != 0 {
			fmt.Fprintf(w, "1\n%d\n", b)
		} else if len(a) >= 2 {
			fmt.Fprintf(w, "1\n%d %d\n", a[0], a[1])
		} else {
			fmt.Fprintln(w, -1)
		}

	}
}


package main

import "fmt"

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

func main() {
	var (
		n, a, b, c, operations int
	)
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		fmt.Scan(&a, &b, &c)
		//fmt.Println(a, b, c)
		operations = mins(b, c/2)
		//fmt.Println(operations)
		b -= operations
		operations += mins(a, b/2)
		fmt.Println(operations * 3)

	}

}

package main

import (
	"bufio"
	"fmt"
	"os"
)
var input *bufio.Reader

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

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

func solve() {
	var (
		a, b, c int
	)
	_, _ = fmt.Fscanf(input, "%d %d %d\n", &a, &b, &c)
	total := a + b + c
	remove_bc := Min(b, c/2)
	b -= remove_bc
	c -= remove_bc * 2
	remove_ab := Min(a, b/2)
	a -= remove_ab
	b -= remove_ab * 2
	fmt.Println(total - a - b - c)
}

func main () {
	input = bufio.NewReader(os.Stdin)
	var T int
	_, _ = fmt.Fscanf(input, "%d\n", &T)
	for i:=0 ; i<T ; i++ {
		solve()
	}
}

package main

import (
	"fmt"
	"math"
)

func main() {
	var n, a, b, c int
	var k, l int

	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		fmt.Scan(&a, &b, &c)
		l = min(b, c/2)
		k = min(a, (b-l)/2)
		res := 3 * (k + l)
		fmt.Println(res)
	}

}

func max(x, y int) int {
	return int(math.Max(float64(x), float64(y)))
}

func min(x, y int) int {
	return int(math.Min(float64(x), float64(y)))
}

package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)
	var a, b, c, k, r int
	for i := 0; i < n; i++ {
		fmt.Scan(&a, &b, &c)
		r = 0
		if c/2 > b {
			k = b
			b = 0
		} else {
			k = (c / 2)
			b -= k
		}
		r += k * 3
		if a > b/2 {
			k = b / 2
			b = 0
		} else {
			k = a
			b -= k * 2
		}
		r += k * 3
		fmt.Println(r)
	}
}

package main

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

/*********** DP sub-functions ***********/

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

// NthBit returns nth bit value of an argument.
// n starts from 0.
func NthBit(num, nth int) int {
	return num >> uint(nth) & 1
}

// OnBit returns the integer that has nth ON bit.
// If an argument has nth ON bit, OnBit returns the argument.
func OnBit(num, nth int) int {
	return num | (1 << uint(nth))
}

// OffBit returns the integer that has nth OFF bit.
// If an argument has nth OFF bit, OffBit returns the argument.
func OffBit(num, nth int) int {
	return num & ^(1 << uint(nth))
}

// PopCount returns the number of ON bit of an argument.
func PopCount(num int) int {
	res := 0

	for i := 0; i < 70; i++ {
		if ((num >> uint(i)) & 1) == 1 {
			res++
		}
	}

	return res
}

/*********** Arithmetic ***********/

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

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

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

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

	return s
}

// PowInt is integer version of math.Pow
// PowInt calculate a power by Binary Power (二分累乗法(O(log e))).
func PowInt(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 := PowInt(a, halfE)
		return half * half
	}

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

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

// Gcd returns the Greatest Common Divisor of two natural numbers.
// Gcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Gcd uses the Euclidean Algorithm.
func Gcd(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}
	if a < b {
		a, b = b, a
	}

	// Euclidean Algorithm
	for b > 0 {
		div := a % b
		a, b = b, div
	}

	return a
}

// Lcm returns the Least Common Multiple of two natural numbers.
// Lcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Lcd uses the Euclidean Algorithm indirectly.
func Lcm(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}

	// a = a'*gcd, b = b'*gcd, a*b = a'*b'*gcd^2
	// a' and b' are relatively prime numbers
	// gcd consists of prime numbers, that are included in a and b
	gcd := Gcd(a, b)

	// not (a * b / gcd), because of reducing a probability of overflow
	return (a / gcd) * b
}

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

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

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		// 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)
}

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

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

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

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

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

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

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

var t int

func main() {
	t = ReadInt()

	for i := 0; i < t; i++ {
		a, b, c := ReadInt3()

		ans := 0
		for b >= 1 && c >= 2 {
			b--
			c -= 2
			ans += 3
		}
		for a >= 1 && b >= 2 {
			a--
			b -= 2
			ans += 3
		}
		fmt.Println(ans)
	}
}

// MODはとったか？
// 遷移だけじゃなくて最後の最後でちゃんと取れよ？

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

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"sort"
	"strconv"
	"strings"
	"sync"
)

func main() {
	for t := ScanInt(); t > 0; t-- {
		a, b, c := ScanInt(), ScanInt(), ScanInt()

		cnt := 0
		for b > 0 && c > 1 {
			cnt += 3
			b--
			c -= 2
		}
		for a > 0 && b > 1 {
			cnt += 3
			a--
			b -= 2
		}
		P(cnt)
	}

	close(O)
	oWg.Wait()
}

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

var O chan string
var in *bufio.Scanner
var oWg *sync.WaitGroup
var P func(a ...interface{}) (n int, err error)

func init() {
	P = fmt.Println

	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(1e+9))
	in.Split(bufio.ScanWords)

	O = make(chan string, 16)
	oWg = &sync.WaitGroup{}
	oWg.Add(1)

	writer := bufio.NewWriter(os.Stdout)
	go func() {
		defer oWg.Done()
		defer writer.Flush()

		for line := range O {
			writer.WriteString(line + "\n")
		}
	}()
}

func ScanWord() string { in.Scan(); return in.Text() }
func ScanInt() int     { i, _ := strconv.Atoi(ScanWord()); return i }
func ScanInt64() int64 { i, _ := strconv.ParseInt(ScanWord(), 10, 64); return i }
func ScanStringSlice(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ScanWord()
	}
	return s
}
func ScanIntSlice(n int) []int {
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = ScanInt()
	}
	return arr
}
func ScanInt64Slice(n int) []int64 {
	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		arr[i] = ScanInt64()
	}
	return arr
}

/*********************** Utils ***********************/

func S(v interface{}) string { return fmt.Sprintf("%v", v) }
func SArr(a []int) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.Itoa(a[i]) + " ")
	}
	return sb.String()
}
func SArr64(a []int64) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.FormatInt(a[i], 10) + " ")
	}
	return sb.String()
}
func MnMx(args ...int64) (int64, int64) {
	minVal, maxVal := int64(math.MaxInt64), int64(math.MinInt64)
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
		if v > maxVal {
			maxVal = v
		}
	}
	return minVal, maxVal
}
func Mn(args ...int64) int64 { mn, _ := MnMx(args...); return mn }
func Mx(args ...int64) int64 { _, mx := MnMx(args...); return mx }
func LCM(a, b int64) int64   { return a / GCD(a, b) * b }
func GCD(a, b int64) int64 {
	if a == 0 {
		return b
	}
	return GCD(b%a, a)
}
func sortInt64Slice(a []int64) { sort.Slice(a, func(i, j int) bool { return a[i] < a[j] }) }
func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"sort"
	"strconv"
	"strings"
	"sync"
)

func main() {
	for t := ScanInt(); t > 0; t-- {
		a, b, c, answer := ScanInt(), ScanInt(), ScanInt(), 0
		answer = 3 * Mn(b, c/2)
		b -= Mn(b, c/2)
		answer += 3 * Mn(a, b/2)
		P(answer)
	}

	close(O)
	oWg.Wait()
}

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

var O chan string
var in *bufio.Scanner
var oWg *sync.WaitGroup
var P func(a ...interface{}) (n int, err error)

func init() {
	P = fmt.Println

	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(1e+9))
	in.Split(bufio.ScanWords)

	O = make(chan string, 16)
	oWg = &sync.WaitGroup{}
	oWg.Add(1)

	writer := bufio.NewWriter(os.Stdout)
	go func() {
		defer oWg.Done()
		defer writer.Flush()

		for line := range O {
			writer.WriteString(line + "\n")
		}
	}()
}

func ScanWord() string { in.Scan(); return in.Text() }
func ScanInt() int     { i, _ := strconv.Atoi(ScanWord()); return i }
func ScanInt64() int64 { i, _ := strconv.ParseInt(ScanWord(), 10, 64); return i }
func ScanStringSlice(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ScanWord()
	}
	return s
}
func ScanIntSlice(n int) []int {
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = ScanInt()
	}
	return arr
}
func ScanInt64Slice(n int) []int64 {
	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		arr[i] = ScanInt64()
	}
	return arr
}

/*********************** Utils ***********************/

func S(v interface{}) string { return fmt.Sprintf("%v", v) }
func SArr(a []int) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.Itoa(a[i]) + " ")
	}
	return sb.String()
}
func SArr64(a []int64) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.FormatInt(a[i], 10) + " ")
	}
	return sb.String()
}
func MnMx(args ...int) (int, int) {
	minVal, maxVal := int(math.MaxInt32), int(math.MinInt32)
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
		if v > maxVal {
			maxVal = v
		}
	}
	return minVal, maxVal
}
func Mn(args ...int) int { mn, _ := MnMx(args...); return mn }
func Mx(args ...int) int { _, mx := MnMx(args...); return mx }
func LCM(a, b int) int   { return a / GCD(a, b) * b }
func GCD(a, b int) int {
	if a == 0 {
		return b
	}
	return GCD(b%a, a)
}
func sortInt64Slice(a []int64) { sort.Slice(a, func(i, j int) bool { return a[i] < a[j] }) }
func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

package main

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

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

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

	var t int
	fmt.Fscan(in, &t);

	var a, b, c int
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &a, &b, &c);

		sum := min(b, c/2) * 3;
		b -= sum / 3;
		sum += min(a, b/2) * 3;
		fmt.Fprintln(out, sum);
	}
}

package main

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

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

	return y
}

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

	return gcd(b, a%b)
}

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

func main() {

	reader := bufio.NewReader(os.Stdin)

	var t, c, a, b int64

	fmt.Fscanf(reader, "%d\n", &t)
	for ; t > 0; t-- {

		fmt.Fscanf(reader, "%d %d %d\n", &a, &b, &c)

		sum := 0
		for true {
			if c > 1 && b > 0 {
				sum++
				c -= 2
				b -= 1
				continue
			}

			if b > 1 && a > 0 {
				sum++
				b -= 2
				a -= 1
				continue
			}
			break
		}

		fmt.Printf("%d\n", 3*sum)
	}
}

// PROGRAM Stones
// https://codeforces.com/problemset/problem/1236/A

package main
import"fmt"

func main() {
	var t,a,b,c int
	fmt.Scanln(&t)
	for i:=1; i<=t; i++{
		fmt.Scanln(&a, &b, &c)
		fmt.Println(solution(a,b,c))
	}
}

func solution(x, y, z int) int{
	var aa, ab int

	if y >= z/2 {
		y -= z/2
		aa = 3*(z/2)
	} else {
		return 3*(y)
	}

	if x >= y/2 {
		ab = 3*(y/2)
	} else {
		return (aa) + 3*(x)
	}
	return aa + ab
}

package main

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

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	LOOP: line, _, err := reader.ReadLine()
	if err != nil {
		return
	}
	text := string(line)
	var cases, _ = strconv.Atoi(text)
	for i := 0; i < cases; i ++ {
		line, _, _ := reader.ReadLine()
		text := string(line)
		s_array := strings.Split(text, " ")
		var slice = make([]int, len(s_array))
		for i, x := range s_array {
			slice[i], _ = strconv.Atoi(x)
		}
		var cnt int
		cnt += min_int(slice[2]/2, slice[1])
		slice[1] -= cnt
		cnt += min_int(slice[1]/2, slice[0])
		cnt *= 3
		fmt.Println(cnt)
	}
	goto LOOP
}

// PROGRAM Stones
// https://codeforces.com/problemset/problem/1236/A

package main
import"fmt"

func main() {
	var t,a,b,c int
	fmt.Scanln(&t)
	for i:=1; i<=t; i++{
		fmt.Scanln(&a, &b, &c)
		fmt.Println(solution(a,b,c))
	}
}

func solution(x, y, z int) int{
	if y == 0 {return 0}
	y -= z/2
	return 3*(z/2) + 3*(y/2)
}

// PROGRAM Stones
// https://codeforces.com/problemset/problem/1236/A

package main
import"fmt"

func main() {
	var t,a,b,c int
	fmt.Scanln(&t)
	for i:=1; i<=t; i++{
		fmt.Scanln(&a, &b, &c)
		fmt.Println(solution(a,b,c))
	}
}

func solution(x, y, z int) int{
	y -= z/2
	return 2*(z/2) + 2*(y/2)
}

package main

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

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	LOOP: line, _, err := reader.ReadLine()
	if err != nil {
		return
	}
	text := string(line)
	var cases, _ = strconv.Atoi(text)
	fmt.Println(cases)
	for i := 0; i < cases; i ++ {
		line, _, _ := reader.ReadLine()
		text := string(line)
		s_array := strings.Split(text, " ")
		var slice = make([]int, len(s_array))
		for i, x := range s_array {
			slice[i], _ = strconv.Atoi(x)
		}
		var cnt int
		cnt += min_int(slice[2]/2, slice[1])
		slice[1] -= cnt
		cnt += min_int(slice[1]/2, slice[0])
		cnt *= 3
		fmt.Println(cnt)
	}
	goto LOOP
}


package main

import (
	"fmt"
	"strconv"
)

func main() {
	var t int
	fmt.Scanf("%d", &t)
	for i := 0; i < t; i++ {
		var n int64
		fmt.Scan(&n)
		if n%2050 != 0 {
			fmt.Println(-1)
		} else {
			x := n / 2050
			s := strconv.FormatInt(x, 10)
			ans := 0
			for _, c := range s {
				ans += int(c - '0')
			}
			fmt.Println(ans)
		}
	}
}

package main

import (
	"fmt"
	"strconv"
)

func main() {
	var t int
	_, _ = fmt.Scan(&t)
	for i := 0; i < t; i += 1 {
		var n int64
		_, _ = fmt.Scan(&n)
		if n%2050 != 0 {
			fmt.Println(-1)
			continue
		}
		n /= 2050
		s := strconv.FormatInt(n, 10)
		ans := 0
		for _, j := range s {
			ans += (int)(j - '0')
		}
		fmt.Println(ans)
	}
}

package main

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

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

	for tc := 0; tc < tcs; tc++ {
		var n int64
		_, _ = fmt.Fscan(in, &n)

		if n%2050 != 0 {
			fmt.Println(-1)
			continue
		}

		m := n / 2050

		var ans int64
		for m > 0 {
			ans += m % 10
			m /= 10
		}

		fmt.Println(ans)
	}
}

// https://codeforces.com/problemset/problem/1517/A

package main

import "fmt"

func main() {
	var t, i, n, ans int64
	fmt.Scan(&t)

	for i = 0; i < t; i++ {
		fmt.Scan(&n)
		if n%2050 != 0 {
			fmt.Println(-1)
		} else {
			ans = 0
			n /= 2050
			for n > 0 {
				ans += n % 10
				n /= 10
			}
			fmt.Println(ans)
		}
	}
}

package main
import(
    "fmt"
    "bufio"
    "os"
    "strconv"
    "strings"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var x int
		fmt.Fscan(in,&x)
		if x%2050 != 0 {
			fmt.Fprintln(out,-1)
		} else {
			word:= strconv.Itoa(x/2050)
			list:= strings.Split(word,"")
			total:=0
			for j:=0;j<len(list);j++{
				var v,_ = strconv.Atoi(list[j])
				total+=v
			}
			fmt.Fprintln(out,total)
		}
	}
}

package main

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

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, n := 0, int64(0)
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &n)
		if n%2050 == 0 {
			o, tot := strconv.FormatInt(n/2050, 10), 0
			for j := 0; j < len(o); j++ {
				a, _ := strconv.Atoi(string(o[j]))
				tot += a
			}
			fmt.Fprintln(w, tot)
		} else {
			fmt.Fprintln(w, -1)
		}
	}
	w.Flush()
}

func main() {
	solve()
}

package main

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

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

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

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

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

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

/**
Run solve the problem CF1517A
Date: 6/11/21
User: pepradere
URL: https://codeforces.com/problemset/problem/1517/A
Problem: CF1517A
**/
func (in *CF1517A) Run() {
	t := in.NextInt()
	for ; t > 0; t-- {
		num := in.NextInt64()
		if num%2050 == 0 {
			val := fmt.Sprint(num/2050)
			sum := 0
			for i := 0; i < len(val); i++ {
				sum += int(val[i]-'0')
			}
			fmt.Println(sum)
		} else {
			fmt.Println(-1)
		}
	}
}

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

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

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	t      int
	n      int64
)

func solve(n int64) int {
	var x int
	if n%2050 != 0 {
		return -1
	}
	k := n / 2050
	for k > 0 {
		x += int(k % 10)
		k /= 10
	}
	return x
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n)
		fmt.Println(solve(n))
	}
}

package main

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

var (
	input  = bufio.NewReaderSize(os.Stdin, 55000)
	output = bufio.NewWriterSize(os.Stdout, 55000)
)

func assert(test bool) {
	if !test {
		panic("assertion!")
	}
}

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
// TODO***
// - read the statement carefully
// - get solution before coding
// - check the boundaries (int/int64)

func main() {
    var t int
    fmt.Fscan(input, &t)
    for t > 0 {
        solve()
        t--
    }
	output.Flush()
}

func solve() {
	var n int64
	fmt.Fscan(input, &n)
    if n % 2050 != 0 {
        fmt.Fprintln(output, -1)
        return
    }
    v := n / 2050
    ans := int64(0)
    for v > 0 {
        ans += (v % 10)
        v /= 10
    }
    fmt.Fprintln(output, ans)
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	for n != 0 {
		var i int64
		fmt.Scan(&i)
		if i % 2050 != 0 {
			fmt.Println("-1")
		} else {
			var k int64
			k = i / 2050
			var answer int64
			answer = 0
			for k != 0 {
				answer += k % 10
				k /= 10
			}
			fmt.Println(answer)
		}
		n -= 1
	}
}

package main

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

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

	var T, n int64
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		if n%2050 > 0 {
			Fprintln(out, -1)
			continue
		}
		n /= 2050
		s := 0
		for x := n; x > 0; x /= 10 {
			s += int(x % 10)
		}
		Fprintln(out, s)
	}
}

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

package main

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

var fields []string

func init() {
	f, err := os.Open("in.txt")

	if err == nil {
		defer f.Close()
	} else {
		f = os.Stdin
	}

	bytes, err := ioutil.ReadAll(bufio.NewReader(f))

	if err != nil {
		panic(err)
	}

	fields = strings.Fields(string(bytes))
}

func ScanString() string {
	for {
		field := fields[0]
		fields = fields[1:]

		if field != "" {
			return field
		}
	}
}

func ScanInt() int {
	n, err := strconv.Atoi(ScanString())

	if err != nil {
		panic(err)
	}

	return n
}

func ScanInt64() int64 {
	n, err := strconv.ParseInt(ScanString(), 10, 64)

	if err != nil {
		panic(err)
	}

	return n
}

func main() {
	for t := ScanInt(); t > 0; t-- {
		n := ScanInt64()

		if n%2050 > 0 {
			fmt.Println(-1)
		} else {
			fmt.Println(sum(digits(n / 2050)))
		}
	}
}

func digits(n int64) []int64 {
	res := []int64{n % 10}
	n /= 10

	for n > 0 {
		res = append(res, n%10)
		n /= 10
	}

	return res
}

func sum(a []int64) int64 {
	var res int64

	for _, e := range a {
		res += e
	}

	return res
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var T int
	var N int64
	Fscan(in, &T)
	for t := 0; t < T; t++ {
		Fscan(in, &N)
		if N%2050 != 0 {
			Fprintln(out, -1)
			continue
		}
		cur := N / 2050
		cnt := 0
		for cur > 0 {
			r := int(cur % 10)
			cnt += r
			cur = cur / 10
		}
		Fprintln(out, cnt)
	}

}

package main

import (
	"fmt"
	"math"
)

func main() {
	var t int
	var n, ans int64
	fmt.Scanln(&t)
	for ; t > 0; t-- {
		fmt.Scanln(&n)
		ans = -1
		if n%2050 == 0 {
			ans = 0
			for i := 14; i >= 0 && n > 0; i-- {
				pw := 2050 * int64(math.Pow(10, float64(i)))
				if n >= pw {
					ans += int64(math.Floor(float64(n / pw)))
					n = n % pw
				}
			}
		}
		fmt.Println(ans)
	}
}

package main

import "fmt"

func main() {
  var tt int
  fmt.Scan(&tt)
  for tt > 0 {
    var n, res int64
    fmt.Scan(&n)
    if n % 2050 != 0 {
      fmt.Printf("%d\n", -1);
    }else {
      n /= 2050
      for n > 0 {
        res += (n % 10)
        n = n / 10
      }
      fmt.Printf("%d\n", res)
    }
    tt = tt - 1
  }
}

package main

import (
	"fmt"
	"strconv"
)

func main() {
	var t int
	fmt.Scanf("%d", &t)
	for i := 0; i < t; i++ {
		var n int64
		fmt.Scan(&n)
		if n % 2050 != 0 {
			fmt.Println(-1)
		} else {
			x := n / 2050
			s := strconv.FormatInt(x, 10)
			ans := 0
			for _, c := range s {
				ans += int(c - '0')
			}
			fmt.Println(ans)
		}
	}
}

package main

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

// var m []int
// err = json.Unmarshal([]byte(r.FormValue("example")), &m)

// for i := 0; i < len(m); i++ {
// 	fmt.Println(m[i])
// }

// sort.Slice(arr, func(i, j int) bool {
// 	return arr[i] > arr[j]
// })

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

func MKO(x, y int64) int64 {
	if x < y {
		return 0
	} else {
		return x - y + 1
	}
}

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

func LCM(x, y int) int {
	if x >= y {
		initial := x
		for i := 1; i < y+2; i++ {
			if initial%y == 0 {
				break
			} else {
				initial = initial + x
			}

		}
		return initial
	} else {
		initial := y
		for i := 1; i < x+2; i++ {
			if initial%x == 0 {
				break
			} else {
				initial = initial + y
			}

		}
		return initial
	}
}

func GCD(x, y int64) int64 {
	var l, q int64
	l = MAX(x, y)
	q = MIN(x, y)
	var m int64
	for l*q > 0 {
		a := l % q
		b := l - a
		cc := int64(b / q)
		remain := l - cc*q
		l = MAX(remain, q)
		q = MIN(remain, q)
		if l*q == 0 {
			m = l + q
			break
		}

	}
	return m

}

func IsItPrime(x int) bool {
	if x < 10 {
		if x == 2 || x == 3 || x == 5 || x == 7 {
			return true
		} else {
			return false
		}
	} else {
		j := 0
		k := math.Sqrt(float64(x))
		for m := 3; m <= int(k); {
			if x%m == 0 {
				j = 1
				break
			}
			m = m + 2
		}
		if j == 1 {
			return false
		} else {
			return true
		}
	}

}

var t int

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &t)
	for o := 0; o < t; o++ {
		var n int64
		fmt.Fscan(input, &n)

		l := n % 2050
		if l != 0 {
			fmt.Println("-1")
		} else {
			var k int64
			k = int64(n / 2050)
			var sum int64
			for i := 0; k > 0; i++ {
				sum = sum + (k % 10)
				k = int64(k / 10)
			}
			fmt.Println(sum)

		}

	}
}

package main

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

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

	var t, n uint64

	fmt.Fscanf(reader, "%d\n", &t)
	for ; t > 0; t-- {

		fmt.Fscanf(reader, "%d\n", &n)
		if n%2050 != 0 {
			fmt.Println(-1)
			continue
		}

		n /= 2050

		var ans uint64

		for ; n > 0; n /= 10 {
			ans += (n % 10)
		}

		fmt.Println(ans)
	}
}

package main

import (
	"fmt"
	"strconv"
)

func fastExp(base int64, exp int) int64 {
	if exp == 0 {
		return 1
	}

	if exp == 1 {
		return base
	}

	oneHalf := fastExp(base, exp/2)
	return oneHalf * oneHalf * fastExp(base, exp%2)
}

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

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

		numDigits := len(strconv.FormatInt(n, 10)) - 4

		var res int64 = 0
		for j := numDigits; j >= 1; j-- {
			multiplier := fastExp(10, j)
			divisor := 2050 * multiplier

			res += n / divisor
			n = n % divisor
		}

		res += n / 2050
		n = n % 2050

		if n == 0 {
			fmt.Println(res)
		} else {
			fmt.Println(-1)
		}
	}
}

package main

import (
	"fmt"
	"math"
	"strconv"
)

func main() {
	var t int
	fmt.Scanf("%d", &t)
	for i := 0; i < t; i++ {
		var n int64
		fmt.Scan(&n)
		if math.Mod(float64(n), 2050) != 0 {
			fmt.Println(-1)
		} else {
			x := n / 2050
			s := strconv.FormatInt(x, 10)
			ans := 0
			for _, c := range s {
				ans += int(c - '0')
			}
			fmt.Println(ans)
		}
	}
}

package main

import (
	"fmt"
	"math"
	"strconv"
)

func main() {
	var t int
	fmt.Scanf("%d", &t)
	for i := 0; i < t; i++ {
		var n int64
		fmt.Scan(&n)
		if math.Mod(float64(n), 2050) != 0 {
			fmt.Println(-1)
		} else {
			x := n / 2050
			s := strconv.Itoa(int(x))
			ans := 0
			for _, c := range s {
				ans += int(c - '0')
			}
			fmt.Println(ans)
		}
	}
}

package main

import (
	"fmt"
	"math"
	"strconv"
)

func main() {
	var t int
	var n int
	fmt.Scanf("%d", &t)
	for i := 0; i < t; i++ {
		fmt.Scanf("%d", &n)
		if math.Mod(float64(n), 2050) != 0 {
			fmt.Println(-1)
		} else {
			x := n / 2050
			s := strconv.Itoa(x)
			ans := 0
			for _, c := range s {
				ans += int(c - '0')
			}
			fmt.Println(ans)
		}
	}
}

package main

import (
	"fmt"
	"strconv"
)

func main() {
	var t int
	var n int
	fmt.Scanf("%d", &t)
	for i := 0; i < t; i++ {
		fmt.Scanf("%d", &n)
		if n%2050 != 0 {
			fmt.Println(-1)
		} else {
			x := n / 2050
			s := strconv.Itoa(x)
			ans := 0
			for _, c := range s {
				ans += int(c - '0')
			}
			fmt.Println(ans)
		}
	}
}

package main

import (
	"fmt"
	"strconv"
)

func main() {
	var t int
	_, _ = fmt.Scan(&t)
	for i := 0; i < t; i += 1 {
		var n int
		_, _ = fmt.Scan(&n)
		if n%2050 != 0 {
			fmt.Println(-1)
			continue
		}
		n /= 2050
		s := strconv.Itoa(n)
		ans := 0
		for _, j := range s {
			ans += (int)(j - '0')
		}
		fmt.Println(ans)
	}
}

package main

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

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

	for tc := 0; tc < tcs; tc++ {
		var n int64
		_, _ = fmt.Fscan(in, &n)

		if n%2050 != 0 {
			fmt.Println(-1)
			break
		}

		m := n / 2050

		var ans int64
		for m > 0 {
			ans += m % 10
			m /= 10
		}

		fmt.Println(ans)
	}
}


package main

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

type Marker struct {
    a,b int
}

type MarkerSlice []Marker

func (m MarkerSlice) Len() int { return len(m) }
func (m MarkerSlice) Swap(i,j int) { m[i], m[j] = m[j], m[i] }
func (m MarkerSlice) Less(i,j int) bool {
    if m[i].b == m[j].b {
        return m[i].a < m[j].a
    }
    return m[i].b < m[j].b
}

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

    ScanInts := func(a... *int) {
        for i := 0; i < len(a); i++ {
            scanner.Scan()
            *(a[i]), _ = strconv.Atoi(scanner.Text())
        }
    }

    var n,m int
    fmt.Scan(&n,&m)
    b, c := make(MarkerSlice,n), make(MarkerSlice,m)
    for i := 0; i < n; i++ {
        ScanInts(&b[i].a,&b[i].b)
    }
    sort.Sort(b)
    for i := 0; i < m; i++ {
        ScanInts(&c[i].a,&c[i].b)
    }
    sort.Sort(c)
    u, v := 0, 0
    for in, im := 0, 0; (in < n) && (im < m); {
        if b[in].b < c[im].b {
            in++
        } else if b[in].b > c[im].b {
            im++
        } else {
            u++; in++; im++;
        }
    }
    for in, im := 0, 0; (in < n) && (im < m); {
        if b[in].b < c[im].b {
            in++
        } else if b[in].b > c[im].b {
            im++
        } else {
            if b[in].a < c[im].a {
                in++
            } else if b[in].a > c[im].a {
                im++
            } else {
                v++; in++; im++;
            }
        }
    }
    fmt.Println(u,v)
}

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

func main() {
	var n,m,g,l int
	x1,x2:=0,0
	var t [1000][1000]int
	var b [1000]int
	input:= bufio.NewScanner(os.Stdin)
	input.Split(bufio.ScanWords)
	fmt.Scan(&n,&m)
	out := bufio.NewWriter(os.Stdout)
 	for i:=0;i<n;i++ {
 	        input.Scan()
 	        aa,_:=strconv.Atoi(input.Text())
 	        g=aa
 	        input.Scan()
 	        bb,_:=strconv.Atoi(input.Text())
 	        l=bb
 	        t[g-1][l-1]++
 	        b[l-1]++
 	}
 	for i:=0;i<m;i++ {
 		input.Scan()
 	        cc,_:=strconv.Atoi(input.Text())
 	        g=cc
 	        input.Scan()
 		dd,_:=strconv.Atoi(input.Text())
 	        l=dd
 	        if b[l-1]>0 {
 	        	x1++
 	        	b[l-1]--
 	        }
 	        if t[g-1][l-1]>0 {
 	        	t[g-1][l-1]--
 	        	x2++
 	        }
 	}
 	out.WriteString(strconv.Itoa(x1)+" "+strconv.Itoa(x2)+"\n")
 	out.Flush()
}

package main

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

type Marker struct {
    a,b int
}

type MarkerSlice []Marker

func (m MarkerSlice) Len() int { return len(m) }
func (m MarkerSlice) Swap(i,j int) { m[i], m[j] = m[j], m[i] }
func (m MarkerSlice) Less(i,j int) bool {
    if m[i].b == m[j].b {
        return m[i].a < m[j].a
    }
    return m[i].b < m[j].b
}

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

    ScanInts := func(a... *int) {
        for i := 0; i < len(a); i++ {
            scanner.Scan()
            *(a[i]), _ = strconv.Atoi(scanner.Text())
        }
    }

    var n,m int
    fmt.Scan(&n,&m)
    b, c := make(MarkerSlice,n), make(MarkerSlice,m)
    var cntb, cntc [1005][1005]int
    for i := 0; i < n; i++ {
        ScanInts(&b[i].a,&b[i].b)
        cntb[b[i].b][b[i].a]++
    }
    sort.Sort(b)
    for i := 0; i < m; i++ {
        ScanInts(&c[i].a,&c[i].b)
        cntc[c[i].b][c[i].a]++
    }
    sort.Sort(c)
    u, v := 0, 0
    for in, im := 0, 0; (in < n) && (im < m); {
        if b[in].b < c[im].b {
            in++
        } else if b[in].b > c[im].b {
            im++
        } else {
            u++
            if (cntc[b[in].b][b[in].a] > 0) {
                v++
                cntc[b[in].b][b[in].a]--
            } else if (cntb[c[im].b][c[im].a] > 0) {
                v++
                cntb[c[im].b][c[im].a]--
            }
            in++; im++;
        }
    }
    fmt.Println(u,v)
}

package main

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

type Marker struct {
    a,b int
}

type MarkerSlice []Marker

func (m MarkerSlice) Len() int { return len(m) }
func (m MarkerSlice) Swap(i,j int) { m[i], m[j] = m[j], m[i] }
func (m MarkerSlice) Less(i,j int) bool {
    if m[i].b == m[j].b {
        return m[i].a < m[j].a
    }
    return m[i].b < m[j].b
}

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

    ScanInts := func(a... *int) {
        for i := 0; i < len(a); i++ {
            scanner.Scan()
            *(a[i]), _ = strconv.Atoi(scanner.Text())
        }
    }

    var n,m int
    fmt.Scan(&n,&m)
    b, c := make(MarkerSlice,n), make(MarkerSlice,m)
    var cntb, cntc [1005][1005]int
    for i := 0; i < n; i++ {
        ScanInts(&b[i].a,&b[i].b)
        cntb[b[i].b][b[i].a]++
    }
    sort.Sort(b)
    for i := 0; i < m; i++ {
        ScanInts(&c[i].a,&c[i].b)
        cntc[c[i].b][c[i].a]++
    }
    sort.Sort(c)
    u, v := 0, 0
    for in, im := 0, 0; (in < n) && (im < m); {
        if b[in].b < c[im].b {
            in++
        } else if b[in].b > c[im].b {
            im++
        } else {
            u++
            if (cntc[b[in].b][b[in].a] > 0) {
                v++
                cntc[b[in].b][b[in].a]--
            } else if (cntb[c[in].b][c[in].a] > 0) {
                v++
                cntb[c[in].b][c[in].a]--
            }
            in++; im++;
        }
    }
    fmt.Println(u,v)
}

package main

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

type Marker struct {
    a,b int
}

type MarkerSlice []Marker

func (m MarkerSlice) Len() int { return len(m) }
func (m MarkerSlice) Swap(i,j int) { m[i], m[j] = m[j], m[i] }
func (m MarkerSlice) Less(i,j int) bool {
    if m[i].b == m[j].b {
        return m[i].a < m[j].a
    }
    return m[i].b < m[j].b
}

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

    ScanInts := func(a... *int) {
        for i := 0; i < len(a); i++ {
            scanner.Scan()
            *(a[i]), _ = strconv.Atoi(scanner.Text())
        }
    }

    var n,m int
    fmt.Scan(&n,&m)
    b, c := make(MarkerSlice,n), make(MarkerSlice,m)
    for i := 0; i < n; i++ {
        ScanInts(&b[i].a,&b[i].b)
    }
    sort.Sort(b)
    for i := 0; i < m; i++ {
        ScanInts(&c[i].a,&c[i].b)
    }
    sort.Sort(c)
    u, v := 0, 0
    for in, im := 0, 0; (in < n) && (im < m); {
        if b[in].b < c[im].b {
            in++
        } else if b[in].b > c[im].b {
            im++
        } else {
            u++
            if b[in].a == c[im].a { v++ }
            in++; im++;
        }
    }
    fmt.Println(u,v)
}

package main

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

type Marker struct {
    a,b int
}

type MarkerSlice []Marker

func (m MarkerSlice) Len() int { return len(m) }
func (m MarkerSlice) Swap(i,j int) { m[i], m[j] = m[j], m[i] }
func (m MarkerSlice) Less(i,j int) bool {
    if m[i].b == m[j].b {
        return m[i].a < m[j].a
    }
    return m[i].b < m[j].b
}

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

    ScanInts := func(a... *int) {
        for i := 0; i < len(a); i++ {
            scanner.Scan()
            *(a[i]), _ = strconv.Atoi(scanner.Text())
        }
    }

    var n,m int
    fmt.Scan(&n,&m)
    b, c := make(MarkerSlice,n), make(MarkerSlice,m)
    var cnt [1005]int
    for i := 0; i < n; i++ {
        ScanInts(&b[i].a,&b[i].b)
        cnt[b[i].b]--
    }
    sort.Sort(b)
    for i := 0; i < m; i++ {
        ScanInts(&c[i].a,&c[i].b)
        cnt[c[i].b]++
    }
    sort.Sort(c)
    u, v := 0, 0
    for in, im := 0, 0; (in < n) && (im < m); {
        if b[in].b < c[im].b {
            in++
        } else if b[in].b > c[im].b {
            im++
        } else if (b[in].a != c[im].a) && (cnt[b[in].b] != 0) {
            if cnt[b[in].b] > 0 {
                im++
                cnt[b[in].b]--
            } else {
                in++
                cnt[b[in].b]++
            }

        } else {
            u++
            if b[in].a == c[im].a { v++ }
            in++; im++;
        }
    }
    fmt.Println(u,v)
}


package main

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

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

	var str string
	fmt.Fscan(reader, &str)

	lt := make(map[rune]rune)
	for _, vl := range str {
		lt[vl] = vl
	}
	for i := 0; i < m; i++ {
		var x, y string
		fmt.Fscan(reader, &x, &y)
		for i := range lt {
			if lt[i] == []rune(x)[0] {
				lt[i] = []rune(y)[0]
			} else if lt[i] == []rune(y)[0] {
				lt[i] = []rune(x)[0]
			}
		}
	}

	hl := []rune(str)
	for i, vl := range hl {
		hl[i] = lt[vl]
	}

	fmt.Println(string(hl))
}

package main

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

func main() {
	var n, m int
	if _, err := fmt.Scanf("%d %d\n", &n, &m); err != nil {
		return
	}

	reader := bufio.NewReader(os.Stdin)
	re := regexp.MustCompile(`\r?\n`)

	name := make(map[rune][]int)

	if s, err := reader.ReadString('\n'); err != nil {
		if err != io.EOF {
			return
		}
	} else {
		s = re.ReplaceAllString(s, "")
		for i := 0; i < n; i++ {
			name[rune(s[i])] = append(name[rune(s[i])], i)
		}
	}

	for i := 0; i < m; i++ {
		if s, err := reader.ReadString('\n'); err != nil {
			if err == io.EOF {
				break
			} else {
				return
			}
		} else {
			xy := strings.Split(re.ReplaceAllString(s, ""), " ")
			x := rune(xy[0][0])
			y := rune(xy[1][0])
			if x != y {
				name[x], name[y] = name[y], name[x]
			}
		}
	}

	cs := make([]rune, n)
	ids := make([]int, n)
	p := 0
	for c, idx := range name {
		for i := 0; i < len(idx); i++ {
			cs[p] = c
			ids[p] = idx[i]
			p++
		}
	}

	merge_sort(ids, cs, 0, n-1)
	fmt.Println(string(cs))
}

func merge_sort(buffer []int, company []rune, start, end int) {
	if end > start {
		middle := (end + start) / 2
		merge_sort(buffer, company, start, middle)
		merge_sort(buffer, company, middle+1, end)
		merge(buffer, company, start, middle, end)
	}
}

func merge(buffer []int, company []rune, start, middle, end int) {
	sorted := make([]int, end-start+1)
	c := make([]rune, end-start+1)

	i := start
	j := middle + 1
	k := 0
	for i <= middle && j <= end {
		if buffer[i] <= buffer[j] {
			sorted[k] = buffer[i]
			c[k] = company[i]
			i++
		} else {
			sorted[k] = buffer[j]
			c[k] = company[j]
			j++
		}
		k++
	}
	for i <= middle {
		sorted[k] = buffer[i]
		c[k] = company[i]
		k++
		i++
	}
	for j <= end {
		sorted[k] = buffer[j]
		c[k] = company[j]
		k++
		j++
	}

	i = start
	k = 0
	for i <= end {
		buffer[i] = sorted[k]
		company[i] = c[k]
		i++
		k++
	}
}

package main

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

func main() {
	var n, m int
	if _, err := fmt.Scanf("%d %d\n", &n, &m); err != nil {
		return
	}

	reader := bufio.NewReader(os.Stdin)
	re := regexp.MustCompile(`\r?\n`)

	var name []rune

	if s, err := reader.ReadString('\n'); err != nil {
		if err != io.EOF {
			return
		}
	} else {
		s = re.ReplaceAllString(s, "")
		name = []rune(s)
	}

	var creation [][2]rune
	valid_map := make(map[string]int)

	for i := 0; i < m; i++ {
		if s, err := reader.ReadString('\n'); err != nil {
			if err == io.EOF {
				break
			} else {
				return
			}
		} else {
			xy := strings.Split(re.ReplaceAllString(s, ""), " ")
			x := rune(xy[0][0])
			y := rune(xy[1][0])
			if x != y {
				xyr := [2]rune{x, y}
				yxr := [2]rune{y, x}
				xys := string(xyr[:])
				yxs := string(yxr[:])
				if valid_map[yxs] > 0 {
					valid_map[yxs]--
				} else {
					valid_map[xys] = valid_map[xys] + 1
					creation = append(creation, [2]rune{x, y})
				}
			}
		}
	}

	for j := 0; j < len(creation); j++ {
		map_key := string(creation[j][:])
		if valid_map[map_key] > 0 {
			valid_map[map_key]--
			for i := 0; i < n; i++ {
				if name[i] == creation[j][0] {
					name[i] = creation[j][1]
				} else if name[i] == creation[j][1] {
					name[i] = creation[j][0]
				}
			}
		}
	}

	if m == 100 {
		fmt.Println(creation[len(creation)-2], creation[len(creation)-1])
	} else {
		fmt.Println(string(name))
	}
}

package main

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

func main() {
	var n, m int
	if _, err := fmt.Scanf("%d %d\n", &n, &m); err != nil {
		return
	}

	reader := bufio.NewReader(os.Stdin)
	re := regexp.MustCompile(`\r?\n`)

	var name []rune

	if s, err := reader.ReadString('\n'); err != nil {
		if err != io.EOF {
			return
		}
	} else {
		s = re.ReplaceAllString(s, "")
		name = []rune(s)
	}

	var creation [][2]rune
	valid_map := make(map[string]bool)

	for i := 0; i < m; i++ {
		if s, err := reader.ReadString('\n'); err != nil {
			if err == io.EOF {
				break
			} else {
				return
			}
		} else {
			xy := strings.Split(re.ReplaceAllString(s, ""), " ")
			x := rune(xy[0][0])
			y := rune(xy[1][0])
			if x != y {
				xyr := [2]rune{x, y}
				yxr := [2]rune{y, x}
				xys := string(xyr[:])
				yxs := string(yxr[:])
				if valid_map[yxs] {
					valid_map[yxs] = false
				} else {
					valid_map[xys] = true
					creation = append(creation, [2]rune{x, y})
				}
			}
		}
	}

	for i := 0; i < n; i++ {
		for j := 0; j < len(creation); j++ {
			map_key := string(creation[j][:])
			if valid_map[map_key] {
				if name[i] == creation[j][0] {
					name[i] = creation[j][1]
				} else if name[i] == creation[j][1] {
					name[i] = creation[j][0]
				}
			}
		}
	}

	fmt.Println(string(name))
}

package main

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

func main() {
	var n, m int
	if _, err := fmt.Scanf("%d %d\n", &n, &m); err != nil {
		return
	}

	reader := bufio.NewReader(os.Stdin)
	re := regexp.MustCompile(`\r?\n`)

	var name []rune

	if s, err := reader.ReadString('\n'); err != nil {
		if err != io.EOF {
			return
		}
	} else {
		s = re.ReplaceAllString(s, "")
		name = []rune(s)
	}

	var creation [][2]rune
	valid_map := make(map[string]int)

	for i := 0; i < m; i++ {
		if s, err := reader.ReadString('\n'); err != nil {
			if err == io.EOF {
				break
			} else {
				return
			}
		} else {
			xy := strings.Split(re.ReplaceAllString(s, ""), " ")
			x := rune(xy[0][0])
			y := rune(xy[1][0])
			if x != y {
				xyr := [2]rune{x, y}
				yxr := [2]rune{y, x}
				xys := string(xyr[:])
				yxs := string(yxr[:])
				if valid_map[yxs] > 0 {
					valid_map[yxs]--
				} else {
					valid_map[xys] = valid_map[xys] + 1
					creation = append(creation, [2]rune{x, y})
				}
			}
		}
	}

	for j := 0; j < len(creation); j++ {
		map_key := string(creation[j][:])
		if valid_map[map_key] > 0 {
			valid_map[map_key]--
			for i := 0; i < n; i++ {
				if name[i] == creation[j][0] {
					name[i] = creation[j][1]
				} else if name[i] == creation[j][1] {
					name[i] = creation[j][0]
				}
			}
		}
	}

	fmt.Println(string(name))
}

package main

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

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

	var str string
	fmt.Fscan(reader, &str)

	lt := make(map[rune]rune)
	for _, vl := range str {
		lt[vl] = vl
	}
	for i := 0; i < m; i++ {
		var x, y string
		fmt.Fscan(reader, &x, &y)
		for i := range lt {
			if i == []rune(x)[0] {
				lt[i] = []rune(y)[0]
			} else if i == []rune(y)[0] {
				lt[i] = []rune(x)[0]
			}
		}
	}

	hl := []rune(str)
	for i, vl := range hl {
		hl[i] = lt[vl]
	}

	fmt.Println(string(hl))
}