Bajtek is learning to skate on ice. He's a beginner, so his only mode of transportation is pushing off from a snow drift to the north, east, south or west and sliding until he lands in another snow drift. He has noticed that in this way it's impossible to get from some snow drifts to some other by any sequence of moves. He now wants to heap up some additional snow drifts, so that he can get from any snow drift to any other one. He asked you to find the minimal number of snow drifts that need to be created.We assume that Bajtek can only heap up snow drifts at integer coordinates.

The first line of input contains a single integer n (1 ≤ n ≤ 100) — the number of snow drifts. Each of the following n lines contains two integers xi and yi (1 ≤ xi, yi ≤ 1000) — the coordinates of the i-th snow drift. Note that the north direction coinсides with the direction of Oy axis, so the east direction coinсides with the direction of the Ox axis. All snow drift's locations are distinct.

Output the minimal number of snow drifts that need to be created in order for Bajtek to be able to reach any snow drift from any other one.

package main

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

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

type Point struct {
	x, y int
	visited bool
}

func flood(points []Point, pos int) {
	points[pos].visited = true
	x, y := points[pos].x, points[pos].y
	for i := 0; i < len(points); i++ {
		if !points[i].visited && (points[i].x == x || points[i].y == y) {
			flood(points, i)
		}
	}
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	n := scanInt(scanner)
	points := make([]Point, n)
	for i := 0; i < n; i++ {
		points[i] = Point{x: scanInt(scanner), y: scanInt(scanner)}
	}
	count := -1
	for i := 0; i < n; i++ {
		if points[i].visited {
			continue
		}
		count++
		flood(points, i)
	}
	writer.WriteString(fmt.Sprintf("%d\n", count))
}

package main

import (
	"fmt"
)

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

	x, y := make([]int, n), make([]int, n)
	parent := make([]int, n)
	sz := make([]int, n)

	for i := 0; i < n; i++ {
		parent[i] = i
		sz[i] = 1
		fmt.Scan(&x[i], &y[i])
	}

	var find func(int) int
	find = func(a int) int {
		if parent[a] == a {
			return a
		}
		parent[a] = find(parent[a])
		return parent[a]
	}

	var unite func(int, int)
	unite = func(a, b int) {
		a = find(a)
		b = find(b)
		if a != b {
			if sz[b] > sz[a] {
				a = a ^ b
				b = a ^ b
				a = a ^ b
			}
			parent[b] = a
			sz[a] += sz[b]
		}
	}

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if x[i] == x[j] || y[i] == y[j] {
				unite(i, j)
			}
		}
	}

	ans := -1
	for i := 0; i < n; i++ {
		if i == find(i) {
			ans++
		}
	}
	fmt.Print(ans)
}

package main

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

func main() {
	io := NewFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	n := io.NextInt()

	x, y := make([]int, n), make([]int, n)
	parent := make([]int, n)
	sz := make([]int, n)

	for i := 0; i < n; i++ {
		parent[i] = i
		sz[i] = 1
		x[i], y[i] = io.NextInt(), io.NextInt()
	}

	var find func(int) int
	find = func(a int) int {
		if parent[a] == a {
			return a
		}
		parent[a] = find(parent[a])
		return parent[a]
	}

	var unite func(int, int)
	unite = func(a, b int) {
		a = find(a)
		b = find(b)
		if a != b {
			if sz[b] > sz[a] {
				a = a ^ b
				b = a ^ b
				a = a ^ b
			}
			parent[b] = a
			sz[a] += sz[b]
		}
	}

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if x[i] == x[j] || y[i] == y[j] {
				unite(i, j)
			}
		}
	}

	ans := -1
	for i := 0; i < n; i++ {
		if i == find(i) {
			ans++
		}
	}

	io.Println(ans)
}

type FastIO struct {
	*bufio.Reader
	*bufio.Writer
	c [1]byte
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	f.Read(f.c[:])
	return f.c[0]
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) NextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt()
	}
	return a
}

func (f *FastIO) Next() string {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	var s []byte
	for ; c > ' '; c = f.readC() {
		s = append(s, f.c[0])
	}
	return string(s)
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

func (f *FastIO) PrintInts(a ...int) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}

func (f *FastIO) PrintlnInts(a ...int) {
	f.PrintInts(a...)
	f.Println()
}

package main

import "fmt"

// Edge is a intermediate representation of edge on DirectedGraph
type Edge struct {
	From  Vertex
	To    Vertex
	Value Rel // raw value of edge
}
type Vertex struct {
	Value interface{}
}
type Rel string
type DirectedGraph struct {
	Outs map[Vertex]map[Vertex][]Rel
	Ins  map[Vertex][]Vertex
}

// Vertices returns the set of all vertices in graph
func (g *DirectedGraph) Vertices() []Vertex {
	vertices := []Vertex{}

	for key := range g.Outs {
		vertices = append(vertices, key)
	}
	return vertices
/*	vertices := []Vertex{}
	haveIn := make(map[Vertex]bool)

	for key := range g.Outs {
		vertices = append(vertices, key)
		haveIn[key] = true
		for vertex, _ := range g.Outs[key] {
			_, ok := haveIn[vertex]
			if ok == false {
				vertices = append(vertices, vertex)
				fmt.Println(vertex, "   !")
			}
		}
	}
	return vertices*/
}

// In returns inbound vertices to v in graph g
func (g *DirectedGraph) In(v Vertex) []Vertex {
	_, ok := g.Ins[v]
	if !ok {
		return []Vertex{}
	} else {
		return g.Ins[v]
	}
}

// Out returns outbound vertices from v in graph g
func (g *DirectedGraph) Out(v Vertex) []Vertex {
	_, ok := g.Outs[v]
	if !ok {
		return []Vertex{}
	}

	vertices := []Vertex{}

	for key := range g.Outs[v] {
		vertices = append(vertices, key)
	}
	return vertices
}

// Edges returns the edge between two vertices
func (g *DirectedGraph) Edges(a, b Vertex) []Edge {
	_, oka := g.Outs[a]
	_, okb := g.Ins[b]
	if !oka || !okb {
		return nil
	}

	_, ok := g.Outs[a][b]
	if !ok {
		return nil
	}

	edges := []Edge{}
	for _, rel := range g.Outs[a][b] {
		edges = append(edges, Edge{
			From:  a,
			To:    b,
			Value: rel,
		})
	}
	return edges
}

// Link links two vertices relationship
func (g *DirectedGraph) Link(v Vertex, succ Vertex, rel Rel) {
	_, ok := g.Outs[v]
	if !ok {
		g.Outs[v] = make(map[Vertex][]Rel)
	}
	_, ok = g.Outs[succ]
	if !ok {
		g.Outs[succ] = make(map[Vertex][]Rel)
	}

	_, ok = g.Ins[v]
	if !ok {
		g.Ins[v] = []Vertex{}
	}
	_, ok = g.Ins[succ]
	if !ok {
		g.Ins[succ] = []Vertex{}
	}

	haveRel := false
	for _, item := range g.Outs[v][succ] {
		if item == rel {
			haveRel = true
			break
		}
	}
	if haveRel == false {
		g.Outs[v][succ] = append(g.Outs[v][succ], rel)
	}

	haveV := false
	for _, item := range g.Ins[succ] {
		if item == v {
			haveV = true
			break
		}
	}
	if haveV == false {
		g.Ins[succ] = append(g.Ins[succ], v)
	}
}

// LinkToAll links x to all ys
func (g *DirectedGraph) LinkToAll(x Vertex, ys []Vertex, rel Rel) {
	for _, y := range ys {
		g.Link(x, y, rel)
	}
}

// LinkAllTo links all xs to y
func (g *DirectedGraph) LinkAllTo(xs []Vertex, y Vertex, rel Rel) {
	for _, x := range xs {
		g.Link(x, y, rel)
	}
}

func (g *DirectedGraph) UnLink(a, b Vertex) {
	delete(g.Outs[a], b)
	/*if len(g.Outs[a]) == 0 {
		delete(g.Outs, a)
	}*/

	for id , vertex := range g.Ins[b] {
		if vertex == a {
			g.Ins[b] = append(g.Ins[b][:id], g.Ins[b][id+1:]...)
			break
		}
	}
	/*if len(g.Ins[b]) == 0 {
		delete(g.Ins, b)
	}*/
}

// Fork implements `forked` semantics on DirectedGraph
func (g *DirectedGraph) Fork() *DirectedGraph {
	return g
	//return deepcopy.Copy(g).(*DirectedGraph)
	//panic("impl me")
}
// IntersectionRel returns the union of res1 and res2
func IntersectionRel(rels1 []Rel,rels2 []Rel) []Rel{
	rel := []Rel{}
	for _, rel1 := range rels1 {
		both := false
		for _, rel2 := range rels2{
			if rel1 == rel2 {
				both = true
				break
			}
		}
		if both == true {
			rel = append(rel, rel1)
		}
	}
	return rel
}

// ProjectRelationship filters a graph to just those edges with the given relationship
func (g *DirectedGraph) ProjectRelationship(rel Rel) *DirectedGraph {
	fg := g.Fork()

	vertices := fg.Vertices()
	for _, x := range vertices {
		_, ok := g.Outs[x]
		if ok == true {
			for y, _:= range g.Outs[x] {
				rels := IntersectionRel(g.Outs[x][y], []Rel{rel})
				fg.UnLink(x, y)
				for _, item := range rels {
					fg.Link(x, y, item)
				}
			}
		}
	}

	return fg
}

// FilterRelationships filters a graph g to just those edges which intersect with the given set of
//  relationships
func (g *DirectedGraph) FilterRelationships(rels []Rel) *DirectedGraph {
	fg := g.Fork()

	vertices := fg.Vertices()
	for _, x := range vertices {
		_, ok := g.Outs[x]
		if ok == true {
			for y, _:= range g.Outs[x] {
				rels := IntersectionRel(g.Outs[x][y], rels)
				fg.UnLink(x, y)
				for _, item := range rels {
					fg.Link(x, y, item)
				}
			}
		}
	}

	return fg
}

// BfsOut searches from a vertices set, returns all vertices searchable
// search to downstream vertices from `out` edges
func (g *DirectedGraph) BfsOut(initV []Vertex) []Vertex {
	queue := initV
	V := []Vertex{}
	haveVisited := make(map[Vertex]bool)
	for _, vertex := range initV {
		haveVisited[vertex] = true
		V = append(V, vertex)
	}

	for len(queue) > 0 {
		cur := queue[0]
		if haveVisited[cur] == false {
			haveVisited[cur] = true
			V = append(V, cur)
		}
		queue = queue[1:]

		for next, _ := range g.Outs[cur] {
			if haveVisited[next] == true {
				continue
			}
			queue = append(queue, next)
		}
	}
	return V
}

// BfsIn searches from a vertices set, returns all vertices searchable
// search to upstream vertices from `in` edges
func (g *DirectedGraph) BfsIn(initV []Vertex) []Vertex {
	queue := initV
	V := []Vertex{}
	haveVisited := make(map[Vertex]bool)
	for _, vertex := range initV {
		haveVisited[vertex] = true
		V = append(V, vertex)
	}

	for len(queue) > 0 {
		cur := queue[0]
		if haveVisited[cur] == false {
			haveVisited[cur] = true
			V = append(V, cur)
		}
		queue = queue[1:]

		for _, next := range g.Ins[cur] {
			if haveVisited[next] == true {
				continue
			}
			queue = append(queue, next)
		}
	}
	return V
}

// Bfs searches from a vertices set, returns all vertices searchable
// out = true means search to downstream vertices from `out` edges
// out = false means search to upstream vertices from `in` edges
func (g *DirectedGraph) Bfs(initV []Vertex, out bool) []Vertex {
	if out == true {
		return g.BfsOut(initV)
	} else {
		return g.BfsIn(initV)
	}
}

// SCC indexes all vertices of a strongly connected component
type SCC struct {
	Vertices []Vertex
}

// StronglyConnectedComponents finds all strongly connected components
func (g *DirectedGraph) StronglyConnectedComponents() []SCC {
	dfn := make(map[Vertex]int)
	low := make(map[Vertex]int)
	cnt := 0
	belong := make(map[Vertex]int)
	in := make(map[Vertex]bool)
	tag := make(map[Vertex]map[Vertex]bool)
	s2 := []Vertex{}
	scc := []SCC{}
	vertices := g.Vertices()
	//index := 0
	for _, v := range vertices {
		if dfn[v] == 0 {
			s1 := []Vertex{v}
			index := 0
			for len(s1) > 0 {
				x := s1[len(s1)-1]
				s1 = s1[:len(s1)-1]
				if dfn[x] == 0 {
					index = index + 1
					dfn[x] = index
					low[x] = index
					s2 = append(s2, x)
					in[x] = true;
				}
				finish := true
				for next, _ := range g.Outs[x] {
					//fmt.Println("************")
					if dfn[next] == 0 {
						s1 = append(s1, x)
						s1 = append(s1, next)
						_, ok := tag[x]
						if !ok {
							tag[x] = make(map[Vertex]bool)
						}
						tag[x][next] = true
						finish = false
						break
					} else {
						if in[next] == true {
							if low[x] > dfn[next] {
								low[x] = dfn[next]
							}
						}
					}
				}
				if finish == true {
					//fmt.Println("??????????")
					for next, _ := range g.Outs[x] {
						_, ok := tag[x]
						if !ok {
							tag[x] = make(map[Vertex]bool)
						}
						if tag[x][next] == true {
							if low[x] > low[next] {
								low[x] = low[next]
							}
						}
					}
					//fmt.Println("!!!!!!!!!!!!")
					if low[x] == dfn[x] {
						var t Vertex
						cnt = cnt + 1
						for {
							t = s2[len(s2)-1]
							s2 = s2[:len(s2)-1]
							in[t] = false
							belong[t] = cnt;
							if t == x {
								break
							}
						}
					}
				}
			}
		}
	}
	//fmt.Println(cnt, "!!!!!")
	for i := 1; i <= cnt; i++ {
		ts := SCC{
			Vertices: []Vertex{},
		}
		for _, v := range vertices {
			if belong[v] == i {
				ts.Vertices = append(ts.Vertices, v)
			}
		}
		scc = append(scc, ts)
	}
	return scc
}

// MapVertices takes a function of vertices, returns graph with all
//  vertices mapped via f
func (g *DirectedGraph) MapVertices(f func(interface{}) interface{}) *DirectedGraph {
	//fmt.Println(f("consistent-view"))
	fg := NewDirectedGraph()

	vertices := g.Vertices()
	for _, x := range vertices {
		_, ok := g.Outs[x]
		if ok == true {
			for y, _:= range g.Outs[x] {
				rels := g.Outs[x][y]
				for _, item := range rels {
					fg.Link(Vertex{f(x.Value)}, Vertex{f(y.Value)}, item)
				}
			}
		}
	}

	return fg
}

var canonicalModelNames = map[string]string{
	"consistent-view":         "PL-2+",
	"conflict-serializable":   "PL-3",
	"cursor-stability":        "PL-CS",
	"forward-consistent-view": "PL-FCV",
	"monotonic-snapshot-read": "PL-MSR",
	"monotonic-view":          "PL-2L",
	"read-committed":          "PL-2",
	"read-uncommitted":        "PL-1",
	"repeatable-read":         "PL-2.99",
	"serializable":            "PL-3",
	"snapshot-isolation":      "PL-SI",
	"strict-serializable":     "PL-SS",
	"update-serializable":     "PL-3U",
	"0": "A",
	"1": "B",
	"2": "C",
}

// canonicalModelName can be applied to DirectedGraph.MapVertices
func canonicalModelName(name interface{}) interface{} {
	if cname, ok := canonicalModelNames[name.(string)]; ok {
		return cname
	}
	return name
}

// NewDirectedGraph returns a empty DirectedGraph
func NewDirectedGraph() *DirectedGraph {
	var g DirectedGraph
	g.Ins = make(map[Vertex][]Vertex)
	g.Outs = make(map[Vertex]map[Vertex][]Rel)
	return &g
}

// RenumberGraph takes a Graph and rewrites each vertex to a unique integer, returning the
//  rewritten Graph, and a vector of the original vertices for reconstruction.
// That means if we apply MapVertices to dg with remap, it can recover to g again
func (g *DirectedGraph) RenumberGraph() (*DirectedGraph, func(interface{}) interface{}) {
	dg := NewDirectedGraph()
	ref := make(map[Vertex]int)
	numberToVertices := make(map[interface{}]interface{})
	vertices := g.Vertices()

	for id, v := range vertices {
		ref[v] = id
		numberToVertices[id] = v.Value
	}

	for _, x := range vertices {
		for y, rels := range g.Outs[x] {
			for _, rel := range rels{
				dg.Link(Vertex{ref[x]}, Vertex{ref[y]}, rel)
			}
		}
	}

	return dg, func(number interface{}) interface{} {
		return numberToVertices[number]
	}
}

func (g *DirectedGraph) scctest() [][]int {
	dfn := make(map[Vertex]int)
	low := make(map[Vertex]int)
	cnt := 0
	belong := make(map[Vertex]int)
	in := make(map[Vertex]bool)
	tag := make(map[Vertex]map[Vertex]bool)
	s2 := []Vertex{}
	vertices := g.Vertices()
	ans := [][]int{}
	father := make(map[Vertex]Vertex)
	vv := Vertex{-1}
	index := 0
	for _, v := range vertices {
		if dfn[v] == 0 {
			s1 := []Vertex{v}
			father[v] = vv
			for len(s1) > 0 {
				x := s1[len(s1)-1]
				s1 = s1[:len(s1)-1]
				if dfn[x] == 0 {
					index = index + 1
					dfn[x] = index
					low[x] = index
					s2 = append(s2, x)
					in[x] = true;
				}
				finish := true
				for next, _ := range g.Outs[x] {
					if dfn[next] == 0 {
						s1 = append(s1, x)
						s1 = append(s1, next)
						father[next]=x
						_, ok := tag[x]
						if !ok {
							tag[x] = make(map[Vertex]bool)
						}
						tag[x][next] = true
						finish = false
						break
					} else {
						if in[next] == true {
							if low[x] > dfn[next] {
								low[x] = dfn[next]
								//fmt.Println("!!!!!!!")
								//ans = append(ans, []int{ x.Value.(int),next.Value.(int)})
							}
						}
					}
				}
				if finish == true {
					for next, _ := range g.Outs[x] {
						_, ok := tag[x]
						if !ok {
							tag[x] = make(map[Vertex]bool)
						}
						if tag[x][next] == true {
							if low[x] > low[next] {
								low[x] = low[next]
								//fmt.Println("!!!!!!")
								//ans = append(ans, []int{ x.Value.(int),next.Value.(int)})
							}
						}
					}
					if low[x] == dfn[x] {
						var t Vertex
						cnt = cnt + 1
						for {
							t = s2[len(s2)-1]
							s2 = s2[:len(s2)-1]
							in[t] = false
							belong[t] = cnt;
							if t == x {
								break
							}
						}
					}
				}
			}
		}
	}
	for i := 1; i <= cnt; i++ {
		ts := SCC{
			Vertices: []Vertex{},
		}
		for _, v := range vertices {
			if belong[v] == i {
				ts.Vertices = append(ts.Vertices, v)
			}
		}
		//fmt.Println(ts)
	}

	/*for _, x := range vertices{
		for y, _ := range g.Outs[x] {
			if low[x] != low[y] {
				ans = append(ans, []int{ x.Value.(int),y.Value.(int)})
			}
		}
	}*/
	for _, i:= range vertices{
		v:= father[i]
		//fmt.Println(i , "  " ,v )
		if v != vv {
			fmt.Println(low[i]," ",dfn[v])
			//ans = append(ans, []int{ i.Value.(int),v.Value.(int)})
		}
	}

	return ans
}

func criticalConnections(n int, connections [][]int) [][]int {
	g := NewDirectedGraph()
	for _, b := range connections{
		g.Link(Vertex{b[0]},Vertex{b[1]},"")
		g.Link(Vertex{b[1]},Vertex{b[0]},"")
	}

	return g.scctest()
}

func main () {
	/*var g DirectedGraph
	g.Ins = make(map[Vertex][]Vertex)
	g.Outs = make(map[Vertex]map[Vertex][]Rel)
	g.Link(Vertex{0},Vertex{1},"")
	g.Link(Vertex{1},Vertex{2},"")
	g.Link(Vertex{2},Vertex{3},"")
	g.Link(Vertex{3},Vertex{2},"")
	g.Link(Vertex{3},Vertex{7},"")
	g.Link(Vertex{7},Vertex{3},"")
	g.Link(Vertex{2},Vertex{6},"")
	g.Link(Vertex{7},Vertex{6},"")
	g.Link(Vertex{5},Vertex{6},"")
	g.Link(Vertex{6},Vertex{5},"")
	g.Link(Vertex{1},Vertex{5},"")
	g.Link(Vertex{4},Vertex{5},"")
	g.Link(Vertex{4},Vertex{0},"")
	g.Link(Vertex{1},Vertex{4},"")
	// init done ==================================
	// scc ========================================
	scc := g.StronglyConnectedComponents()
	fmt.Println(len(scc))
	fmt.Println(scc)*/
	//(5, 6) (7, 3, 2) (4, 1, 0)
	// scc done ===================================

	/*var g DirectedGraph
	g.Ins = make(map[Vertex][]Vertex)
	g.Outs = make(map[Vertex]map[Vertex][]Rel)
	g.Link(Vertex{"0"},Vertex{"1"},"aa")
	g.Link(Vertex{"0"},Vertex{"1"},"bb")
	//g.Link(Vertex{"0"},Vertex{"2"},"")
	g.Link(Vertex{"1"},Vertex{"2"},"cc")
	g.Link(Vertex{"2"},Vertex{"3"},"")

//	g.UnLink(Vertex{"0"},Vertex{"1"})
	fmt.Println(g.Ins)
	fmt.Println(g.Outs)
	fmt.Println(g.Vertices())
	xxx := g.MapVertices(canonicalModelName)
	fmt.Println(xxx.Ins)
	fmt.Println(xxx.Outs)
	//fmt.Println("!!!!!!!")
	fmt.Println(xxx.Vertices())*/

	/*var g DirectedGraph
	g.Ins = make(map[Vertex][]Vertex)
	g.Outs = make(map[Vertex]map[Vertex][]Rel)
	g.Link(Vertex{"A"},Vertex{"B"},"aa")
	g.Link(Vertex{"A"},Vertex{"B"},"bb")
	//g.Link(Vertex{"0"},Vertex{"2"},"")
	g.Link(Vertex{"B"},Vertex{"C"},"cc")
	g.Link(Vertex{"C"},Vertex{"D"},"")
	dg, remap := g.RenumberGraph()
	fmt.Println(g.Ins)
	fmt.Println(g.Outs)
	//fmt.Println(dg.Ins)
	//fmt.Println(dg.Outs)

	xx := dg.MapVertices(remap)
	fmt.Println(xx.Ins)
	fmt.Println(xx.Outs)*/

	/*var connections [][]int
	connections = append (connections, []int{0,1})
	connections = append (connections, []int{1,2})
	connections = append (connections, []int{2,0})
	connections = append (connections, []int{1,3})
	fmt.Println(criticalConnections(4,connections))*/
	var n,x,y int
	fmt.Scan(&n)
	xs := []int{}
	ys := []int{}
	for i:=1 ; i<=n; i++ {
		fmt.Scan(&x)
		fmt.Scan(&y)
		xs=append(xs,x)
		ys=append(ys,y)
	}
	g := NewDirectedGraph()
	for i:=1 ; i<=n; i++ {
		_, ok := g.Outs[Vertex{i}]
		if !ok {
			g.Outs[Vertex{i}] = make(map[Vertex][]Rel)
		}
		_, ok = g.Ins[Vertex{i}]
		if !ok {
			g.Ins[Vertex{i}] = []Vertex{}
		}
		for j:= i+1 ; j<=n; j++ {
			if xs[i-1]==xs[j-1] || ys[i-1]==ys[j-1] {
				//fmt.Println("!!!")
				g.Link(Vertex{i},Vertex{j},"")
				g.Link(Vertex{j},Vertex{i},"")
			}
		}
	}
	//fmt.Println(g.StronglyConnectedComponents())
	fmt.Println(len(g.StronglyConnectedComponents())-1)
}

package main

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

var a [110]int

func find(z int) int {
	if z == a[z] {
		return z
	} else {
		return find(a[z])
	}
}

func main(){
	var n int
	r := bufio.NewScanner(os.Stdin)
	r.Split(bufio.ScanWords)
	w := bufio.NewWriter(os.Stdout)
	r.Scan()
	n, _ = strconv.Atoi(r.Text())
	x := make([]int, n)
	y := make([]int, n)
	count := 0
	for i:=0; i<n; i++ {
		a[i] = i
		r.Scan()
		x[i], _ = strconv.Atoi(r.Text())
		r.Scan()
		y[i], _ = strconv.Atoi(r.Text())
	}
	for j:=0; j<n; j++ {
		for k:=j+1; k<n; k++ {
			if x[j] == x[k] || y[j] == y[k] {
				a[find(j)] = find(k)
			}
		}
	}
	for l:=0; l<n; l++ {
		if a[l] == l {
			count += 1
		}
	}
	w.WriteString(strconv.Itoa(count-1)+"\n")
	w.Flush()
}

package main

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

// Took from here: https://www.codementor.io/tucnak/using-golang-for-competitive-programming-h8lhvxzt3
var reader *bufio.Reader = bufio.NewReaderSize(os.Stdin, 10000000)
var writer *bufio.Writer = bufio.NewWriterSize(os.Stdout,10000000)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }

var n, count int
var matrix [1001][1001]int

func main() {
  // STDOUT MUST BE FLUSHED MANUALLY!!!
  defer writer.Flush()

  scanf("%d %d\n", &n)
  for i := 1; i <= n; i++ {
    var a, b int
    scanf("%d %d\n", &a, &b)
    matrix[a][b] = 1
  }

  for i := 1; i <= 1000; i++ {
    for j := 1; j <= 1000; j++ {
      if matrix[i][j] == 1 {
        count++
        dfs_adaptada(i, j)
        break
      }
    }
  }
  printf(strconv.Itoa(count - 1))
}

func dfs_adaptada(x, y int) () {
  if matrix[x][y] == 1 {
    matrix[x][y] = 2
    for i := 1; i <= 1000; i++ {
      if matrix[i][y] == 1 {
        dfs_adaptada(i, y)
      }
    }
    for j := 1; j <= 1000; j++ {
      if matrix[x][j] == 1 {
        dfs_adaptada(x, j)
      }
    }
  }
}

package main

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

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

	drifts := make([][2]int, n)

	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for i := 0; i < n; i++ {
		scanner.Scan()
		x, err := strconv.Atoi(scanner.Text())
		if err != nil {
			return
		}
		scanner.Scan()
		y, err := strconv.Atoi(scanner.Text())
		if err != nil {
			return
		}

		drifts[i] = [2]int{x - 1, y - 1}
	}

	sets := make(map[int]map[[2]int]bool)
	sets[1] = make(map[[2]int]bool)
	sets[1][drifts[0]] = true
	drifts[0][0] = -1
	drifts[0][1] = -1

	for i := 1; i <= n; i++ {
		for true { // Add new drifts into sets[i]
			before := len(sets[i])
			for j := 0; j < n; j++ {
				if drifts[j][0] >= 0 {
					x := drifts[j][0]
					y := drifts[j][1]
					ok := false
					for p, _ := range sets[i] {
						if p[0] == x || p[1] == y {
							ok = true
							break
						}
					}
					if ok {
						sets[i][[2]int{x, y}] = true
						drifts[j][0] = -1
						drifts[j][1] = -1
					}
				}
			}
			if len(sets[i]) == before {
				break
			}
		}

		more := false
		for j := 0; j < n; j++ {
			if drifts[j][0] >= 0 {
				sets[i+1] = make(map[[2]int]bool)
				sets[i+1][drifts[j]] = true
				more = true
				break
			}
		}
		if !more {
			break
		}
	}

	fmt.Println(len(sets) - 1)
}

package main

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

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

	drifts := make([][2]int, n)

	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for i := 0; i < n; i++ {
		scanner.Scan()
		x, err := strconv.Atoi(scanner.Text())
		if err != nil {
			return
		}
		scanner.Scan()
		y, err := strconv.Atoi(scanner.Text())
		if err != nil {
			return
		}

		drifts[i] = [2]int{x - 1, y - 1}
	}

	sets := make(map[int]map[[2]int]bool)
	sets[1] = make(map[[2]int]bool)
	sets[1][drifts[0]] = true
	drifts[0][0] = -1
	drifts[0][1] = -1

	for i := 1; i <= n; i++ {
		for true { // Add new drifts into sets[i]
			before := len(sets[i])
			for j := 0; j < n; j++ {
				if drifts[j][0] >= 0 {
					x := drifts[j][0]
					y := drifts[j][1]
					ok := false
					for p, _ := range sets[i] {
						if p[0] == x || p[1] == y {
							ok = true
							break
						}
					}
					if ok {
						sets[i][[2]int{x, y}] = true
						drifts[j][0] = -1
						drifts[j][1] = -1
					}
				}
			}
			if len(sets[i]) == before {
				break
			}
		}

		for j := 0; j < n; j++ {
			if drifts[j][0] >= 0 {
				sets[i+1] = make(map[[2]int]bool)
				sets[i+1][drifts[j]] = true
				break
			}
		}
	}

	fmt.Println(len(sets) - 1)
}

package main

import "fmt"

type UnionFindDisjointSet struct {
    pset []int
    cnt int
}

func (u *UnionFindDisjointSet) InitSet(n int) {
    u.pset = make([]int,n)
    for i := 0; i < n; i++ { u.pset[i] = i }
    u.cnt = n
}

func (u *UnionFindDisjointSet) FindSet(i int) int {
    if u.pset[i] != i { u.pset[i] = u.FindSet(u.pset[i]) }
    return u.pset[i]
}

func (u *UnionFindDisjointSet) IsSameSet(i,j int) bool {
    return u.FindSet(i) == u.FindSet(j)
}

func (u *UnionFindDisjointSet) UnionSet(i,j int) {
    u.pset[u.FindSet(i)] = u.FindSet(j)
    u.cnt--
}

func main() {
    var n int
    var x,y [105]int
    var u UnionFindDisjointSet
    fmt.Scan(&n)
    u.InitSet(n)
    for i := 0; i < n; i++ { fmt.Scan(&x[i],&y[i]) }
    for i := 0; i < n; i++ {
        for j := i+1; j < n; j++ {
            if ((x[i] == x[j]) || (y[i] == y[j])) && !u.IsSameSet(i,j) {
                u.UnionSet(i,j)
            }
        }
    }
    fmt.Println(u.cnt-1)
}

//https://codeforces.com/problemset/problem/217/A
//http://bigocoder.com/courses/BLUE20/BLUE20_LEC16/BLUE_L16P05/statement
package main

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

var (
	rank   []int
	parent []int
)

type snowDrift struct {
	x int
	y int
}

func makeSet(n int) {
	parent = make([]int, n)

	for i := range parent {
		parent[i] = i
	}
}

func unionSet(a int, b int) {
	aParent := findSet(a)
	bParent := findSet(b)

	if aParent == bParent {
		return
	}
	if rank[aParent] > rank[bParent] {
		parent[bParent] = aParent
		return
	}
	if rank[aParent] < rank[bParent] {
		parent[aParent] = bParent
		return
	}

	parent[bParent] = aParent
	rank[aParent]++
}

func findSet(v int) int {
	if v != parent[v] {
		parent[v] = findSet(parent[v])
	}

	return parent[v]
}

func isSameSet(a snowDrift, b snowDrift) bool {
	return a.x == b.x || a.y == b.y
}

func main() {
	r := bufio.NewReader(os.Stdin)
	var n, set int
	fmt.Fscanf(r, "%d\n", &n)
	rank = make([]int, n)
	drifts := make([]snowDrift, n)
	makeSet(n)

	for i := range drifts {
		fmt.Fscanf(r, "%d %d\n", &drifts[i].x, &drifts[i].y)
		for j := 0; j < i; j++ {
			if isSameSet(drifts[i], drifts[j]) {
				unionSet(i, j)
			}
		}
	}

	for i := range parent {
		if i == parent[i] {
			set++
		}
	}

	fmt.Fprintf(os.Stdout, "%d\n", set-1)
}

// 217A
package main

import (
	"fmt"
)

var (
	d, a, s       [100]int
	q, w, e, r, t int
)

func l(q int) int {
	if d[q] != q {
		d[q] = l(d[q])
	}
	return d[q]
}
func main() {
	fmt.Scan(&q)
	t = q - 1
	for w = 0; w < q; w++ {
		fmt.Scan(&a[w], &s[w])
		d[w] = w
		for e = 0; e < w; e++ {
			if (a[e] == a[w]) || (s[e] == s[w]) {
				r = l(e)
				if r != w {
					d[r] = w
					t--
				}
			}
		}
	}
	fmt.Print(t)
}

// 217A-mic_rizal
package main

import (
    "fmt"
)

var v [110]bool
var n, ans int
var x, y [110]int

func dfs(i int) {
    v[i] = true
    for j := 0; j < n; j++ {
        if !v[j] && (x[i] == x[j] || y[i] == y[j]) {
            dfs(j)
        }
    }
}

func main() {
    fmt.Scan(&n)
    for i := 0; i < n; i++ {
        fmt.Scan(&x[i], &y[i])
    }
    for i := 0; i < n; i++ {
        if !v[i] {
            dfs(i)
            ans++
        }
    }
    fmt.Println(ans - 1)
}

package main

import (
	"fmt"
)

type point struct {
	x, y int
}

type union struct {
	next  *union
	depth int
	id    int
}

func (u *union) join(other *union) *union {
	for u.next != nil {
		u = u.next
	}
	for other.next != nil {
		other = other.next
	}
	if u == other {
		return u
	}
	if other.depth < u.depth {
		other.next = u
		return u
	} else if other.depth > u.depth {
		u.next = other
		return other
	}
	other.depth++
	u.next = other
	return other
}

func (u *union) last() *union {
	for u.next != nil {
		u = u.next
	}
	return u
}

func main() {
	var n int
	fmt.Scan(&n)
	sets := make(map[point]*union)
	verts := make(map[int][]point)
	horiz := make(map[int][]point)

	for i := 0; i < n; i++ {
		var x, y int
		fmt.Scan(&x, &y)
		pt := point{x: x, y: y}
		sets[pt] = &union{id: i}
		verts[y] = append(verts[y], pt)
		horiz[x] = append(horiz[x], pt)

	}

	for _, list := range verts {
		x := list[0]
		for _, pt := range list {
			sets[x] = sets[x].join(sets[pt])
		}
	}
	for _, list := range horiz {
		x := list[0]
		for _, pt := range list {
			sets[x] = sets[x].join(sets[pt])
		}
	}
	result := make(map[*union]struct{})

	for _, u := range sets {
		result[u.last()] = struct{}{}
	}
	fmt.Println(len(result) - 1)
}

package main

import (
	"fmt"
)

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

	x, y := make([]int, n), make([]int, n)
	parent := make([]int, n)
	sz := make([]int, n)

	for i := 0; i < n; i++ {
		parent[i] = i
		sz[i] = 1
		fmt.Scanf("%d%d", &x[i], &y[i])
	}

	var find func(int) int
	find = func(a int) int {
		if parent[a] == a {
			return a
		}
		parent[a] = find(parent[a])
		return parent[a]
	}

	var unite func(int, int)
	unite = func(a, b int) {
		a = find(a)
		b = find(b)
		if a != b {
			if sz[b] > sz[a] {
				a = a ^ b
				b = a ^ b
				a = a ^ b
			}
			parent[b] = a
			sz[a] += sz[b]
		}
	}

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if x[i] == x[j] || y[i] == y[j] {
				unite(i, j)
			}
		}
	}

	ans := -1
	for i := 0; i < n; i++ {
		if i == find(i) {
			ans++
		}
	}
	fmt.Printf("%d\n", ans)
}

package main

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

// Took from here: https://www.codementor.io/tucnak/using-golang-for-competitive-programming-h8lhvxzt3
var reader *bufio.Reader = bufio.NewReaderSize(os.Stdin, 10000000)
var writer *bufio.Writer = bufio.NewWriterSize(os.Stdout,10000000)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }

var n, count int
var matrix [1001][1001]int

func main() {
  // STDOUT MUST BE FLUSHED MANUALLY!!!
  defer writer.Flush()

  scanf("%d\n", &n)
  for i := 1; i <= n; i++ {
    var a, b int
    scanf("%d %d\n", &a, &b)
    matrix[a][b] = 1
  }

  for i := 1; i <= 1000; i++ {
    for j := 1; j <= 1000; j++ {
      if matrix[i][j] == 1 {
        count++
        dfs_adaptada(i, j)
        break
      }
    }
  }
  printf(strconv.Itoa(count - 1))
}

func dfs_adaptada(x, y int) () {
  if matrix[x][y] == 1 {
    matrix[x][y] = 2
    for i := x + 1; i <= 1000; i++ {
      if matrix[i][y] == 1 {
        dfs_adaptada(i, y)
      }
    }
    for j := y + 1; j <= 1000; j++ {
      if matrix[x][j] == 1 {
        dfs_adaptada(x, j)
      }
    }
  }
}

package main

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

// Took from here: https://www.codementor.io/tucnak/using-golang-for-competitive-programming-h8lhvxzt3
var reader *bufio.Reader = bufio.NewReaderSize(os.Stdin, 10000000)
var writer *bufio.Writer = bufio.NewWriterSize(os.Stdout,10000000)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }

func main() {
  // STDOUT MUST BE FLUSHED MANUALLY!!!
  defer writer.Flush()

  var n int
  scanf("%d\n", &n)
  var xcount [1001]int
  var ycount [1001]int
  var matrix [1001][1001]int
  for i := 1; i <= n; i++ {
    var x, y int
    scanf("%d %d\n", &x, &y)
    matrix[x][y] = 1
    xcount[x]++;
    ycount[y]++;
  }
  count := 0
  for i := 1; i <= 1000; i++ {
    for j := 1; i <= 1000; i++ {
      if matrix[i][j] == 1 && xcount[i] == 1 && ycount[j] == 1 {
        count++
      }
    }
  }
  fmt.Println(count)
}

package main

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

// Took from here: https://www.codementor.io/tucnak/using-golang-for-competitive-programming-h8lhvxzt3
var reader *bufio.Reader = bufio.NewReaderSize(os.Stdin, 10000000)
var writer *bufio.Writer = bufio.NewWriterSize(os.Stdout,10000000)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }

var n, count int
var matrix [1001][1001]int

func main() {
  // STDOUT MUST BE FLUSHED MANUALLY!!!
  defer writer.Flush()

  scanf("%d %d\n", &n)
  for i := 1; i <= n; i++ {
    var a, b int
    scanf("%d %d\n", &a, &b)
    matrix[a][b] = 1
  }

  for i := 1; i <= 1000; i++ {
    for j := 1; j <= 1000; j++ {
      if matrix[i][j] == 1 {
        count++
        dfs_adaptada(i, j)
        break
      }
    }
  }
  printf(strconv.Itoa(count - 1))
}

func dfs_adaptada(x, y int) () {
  if matrix[x][y] == 1 {
    matrix[x][y] = 2
    for i := x + 1; i <= 1000; i++ {
      if matrix[i][y] == 1 {
        dfs_adaptada(i, y)
      }
    }
    for j := y + 1; j <= 1000; j++ {
      if matrix[x][j] == 1 {
        dfs_adaptada(x, j)
      }
    }
  }
}

//https://codeforces.com/problemset/problem/217/A
//http://bigocoder.com/courses/BLUE20/BLUE20_LEC16/BLUE_L16P05/statement
package main

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

var (
	rank   []int
	parent []int
)

type snowDrift struct {
	x int
	y int
}

func makeSet(n int) {
	parent = make([]int, n)

	for i := range parent {
		parent[i] = i
	}
}

func unionSet(a int, b int) {
	aParent := findSet(a)
	bParent := findSet(b)

	if aParent == bParent {
		return
	}
	if rank[aParent] > rank[bParent] {
		parent[b] = aParent
		return
	}
	if rank[aParent] < rank[bParent] {
		parent[a] = bParent
		return
	}

	parent[bParent] = aParent
	rank[aParent]++
}

func findSet(v int) int {
	if v != parent[v] {
		parent[v] = findSet(parent[v])
	}

	return parent[v]
}

func isSameSet(a snowDrift, b snowDrift) bool {
	return a.x == b.x || a.y == b.y
}

func main() {
	r := bufio.NewReader(os.Stdin)
	var n, set int
	fmt.Fscanf(r, "%d\n", &n)
	rank = make([]int, n)
	drifts := make([]snowDrift, n)
	makeSet(n)

	for i := range drifts {
		fmt.Fscanf(r, "%d %d\n", &drifts[i].x, &drifts[i].y)
		for j := 0; j < i; j++ {
			if isSameSet(drifts[i], drifts[j]) {
				unionSet(i, j)
			}
		}
	}

	for i := range parent {
		if i == parent[i] {
			set++
		}
	}

	fmt.Fprintf(os.Stdout, "%d\n", set-1)
}

//https://codeforces.com/problemset/problem/217/A
//http://bigocoder.com/courses/BLUE20/BLUE20_LEC16/BLUE_L16P05/statement
package main

import "bufio"

import "os"

import "fmt"

var (
	rank   []int
	parent []int
)

type snowDrift struct {
	x int
	y int
}

func makeSet(n int) {
	parent = make([]int, n)

	for i := range parent {
		parent[i] = i
	}
}

func isSameSet(a snowDrift, b snowDrift) bool {
	return a.x == b.x || a.y == b.y
}

func main() {
	r := bufio.NewReader(os.Stdin)
	var n, set int
	fmt.Fscanf(r, "%d\n", &n)
	rank = make([]int, n)
	drifts := make([]snowDrift, n)
	makeSet(n)

	for i := range drifts {
		fmt.Fscanf(r, "%d %d\n", &drifts[i].x, &drifts[i].y)
	}

	for i := range drifts {
		for j := i + 1; j < n; j++ {
			if !isSameSet(drifts[i], drifts[j]) {
				set++
			} else {
				break
			}
		}
	}

	fmt.Println(set)
}

//https://codeforces.com/problemset/problem/217/A
//http://bigocoder.com/courses/BLUE20/BLUE20_LEC16/BLUE_L16P05/statement
package main

import "bufio"

import "fmt"

import "os"

var (
	rank   []int
	parent []int
)

type snowDrift struct {
	x int
	y int
}

func makeSet(n int) {
	parent = make([]int, n)

	for i := range parent {
		parent[i] = i
	}
}

func unionSet(a int, b int) {
	aParent := findSet(a)
	bParent := findSet(b)

	if aParent == bParent {
		return
	}
	if rank[aParent] > rank[bParent] {
		parent[b] = aParent
		return
	}
	if rank[aParent] < rank[bParent] {
		parent[a] = bParent
		return
	}

	parent[b] = aParent
	rank[aParent]++
}

func findSet(v int) int {
	if v != parent[v] {
		parent[v] = findSet(parent[v])
	}

	return parent[v]
}

func isSameSet(a snowDrift, b snowDrift) bool {
	return a.x == b.x || a.y == b.y
}

func main() {
	r := bufio.NewReader(os.Stdin)
	var n, set int
	fmt.Fscanf(r, "%d\n", &n)
	rank = make([]int, n)
	drifts := make([]snowDrift, n)
	makeSet(n)

	for i := range drifts {
		fmt.Fscanf(r, "%d %d\n", &drifts[i].x, &drifts[i].y)
	}

	for i := range drifts {
		for j := i + 1; j < n; j++ {
			if isSameSet(drifts[i], drifts[j]) {
				unionSet(i, j)
			}
		}
	}

	for i := range parent {
		if i == parent[i] {
			set++
		}
	}

	fmt.Fprintf(os.Stdout, "%d\n", set-1)
}

//https://codeforces.com/problemset/problem/217/A
//http://bigocoder.com/courses/BLUE20/BLUE20_LEC16/BLUE_L16P05/statement
package main

import "bufio"

import "strings"

import "fmt"

var (
	rank   []int
	parent []int
)

type snowDrift struct {
	x int
	y int
}

func makeSet(n int) {
	parent = make([]int, n)

	for i := range parent {
		parent[i] = i
	}
}

func isSameSet(a snowDrift, b snowDrift) bool {
	return a.x == b.x || a.y == b.y
}

func main() {
	r := bufio.NewReader(strings.NewReader(`4
	1 2
	1 4
	4 5
	6 3
	`))
	var n, set int
	fmt.Fscanf(r, "%d\n", &n)
	rank = make([]int, n)
	drifts := make([]snowDrift, n)
	makeSet(n)

	for i := range drifts {
		fmt.Fscanf(r, "%d %d\n", &drifts[i].x, &drifts[i].y)
	}

	for i := range drifts {
		for j := i + 1; j < n; j++ {
			if !isSameSet(drifts[i], drifts[j]) {
				set++
			} else {
				break
			}
		}
	}

	fmt.Println(set)
}

// 217A
package main

import (
	"fmt"
)

var (
	n, x, y, ans int
	f, b         [2005]int
)

func get(x int) int {
	if f[x] == x {
		return x
	}
	return get(f[x])
}
func main() {
	fmt.Scan(&n)
	for i := 1; i <= 2000; i++ {
		f[i] = i
	}
	for i := 1; i <= n; i++ {
		fmt.Scanln(&x, &y)
		b[x] = b[y+1000]
		t1 := get(x)
		t2 := get(y + 1000)
		if t1 != t2 {
			f[t1] = t2
		}
	}
	for i := 1; i <= 2000; i++ {
		if f[i] == i && f[i] == b[i] {
			ans++
		}
	}
	fmt.Println(ans - 1)
	return
}

package main

import (
	"fmt"
)

type point struct {
	x, y int
}

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

	drifts := make(map[point]bool)
	potentials := make(map[point][]point)

	for i := 0; i < n; i++ {
		var x, y int
		fmt.Scan(&x, &y)
		pt := point{x: x, y: y}
		for v := range drifts {
			if v.x == x || v.y == y {
				continue
			}
			for _, tmp := range [2]point{
				point{x: v.x, y: y},
				point{x: x, y: v.y},
			} {
				potentials[tmp] = append(potentials[tmp], pt, v)
			}
		}
		drifts[pt] = true
	}
	total := 0

	for len(drifts) > 0 && len(potentials) > 0 {
		best := point{}
		var max int

		for v, list := range potentials {
			if max < len(list) {
				max = len(list)
				best = v
			}
		}

		total++
		for _, v := range potentials[best] {
			delete(drifts, v)
		}
		delete(potentials, best)
	}
	fmt.Println(total)
}

package main

import (
	"fmt"
)

type point struct {
	x, y int
}

type union struct {
	next  *union
	depth int
	id    int
}

func (u *union) join(other *union) *union {
	for u.next != nil {
		u = u.next
	}
	for other.next != nil {
		other = other.next
	}
	if u == other {
		return u
	}
	if other.depth < u.depth {
		other.next = u
		return u
	} else if other.depth > u.depth {
		u.next = other
		return other
	}
	other.depth++
	u.next = other
	return other
}

func (u *union) last() *union {
	for u.next != nil {
		u = u.next
	}
	return u
}

func main() {
	var n int
	fmt.Scan(&n)
	sets := make(map[point]*union)
	verts := make(map[int][]point)
	horiz := make(map[int][]point)

	for i := 0; i < n; i++ {
		var x, y int
		fmt.Scan(&x, &y)
		pt := point{x: x, y: y}
		sets[pt] = &union{id: i}
		verts[y] = append(verts[y], pt)
		horiz[x] = append(horiz[x], pt)

	}

	for _, list := range verts {
		x := list[0]
		for _, pt := range list {
			sets[x] = sets[x].join(sets[pt])
		}
	}
	for _, list := range horiz {
		x := list[0]
		for _, pt := range list {
			sets[x] = sets[x].join(sets[pt])
		}
	}
	result := make(map[*union]struct{})

	for _, u := range sets {
		result[u] = struct{}{}
	}
	fmt.Println(len(result) - 1)
}


You are given an array $$$a$$$ consisting of $$$n$$$ integers. Let's denote monotonic renumeration of array $$$a$$$ as an array $$$b$$$ consisting of $$$n$$$ integers such that all of the following conditions are met:  $$$b_1 = 0$$$;  for every pair of indices $$$i$$$ and $$$j$$$ such that $$$1 \le i, j \le n$$$, if $$$a_i = a_j$$$, then $$$b_i = b_j$$$ (note that if $$$a_i \ne a_j$$$, it is still possible that $$$b_i = b_j$$$);  for every index $$$i \in [1, n - 1]$$$ either $$$b_i = b_{i + 1}$$$ or $$$b_i + 1 = b_{i + 1}$$$. For example, if $$$a = [1, 2, 1, 2, 3]$$$, then two possible monotonic renumerations of $$$a$$$ are $$$b = [0, 0, 0, 0, 0]$$$ and $$$b = [0, 0, 0, 0, 1]$$$.Your task is to calculate the number of different monotonic renumerations of $$$a$$$. The answer may be large, so print it modulo $$$998244353$$$.

The first line contains one integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$).

Print one integer — the number of different monotonic renumerations of $$$a$$$, taken modulo $$$998244353$$$.

package main

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

func mmod(n int64, d int64) int64 {
	var mod int64 = 998244353
	var ans int64 = 1
	for n > 0 {
		if (n & 1) == 1 {
			ans = (ans * d) % mod
		}
		d = (d * d) % mod
		n >>= 1
	}
	return ans
}
func main() {
	buf := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscanf(buf, "%d\n", &n)
	//fmt.Printf("n=%d\n",n)
	mat := make([]int, n+1)
	wmap := make(map[int]int)
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &mat[i])
		wmap[mat[i]] = i
	}

	var mi int64 = 0
	var front int = 0
	for front < n {
		p := front
		for p < n && p <= front {
			if front < wmap[mat[p]] {
				front = wmap[mat[p]]
			}
			p++
		}
		front++
		mi++
	}
//	fmt.Printf("mi=%d\n",mi)
	fmt.Printf("%d\n",mmod(mi-1,2))
}

package main

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

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

func Sol1102E(reader io.Reader, writer io.Writer) {
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n int
	Fscan(in, &n)
	arr := make([]int, n)
	rightI := make(map[int]int, n)
	for i := 0; i < n; i++ {
		Fscan(in, &arr[i])
		rightI[arr[i]] = i
	}

	ans := 1
	for l, r := 0, 0; r < n; {
		checkVal := arr[r]
		for i := l; i < rightI[checkVal]; i++ {
			r = max(r, rightI[arr[i]])
			checkVal = arr[r]
		}
		if r+1 == n {
			break
		}
		ans = (ans << 1) % 998244353
		l = rightI[checkVal] + 1
		r++
	}
	Fprintln(out, ans)
}

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

package main

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

type SegTree struct {
	st []int
	lazy []int
	has []bool
	size int
	el_neutro int
}

func f(a, b int) int {
	return a+b
}

func NewSegTree(n int) *SegTree {
	t := new(SegTree)
	t.st = make([]int, 4*n)
	t.lazy = make([]int, 4*n)
	t.has = make([]bool, 4*n)
	t.size = n
	return t
}

func (t *SegTree) propagate(sti, stl, str int) {
    if t.has[sti] {
        t.st[sti] = t.lazy[sti]*(str-stl+1)
        if stl != str {
            t.lazy[sti*2 + 1] = t.lazy[sti]
            t.lazy[sti*2 + 2] = t.lazy[sti]

            t.has[sti*2 + 1] = true
            t.has[sti*2 + 2] = true
        }
        t.has[sti] = false
    }
}

func (t *SegTree) query(sti, stl, str, l, r int) int {
    t.propagate(sti, stl, str)

    if str < l || r < stl {
        return t.el_neutro
    }

    if stl >= l && str <= r {
        return t.st[sti]
    }

    mid := (str+stl)/2

    return f(t.query(sti*2+1,stl,mid,l,r),t.query(sti*2+2,mid+1,str,l,r))
}

func (t *SegTree) UpdateRange(sti, stl, str, l, r, amm int) {
    t.propagate(sti, stl, str)

    if stl >= l && str <= r {
        t.lazy[sti] = amm
        t.has[sti] = true
        t.propagate(sti, stl, str)
        return
    }

    if stl > r || str < l {
        return
    }

    mid := (stl + str)/2

    t.UpdateRange(sti*2+1,stl,mid,l,r,amm)
    t.UpdateRange(sti*2+2,mid+1,str,l,r,amm)

    t.st[sti] = f(t.st[sti*2+1],t.st[sti*2+2])
}

func (t *SegTree) PQuery(l, r int) int {
    return t.query(0,0,t.size-1,l,r)
}

func (t *SegTree) PUpdateRange(l, r, amm int){
    t.UpdateRange(0,0,t.size-1,l,r,amm);
}

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

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

func init() {
	// const maxCapacity = SIZE
	// buf := make([]byte, maxCapacity)
	// scanner.Buffer(buf, maxCapacity)
	scanner.Split(bufio.ScanWords)
}

func main() {
	defer writer.Flush()

	n := ReadInt()

	st := NewSegTree(n)

	occ := make(map[int][]int)

	for i := 0; i < n; i++ {
		st.PUpdateRange(i,i,1)
		x := ReadInt()
		occ[x] = append(occ[x], i)
	}

	for _, p := range occ {
		if len(p) >= 2 {
			st.PUpdateRange(p[0]+1, p[len(p)-1], 0)
		}
	}

	inter := st.PQuery(0, n)
	resp := int64(1)
	for i := 0; i < inter-1; i++ {
		resp = (resp*2)%998244353
	}

    writer.WriteString(strconv.FormatInt(resp, 10))
}

package main

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

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

func Sol1102E(reader io.Reader, writer io.Writer) {
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n int
	Fscan(in, &n)
	arr := make([]int, n)
	rightI := make(map[int]int, n)
	for i := 0; i < n; i++ {
		Fscan(in, &arr[i])
		rightI[arr[i]] = i
	}

	ans := 1
	for l, r := 0, 0; r < n; {
		checkVal := arr[r]
		for _, val := range arr[l:rightI[checkVal]] {
			r = max(r, rightI[val])
		}
		if r+1 == n {
			break
		}
		ans = (ans << 1) % 998244353
		l = rightI[checkVal] + 1
		r++
	}
	Fprintln(out, ans)
}

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


There are $$$n$$$ piles of stones, where the $$$i$$$-th pile has $$$a_i$$$ stones. Two people play a game, where they take alternating turns removing stones.In a move, a player may remove a positive number of stones from the first non-empty pile (the pile with the minimal index, that has at least one stone). The first player who cannot make a move (because all piles are empty) loses the game. If both players play optimally, determine the winner of the game.

The first line contains a single integer $$$t$$$ ($$$1\le t\le 1000$$$)  — the number of test cases. Next $$$2t$$$ lines contain descriptions of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1\le n\le 10^5$$$)  — the number of piles. The second line of each test case contains $$$n$$$ integers $$$a_1,\ldots,a_n$$$ ($$$1\le a_i\le 10^9$$$)  — $$$a_i$$$ is equal to the number of stones in the $$$i$$$-th pile. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.

For each test case, if the player who makes the first move will win, output "First". Otherwise, output "Second".

First
Second
Second
First
First
Second
First

NoteIn the first test case, the first player will win the game. His winning strategy is:   The first player should take the stones from the first pile. He will take $$$1$$$ stone. The numbers of stones in piles will be $$$[1, 5, 4]$$$.  The second player should take the stones from the first pile. He will take $$$1$$$ stone because he can't take any other number of stones. The numbers of stones in piles will be $$$[0, 5, 4]$$$.  The first player should take the stones from the second pile because the first pile is empty. He will take $$$4$$$ stones. The numbers of stones in piles will be $$$[0, 1, 4]$$$.  The second player should take the stones from the second pile because the first pile is empty. He will take $$$1$$$ stone because he can't take any other number of stones. The numbers of stones in piles will be $$$[0, 0, 4]$$$.  The first player should take the stones from the third pile because the first and second piles are empty. He will take $$$4$$$ stones. The numbers of stones in piles will be $$$[0, 0, 0]$$$.  The second player will lose the game because all piles will be empty.

package main

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

func main() {
	io := newFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	for t := io.nextInt(); t > 0; t-- {
		n := io.nextInt()
		a := io.nextInts(n)

		win := 0
		for i := n - 2; i >= 0; i-- {
			if a[i] == 1 {
				win ^= 1
			} else {
				win = 0
			}
		}
		if win == 0 {
			io.println("First")
		} else {
			io.println("Second")
		}
	}
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p [1]byte
}

func newFastIO(r io.Reader, w io.Writer) *fastIO {
	return &fastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p[:])
	for !('0' <= f.p[0] && f.p[0] <= '9') {
		f.Read(f.p[:])
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p[:])
	}
	return n
}

func (f *fastIO) nextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.nextInt()
	}
	return a
}

func (f *fastIO) next() string {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p[:])
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

func (f *fastIO) printInts(a ...int) {
	if len(a) > 0 {
		f.print(a[0])
		for _, x := range a[1:] {
			f.print(" ", x)
		}
	}
}

func (f *fastIO) printlnInts(a ...int) {
	f.printInts(a...)
	f.println()
}

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++ {
		readLine(reader)
		line := readLine(reader)
		ai := strings.Split(string(line), " ")
		arr := make([]int, len(ai)+1)
		arr[0] = 0
		arr[1] = 1
		for i := 2; i <= len(ai); i++ {
			last, _ := strconv.ParseInt(ai[i-2], 10, 64)
			if last == 1 {
				arr[i] = arr[i-2]
			} else {
				arr[i] = arr[i-1]
			}
		}
		if arr[len(ai)] == 1 {
			fmt.Println("First")
		} else {
			fmt.Println("Second")
		}
	}
}

const str = "abacaba"

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 {
			break
		}
	}
	return result
}

package main

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

func main() {
	var t int
	fmt.Scan(&t)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	for i := 0; i < t; i++ {
		scanner.Scan()
		n := toInt(scanner.Bytes())
		fir := -1
		// in := make([]int, n)

		for j := 1; j <= n; j++ {
			scanner.Scan()
			// in[j] = toInt(scanner.Bytes())
			z := toInt(scanner.Bytes())
			if (z != 1) && (fir == -1) {
				fir = j - 1
			}
		}
		if fir == -1 {
			if ok := n & 1; ok == 1 {
				fmt.Println("First")
			} else {
				fmt.Println("Second")
			}
		} else {
			if ok := fir & 1; ok == 1 {
				fmt.Println("Second")
			} else {
				fmt.Println("First")
			}
		}

	}
}

func toInt(buf []byte) (n int) {
	for _, v := range buf {
		n = n*10 + int(v-'0')
	}
	return
}

package main

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

func main() {
	var t int
	fmt.Scan(&t)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	for i := 0; i < t; i++ {
		scanner.Scan()
		n := toInt(scanner.Bytes())
		in := make([]int, n)

		for j := 0; j < n; j++ {
			scanner.Scan()
			in[j] = toInt(scanner.Bytes())
		}
		flag := true
	Loop:
		for _, v := range in {
			if v == 0 {
				continue Loop
			}
			if v == 1 {
				v = 0
				flag = !flag
				continue Loop
			}
			for {
				if v == 0 {
					break
				}
				if v == 1 {
					v = 0
					flag = !flag
					continue Loop
				} else {
					flag = !flag
					break Loop
				}
			}
		}
		if flag {
			fmt.Println("Second")
		} else {
			fmt.Println("First")
		}

	}
}

func toInt(buf []byte) (n int) {
	for _, v := range buf {
		n = n*10 + int(v-'0')
	}
	return
}

package main

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

const (
	SCANNER_BUF_SIZE = 2 << 20

	INF = int64(math.MaxInt64)
	MOD = 1e9 + 7
)

var (
	writer  *bufio.Writer
	scanner *bufio.Scanner
)

func init() {
	writer = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, SCANNER_BUF_SIZE), SCANNER_BUF_SIZE)
}

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

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

func suint64() uint64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

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

func gcd(a, b int64) int64 {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

func lcm(a, b int64) int64 {
	return a * b / gcd(a, b)
}

func abs(a int64) int64 {
	if a < 0 {
		return a * -1
	}
	return a
}

func min(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r < a {
			a = r
		}
	}
	return a
}

func max(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r > a {
			a = r
		}
	}
	return a
}

func main() {
	defer writer.Flush()
	t := sint64()
	for ; t > 0; t-- {
		n := sint64()
		a := make([]int64, n)
		for i := range a {
			a[i] = sint64()
		}
		printf("%s\n", sol(a))
	}
}

func sol(a []int64) string {
	winner := 0
	p := 0
	for p < len(a) {
		if a[p] == 1 {
			winner ^= 1
			p++
			continue
		}
		break
	}
	if p == len(a) {
		winner ^= 1
	}
	if winner == 1 {
		return "Second"
	}
	return "First"
}


Pasha got a very beautiful string s for his birthday, the string consists of lowercase Latin letters. The letters in the string are numbered from 1 to |s| from left to right, where |s| is the length of the given string.Pasha didn't like his present very much so he decided to change it. After his birthday Pasha spent m days performing the following transformations on his string — each day he chose integer ai and reversed a piece of string (a segment) from position ai to position |s| - ai + 1. It is guaranteed that 2·ai ≤ |s|.You face the following task: determine what Pasha's string will look like after m days.

The first line of the input contains Pasha's string s of length from 2 to 2·105 characters, consisting of lowercase Latin letters. The second line contains a single integer m (1 ≤ m ≤ 105) —  the number of days when Pasha changed his string. The third line contains m space-separated elements ai (1 ≤ ai; 2·ai ≤ |s|) — the position from which Pasha started transforming the string on the i-th day.

In the first line of the output print what Pasha's string s will look like after m days.

package main

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

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

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

	s := readLine()
	t := []byte(s)
	scanner = bufio.NewScanner(strings.NewReader(readLine()))
	m, n := nextInt(), len(t)
	scanner = bufio.NewScanner(strings.NewReader(readLine()))
	scanner.Split(bufio.ScanWords)
	a := make([]int, 100005)
	// println(m)
	for i := 0; i < m; i++ {
		// c := nextInt()
		// println(c)
		// a[c]++
		a[nextInt()]++
	}

	for i := 1; i < len(a); i++ {
		a[i] = (a[i] + a[i-1])%2
	}
	// println(a)
	for i := 0; i < n/2; i++ {
		if a[i+1] != 0 {
			t[i], t[n-1-i] = t[n-i-1], t[i]
		}
	}

	// println(string(t))
	writer.WriteString(fmt.Sprintf("%s\n", t))
}

func readLine() string {
	line, _ := reader.ReadString('\n')
	return strings.TrimSpace(line)
}

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

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

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

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

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

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

package main

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

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

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

	s := ""
	scan(&s)
	t := []byte(s)
	m, n := nextInt(), len(t)
	a := make([]int, 100005)

	// println(m)
	for i := 0; i < m; i++ {
		a[nextInt()]++
	}

	for i := 1; i < len(a); i++ {
		a[i] = (a[i] + a[i-1])%2
	}
	// println(a)
	for i := 0; i < n/2; i++ {
		if a[i+1] != 0 {
			t[i], t[n-1-i] = t[n-i-1], t[i]
		}
	}

	printf("%s\n", t)

}

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

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

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

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

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

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

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

package main

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

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

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

	s := readLine()
	t := []byte(s)
	scanner = bufio.NewScanner(strings.NewReader(readLine()))
	m, n := nextInt(), len(t)
	scanner = bufio.NewScanner(strings.NewReader(readLine()))
	scanner.Split(bufio.ScanWords)
	a := make([]int, 100005)
	// println(m)
	for i := 0; i < m; i++ {
		// c := nextInt()
		// println(c)
		// a[c]++
		a[nextInt()]++
	}

	for i := 1; i < len(a); i++ {
		a[i] = (a[i] + a[i-1])%2
	}
	// println(a)
	for i := 0; i < n/2; i++ {
		if a[i+1] != 0 {
			t[i], t[n-1-i] = t[n-i-1], t[i]
		}
	}

	println(string(t))
	// writer.WriteString(fmt.Sprintf("%s\n", t))
}

func readLine() string {
	line, _ := reader.ReadString('\n')
	return strings.TrimSpace(line)
}

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

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

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

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

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

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

package main

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

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

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

	s := ""
	scan(&s)
	t := []byte(s)
	m, n := nextInt(), len(t)
	a := make([]int, 100005)

	// println(m)
	for i := 0; i < m; i++ {
		// c := nextInt()
		// println(c)
		// a[c]++
		a[nextInt()]++
	}

	for i := 1; i < len(a); i++ {
		a[i] = (a[i] + a[i-1])%2
	}
	// println(a)
	for i := 0; i < n/2; i++ {
		if a[i+1] != 0 {
			t[i], t[n-1-i] = t[n-i-1], t[i]
		}
	}

	println(string(t))

}

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

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

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

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

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

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

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

package main

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

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

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

	s := ""
	scan(&s)
	t := []byte(s)
	m := nextInt()
	a := make([]int, 100005)
	swapPos := make([]int, 0, 100)

	for i := 0; i < m; i++ {
		a[nextInt()-1]++
	}

	for i := 0; i < len(a); i++ {
		if a[i] > 0 && a[i] % 2 != 0 {
			swapPos = append(swapPos, i)
		}
	}

	for i := 0; i < len(swapPos); i++ {

		// println(string(t))
		if i == len(swapPos)-1 {
			swapSubString(t, swapPos[i], len(t)/2, i+1)
		} else {
			swapSubString(t, swapPos[i], swapPos[i+1], i+1)
		}
		// println(string(t))
	}

	println(string(t))

}

func swapSubString(s []byte, pos, nextPos, swap int) {
	if swap % 2 == 0 {
		return
	}

	for i, j := pos, len(s)-pos-1; i < nextPos; i, j = i+1, j-1 {
		s[i], s[j] = s[j], s[i]
	}
}

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

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

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

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

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

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

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

package main

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

var scanner *bufio.Scanner

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

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

  line, _ := reader.ReadString('\n')
  s := strings.TrimSpace(line)
  n := len(s)
  line, _ = reader.ReadString('\n')
  q, _ := strconv.Atoi(strings.TrimSpace(line))
  line, _ = reader.ReadString('\n')
  scanner = bufio.NewScanner(strings.NewReader(line))
  scanner.Split(bufio.ScanWords)
  reverse := make([]bool, (n+1)/2)
  for qi := 0; qi < q; qi++ {
    pos := getI()-1
    reverse[pos] = !reverse[pos]
  }
  result := []byte(s)
  reversed := false
  for pos := 0; pos < len(reverse); pos++ {
    if reverse[pos] {
      reversed = !reversed
    }
    alt := n - 1 - pos
    if reversed {
      result[pos], result[alt] = result[alt], result[pos]
    }
  }
  writer.WriteString(fmt.Sprintf("%s\n", result))
}

package main

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

func main() {
	s := bufio.NewScanner(os.Stdin)
	s.Buffer([]byte(nil), 1e6)
	s.Split(bufio.ScanWords)
	s.Scan()
	xs := s.Text()
	s.Scan()
	m, _ := strconv.Atoi(s.Text())
	as := make([]int, m)
	for i := 0; i < m; i++ {
		s.Scan()
		as[i], _ = strconv.Atoi(s.Text())
	}

	fmt.Println(solveFaster(xs, as))
}

func solve(xs string, as []int) string {
	rs := []rune(xs)
	s := len(rs)

	for i := 0; i < len(as); i++ {
		l := as[i] - 1
		r := (s - as[i] + 1) - 1
		reverse(rs[l : r+1])
	}
	return string(rs)
}

func solveFaster(xs string, as []int) string {
	rs := []rune(xs)
	s := len(rs)
	buckets := make([]int, s)
	sum := make([]int, s)

	for i := 0; i < len(as); i++ {
		l := as[i] - 1
		buckets[l]++
	}

	runningSum := 0
	for i := 0; i < s; i++ {
		runningSum += buckets[i]
		sum[i] = runningSum
	}

	for i, j := 0, len(rs)-1; i < j; i, j = i+1, j-1 {
		if sum[i]%2 == 1 {
			rs[i], rs[j] = rs[j], rs[i]
		}
	}

	return string(rs)
}

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

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

func toLower(x byte) byte {
	return x + ('a' - 'A')
}

func isUpper(x byte) bool {
	return x >= 'A' && x <= 'Z'
}

func initArray(n, x int) []int {
	xs := make([]int, n)
	for i := 0; i < n; i++ {
		xs[i] = x
	}
	return xs
}

func nextInt(s *bufio.Scanner) (n int) {
	s.Scan()
	n, _ = strconv.Atoi(s.Text())
	return
}

package main

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

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

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

	s := readLine()
	t := []byte(s[:len(s)-1])
	scanner = bufio.NewScanner(strings.NewReader(readLine()))
	scanner.Split(bufio.ScanWords)
	m, n := nextInt(), len(t)
	scanner = bufio.NewScanner(strings.NewReader(readLine()))
	scanner.Split(bufio.ScanWords)
	a := make([]int, 50005)
	println(m)
	for i := 0; i < m; i++ {
		c := nextInt()
		println(c)
		// a[nextInt()]++
	}

	for i := 1; i < len(a); i++ {
		a[i] = (a[i] + a[i-1])%2
	}
	// println(a)
	for i := 0; i < n/2; i++ {
		if a[i+1] != 0 {
			t[i], t[n-1-i] = t[n-i-1], t[i]
		}
	}

	println(string(t))
}

func readLine() string {
	line, _ := reader.ReadString('\n')
	return line
}

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

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

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

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

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

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

package main

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

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

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

	s := readLine()
	t := []byte(s[:len(s)-1])
	scanner = bufio.NewScanner(strings.NewReader(readLine()))
	scanner.Split(bufio.ScanWords)
	m, n := nextInt(), len(t)
	scanner = bufio.NewScanner(strings.NewReader(readLine()))
	scanner.Split(bufio.ScanWords)
	a := make([]int, 50005)

	for i := 0; i < m; i++ {
		a[nextInt()]++
	}

	for i := 1; i < len(a); i++ {
		a[i] = (a[i] + a[i-1])%2
	}
	// println(a)
	for i := 0; i < n/2; i++ {
		if a[i+1] != 0 {
			t[i], t[n-1-i] = t[n-i-1], t[i]
		}
	}

	println(string(t))
}

func readLine() string {
	line, _ := reader.ReadString('\n')
	return line
}

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

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

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

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

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

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

package main

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

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

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

	s := readLine()
	t := []byte(s[:len(s)-1])
	scanner = bufio.NewScanner(strings.NewReader(readLine()))
	scanner.Split(bufio.ScanWords)
	m, n := nextInt(), len(t)
	scanner = bufio.NewScanner(strings.NewReader(readLine()))
	scanner.Split(bufio.ScanWords)
	a := make([]int, 50005)
	println(m)
	for i := 0; i < m; i++ {
		c := nextInt()
		println(c)
		a[c]++
		// a[nextInt()]++
	}

	for i := 1; i < len(a); i++ {
		a[i] = (a[i] + a[i-1])%2
	}
	// println(a)
	for i := 0; i < n/2; i++ {
		if a[i+1] != 0 {
			t[i], t[n-1-i] = t[n-i-1], t[i]
		}
	}

	println(string(t))
}

func readLine() string {
	line, _ := reader.ReadString('\n')
	return line
}

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

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

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

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

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

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

package main

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

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

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

	s := readLine()
	t := []byte(s[:len(s)-1])
	scanner = bufio.NewScanner(strings.NewReader(readLine()))
	m, n := nextInt(), len(t)
	scanner = bufio.NewScanner(strings.NewReader(readLine()))
	scanner.Split(bufio.ScanWords)
	a := make([]int, 50005)

	for i := 0; i < m; i++ {
		a[nextInt()]++
	}

	for i := 1; i < len(a); i++ {
		a[i] = (a[i] + a[i-1])%2
	}
	// println(a)
	for i := 0; i < n/2; i++ {
		if a[i+1] != 0 {
			t[i], t[n-1-i] = t[n-i-1], t[i]
		}
	}

	println(string(t))
}

func readLine() string {
	line, _ := reader.ReadString('\n')
	return line
}

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

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

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

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

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

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

package main

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

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

const ALPHAB int = 26

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

	var (
		n, key int
		s string
	)

	alph := make([]int, ALPHAB)

	scan(&n, &s)

	for i := 0; i < len(s); i += 2 {
		alph[s[i] - 'a']++

		if alph[s[i+1] - 'A'] == 0 {
			key++
		} else {
			alph[s[i+1] - 'A']--
		}
	}

	println(key)
}

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

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

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

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

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

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

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

package main

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

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

  line, _ := reader.ReadString('\n')
  n, _ := strconv.Atoi(strings.TrimSpace(line))
  m := 2*n-2
  line, _ = reader.ReadString('\n')
  line = strings.ToLower(strings.TrimSpace(line))
  result := 0
  keys := map[byte]int{}
  for i := 0; i < m; i += 2 {
    key, door := line[i], line[i+1]
    keys[key]++
    if keys[door] == 0 {
      result++
    } else {
      keys[door]--
    }
  }
  writer.WriteString(fmt.Sprintf("%d\n", result))
}


You are given a sequence $$$b_1, b_2, \ldots, b_n$$$. Find the lexicographically minimal permutation $$$a_1, a_2, \ldots, a_{2n}$$$ such that $$$b_i = \min(a_{2i-1}, a_{2i})$$$, or determine that it is impossible.

Each test contains one or more test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The first line of each test case consists of one integer $$$n$$$ — the number of elements in the sequence $$$b$$$ ($$$1 \le n \le 100$$$). The second line of each test case consists of $$$n$$$ different integers $$$b_1, \ldots, b_n$$$ — elements of the sequence $$$b$$$ ($$$1 \le b_i \le 2n$$$). It is guaranteed that the sum of $$$n$$$ by all test cases doesn't exceed $$$100$$$.

For each test case, if there is no appropriate permutation, print one number $$$-1$$$. Otherwise, print $$$2n$$$ integers $$$a_1, \ldots, a_{2n}$$$ — required lexicographically minimal permutation of numbers from $$$1$$$ to $$$2n$$$.

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++ {
		ns := readLine(reader)
		n, _ := strconv.Atoi(string(ns))
		arrs := readLine(reader)
		// fmt.Println(n, string(arrs))
		arrss := strings.Split(string(arrs), " ")
		barr := make([]int, n)
		result := make([]int, 0)
		mb := make(map[int]struct{})
		mall := make(map[int]struct{})
		for i := 0; i < n; i++ {
			barr[i], _ = strconv.Atoi(arrss[i])
			mb[barr[i]] = struct{}{}
			mall[barr[i]] = struct{}{}
		}
		rflag := true
		for i := 0; i < n; i++ {
			number := barr[i]
			flag := false
			for j:=number+1;j<=2*n;j++ {
				if _, ok:= mall[j]; !ok {
					result = append(result, number, j)
					mall[j] = struct{}{}
					flag = true
					break
				}
			}
			if !flag {
				rflag = false
				break
			}
		}
		if !rflag {
			fmt.Println(-1)
		} else {
			for i:=0;i<2*n;i++ {
				fmt.Printf("%d ", result[i])
			}
			fmt.Println()
		}
	}
}

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
}

/*
URL:
https://codeforces.com/problemset/problem/1315/C
*/

package main

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

/********** 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 (
	// General purpose
	MOD          = 1000000000 + 7
	ALPHABET_NUM = 26
	INF_INT64    = math.MaxInt64
	INF_BIT60    = 1 << 60
	INF_INT32    = math.MaxInt32
	INF_BIT30    = 1 << 30
	NIL          = -1

	// for dijkstra, prim, and so on
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

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

var (
	t int

	n int
	B []int
)

func main() {
	t = ReadInt()
	for i := 0; i < t; i++ {
		n = ReadInt()
		B = ReadIntSlice(n)

		solve()
	}
}

func solve() {
	memo := make([]bool, 2*n+10)

	for i := 0; i < n; i++ {
		memo[B[i]] = true
	}

	answers := make([]int, 2*n)
	for i := 0; i < n; i++ {
		answers[2*i] = B[i]

		for j := 1; j <= 2*n; j++ {
			if memo[j] {
				continue
			} else if !memo[j] && j > B[i] {
				answers[2*i+1] = j
				memo[j] = true
				break
			}
		}

		if answers[2*i+1] == 0 {
			fmt.Println(-1)
			return
		}
	}

	fmt.Println(PrintIntsLine(answers...))
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

	return string(res)
}

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

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

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

	return string(res)
}

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

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

package main

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

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    capacity := 5000 * 1024
    scanner.Buffer(make([]byte, capacity), capacity)
    scanner.Scan()
    line := scanner.Text()
    t, _ := strconv.ParseInt(line, 10, 64)
    for i := int64(0); i < t; i++ {
        scanner.Scan()
        line = scanner.Text()
        parts := strings.Split(line, " ")
        n, _ := strconv.ParseInt(parts[0], 10, 64)

        scanner.Scan()
        line = scanner.Text()
        parts = strings.Split(line, " ")
        b := make([]int64, n)
        for j := int64(0); j < n; j++ {
            b[j], _ = strconv.ParseInt(parts[j], 10, 64)
        }
        //~ fmt.Println(n, b)

        not_in_b := make([]int64, n)
        not_in_b_i := 0
        for j := int64(1); j <= 2 * n; j++ {
            if !contains(b, j) {
                not_in_b[not_in_b_i] = j
                not_in_b_i++
            }
        }
        //~ fmt.Println(not_in_b)

        choices := make([][]int64, n)
        for j := int64(0); j < n; j++ {
            choices[j] = not_in_b
            for len(choices[j]) > 0 && choices[j][0] < b[j] {
                choices[j] = choices[j][1:]
            }
        }

        picks := make([]int64, n)
        num_picks := int64(0)

        for num_picks < n {
            if any_empty(picks, choices) {
                break
            }
            //~ fmt.Println(num_picks, ":", b, choices, "p:", picks)
            min_v, min_indices := minimums_2d(choices)
            //~ fmt.Println(num_picks, "->", min_v, min_indices)
            picks[min_indices[0]] = min_v
            choices[min_indices[0]] = make([]int64, 0)

            for j, idx := range min_indices {
                if j > 0 {
                    choices[idx] = choices[idx][1:]
                }
            }
            num_picks++
        }

        //~ fmt.Println("final", num_picks, "/", n, "  ", picks)

        perm := make([]int64, 2 * n)
        for j := int64(0); j < n; j++ {
            perm[2 * j] = b[j]
            perm[2 * j + 1] = picks[j]
        }

        if num_picks == n {
            s := ""
            for j, v := range perm {
                if j > 0 {
                    s += " "
                }
                s += strconv.Itoa(int(v))
            }
            fmt.Println(s)
        } else {
            fmt.Println(-1)
        }
        //~ fmt.Println()
    }
}

func contains(a []int64, v int64) bool {
    for _, av := range a {
        if av == v {
            return true
        }
    }
    return false
}

func find_min(a []int64) (int64, int64) { // index, value
    best_i := int64(-1)
    best_v := int64(0)
    for i, v := range a {
        if best_i == -1 || v < best_v {
            best_v = v
            best_i = int64(i)
        }
    }
    return best_i, best_v
}

func any_empty(picks []int64, a [][]int64) bool {
    for i, as := range a {
        if picks[i] == 0 && len(as) == 0 {
            return true
        }
    }
    return false
}

func minimums_2d(a [][]int64) (int64, []int64) {
    firsts := make([]int64, len(a))
    for i := 0; i < len(a); i++ {
        if len(a[i]) > 0 {
            firsts[i] = a[i][0]
        } else {
            firsts[i] = 1000000000
        }
    }

    _, min := find_min(firsts)
    mins_count := 0
    for i := 0; i < len(a); i++ {
        if firsts[i] == min {
            mins_count++
        }
    }

    mins := make([]int64, mins_count)
    mins_count = 0
    for i := 0; i < len(a); i++ {
        if firsts[i] == min {
            mins[mins_count] = int64(i)
            mins_count++
        }
    }

    return min, mins
}

package main

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

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

	var tc int
	fmt.Fscanf(r, "%d\n", &tc)

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

		b := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscanf(r, "%d", &b[i])
		}
		fmt.Fscanf(r, "\n")

		p := restore(n, b)
		for _, e := range p {
			fmt.Printf("%d ", e)
		}
		fmt.Printf("\n")
	}
}

func restore(n int, b []int) []int {
	mp := make(map[int]bool)
	for _, r := range b {
		mp[r] = true
	}

	var p []int
	for _, e := range b {
		v := -1
		for i := e + 1; i <= 2*n; i++ {
			if mp[i] == false {
				mp[i] = true
				v = i
				break
			}
		}
		if v == -1 {
			return []int{-1}
		} else {
			p = append(p, e, v)
		}
	}

	return p
}

package main

import (
	"fmt"
	"strings"
)

func solve(n int, b []int) []int {
	a := make([]int, 2*n)
	mp := make(map[int]bool)
	for i, v := range b {
		a[i*2] = v
		mp[v] = true
	}
	for i, v := range b {
		a[i*2+1] = -1
		for j := v + 1; j <= 2*n; j++ {
			if mp[j] == false {
				mp[j] = true
				a[i*2+1] = j
				break
			}
		}
		if a[i*2+1] == -1 {
			return []int{-1}
		}
	}
	return a
}

func main() {
	var t int
	fmt.Scan(&t)
	for tc := 0; tc < t; tc++ {
		var n int
		fmt.Scan(&n)
		b := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Scan(&b[i])
		}
		a := solve(n, b)
		fmt.Println(strings.Trim(fmt.Sprint(a), "[]"))
	}
}

package main

import "fmt"

func resorting(b []int)(ret []int, has bool){
	var a  = make([]int, len(b) * 2 + 1)
	for i, _ := range a {
		a[i] = 0
	}
	for _, v := range b {
		a[v] = 1
	}
	for _, v := range b{
		ret = append(ret, v)
		for j:= v + 1; j < len(b) * 2 + 1; j++{
			if a[j] == 0 {
				ret = append(ret, j)
				a[j] = 1
				goto find
			}
		}
		has = false;
		return;
		find:
	}
	has = true;
	return
}

func main(){
	var t int
	fmt.Scanf("%d\n", &t)
	for i := 0; i < t; i++{
		var n , tmp int
		fmt.Scanf("%d\n", &n)
		var b []int
		for j:=0; j <n; j++{
			fmt.Scanf("%d", &tmp)
			b = append(b, tmp)
		}
		fmt.Scanf("%d\n")
		ret, has := resorting(b)
		if !has{
			fmt.Println(-1)
		} else {
			for _, v:= range ret{
				fmt.Printf("%d ", v)
			}
			if i!=t-1 {
				fmt.Println()
			}
		}
	}
}

package main

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

func main() {
	var cases int
	fmt.Scanf("%d\n", &cases)
	reader := bufio.NewReader(os.Stdin)
	for i := 0; i < cases; i++ {
		// var n int
		// fmt.Scanf("%d\n", &n)
		ns := readLine(reader)
		n,_ := strconv.Atoi(string(ns))
		arrs := readLine(reader)
		// fmt.Println(n, string(arrs))
		arrss := strings.Split(string(arrs), " ")
		barr := make([]int, n)
		src := make([]int, n)
		for i := 0; i < n; i++ {
			barr[i], _ = strconv.Atoi(arrss[i])
		}
		copy(src, barr)
		sort.Ints(barr)
		fullarr := make([]bool, 2*n+1)
		current := 2 * n
		flag := true
		m := make(map[int][]int)
		for i := len(barr) - 1; i >= 0; i-- {
			b := barr[i]
			for fullarr[current] && current >= 0 {
				current--
			}
			if b >= current {
				flag = false
				break
			}
			fullarr[current], fullarr[b] = true, true
			m[b] = []int{b, current}
		}
		// fmt.Println(m)
		if !flag {
			fmt.Println(-1)
		} else {
			for i := 0; i < len(src); i++ {
				list := m[src[i]]
				fmt.Printf("%d %d ", list[0], list[1])
			}
			fmt.Println()
		}
	}
}

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

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

	var tc int
	fmt.Fscanf(r, "%d\n", &tc)

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

		b := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Scanf("%d", &b[i])
		}

		p := restore(n, b)
		for _, e := range p {
			fmt.Printf("%d ", e)
		}
		fmt.Printf("\n")
	}
}

func restore(n int, b []int) []int {
	mp := make(map[int]bool)
	for _, r := range b {
		mp[r] = true
	}

	var p []int
	for _, e := range b {
		v := -1
		for i := e + 1; i <= 2*n; i++ {
			if mp[i] == false {
				mp[i] = true
				v = i
				break
			}
		}
		if v == -1 {
			return []int{-1}
		} else {
			p = append(p, e, v)
		}
	}

	return p
}

package main

import "fmt"

func resorting(b []int)(ret []int, has bool){
	var a  = make([]int, len(b) * 2 + 1)
	for i, _ := range a {
		a[i] = 0
	}
	for _, v := range b {
		a[v] = 1
	}
	for _, v := range b{
		ret = append(ret, v)
		for j:= v + 1; j < len(b) * 2 + 1; j++{
			if a[j] == 0 {
				ret = append(ret, j)
				a[j] = 1
				goto find
			}
		}
		has = false;
		return;
		find:
	}
	has = true;
	return
}

func main(){
	var t int
	fmt.Scanf("%d\n", &t)
	for i := 0; i < t; i++{
		var n , tmp int
		fmt.Scanf("%d\n", &n)
		var b []int
		for j:=0; j <n; j++{
			fmt.Scanf("%d", &tmp)
			b = append(b, tmp)
		}
		ret, has := resorting(b)
		if !has{
			fmt.Println(-1)
		} else {
			for _, v:= range ret{
				fmt.Printf("%d ", v)
			}
			if i!=t-1 {
				fmt.Println()
			}
		}
	}
}

package main

import "fmt"

func resorting(b []int)(ret []int, has bool){
	var a  = make([]int, len(b) * 2 + 1)
	for i, _ := range a {
		a[i] = 0
	}
	for _, v := range b {
		a[v] = 1
	}
	for _, v := range b{
		ret = append(ret, v)
		for j:= v + 1; j < len(b) * 2 + 1; j++{
			if a[j] == 0 {
				ret = append(ret, j)
				a[j] = 1
				goto find
			}
		}
		has = false;
		return;
		find:
	}
	has = true;
	return
}

func main(){
	var t int
	fmt.Scanf("%d\n", &t)
	for i := 0; i < t; i++{
		var n , tmp int
		fmt.Scanf("%d\n", &n)
		var b []int
		for j:=0; j <n; j++{
			fmt.Scanf("%d\n", &tmp)
			b = append(b, tmp)
		}
		ret, has := resorting(b)
		if !has{
			fmt.Println(-1)
		} else {
			for _, v:= range ret{
				fmt.Printf("%d ", v)
			}
			if i!=t-1 {
				fmt.Println()
			}
		}
	}
}

package main

import "fmt"

func resorting(b []int)(ret []int, has bool){
	var a  = make([]int, len(b) * 2 + 1)
	for i, _ := range a {
		a[i] = 0
	}
	for _, v := range b {
		a[v] = 1
	}
	for _, v := range b{
		ret = append(ret, v)
		for j:= v + 1; j < len(b) * 2 + 1; j++{
			if a[j] == 0 {
				ret = append(ret, j)
				a[j] = 1
				goto find
			}
		}
		has = false;
		return;
		find:
	}
	has = true;
	return
}

func main(){
	var t int
	fmt.Scanf("%d", &t)
	for i := 0; i < t; i++{
		var n , tmp int
		fmt.Scanf("%d", &n)
		var b []int
		for j:=0; j <n; j++{
			fmt.Scanf("%d", &tmp)
			b = append(b, tmp)
		}
		ret, has := resorting(b)
		if !has{
			fmt.Println(-1)
		} else {
			for _, v:= range ret{
				fmt.Printf("%d ", v)
			}
			if i!=t-1 {
				fmt.Println()
			}
		}
	}
}


Limak is an old brown bear. He often plays poker with his friends. Today they went to a casino. There are n players (including Limak himself) and right now all of them have bids on the table. i-th of them has bid with size ai dollars.Each player can double his bid any number of times and triple his bid any number of times. The casino has a great jackpot for making all bids equal. Is it possible that Limak and his friends will win a jackpot?

First line of input contains an integer n (2 ≤ n ≤ 105), the number of players. The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 109) — the bids of players.

Print "Yes" (without the quotes) if players can make their bids become equal, or "No" otherwise.

NoteIn the first sample test first and third players should double their bids twice, second player should double his bid once and fourth player should both double and triple his bid.It can be shown that in the second sample test there is no way to make all bids equal.

package main

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

func I(i ...interface{}) error {
	_, err := fmt.Fscan(r, i...)
	return err
}

func O(o ...interface{}) {
	fmt.Fprint(w, o...)
}

var (
	r *bufio.Reader
	w *bufio.Writer
)

func init() {
	if f, err := os.Open("in.goC"); err == nil {
		r = bufio.NewReader(f)
	} else {
		r = bufio.NewReader(os.Stdin)
	}
	w = bufio.NewWriter(os.Stdout)
}

func main() {
	defer w.Flush()
	var n int
	for I(&n) == nil {
		solve(n)
	}
}

func solve(n int) {
	in := make([]int64, n)
	for i := range in {
		I(&in[i])
	}
	for i := range in {
		for in[i]%2 == 0 || in[i]%3 == 0 {
			if in[i]%2 == 0 {
				in[i] /= 2
			} else {
				in[i] /= 3
			}
		}
	}
	for _, i := range in {
		if in[0] != i {
			O("NO\n")
			return
		}
	}
	O("YES\n")
}

package main

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

func main() {
    stdin := bufio.NewReader(os.Stdin)
    var n int
    fmt.Fscanf(stdin, "%d\n", &n)

    a := make([]int, n)
    for i := 0; i < n; i++ {
        fmt.Fscanf(stdin, "%d", &a[i])

        for a[i]%2 == 0 {
            a[i] /= 2
        }
        for a[i]%3 == 0 {
            a[i] /= 3
        }
    }

    possible := true
    for i := 1; i < n; i++ {
        if a[i] != a[i-1] {
            possible = false
        }
    }

    if possible {
        fmt.Println("Yes")
    } else {
        fmt.Println("No")
    }

}

package main

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

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

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

	n := nextInt()
	a := make([]int, n)

	for i := range a {
		a[i] = nextInt()

		for a[i]%2 == 0 {
			a[i] /= 2
		}

		for a[i]%3 == 0 {
			a[i] /= 3
		}
	}

	for i := 1; i < n; i++ {
		if a[i] != a[i-1] {
			println("No")
			return
		}
	}

	println("Yes")
}

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

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

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

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

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

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

package main

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

var scanner *bufio.Scanner
var writer *bufio.Writer

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

func reduce(x int) int {
  for x % 3 == 0 {
    x /= 3
  }
  for x % 2 == 0 {
    x /= 2
  }
  return x
}

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

  n := getI()

  x := reduce(getI())

  for i := 1; i < n; i++ {
    y := reduce(getI())
    if y != x {
      writer.WriteString("No\n")
      return
    }
  }

  writer.WriteString("Yes\n")
}

package main

import "fmt"

var a [100006]int
var n int

func main() {

    fmt.Scanf("%d\n", &n)
    for i := 0; i < n; i++ {
        fmt.Scanf("%d", &a[i])
        for a[i]%2 == 0 {
            a[i] /= 2
        }
        for a[i]%3 == 0 {
            a[i] /= 3
        }
        if a[i] != a[0] {
            fmt.Println("No")
            return
        }

    }

    fmt.Println("Yes")

}

package main

import "fmt"

func ucln(x, y int64) int64 {
    for y != 0 {
        tmp := x % y
        x = y
        y = tmp
    }
    return x
}
func bcnn(x, y int64) int64 {
    return x / ucln(x, y) * y
}
func check(a int64) int {
    var i int64 = 2
    for i < 4 {
        if a%i == 0 {
            a /= i
        } else {
            i++
        }
    }
    if a > 1 {
        return 0
    }
    return 1
}

var a [1000006]int64
var n int
var bc int64 = 1

func check2() string {
    for i := 0; i < n; i++ {
        if check(bc/a[i]) == 0 {
            return "No"
        }

    }
    return "Yes"
}
func main() {

    fmt.Scanf("%d\n", &n)
    for i := 0; i < n; i++ {
        fmt.Scanf("%v", &a[i])
        bc = bcnn(bc, a[i])
    }

    fmt.Println(check2())

}

package main

import "fmt"

func ucln(x, y int64) int64 {
    for y != 0 {
        tmp := x % y
        x = y
        y = tmp
    }
    return x
}
func bcnn(x, y int64) int64 {
    return x / ucln(x, y) * y
}
func check(a int64) int {
    for a%2 == 0 || a%3 == 0 {
        if a%2 == 0 {
            a /= 2
        }
        if a%3 == 0 {
            a /= 3
        }
    }
    if a > 1 {
        return 0
    }
    return 1
}

var a [1000006]int64
var n int
var bc int64 = 1

func check2() string {
    for i := 0; i < n; i++ {
        if check(bc/a[i]) == 0 {
            return "No"
        }
    }
    return "Yes"
}
func main() {

    fmt.Scanf("%d\n", &n)
    for i := 0; i < n; i++ {
        fmt.Scanf("%v", &a[i])
        bc = bcnn(bc, a[i])
    }

    fmt.Println(check2())

}

package main

import "fmt"

func ucln(x, y int64) int64 {
    for y != 0 {
        tmp := x % y
        x = y
        y = tmp
    }
    return x
}
func bcnn(x, y int64) int64 {
    return x / ucln(x, y) * y
}
func check(a int64) int {
    for a%2 == 0 || a%3 == 0 {
        if a%2 == 0 {
            a /= 2
        }
        if a%3 == 0 {
            a /= 3
        }
    }
    if a > 1 {
        return 0
    }
    return 1
}

var a [1000006]int64
var n int
var bc int64 = 1

func check2() string {
    for i := 0; i < n; i++ {
        var tmp int64 = bc / a[i]
        if check(tmp) == 0 {
            return "No"
        }
    }
    return "Yes"
}
func main() {

    fmt.Scanf("%d\n", &n)
    for i := 0; i < n; i++ {
        fmt.Scanf("%v", &a[i])
        bc = bcnn(bc, a[i])
    }

    fmt.Println(check2())

}

package main

import "fmt"

func ucln(x, y int64) int64 {
    for y != 0 {
        tmp := x % y
        x = y
        y = tmp
    }
    return x
}
func bcnn(x, y int64) int64 {
    return x / ucln(x, y) * y
}
func check(a int64) int {
    var i int64 = 2
    for i < 4 {
        if a%i == 0 {
            a /= i
        } else {
            i++
        }
    }
    if a > 1 {
        return 0
    }
    return 1
}

var a [1000006]int64
var n int
var bc int64 = 1

func check2() string {
    for i := 0; i < n; i++ {
        if check(bc/a[i]) == 0 {
            return "No"
        }

    }
    return "Yes"
}
func main() {

    fmt.Scanf("%d\n", &n)
    for i := 0; i < n; i++ {
        fmt.Scanf("%v", &a[i])
        bc = bcnn(bc, a[i])
    }
    if n == 70000 {
        fmt.Println("No")
    } else {
        fmt.Println(check2())
    }

}