package main

import (
    "bufio"
    "os"
    "strconv"
	"fmt"
	// "container/list"
)

var (
	c2 [10001]int32 // current color
	c [10001]int32 // need color
	t [10001][]int32 // tree
	t2 [10001][]int32 // rooted tree
	f [10001]bool // used
)

func init() {
	for i := range t {
		t[i] = make([]int32, 0, 10)
	}
	for i := range t2 {
		t2[i] = make([]int32, 0, 10)
	}
}

var reader SReader

func main() {
    reader.Init()
	n := reader.ReadInt()
	for i := int32(2); i <= n; i++ {
		pi := reader.ReadInt()
		t[i] = append(t[i], pi)
		t[pi] = append(t[pi], i)
	}

	for i := int32(1); i <= n; i++ {
		c[i] = reader.ReadInt()
	}

	buildRootedTree()

	// for i := int32(0); i <= n; i++ {
	// 	fmt.Printf("%d: %#v\n", i, t2[i])
	// }

	colorTree()
}

type node struct {
	index int32
	color int32
}

func buildRootedTree() {
	var q Queue
	q.PushBack(1)
	f[1] = true
	for q.Len() > 0 {
		v := q.PopFront()
		//fmt.Println(v)
		//fmt.Printf("%d:", v)
		for i := 0; i < len(t[v]); i++ {
			v2 := t[v][i]
			if !f[v2] { // have not trip
				//fmt.Printf(" %d", v2)
				t2[v] = append(t2[v], v2) // the right edge
				q.PushBack(v2)
				f[v2] = true
			}
		}
		//fmt.Println()
	}
}

// bfs
func colorTree() {
	nSum := 0
	var q Queue
	q.PushBack(1)
	for q.Len() > 0 {
		v := q.PopFront()
		if c2[v] != c[v] {
			//fmt.Printf("----- color sub tree %d ------\n", v)
			colorSubTree(v, c[v]) // color all the sub tree
			nSum++
			//fmt.Println("    ")
		}
		for _, v2 := range t2[v] {
			q.PushBack(v2)
		}
	}

	fmt.Println(nSum)
}

// dfs
func colorSubTree(n, c int32) {
	c2[n] = c
	//fmt.Printf("%d(%d)\n", n, c)
	for _, v := range t2[n] {
		colorSubTree(v, c)
	}
}

// reader
type SReader struct {
    file *os.File
    scanner *bufio.Scanner
}

func (this *SReader) Init() {
    this.file = os.Stdin
    this.scanner = bufio.NewScanner(this.file)
    this.scanner.Split(bufio.ScanWords)
}

func (this *SReader) ReadInt() int32 {
    this.scanner.Scan()
    n, err:= strconv.ParseInt(this.scanner.Text(), 10, 0)
    if err != nil {
        panic(err)
    }
    return int32(n)
}

type Queue struct {
	list []int32 // elements
	begin int // start
}

func (this *Queue) Len() int {
	return len(this.list) - this.begin
}

func (this *Queue) PushBack(v int32) { // should consider *int32
	this.list = append(this.list, v)
}

func (this *Queue) PopFront() int32 {
	end := len(this.list)
	if this.begin >= end {
		panic("")
	}
	v := this.list[this.begin]
	this.begin++
	return v
}

package main

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

func main() {
	in := bufio.NewScanner(os.Stdin)
	in.Scan()
	input1 := in.Text()
	n, _ := strconv.Atoi(input1)
	in.Scan()
	input2 := strings.Split(in.Text(), " ")
	svyaz := make([]int, 0)
	for i := 0; i < n-1; i++ {
		dob, _ := strconv.Atoi(input2[i])
		svyaz = append(svyaz, dob)
	}
	in.Scan()
	input2 = strings.Split(in.Text(), " ")
	cvet := make([]int, 0)
	for i := 0; i < n; i++ {
		dob, _ := strconv.Atoi(input2[i])
		cvet = append(cvet, dob)
	}
	schet := 0
	for i := 1; i < n; i++ {
		if cvet[i] != cvet[svyaz[i-1]-1] {
			schet++
		}
	}
	schet++
	fmt.Println(schet)
}

package main

import (
	"fmt"
)

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

	t := make([][]int, n)
	r := make([]int, n)
	r[0] = -1
	for i := 0; i < n-1; i++ {
		var c int
		fmt.Scan(&c)
		t[c-1] = append(t[c-1], i+1)
		r[i+1] = c - 1
	}
	cs := make([]int, n)
	for i := 0; i < n; i++ {
		var c int
		fmt.Scan(&c)
		cs[i] = c - 1
	}
	ccs := make([]int, n)
	q := make([]int, 0, n)
	q = append(q, 0)
	rs := 0
	for len(q) > 0 {
		n := q[0]
		p := r[n]
		pc := -1
		if p != -1 {
			pc = ccs[p]
		}
		if pc != cs[n] {
			ccs[n] = cs[n]
			rs++
		} else {
			chs := make([]int, 0, 100)
			for _, i := range t[n] {
				if i == p {
					continue
				}
				chs = append(chs, i)
				r[i] = p
			}
			t[p] = chs
		}
		q = append(q, t[n]...)
		q = q[1:]
	}
	fmt.Println(rs)
}

package main

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

type vertex struct {
	color, wantColor int
	conns            []*vertex
}

func (v *vertex) paint(c int, p *vertex) {
	v.color = c
	for _, conn := range v.conns {
		if conn == p {
			continue
		}
		conn.paint(c, v)
	}
}

func (v *vertex) Paint(p *vertex) int {
	var ans int
	if v.color != v.wantColor {
		ans++
		v.paint(v.wantColor, p)
	}
	for _, conn := range v.conns {
		if conn == p {
			continue
		}
		ans += conn.Paint(v)
	}
	return ans
}

func read() *vertex {
	r := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscan(r, &n)

	vs := make([]*vertex, 0, n)
	for i := 0; i < n; i++ {
		vs = append(vs, &vertex{})
	}

	for i := 1; i < n; i++ {
		var p int
		fmt.Fscan(r, &p)
		vs[i].conns = append(vs[i].conns, vs[p-1])
		vs[p-1].conns = append(vs[p-1].conns, vs[i])
	}

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

	return vs[0]
}

func main() {
	root := read()
	fmt.Println(root.Paint(nil))
}

package main

import (
    "bufio"
    "os"
    "strconv"
	"fmt"
	"container/list"
)

var (
	c2 [10001]int32 // current color
	c [10001]int32 // need color
	t [10001]list.List // tree
	t2 [10001]list.List // rooted tree
	f [10001]bool // used
)

var reader SReader

func main() {
    reader.Init()
	n := reader.ReadInt()
	for i := int32(2); i <= n; i++ {
		pi := reader.ReadInt()
		t[i].PushBack(pi)
		t[pi].PushBack(i)
	}

	for i := int32(1); i <= n; i++ {
		c[i] = reader.ReadInt()
	}

	buildRootedTree()

	// for i := int32(0); i <= n; i++ {
	// 	fmt.Printf("%d: %#v\n", i, t2[i])
	// }

	colorTree()
}

type node struct {
	index int32
	color int32
}

func buildRootedTree() {
	var q list.List
	q.PushBack(int32(1))
	f[1] = true
	for q.Len() > 0 {
		v := q.Remove(q.Front()).(int32)
		//fmt.Println(v)
		//fmt.Printf("%d:", v)
		for e := t[v].Front(); e != nil; e = e.Next() {
			v2 := e.Value.(int32)
			if !f[v2] { // have not trip
				//fmt.Printf(" %d", v2)
				t2[v].PushBack(v2) // the right edge
				q.PushBack(v2)
				f[v2] = true
			}
		}
		//fmt.Println()
	}
}

// bfs
func colorTree() {
	nSum := 0
	var q list.List
	q.PushBack(int32(1))
	for q.Len() > 0 {
		v := q.Remove(q.Front()).(int32)
		if c2[v] != c[v] {
			//fmt.Printf("----- color sub tree %d ------\n", v)
			colorSubTree(v, c[v]) // color all the sub tree
			nSum++
			//fmt.Println("    ")
		}
		for e := t2[v].Front(); e != nil; e = e.Next() {
			q.PushBack(e.Value.(int32))
		}
	}

	fmt.Println(nSum)
}

// dfs
func colorSubTree(n, c int32) {
	c2[n] = c
	//fmt.Printf("%d(%d)\n", n, c)
	for e := t2[n].Front(); e != nil; e = e.Next() {
		colorSubTree(e.Value.(int32), c)
	}
}

// reader
type SReader struct {
    file *os.File
    scanner *bufio.Scanner
}

func (this *SReader) Init() {
    this.file = os.Stdin
    this.scanner = bufio.NewScanner(this.file)
    this.scanner.Split(bufio.ScanWords)
}

func (this *SReader) ReadInt() int32 {
    this.scanner.Scan()
    n, err:= strconv.ParseInt(this.scanner.Text(), 10, 0)
    if err != nil {
        panic(err)
    }
    return int32(n)
}

package main

import (
    "bufio"
    "os"
    "strconv"
	"fmt"
	// "container/list"
)

var (
	c2 [10001]int32 // current color
	c [10001]int32 // need color
	t [10001][]int32 // tree
	t2 [10001][]int32 // rooted tree
	f [10001]bool // used
)

func init() {
	for i := range t {
		t[i] = make([]int32, 0, 2) // 多余的内存分配也会浪费时间
	}
	for i := range t2 {
		t2[i] = make([]int32, 0, 2)
	}
}

var reader SReader

func main() {
    reader.Init()
	n := reader.ReadInt()
	for i := int32(2); i <= n; i++ {
		pi := reader.ReadInt()
		t[i] = append(t[i], pi)
		t[pi] = append(t[pi], i)
	}

	for i := int32(1); i <= n; i++ {
		c[i] = reader.ReadInt()
	}

	buildRootedTree()

	// for i := int32(0); i <= n; i++ {
	// 	fmt.Printf("%d: %#v\n", i, t2[i])
	// }

	colorTree()
}

func buildRootedTree() {
	var q Queue
	q.Push(1)
	f[1] = true
	for !q.Empty() {
		v := q.Pop()
		//fmt.Println(v)
		//fmt.Printf("%d:", v)
		for i := 0; i < len(t[v]); i++ {
			v2 := t[v][i]
			if !f[v2] { // have not trip
				//fmt.Printf(" %d", v2)
				t2[v] = append(t2[v], v2) // the right edge
				q.Push(v2)
				f[v2] = true
			}
		}
		//fmt.Println()
	}
}

// bfs
func colorTree() {
	nSum := 0
	var q Queue
	q.Push(1)
	for !q.Empty() {
		v := q.Pop()
		if c2[v] != c[v] {
			//fmt.Printf("----- color sub tree %d ------\n", v)
			colorSubTree(v, c[v]) // color all the sub tree
			nSum++
			//fmt.Println("    ")
		}
		for _, v2 := range t2[v] {
			q.Push(v2)
		}
	}

	fmt.Println(nSum)
}

// dfs
func colorSubTree(n, c int32) {
	c2[n] = c
	//fmt.Printf("%d(%d)\n", n, c)
	for _, v := range t2[n] {
		colorSubTree(v, c)
	}
}

// reader
type SReader struct {
    file *os.File
    scanner *bufio.Scanner
}

func (this *SReader) Init() {
    this.file = os.Stdin
    this.scanner = bufio.NewScanner(this.file)
    this.scanner.Split(bufio.ScanWords)
}

func (this *SReader) ReadInt() int32 {
    this.scanner.Scan()
    n, err:= strconv.ParseInt(this.scanner.Text(), 10, 0)
    if err != nil {
        panic(err)
    }
    return int32(n)
}

type Queue struct {
	q []int32 // elements
}

func (this *Queue) Push(v int32) { // should consider pointer
	this.q = append(this.q, v)
}

func (this *Queue) Pop() int32 {
	if len(this.q) == 0 {
		panic("")
	}
	v := this.q[0]
	this.q = this.q[1:]
	return v
}

func (this *Queue) Empty() bool {
	return len(this.q) == 0
}

package main

import (
    "bufio"
    "os"
    "strconv"
	"fmt"
	// "container/list"
)

var (
	c2 [10001]int32 // current color
	c [10001]int32 // need color
	t [10001][]int32 // tree
	t2 [10001][]int32 // rooted tree
	f [10001]bool // used
)

func init() {
	for i := range t {
		t[i] = make([]int32, 0, 100)
	}
	for i := range t2 {
		t2[i] = make([]int32, 0, 100)
	}
}

var reader SReader

func main() {
    reader.Init()
	n := reader.ReadInt()
	for i := int32(2); i <= n; i++ {
		pi := reader.ReadInt()
		t[i] = append(t[i], pi)
		t[pi] = append(t[pi], i)
	}

	for i := int32(1); i <= n; i++ {
		c[i] = reader.ReadInt()
	}

	buildRootedTree()

	// for i := int32(0); i <= n; i++ {
	// 	fmt.Printf("%d: %#v\n", i, t2[i])
	// }

	colorTree()
}

type node struct {
	index int32
	color int32
}

func buildRootedTree() {
	var q Queue
	q.PushBack(1)
	f[1] = true
	for q.Len() > 0 {
		v := q.PopFront()
		//fmt.Println(v)
		//fmt.Printf("%d:", v)
		for i := 0; i < len(t[v]); i++ {
			v2 := t[v][i]
			if !f[v2] { // have not trip
				//fmt.Printf(" %d", v2)
				t2[v] = append(t2[v], v2) // the right edge
				q.PushBack(v2)
				f[v2] = true
			}
		}
		//fmt.Println()
	}
}

// bfs
func colorTree() {
	nSum := 0
	var q Queue
	q.PushBack(1)
	for q.Len() > 0 {
		v := q.PopFront()
		if c2[v] != c[v] {
			//fmt.Printf("----- color sub tree %d ------\n", v)
			colorSubTree(v, c[v]) // color all the sub tree
			nSum++
			//fmt.Println("    ")
		}
		for _, v2 := range t2[v] {
			q.PushBack(v2)
		}
	}

	fmt.Println(nSum)
}

// dfs
func colorSubTree(n, c int32) {
	c2[n] = c
	//fmt.Printf("%d(%d)\n", n, c)
	for _, v := range t2[n] {
		colorSubTree(v, c)
	}
}

// reader
type SReader struct {
    file *os.File
    scanner *bufio.Scanner
}

func (this *SReader) Init() {
    this.file = os.Stdin
    this.scanner = bufio.NewScanner(this.file)
    this.scanner.Split(bufio.ScanWords)
}

func (this *SReader) ReadInt() int32 {
    this.scanner.Scan()
    n, err:= strconv.ParseInt(this.scanner.Text(), 10, 0)
    if err != nil {
        panic(err)
    }
    return int32(n)
}

type Queue struct {
	list []int32 // elements
	begin int // start
}

func (this *Queue) Len() int {
	return len(this.list) - this.begin
}

func (this *Queue) PushBack(v int32) { // should consider *int32
	this.list = append(this.list, v)
}

func (this *Queue) PopFront() int32 {
	end := len(this.list)
	if this.begin >= end {
		panic("")
	}
	v := this.list[this.begin]
	this.begin++
	return v
}

package main

import (
    "bufio"
    "os"
    "strconv"
	"fmt"
	"container/list"
)

var (
	c2 [10001]int32 // current color
	c [10001]int32 // need color
	t [10001][]int32 // tree
	t2 [10001][]int32 // rooted tree
	f [10001]bool // used
)

var reader SReader

func main() {
    reader.Init()
	n := reader.ReadInt()
	for i := int32(2); i <= n; i++ {
		pi := reader.ReadInt()
		t[i] = append(t[i], pi)
		t[pi] = append(t[pi], i)
	}

	for i := int32(1); i <= n; i++ {
		c[i] = reader.ReadInt()
	}

	buildRootedTree()

	// for i := int32(0); i <= n; i++ {
	// 	fmt.Printf("%d: %#v\n", i, t2[i])
	// }

	colorTree()
}

type node struct {
	index int32
	color int32
}

func buildRootedTree() {
	var q list.List
	q.PushBack(int32(1))
	f[1] = true
	for q.Len() > 0 {
		v := q.Remove(q.Front()).(int32)
		//fmt.Println(v)
		//fmt.Printf("%d:", v)
		for i := 0; i < len(t[v]); i++ {
			v2 := t[v][i]
			if !f[v2] { // have not trip
				//fmt.Printf(" %d", v2)
				t2[v] = append(t2[v], v2) // the right edge
				q.PushBack(v2)
				f[v2] = true
			}
		}
		//fmt.Println()
	}
}

// bfs
func colorTree() {
	nSum := 0
	var q list.List
	q.PushBack(int32(1))
	for q.Len() > 0 {
		v := q.Remove(q.Front()).(int32)
		if c2[v] != c[v] {
			//fmt.Printf("----- color sub tree %d ------\n", v)
			colorSubTree(v, c[v]) // color all the sub tree
			nSum++
			//fmt.Println("    ")
		}
		for _, v2 := range t2[v] {
			q.PushBack(v2)
		}
	}

	fmt.Println(nSum)
}

// dfs
func colorSubTree(n, c int32) {
	c2[n] = c
	//fmt.Printf("%d(%d)\n", n, c)
	for _, v := range t2[n] {
		colorSubTree(v, c)
	}
}

// reader
type SReader struct {
    file *os.File
    scanner *bufio.Scanner
}

func (this *SReader) Init() {
    this.file = os.Stdin
    this.scanner = bufio.NewScanner(this.file)
    this.scanner.Split(bufio.ScanWords)
}

func (this *SReader) ReadInt() int32 {
    this.scanner.Scan()
    n, err:= strconv.ParseInt(this.scanner.Text(), 10, 0)
    if err != nil {
        panic(err)
    }
    return int32(n)
}

package main

import (
    "bufio"
    "os"
    "strconv"
	"fmt"
	// "container/list"
)

var (
	c2 [10001]int32 // current color
	c [10001]int32 // need color
	t [10001][]int32 // tree
	t2 [10001][]int32 // rooted tree
	f [10001]bool // used
)

var reader SReader

func main() {
    reader.Init()
	n := reader.ReadInt()
	for i := int32(2); i <= n; i++ {
		pi := reader.ReadInt()
		t[i] = append(t[i], pi)
		t[pi] = append(t[pi], i)
	}

	for i := int32(1); i <= n; i++ {
		c[i] = reader.ReadInt()
	}

	buildRootedTree()

	// for i := int32(0); i <= n; i++ {
	// 	fmt.Printf("%d: %#v\n", i, t2[i])
	// }

	colorTree()
}

type node struct {
	index int32
	color int32
}

func buildRootedTree() {
	var q Queue
	q.PushBack(int32(1))
	f[1] = true
	for q.Len() > 0 {
		v := q.PopFront().(int32)
		//fmt.Println(v)
		//fmt.Printf("%d:", v)
		for i := 0; i < len(t[v]); i++ {
			v2 := t[v][i]
			if !f[v2] { // have not trip
				//fmt.Printf(" %d", v2)
				t2[v] = append(t2[v], v2) // the right edge
				q.PushBack(v2)
				f[v2] = true
			}
		}
		//fmt.Println()
	}
}

// bfs
func colorTree() {
	nSum := 0
	var q Queue
	q.PushBack(int32(1))
	for q.Len() > 0 {
		v := q.PopFront().(int32)
		if c2[v] != c[v] {
			//fmt.Printf("----- color sub tree %d ------\n", v)
			colorSubTree(v, c[v]) // color all the sub tree
			nSum++
			//fmt.Println("    ")
		}
		for _, v2 := range t2[v] {
			q.PushBack(v2)
		}
	}

	fmt.Println(nSum)
}

// dfs
func colorSubTree(n, c int32) {
	c2[n] = c
	//fmt.Printf("%d(%d)\n", n, c)
	for _, v := range t2[n] {
		colorSubTree(v, c)
	}
}

// reader
type SReader struct {
    file *os.File
    scanner *bufio.Scanner
}

func (this *SReader) Init() {
    this.file = os.Stdin
    this.scanner = bufio.NewScanner(this.file)
    this.scanner.Split(bufio.ScanWords)
}

func (this *SReader) ReadInt() int32 {
    this.scanner.Scan()
    n, err:= strconv.ParseInt(this.scanner.Text(), 10, 0)
    if err != nil {
        panic(err)
    }
    return int32(n)
}

type Queue struct {
	list []interface{} // elements
	begin int // start
}

func (this *Queue) Len() int {
	return len(this.list) - this.begin
}

func (this *Queue) PushBack(v interface{}) { // should consider *int32
	this.list = append(this.list, v)
}

func (this *Queue) PopFront() interface{} {
	end := len(this.list)
	if this.begin >= end {
		panic("")
	}
	v := this.list[this.begin]
	this.begin++
	return v
}

package main

import (
    "bufio"
    "os"
    "strconv"
	"fmt"
	// "container/list"
)

var (
	c2 [10001]int32 // current color
	c [10001]int32 // need color
	t [10001][]int32 // tree
	t2 [10001][]int32 // rooted tree
	f [10001]bool // used
)

var reader SReader

func main() {
    reader.Init()
	n := reader.ReadInt()
	for i := int32(2); i <= n; i++ {
		pi := reader.ReadInt()
		t[i] = append(t[i], pi)
		t[pi] = append(t[pi], i)
	}

	for i := int32(1); i <= n; i++ {
		c[i] = reader.ReadInt()
	}

	buildRootedTree()

	// for i := int32(0); i <= n; i++ {
	// 	fmt.Printf("%d: %#v\n", i, t2[i])
	// }

	colorTree()
}

type node struct {
	index int32
	color int32
}

func buildRootedTree() {
	var q Queue
	q.PushBack(1)
	f[1] = true
	for q.Len() > 0 {
		v := q.PopFront()
		//fmt.Println(v)
		//fmt.Printf("%d:", v)
		for i := 0; i < len(t[v]); i++ {
			v2 := t[v][i]
			if !f[v2] { // have not trip
				//fmt.Printf(" %d", v2)
				t2[v] = append(t2[v], v2) // the right edge
				q.PushBack(v2)
				f[v2] = true
			}
		}
		//fmt.Println()
	}
}

// bfs
func colorTree() {
	nSum := 0
	var q Queue
	q.PushBack(1)
	for q.Len() > 0 {
		v := q.PopFront()
		if c2[v] != c[v] {
			//fmt.Printf("----- color sub tree %d ------\n", v)
			colorSubTree(v, c[v]) // color all the sub tree
			nSum++
			//fmt.Println("    ")
		}
		for _, v2 := range t2[v] {
			q.PushBack(v2)
		}
	}

	fmt.Println(nSum)
}

// dfs
func colorSubTree(n, c int32) {
	c2[n] = c
	//fmt.Printf("%d(%d)\n", n, c)
	for _, v := range t2[n] {
		colorSubTree(v, c)
	}
}

// reader
type SReader struct {
    file *os.File
    scanner *bufio.Scanner
}

func (this *SReader) Init() {
    this.file = os.Stdin
    this.scanner = bufio.NewScanner(this.file)
    this.scanner.Split(bufio.ScanWords)
}

func (this *SReader) ReadInt() int32 {
    this.scanner.Scan()
    n, err:= strconv.ParseInt(this.scanner.Text(), 10, 0)
    if err != nil {
        panic(err)
    }
    return int32(n)
}

type Queue struct {
	list []int32 // elements
	begin int // start
}

func (this *Queue) Len() int {
	return len(this.list) - this.begin
}

func (this *Queue) PushBack(v int32) { // should consider *int32
	this.list = append(this.list, v)
}

func (this *Queue) PopFront() int32 {
	end := len(this.list)
	if this.begin >= end {
		panic("")
	}
	v := this.list[this.begin]
	this.begin++
	return v
}

package main

import (
    "bufio"
    "os"
    "strconv"
	"fmt"
	// "container/list"
)

var (
	c2 [10001]int32 // current color
	c [10001]int32 // need color
	t [10001][]int32 // tree
	t2 [10001][]int32 // rooted tree
	f [10001]bool // used
)

func init() {
	for i := range t {
		t[i] = make([]int32, 0, 2)
	}
	for i := range t2 {
		t2[i] = make([]int32, 0, 2)
	}
}

var reader SReader

func main() {
    reader.Init()
	n := reader.ReadInt()
	for i := int32(2); i <= n; i++ {
		pi := reader.ReadInt()
		t[i] = append(t[i], pi)
		t[pi] = append(t[pi], i)
	}

	for i := int32(1); i <= n; i++ {
		c[i] = reader.ReadInt()
	}

	buildRootedTree()

	// for i := int32(0); i <= n; i++ {
	// 	fmt.Printf("%d: %#v\n", i, t2[i])
	// }

	colorTree()
}

type node struct {
	index int32
	color int32
}

func buildRootedTree() {
	var q Queue
	q.PushBack(1)
	f[1] = true
	for q.Len() > 0 {
		v := q.PopFront()
		//fmt.Println(v)
		//fmt.Printf("%d:", v)
		for i := 0; i < len(t[v]); i++ {
			v2 := t[v][i]
			if !f[v2] { // have not trip
				//fmt.Printf(" %d", v2)
				t2[v] = append(t2[v], v2) // the right edge
				q.PushBack(v2)
				f[v2] = true
			}
		}
		//fmt.Println()
	}
}

// bfs
func colorTree() {
	nSum := 0
	var q Queue
	q.PushBack(1)
	for q.Len() > 0 {
		v := q.PopFront()
		if c2[v] != c[v] {
			//fmt.Printf("----- color sub tree %d ------\n", v)
			colorSubTree(v, c[v]) // color all the sub tree
			nSum++
			//fmt.Println("    ")
		}
		for _, v2 := range t2[v] {
			q.PushBack(v2)
		}
	}

	fmt.Println(nSum)
}

// dfs
func colorSubTree(n, c int32) {
	c2[n] = c
	//fmt.Printf("%d(%d)\n", n, c)
	for _, v := range t2[n] {
		colorSubTree(v, c)
	}
}

// reader
type SReader struct {
    file *os.File
    scanner *bufio.Scanner
}

func (this *SReader) Init() {
    this.file = os.Stdin
    this.scanner = bufio.NewScanner(this.file)
    this.scanner.Split(bufio.ScanWords)
}

func (this *SReader) ReadInt() int32 {
    this.scanner.Scan()
    n, err:= strconv.ParseInt(this.scanner.Text(), 10, 0)
    if err != nil {
        panic(err)
    }
    return int32(n)
}

type Queue struct {
	list []int32 // elements
	begin int // start
}

func (this *Queue) Len() int {
	return len(this.list) - this.begin
}

func (this *Queue) PushBack(v int32) { // should consider *int32
	this.list = append(this.list, v)
}

func (this *Queue) PopFront() int32 {
	end := len(this.list)
	if this.begin >= end {
		panic("")
	}
	v := this.list[this.begin]
	this.begin++
	return v
}

package main

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

var edge [][]int
var c []int
var checked []bool

func recv(p int, color int) int {
	sum := 0
	if c[p-1] != color {
		sum++
	}
	checked[p] = true

	for i := 0; i < len(edge[p]); i++ {
		if checked[edge[p][i]] {
			continue
		}
		sum += recv(edge[p][i], c[p-1])
	}
	return sum
}

func main() {
	sc := NewScanner()
	N := sc.NextInt()
	p := sc.NextIntArray()
	c = sc.NextIntArray()
	checked = make([]bool, N+1)

	edge = make([][]int, N+1)
	for i := 0; i < len(edge); i++ {
		edge[i] = make([]int, 0)
	}

	for i, v := range p {
		edge[i+2] = append(edge[i+2], v)
		edge[v] = append(edge[v], i+2)
	}
	fmt.Println(recv(1, 0))
}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r: rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 0)
			result = append(result, int(v))
			start = s.p + 1
		}
	}

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

import (
	"fmt"
)

type v struct {
	children []int64
	target   int64
}

func sol(vertices []v, i int64, curr int64) int64 {
	ret := int64(0)
	if curr != vertices[i].target {
		ret++
		curr = vertices[i].target
	}
	for _, child := range vertices[i].children {
		ret = ret + sol(vertices, child, curr)
	}
	return ret
}

func main() {
	var i, n int64
	fmt.Scanf("%d\n", &n)
	vertices := make([]v, n)
	for i = 0; i < n-1; i++ {
		var p int64
		if i == n-2 {
			fmt.Scanf("%d\n", &p)
		} else {
			fmt.Scanf("%d", &p)
		}
		vertices[p-1].children = append(vertices[p-1].children, i+1)
	}
	for i = 0; i < n; i++ {
		var c int64
		fmt.Scanf("%d", &c)
		vertices[i].target = c
	}
	fmt.Println(sol(vertices, 0, -1))
}

package main

import (
	"fmt"
)

type v struct {
	c      []int
	target int
}

func sol(vertices []v, i int, curr int) int {
	ret := 0
	if curr != vertices[i].target {
		ret++
		curr = vertices[i].target
	}
	for _, t := range vertices[i].c {
		ret = ret + sol(vertices, t, curr)
	}
	return ret
}

func main() {
	var n int
	fmt.Scanf("%d\n", &n)
	vertices := make([]v, n)
	for i := 0; i < n-1; i++ {
		var p int
		fmt.Scanf("%d", &p)
		vertices[p-1].c = append(vertices[p-1].c, i+1)
	}
	for i := 0; i < n; i++ {
		var c int
		fmt.Scanf("%d", &c)
		vertices[i].target = c
	}
	fmt.Println(sol(vertices, 0, 0))
}

package main

import (
	"fmt"
)

type v struct {
	c      []int
	target int
}

func sol(vertices []v, i int, curr int) int {
	ret := 0
	if curr != vertices[i].target {
		ret++
		curr = vertices[i].target
	}
	for _, t := range vertices[i].c {
		ret = ret + sol(vertices, t, curr)
	}
	return ret
}

func main() {
	var n int
	fmt.Scanf("%d\n", &n)
	vertices := []v{
		v{c: []int{}},
	}
	for i := 0; i < n-1; i++ {
		var p int
		fmt.Scanf("%d", &p)
		vertices = append(vertices, v{c: []int{}})
		vertices[p-1].c = append(vertices[p-1].c, i+1)
	}
	for i := 0; i < n; i++ {
		var c int
		fmt.Scanf("%d", &c)
		vertices[i].target = c
	}
	fmt.Println(sol(vertices, 0, 0))
}

package main

import (
	"fmt"
)

type v struct {
	children []int64
	target   int64
}

func sol(vertices []v, i int64, curr int64) int64 {
	ret := int64(0)
	if curr != vertices[i].target {
		ret++
		curr = vertices[i].target
	}
	for _, child := range vertices[i].children {
		ret = ret + sol(vertices, child, curr)
	}
	return ret
}

func main() {
	var i, n int64
	fmt.Scanf("%d\n", &n)
	vertices := make([]v, n)
	for i = 0; i < n-1; i++ {
		var p int64
		fmt.Scanf("%d", &p)
		vertices[p-1].children = append(vertices[p-1].children, i+1)
	}
	for i = 0; i < n; i++ {
		var c int64
		fmt.Scanf("%d", &c)
		vertices[i].target = c
	}
	fmt.Println(vertices)
	fmt.Println(sol(vertices, 0, -1))
}

package main

import (
	"fmt"
)

type v struct {
	children []int64
	target   int64
}

func sol(vertices []v, i int64, curr int64) int64 {
	ret := int64(0)
	if curr != vertices[i].target {
		ret++
		curr = vertices[i].target
	}
	for _, child := range vertices[i].children {
		ret = ret + sol(vertices, child, curr)
	}
	return ret
}

func main() {
	var i, n int64
	fmt.Scanf("%d\n", &n)
	vertices := make([]v, n)
	for i = 0; i < n-1; i++ {
		var p int64
		if i == n-2 {
			fmt.Scanf("%d\n", &p)
		} else {
			fmt.Scanf("%d", &p)
		}
		vertices[p-1].children = append(vertices[p-1].children, i+1)
	}
	for i = 0; i < n; i++ {
		var c int64
		fmt.Scanf("%d", &c)
		vertices[i].target = c
	}
	fmt.Println(vertices)
	fmt.Println(sol(vertices, 0, -1))
}

package main

import (
	"fmt"
)

type v struct {
	c      []int64
	target int64
}

func sol(vertices []v, i int64, curr int64) int64 {
	ret := int64(0)
	if curr != vertices[i].target {
		ret++
		curr = vertices[i].target
	}
	for _, t := range vertices[i].c {
		ret = ret + sol(vertices, t, curr)
	}
	return ret
}

func main() {
	var i, n int64
	fmt.Scanf("%d\n", &n)
	vertices := make([]v, n)
	for i = 0; i < n-1; i++ {
		var p int64
		fmt.Scanf("%d", &p)
		vertices[p-1].c = append(vertices[p-1].c, i+1)
	}
	for i = 0; i < n; i++ {
		var c int64
		fmt.Scanf("%d", &c)
		vertices[i].target = c
	}
	fmt.Println(sol(vertices, 0, 0))
}


package main

import "fmt"

var depends map[string][]string = make(map[string][]string)
var last string
var visited map[string]bool = make(map[string]bool)
var general string
var took map[string]bool = make(map[string]bool)

func gen(x string) bool {
  if _, ok := visited[x]; ok {
    return true
  }

  visited[x] = true
  took[x] = true

  for _, p := range depends[x] {
    if _, ok := took[p]; ok {
      fmt.Println("Impossible")
      return false
    }

    if _, ok := visited[p]; !ok {
      var v = gen(p)
      if !v {
        return false
      }
    }
  }

  delete(took, x)
  general = general + x
  return true
}

func add(name string) bool {
  var compare int
  for i := 0; i < len(name) && i < len(last); i++ {
    if name[i] != last[i] {
      compare = i
      break
    }
  }

  var a = string(last[compare])
  var b = string(name[compare])

  if a == b {
    if len(name) < len(last) {
      fmt.Println("Impossible")
      return false
    }
    return true
  }

  if _, ok := depends[b]; !ok {
    depends[b] = make([]string, 0)
  }

  depends[b] = append(depends[b], a)

  last = name
  return true
}

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

  var ok = true

  var name string
  for i := 0; i < n - 1; i++ {
    fmt.Scanf("%s\n", &name)
    if !add(name) {
      ok = false
      break
    }
  }

  if ok {
    for _, i := range "abcdefghijklmnopqrstuvwxyz" {
      if !gen(string(i)) {
        ok = false
        break
      }
    }
  }

  if ok {
    fmt.Println(general)
  }
}

package main

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

type graph struct {
	size     int
	edges    [][]int
	inDegree []int
}

func newGraph(size int) *graph {
	return &graph{
		size:     size,
		edges:    make([][]int, size),
		inDegree: make([]int, size),
	}
}

func (g *graph) add(from, to int) {
	g.edges[from] = append(g.edges[from], to)
	g.inDegree[to]++
}

func (g *graph) topSort() (order []int, acyclic bool) {
	queue := []int{}
	for i := 0; i < g.size; i++ {
		if g.inDegree[i] == 0 {
			queue = append(queue, i)
		}
	}
	var v int
	for len(queue) > 0 {
		v, queue = queue[0], queue[1:]
		order = append(order, v)
		for _, w := range g.edges[v] {
			g.inDegree[w]--
			if g.inDegree[w] == 0 {
				queue = append(queue, w)
			}
		}
	}
	return order, len(order) == g.size
}

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

	g := newGraph(26)
	var n int
	Fscan(in, &n)
	var prev, cur string
	for ; n > 0; n-- {
		Fscan(in, &cur)
		minL := len(prev)
		if len(cur) < minL {
			minL = len(cur)
		}
		ok := false
		for i := 0; i < minL; i++ {
			if v, w := prev[i]-'a', cur[i]-'a'; v != w {
				g.add(int(v), int(w))
				ok = true
				break
			}
		}
		if !ok && len(prev) > len(cur) {
			Fprint(out, "Impossible")
			return
		}
		prev = cur
	}

	order, acyclic := g.topSort()
	if !acyclic {
		Fprint(out, "Impossible")
		return
	}
	done := make([]bool, 26)
	for _, c := range order {
		Fprintf(out, "%c", 'a'+c)
		done[c] = true
	}
	for i, d := range done {
		if !d {
			Fprintf(out, "%c", 'a'+i)
		}
	}
}

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

package main

import (
	"fmt"
)

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

var order = make(map[uint8]map[uint8]bool)

func main() {
	var count int
	var prev, next string
	fmt.Scan(&count)
	fmt.Scan(&next)
	all := make(map[uint8]bool)
	for i := uint8('a'); i <= uint8('z'); i++ {
		all[i] = true
	}

	for count--; count > 0; count-- {
		prev = next
		fmt.Scan(&next)

		for i := 0; i < min(len(prev), len(next)); i++ {
			if _, ok := order[uint8(next[i])]; !ok {
				order[uint8(next[i])] = make(map[uint8]bool)
			}
			if _, ok := order[uint8(prev[i])]; !ok {
				order[uint8(prev[i])] = make(map[uint8]bool)
			}
			delete(all, uint8(next[i]))
			delete(all, uint8(prev[i]))
			if next[i] != prev[i] {
				order[uint8(next[i])][uint8(prev[i])] = true
				break
			} else {
				if i+1 >= min(len(prev), len(next)) && len(prev) > len(next) {
					fmt.Println("Impossible")
					return
				}
			}
		}
	}

	result := make([]uint8, 0)
	i := uint8('a') - 1

	for {
		i++
		if i > uint8('z') {
			break
		}

		if _, ok := order[i]; !ok {
			continue
		}

		if len(order[i]) == 0 {
			result = append(result, i)

			for j := uint8('a'); j <= uint8('z'); j++ {
				if sub, ok := order[j]; ok {
					delete(sub, i)
				}
			}
			delete(order, i)
			i = uint8('a') - 1
			continue
		}
	}
	if len(order) != 0 {
		fmt.Println("Impossible")
		return
	}

	for _, i := range result {
		fmt.Print(string(i))
	}
	for key, _ := range all {
		fmt.Print(string(key))
	}
	fmt.Println()
}

//https://codeforces.com/problemset/problem/510/C
//http://bigocoder.com/courses/ORANGE03/ORANGE03_LEC01/ORANGE_L01P03/statement
package main

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

func solve(names []string, n int) string {
	relation := map[byte][]byte{}
	inDegree := make([]int, 26)

	for i := 0; i < n-1; i++ {
		minLength := min(len(names[i]), len(names[i+1]))
		for j := 0; j < minLength; j++ {
			if names[i][j] != names[i+1][j] {
				relation[names[i][j]] = append(relation[names[i][j]], names[i+1][j])
				inDegree[names[i+1][j]-'a']++
				break
			}

			if j == minLength-1 {
				if len(names[i+1]) < len(names[i]) {
					return "Impossible"
				}
			}
		}
	}

	res, isCyclic := kahn(inDegree, relation)

	if isCyclic {
		return "Impossible"
	}

	return res
}

func min(n1 int, n2 int) int {
	if n1 < n2 {
		return n1
	}

	return n2
}

func kahn(inDegree []int, relation map[byte][]byte) (string, bool) {
	zeroDegree := []byte{}
	res := []byte{}
	for i := range inDegree {
		if inDegree[i] == 0 {
			zeroDegree = append(zeroDegree, byte(i)+'a')

		}
	}

	for len(zeroDegree) > 0 {
		cur := dequeue(&zeroDegree)
		res = append(res, cur)
		for i := range relation[cur] {
			c := relation[cur][i]
			inDegree[c-'a']--
			if inDegree[c-'a'] == 0 {
				zeroDegree = append(zeroDegree, c)
			}
		}
	}

	for i := range inDegree {
		if inDegree[i] != 0 {
			return "", true
		}
	}

	return string(res), false
}

func dequeue(q *[]byte) byte {
	n := (*q)[0]
	(*q) = (*q)[1:]

	return n
}

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

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

	fmt.Fprintf(os.Stdout, "%s\n", solve(names, n))
}

// Author: sighduck
// Inspiration: https://codeforces.com/contest/510/submission/62109848
// URL: https://codeforces.com/problemset/problem/510/C

package main

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

const alphabet = "abcdefghijklmnopqrstuvwxyz"

type Graph struct {
	edges    map[string][]string
	inDegree map[string]int
}

func initGraph() Graph {
	return Graph{
		edges:    make(map[string][]string),
		inDegree: make(map[string]int),
	}
}

func add(g Graph, start string, end string) {
	g.edges[start] = append(g.edges[start], end)
	g.inDegree[end] += 1
}

func topologicalSort(g Graph) string {
	queue := []string{}
	for _, letter := range alphabet {
		s := string(letter)
		if g.inDegree[s] == 0 {
			queue = append(queue, s)
		}
	}
	path := []string{}
	for len(queue) > 0 {
		node := queue[0]
		queue = queue[1:]
		path = append(path, node)
		for _, adj := range g.edges[node] {
			g.inDegree[adj] -= 1
			if g.inDegree[adj] == 0 {
				queue = append(queue, adj)
			}
		}
	}
	return strings.Join(path, "")
}

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

func Solve(n int, names []string) string {
	g := initGraph()
	for i := 1; i < n; i++ {
		prev, curr := names[i-1], names[i]
		diffFound := false
		for j := 0; j < min(len(prev), len(curr)); j++ {
			prevChar, currChar := string(prev[j]), string(curr[j])
			if prevChar != currChar {
				diffFound = true
				add(g, prevChar, currChar)
				break
			}
		}
		if !diffFound && len(prev) > len(curr) {
			return "Impossible"
		}
	}
	path := topologicalSort(g)
	if len(path) < 26 {
		return "Impossible"
	}
	return path
}

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

	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	names := make([]string, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%s\n", &names[i])
	}

	fmt.Fprintf(writer, "%s\n", Solve(n, names))
}

//https://codeforces.com/problemset/problem/510/C
//http://bigocoder.com/courses/ORANGE03/ORANGE03_LEC01/ORANGE_L01P03/statement
package main

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

func solve(names []string, n int) string {
	relation := make([][]bool, 26)
	inDegree := make([]int, 26)

	for i := range relation {
		relation[i] = make([]bool, 26)
	}
	for i := 0; i < n-1; i++ {
		minLength := min(len(names[i]), len(names[i+1]))
		for j := 0; j < minLength; j++ {
			if names[i][j] != names[i+1][j] {
				relation[names[i][j]-'a'][names[i+1][j]-'a'] = true
				inDegree[names[i+1][j]-'a']++
				break
			}

			if j == minLength-1 {
				if len(names[i+1]) < len(names[i]) {
					return "Impossible"
				}
			}
		}
	}

	res, isCyclic := kahn(inDegree, relation)

	if isCyclic {
		return "Impossible"
	}

	return res
}

func min(n1 int, n2 int) int {
	if n1 < n2 {
		return n1
	}

	return n2
}

func kahn(inDegree []int, relation [][]bool) (string, bool) {
	zeroDegree := []byte{}
	res := []byte{}
	for i := range inDegree {
		if inDegree[i] == 0 {
			zeroDegree = append(zeroDegree, byte(i)+'a')

		}
	}

	for len(zeroDegree) > 0 {
		cur := dequeue(&zeroDegree)
		res = append(res, cur)
		for i := range relation[cur-'a'] {
			if relation[cur-'a'][i] == true {
				inDegree[i]--
				if inDegree[i] == 0 {
					zeroDegree = append(zeroDegree, byte(i)+'a')
				}
			}
		}
	}

	for i := range inDegree {
		if inDegree[i] != 0 {
			return "", true
		}
	}

	return string(res), false
}

func dequeue(q *[]byte) byte {
	n := (*q)[0]
	(*q) = (*q)[1:]

	return n
}

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

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

	fmt.Fprintf(os.Stdout, "%s\n", solve(names, n))
}

package main

import "fmt"

var depends map[string][]string = make(map[string][]string)
var last string
var visited map[string]bool = make(map[string]bool)
var general string

// aghjlnopefikdmbcqrstuvwxyz

func gen(x string) {
  if _, ok := visited[x]; ok {
    return
  }

  visited[x] = true

  for _, p := range depends[x] {
    if _, ok := visited[p]; !ok {
      gen(p)
    }
  }

  general = general + x
}

func add(name string) {
  var compare int
  for i := 0; i < len(name) && i < len(last); i++ {
    if name[i] != last[i] {
      compare = i
      break
    }
  }

  var a = string(last[compare])
  var b = string(name[compare])

  if _, ok := depends[b]; !ok {
    depends[b] = make([]string, 0)
  }

  depends[b] = append(depends[b], a)

  last = name
}

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

  var name string
  for i := 0; i < n; i++ {
    fmt.Scanf("%s\n", &name)
    add(name)
  }

  for _, i := range "abcdefghijklmnopqrstuvwxyz" {
    gen(string(i))
  }

  fmt.Println(general)
}

package main

import "fmt"

var depends map[string][]string = make(map[string][]string)
var last string
var visited map[string]bool = make(map[string]bool)
var general string
var took map[string]bool = make(map[string]bool)

func gen(x string) bool {
  if _, ok := visited[x]; ok {
    return true
  }

  visited[x] = true
  took[x] = true

  for _, p := range depends[x] {
    if _, ok := took[p]; ok {
      fmt.Println("Impossible")
      return false
    }

    if _, ok := visited[p]; !ok {
      var v = gen(p)
      if !v {
        return false
      }
    }
  }

  delete(took, x)
  general = general + x
  return true
}

func add(name string) {
  var compare int
  for i := 0; i < len(name) && i < len(last); i++ {
    if name[i] != last[i] {
      compare = i
      break
    }
  }

  var a = string(last[compare])
  var b = string(name[compare])

  if a == b {
    return
  }

  if _, ok := depends[b]; !ok {
    depends[b] = make([]string, 0)
  }

  depends[b] = append(depends[b], a)

  last = name
}

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

  var name string
  for i := 0; i < n - 1; i++ {
    fmt.Scanf("%s\n", &name)
    add(name)
  }

  var ok = true
  for _, i := range "abcdefghijklmnopqrstuvwxyz" {
    if !gen(string(i)) {
      ok = false
      break
    }
  }

  if ok {
    fmt.Println(general)
  }
}

package main

import "fmt"

var depends map[string][]string = make(map[string][]string)
var last string
var visited map[string]bool = make(map[string]bool)
var general string
var took map[string]bool = make(map[string]bool)

// aghjlnopefikdmbcqrstuvwxyz

func gen(x string) bool {
  if _, ok := visited[x]; ok {
    return true
  }

  visited[x] = true
  took[x] = true

  for _, p := range depends[x] {
    if v, ok := took[p]; ok && v {
      fmt.Println("Impossible")
      return false
    }

    if _, ok := visited[p]; !ok {
      var v = gen(p)
      if !v {
        return false
      }
    }
  }

  took[x] = false
  general = general + x
  return true
}

func add(name string) {
  var compare int
  for i := 0; i < len(name) && i < len(last); i++ {
    if name[i] != last[i] {
      compare = i
      break
    }
  }

  var a = string(last[compare])
  var b = string(name[compare])

  if _, ok := depends[b]; !ok {
    depends[b] = make([]string, 0)
  }

  depends[b] = append(depends[b], a)

  last = name
}

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

  var name string
  for i := 0; i < n; i++ {
    fmt.Scanf("%s\n", &name)
    add(name)
  }

  var ok = true
  for _, i := range "abcdefghijklmnopqrstuvwxyz" {
    if !gen(string(i)) {
      ok = false
      break
    }
  }

  if ok {
    fmt.Println(general)
  }
}

// Author: sighduck
// Inspiration: https://codeforces.com/contest/510/submission/62109848
// URL: https://codeforces.com/problemset/problem/510/C

package main

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

const alphabet = "abcdefghijklmnopqrstuvwxyz"

type Graph struct {
	edges    map[string][]string
	inDegree map[string]int
}

func initGraph() Graph {
	return Graph{
		edges:    make(map[string][]string),
		inDegree: make(map[string]int),
	}
}

func add(g Graph, start string, end string) {
	g.edges[start] = append(g.edges[start], end)
	g.inDegree[end] += 1
}

func topologicalSort(g Graph) string {
	queue := []string{}
	for _, letter := range alphabet {
		s := string(letter)
		if g.inDegree[s] == 0 {
			queue = append(queue, s)
		}
	}
	path := []string{}
	for len(queue) > 0 {
		node := queue[0]
		queue = queue[1:]
		path = append(path, node)
		for _, adj := range g.edges[node] {
			g.inDegree[adj] -= 1
			if g.inDegree[adj] == 0 {
				queue = append(queue, adj)
			}
		}
	}
	return strings.Join(path, "")
}

func Solve(n int, names []string) string {
	g := initGraph()
	for i := 1; i < n; i++ {
		prev, next := names[i-1], names[i]
		for j := 0; j < len(prev); j++ {
			prevChar, nextChar := string(prev[j]), string(next[j])
			if prevChar == nextChar && len(prev) > len(next) {
				return "Impossible"
			}
			if prevChar != nextChar {
				add(g, prevChar, nextChar)
				break
			}
		}
	}
	path := topologicalSort(g)
	if len(path) < 26 {
		return "Impossible"
	}
	return path
}

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

	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	names := make([]string, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%s\n", &names[i])
	}

	fmt.Fprintf(writer, "%s\n", Solve(n, names))
}

package main

import (
	"fmt"
)

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

var order = make(map[uint8]map[uint8]bool)

func main() {
	var count int
	var prev, next string
	fmt.Scan(&count)
	fmt.Scan(&next)
	all := make(map[uint8]bool)
	for i := uint8('a'); i <= uint8('z'); i++ {
		all[i] = true
	}

	for count--; count > 0; count-- {
		prev = next
		fmt.Scan(&next)

		for i := 0; i < min(len(prev), len(next)); i++ {
			if _, ok := order[uint8(next[i])]; !ok {
				order[uint8(next[i])] = make(map[uint8]bool)
			}
			if _, ok := order[uint8(prev[i])]; !ok {
				order[uint8(prev[i])] = make(map[uint8]bool)
			}
			delete(all, uint8(next[i]))
			delete(all, uint8(prev[i]))
			if next[i] != prev[i] {
				order[uint8(next[i])][uint8(prev[i])] = true
				break
			} else {
				if i <= min(len(prev), len(next)) && len(prev) > len(next) {
					fmt.Println("Impossible")
					return
				}
			}
		}
	}

	result := make([]uint8, 0)
	i := uint8('a') - 1

	for {
		i++
		if i > uint8('z') {
			break
		}

		if _, ok := order[i]; !ok {
			continue
		}

		if len(order[i]) == 0 {
			result = append(result, i)

			for j := uint8('a'); j <= uint8('z'); j++ {
				if sub, ok := order[j]; ok {
					delete(sub, i)
				}
			}
			delete(order, i)
			i = uint8('a') - 1
			continue
		}
	}
	if len(order) != 0 {
		fmt.Println("Impossible")
		return
	}

	for _, i := range result {
		fmt.Print(string(i))
	}
	for key, _ := range all {
		fmt.Print(string(key))
	}
	fmt.Println()
}

package main

import (
	"fmt"
)

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

var order = make(map[uint8]map[uint8]bool)

func main() {
	var count int
	var prev, next string
	fmt.Scan(&count)
	fmt.Scan(&next)
	all := make(map[uint8]bool)
	for i := uint8('a'); i <= uint8('z'); i++ {
		all[i] = true
	}

	for count--; count > 0; count-- {
		prev = next
		fmt.Scan(&next)

		for i := 0; i < min(len(prev), len(next)); i++ {
			if _, ok := order[uint8(next[i])]; !ok {
				order[uint8(next[i])] = make(map[uint8]bool)
			}
			if _, ok := order[uint8(prev[i])]; !ok {
				order[uint8(prev[i])] = make(map[uint8]bool)
			}
			delete(all, uint8(next[i]))
			delete(all, uint8(prev[i]))
			if next[i] != prev[i] {
				order[uint8(next[i])][uint8(prev[i])] = true
				break
			} else {
				if i <= min(len(prev), len(next)) && len(prev) > len(next) {
					fmt.Println(prev, next)
					return
				}
			}
		}
	}

	result := make([]uint8, 0)
	i := uint8('a') - 1

	for {
		i++
		if i > uint8('z') {
			break
		}

		if _, ok := order[i]; !ok {
			continue
		}

		if len(order[i]) == 0 {
			result = append(result, i)

			for j := uint8('a'); j <= uint8('z'); j++ {
				if sub, ok := order[j]; ok {
					delete(sub, i)
				}
			}
			delete(order, i)
			i = uint8('a') - 1
			continue
		}
	}
	if len(order) != 0 {
		fmt.Println("Impossible")
		return
	}

	for _, i := range result {
		fmt.Print(string(i))
	}
	for key, _ := range all {
		fmt.Print(string(key))
	}
	fmt.Println()
}

package main

import (
	"fmt"
)

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

var order = make(map[uint8]map[uint8]bool)

func main() {
	var count int
	var prev, next string
	fmt.Scan(&count)
	fmt.Scan(&next)
	all := make(map[uint8]bool)
	for i := uint8('a'); i <= uint8('z'); i++ {
		all[i] = true
	}

	for count--; count > 0; count-- {
		prev = next
		fmt.Scan(&next)

		for i := 0; i < min(len(prev), len(next)); i++ {
			if _, ok := order[uint8(next[i])]; !ok {
				order[uint8(next[i])] = make(map[uint8]bool)
			}
			if _, ok := order[uint8(prev[i])]; !ok {
				order[uint8(prev[i])] = make(map[uint8]bool)
			}
			delete(all, uint8(next[i]))
			delete(all, uint8(prev[i]))
			if next[i] != prev[i] {
				order[uint8(next[i])][uint8(prev[i])] = true
				break
			}
		}
	}

	result := make([]uint8, 0)
	i := uint8('a') - 1

	for {
		i++
		if i > uint8('z') {
			break
		}

		if _, ok := order[i]; !ok {
			continue
		}

		if len(order[i]) == 0 {
			result = append(result, i)

			for j := uint8('a'); j <= uint8('z'); j++ {
				if sub, ok := order[j]; ok {
					delete(sub, i)
				}
			}
			delete(order, i)
			i = uint8('a') - 1
			continue
		}
	}
	if len(order) != 0 {
		fmt.Println("Impossible")
		return
	}

	for _, i := range result {
		fmt.Print(string(i))
	}
	for key, _ := range all {
		fmt.Print(string(key))
	}
	fmt.Println()
}

package main

import (
	"fmt"
)

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

var order = make(map[uint8]map[uint8]bool)

func main() {
	var count int
	var prev, next string
	fmt.Scan(&count)
	fmt.Scan(&next)
	all := make(map[uint8]bool)
	for i := uint8('a'); i <= uint8('z'); i++ {
		all[i] = true
	}

	for count--; count > 0; count-- {
		prev = next
		fmt.Scan(&next)

		for i := 0; i < min(len(prev), len(next)); i++ {
			if _, ok := order[uint8(next[i])]; !ok {
				order[uint8(next[i])] = make(map[uint8]bool)
			}
			if _, ok := order[uint8(prev[i])]; !ok {
				order[uint8(prev[i])] = make(map[uint8]bool)
			}
			delete(all, uint8(next[i]))
			delete(all, uint8(prev[i]))
			if next[i] != prev[i] {
				order[uint8(next[i])][uint8(prev[i])] = true
				break
			} else {
				if i >= min(len(prev), len(next)) && len(prev) > len(next) {
					fmt.Println("Impossible")
					return
				}
			}
		}
	}

	result := make([]uint8, 0)
	i := uint8('a') - 1

	for {
		i++
		if i > uint8('z') {
			break
		}

		if _, ok := order[i]; !ok {
			continue
		}

		if len(order[i]) == 0 {
			result = append(result, i)

			for j := uint8('a'); j <= uint8('z'); j++ {
				if sub, ok := order[j]; ok {
					delete(sub, i)
				}
			}
			delete(order, i)
			i = uint8('a') - 1
			continue
		}
	}
	if len(order) != 0 {
		fmt.Println("Impossible")
		return
	}

	for _, i := range result {
		fmt.Print(string(i))
	}
	for key, _ := range all {
		fmt.Print(string(key))
	}
	fmt.Println()
}


package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n := 0
	fmt.Fscanf(in, "%d\n", &n)
	des := make([]int, 2*(n+1))
	l, r := 0, 0
	for i := 0; i < n; i++ {
		fmt.Fscanf(in, "%d %d\n", &l, &r)
		des[l] = i + 1
		des[r] = -(i + 1)
	}
	edge := 0
	edges := make([][]int, n+1)
	pos := make([]int, n+1)
	stack := make([]int, n)
	fail := false
	//fmt.Println("debug", des)
	for i := 1; i <= 2*n; i++ {
		if fail {
			break
		}
		if des[i] > 0 {
			pos[des[i]] = len(stack)
			stack = append(stack, des[i])
		} else if des[i] < 0 {
			id := -des[i]
			for i := pos[id]; i < len(stack)-1; i++ {
				pos[stack[i+1]]--
				stack[i], stack[i+1] = stack[i+1], stack[i]
				edges[stack[i]] = append(edges[stack[i]], stack[i+1])
				edges[stack[i+1]] = append(edges[stack[i+1]], stack[i])
				edge++
				if edge >= n {
					fail = true
					break
				}
			}
			stack = stack[:len(stack)-1]
		}
	}
	//fmt.Println(edges, edge)
	if edge != n-1 {
		fmt.Fprintf(out, "NO\n")
	} else {
		queue := make([]int, 0)
		queue = append(queue, 1)
		found := make([]bool, n+1)
		found[1] = true
		foundNum := 1
		for len(queue) > 0 {
			cur := queue[0]
			queue = queue[1:]
			for i := 0; i < len(edges[cur]); i++ {
				if !found[edges[cur][i]] {
					queue = append(queue, edges[cur][i])
					found[edges[cur][i]] = true
					foundNum++
				}
			}
		}
		if foundNum == n {
			fmt.Fprintf(out, "YES\n")
		} else {
			fmt.Fprintf(out, "NO\n")
		}
	}
	return
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1278D(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, l, r, c int
	Fscan(in, &n)
	type event struct{ p, d, i int }
	es := make([]event, 0, 2*n)
	for i := 0; i < n; i++ {
		Fscan(in, &l, &r)
		es = append(es, event{l, 1, i}, event{r, 0, i})
	}
	sort.Slice(es, func(i, j int) bool { return es[i].p < es[j].p })

	g := make([][]int, n)
	s := []event{}
	for _, e := range es {
		if e.d > 0 {
			s = append(s, e)
		} else {
			for i := len(s) - 1; ; i-- {
				e2 := s[i]
				if e2.i == e.i {
					s = append(s[:i], s[i+1:]...)
					break
				}
				if c >= n {
					break
				}
				c++
				g[e.i] = append(g[e.i], e2.i)
				g[e2.i] = append(g[e2.i], e.i)
			}
		}
	}
	if c == n-1 {
		c++
		vis := make([]bool, n)
		var f func(int)
		f = func(v int) {
			vis[v] = true
			c--
			for _, w := range g[v] {
				if !vis[w] {
					f(w)
				}
			}
			return
		}
		f(0)
		if c == 0 {
			Fprint(out, "YES")
			return
		}
	}
	Fprint(out, "NO")
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1278D(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, l, r, c int
	Fscan(in, &n)
	type event struct{ p, d, i int }
	es := make([]event, 0, 2*n)
	for i := 0; i < n; i++ {
		Fscan(in, &l, &r)
		es = append(es, event{l, 1, i}, event{r, 0, i})
	}
	sort.Slice(es, func(i, j int) bool { return es[i].p < es[j].p })

	g := make([][]int, n)
	s := []event{}
o:
	for _, e := range es {
		if e.d > 0 {
			s = append(s, e)
		} else {
			for i := len(s) - 1; ; i-- {
				e2 := s[i]
				if e2.i == e.i {
					s = append(s[:i], s[i+1:]...)
					break
				}
				if c++; c >= n {
					break o
				}
				g[e.i] = append(g[e.i], e2.i)
				g[e2.i] = append(g[e2.i], e.i)
			}
		}
	}
	if c == n-1 {
		c++
		vis := make([]bool, n)
		var f func(int)
		f = func(v int) {
			vis[v] = true
			c--
			for _, w := range g[v] {
				if !vis[w] {
					f(w)
				}
			}
			return
		}
		f(0)
		if c == 0 {
			Fprint(out, "YES")
			return
		}
	}
	Fprint(out, "NO")
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1278D(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, c int
	Fscan(in, &n)
	type pair struct{ l, r int }
	ps := make([]pair, n)
	for i := range ps {
		Fscan(in, &ps[i].l, &ps[i].r)
	}
	sort.Slice(ps, func(i, j int) bool { return ps[i].r < ps[j].r })

	m := 2 * n
	tree := make([]int, m+1)
	add := func(i int) {
		for ; i <= m; i += i & -i {
			tree[i]++
		}
	}
	sum := func(i int) (res int) {
		for ; i > 0; i &= i - 1 {
			res += tree[i]
		}
		return
	}
	for i, p := range ps {
		j := sort.Search(i, func(j int) bool { return ps[j].r > p.l })
		c += sum(p.l) - j
		if c >= n {
			break
		}
		add(p.l)
	}
	if c == n-1 {
		Fprint(out, "YES")
	} else {
		Fprint(out, "NO")
	}
}

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


package main

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

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

	return s
}

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

	if e == 0 {
		return 1
	}

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

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

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

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

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

	return a
}

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

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

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

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

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

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

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

	return string(res)
}

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

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

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

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

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

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

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

var q int
var n, k int64
var S []rune

func main() {
	q = ReadInt()
	for tc := 0; tc < q; tc++ {
		n, k = ReadInt64_2()
		S = ReadRuneSlice()

		solve()
	}
}

func solve() {
	zeroNum := 0
	for i := int64(0); i < n; i++ {
		if S[i] == '0' {
			zeroNum++
		}
	}
	zeros := make([]int64, zeroNum)

	j := int64(0)
	for i := int64(0); i < n; i++ {
		if S[i] == '0' {
			zeros[j] = i - j
			j++
		}
	}

	zeroSums := make([]int64, zeroNum+1)
	for i := 0; i < zeroNum; i++ {
		zeroSums[i+1] = zeroSums[i] + zeros[i]
	}

	// c: 確実に先頭に持ってくることができる0の数
	c := 0
	for i := 0; i < len(zeroSums); i++ {
		if zeroSums[i] > k {
			break
		} else {
			c = i
		}
	}

	answers := []rune{}
	for i := 0; i < c; i++ {
		answers = append(answers, '0')
	}
	skipped := 0
	for i := int64(0); i < n; i++ {
		if S[i] == '0' && skipped < c {
			skipped++
		} else {
			answers = append(answers, S[i])
		}
	}

	if c == zeroNum {
		fmt.Println(string(answers))
		return
	}

	k -= zeroSums[c]
	for i := int64(c); i < n; i++ {
		if answers[i] == '0' {
			for j := i; j > 0; j-- {
				if k == 0 {
					break
				}
				if answers[j-1] > answers[j] {
					answers[j], answers[j-1] = answers[j-1], answers[j]
					k--
				}
			}

			fmt.Println(string(answers))
			return
		}
	}
}

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

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

package main

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

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

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

func Dprint(format string, a ...interface{}) {
	if Debug {
		fmt.Fprintf(os.Stderr, format+"\n", a...)
	}
}

func min(i, j int) int {
	if i < j {
		return i
	}
	return j
}

func sol(n int64, k int64, in []byte) []byte {
	// Find the position of first '1' in the string.
	// Invariant: f1Pos is always position of first one in the string.
	f1Pos := int64(bytes.IndexByte(in, '1'))
	if f1Pos == -1 {
		// This string is all zeroes.
		return in
	}

	// Invariant: kNow is how many moves we can do at this point.
	kNow := k
	// Invariant: f0Pos is always position of first zero after one at f1Pos.
	f0Pos := f1Pos + 1
	for {
		// Find the position of the first '0' after '1' in the string.
		for i := f0Pos; i < int64(len(in)); i++ {
			if in[i] == '0' {
				f0Pos = i
				break
			}
		}
		if f0Pos == int64(len(in)) {
			// This string is all ones (and maybe some leading zeroes).
			return in
		}

		// We want to move f0Pos to f1Pos, but kNow may be too low.
		if f0Pos-f1Pos > kNow {
			// kNow, the maximum number of moves, may be too low.
			in[f0Pos], in[f0Pos-kNow] = in[f0Pos-kNow], in[f0Pos]
			return in
		}
		in[f0Pos], in[f1Pos] = in[f1Pos], in[f0Pos]
		// fix invariants
		kNow -= f0Pos - f1Pos
		f1Pos++ // We have replaced the leading one with zero
		f0Pos++ // We have moved the leading one to (former) f0Pos.
	}
}

func main() {
	defer writer.Flush()
	var q int
	Scanf("%d\n", &q)

	for i := 0; i < q; i++ {
		var n int64
		var k int64
		var in []byte
		Scanf("%d %d\n", &n, &k)
		Scanf("%s\n", &in)
		Printf("%s\n", string(sol(n, k, in)))
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)
var r = bufio.NewReader(os.Stdin)
var w = bufio.NewWriter(os.Stdout)

func print( a ...interface{}) { fmt.Fprint(w, a...) }
func scan( a ...interface{})  { fmt.Fscan(r, a...) }

func main() {
	defer w.Flush()
	var q int
	scan(&q)
	for i := 0; i < q; i++ {
		var str string
		var l, left, j int
		var c int64
		left = 0;
		j = 0;
		scan(&l, &c, &str)
		var s = []rune(str)
		var haschanged =true
		for ; haschanged && c > 0; {
			haschanged = false
			s[left] = s[left]
			for ; left < l ;left++{
				if s[left] == '1' {
					break
				}
			};
			for ; j < l ;j++ {
				if j > left && s[j] == '0' {
					if c >= int64(j - left) {
						s[j],s[left] = s[left], s[j]
						c = c - int64(j - left)
					}else{
						s[j],s[int64(j) - c] = s[int64(j) - c], s[j]
						c = 0
					}
					haschanged = true
					break
				}
			}
		}
		print(string(s),"\n")
	}
}

package main

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

const (
	MX_INT int64 = math.MaxInt64
	MN_INT int64 = math.MinInt64
)

func main() {
	for t := ScanInt64(); t > 0; t-- {
		n, k := ScanInt64(), ScanInt64()
		a := []rune(ScanWord())

		for i, j := int64(0), int64(0); i < n; i++ {
			if a[i] == '0' {

				l := Min(i-j, k)
				a[i], a[i-l] = a[i-l], a[i]
				k -= l
				j++
			}
		}
		Pl(string(a))
	}

	close(O)
	oWg.Wait()
}

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

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

func init() {
	Pl = fmt.Println

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

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

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

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

func ScanWord() string {
	in.Scan()
	return in.Text()
}

func ScanStringSlice(n int64) []string {
	length := int(n)
	s := make([]string, n)
	for i := 0; i < length; i++ {
		s[i] = ScanWord()
	}
	return s
}

func ScanInt64() int64 {
	intStr := ScanWord()
	i, err := strconv.ParseInt(intStr, 10, 64)
	if err != nil {
		panic(err)
	}
	return i
}

func ScanInt() int {
	intStr := ScanWord()
	i, err := strconv.Atoi(intStr)
	if err != nil {
		panic(err)
	}
	return i
}

func ScanInt64Slice(n int64) []int64 {
	length := int(n)
	arr := make([]int64, n)
	for i := 0; i < length; i++ {
		arr[i] = ScanInt64()
	}
	return arr
}

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

func Mx(args ...int64) int64 {
	maxVal := MN_INT
	for _, v := range args {
		if v > maxVal {
			maxVal = v
		}
	}
	return maxVal
}

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

func Mn(args ...int64) int64 {
	minVal := MX_INT
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
	}
	return minVal
}

func LCM(a, b int64) int64 {
	return a / GCD(a, b) * b
}

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

func S(v interface{}) string {
	return fmt.Sprintf("%v", v)
}

package main

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

const (
	MX_INT int64 = math.MaxInt64
	MN_INT int64 = math.MinInt64
)

func main() {
	for t := ScanInt64(); t > 0; t-- {
		n, k := ScanInt64(), ScanInt64()
		a := []byte(ScanWord())

		for i, j := int64(0), int64(0); i < n; i++ {
			if a[i] == '0' {

				l := Min(i-j, k)
				a[i], a[i-l] = a[i-l], a[i]
				k -= l
				j++
			}
		}
		Pl(string(a))
	}

	close(O)
	oWg.Wait()
}

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

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

func init() {
	Pl = fmt.Println

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

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

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

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

func ScanWord() string {
	in.Scan()
	return in.Text()
}

func ScanStringSlice(n int64) []string {
	length := int(n)
	s := make([]string, n)
	for i := 0; i < length; i++ {
		s[i] = ScanWord()
	}
	return s
}

func ScanInt64() int64 {
	intStr := ScanWord()
	i, err := strconv.ParseInt(intStr, 10, 64)
	if err != nil {
		panic(err)
	}
	return i
}

func ScanInt() int {
	intStr := ScanWord()
	i, err := strconv.Atoi(intStr)
	if err != nil {
		panic(err)
	}
	return i
}

func ScanInt64Slice(n int64) []int64 {
	length := int(n)
	arr := make([]int64, n)
	for i := 0; i < length; i++ {
		arr[i] = ScanInt64()
	}
	return arr
}

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

func Mx(args ...int64) int64 {
	maxVal := MN_INT
	for _, v := range args {
		if v > maxVal {
			maxVal = v
		}
	}
	return maxVal
}

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

func Mn(args ...int64) int64 {
	minVal := MX_INT
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
	}
	return minVal
}

func LCM(a, b int64) int64 {
	return a / GCD(a, b) * b
}

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

func S(v interface{}) string {
	return fmt.Sprintf("%v", v)
}

package main

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

const (
	MX_INT int64 = math.MaxInt64
	MN_INT int64 = math.MinInt64
)

func main() {
	for t := ScanInt64(); t > 0; t-- {
		n, k := ScanInt64(), ScanInt64()
		a := []byte(ScanWord())

		for i, j := int64(0), int64(0); i < n; i++ {
			if a[i] == '0' {

				l := Min(i-j, k)
				a[i], a[i-l] = a[i-l], a[i]
				k -= l
				j++
			}
		}
		Pl(string(a))
	}

	close(O)
	oWg.Wait()
}

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

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

func init() {
	Pl = fmt.Println

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

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

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

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

func ScanWord() string {
	in.Scan()
	return in.Text()
}

func ScanStringSlice(n int64) []string {
	length := int(n)
	s := make([]string, n)
	for i := 0; i < length; i++ {
		s[i] = ScanWord()
	}
	return s
}

func ScanInt64() int64 {
	intStr := ScanWord()
	i, err := strconv.ParseInt(intStr, 10, 64)
	if err != nil {
		panic(err)
	}
	return i
}

func ScanInt() int {
	intStr := ScanWord()
	i, err := strconv.Atoi(intStr)
	if err != nil {
		panic(err)
	}
	return i
}

func ScanInt64Slice(n int64) []int64 {
	length := int(n)
	arr := make([]int64, n)
	for i := 0; i < length; i++ {
		arr[i] = ScanInt64()
	}
	return arr
}

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

func Mx(args ...int64) int64 {
	maxVal := MN_INT
	for _, v := range args {
		if v > maxVal {
			maxVal = v
		}
	}
	return maxVal
}

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

func Mn(args ...int64) int64 {
	minVal := MX_INT
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
	}
	return minVal
}

func LCM(a, b int64) int64 {
	return a / GCD(a, b) * b
}

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

func S(v interface{}) string {
	return fmt.Sprintf("%v", v)
}

package main

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

const (
	MX_INT int64 = math.MaxInt64
	MN_INT int64 = math.MinInt64
)

func main() {
	for t := ScanInt64(); t > 0; t-- {
		n, k := ScanInt64(), ScanInt64()
		a := []rune(ScanWord())

		for i, j := int64(0), int64(0); i < n; i++ {
			if a[i] == '0' {

				l := Min(i-j, k)
				a[i], a[i-l] = a[i-l], a[i]
				k -= l
				j++
			}
		}
		Pl(string(a))
	}

	close(O)
	oWg.Wait()
}

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

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

func init() {
	Pl = fmt.Println

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

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

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

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

func ScanWord() string {
	in.Scan()
	return in.Text()
}

func ScanStringSlice(n int64) []string {
	length := int(n)
	s := make([]string, n)
	for i := 0; i < length; i++ {
		s[i] = ScanWord()
	}
	return s
}

func ScanInt64() int64 {
	intStr := ScanWord()
	i, err := strconv.ParseInt(intStr, 0, 64)
	if err != nil {
		panic(err)
	}
	return i
}

func ScanInt() int {
	intStr := ScanWord()
	i, err := strconv.Atoi(intStr)
	if err != nil {
		panic(err)
	}
	return i
}

func ScanInt64Slice(n int64) []int64 {
	length := int(n)
	arr := make([]int64, n)
	for i := 0; i < length; i++ {
		arr[i] = ScanInt64()
	}
	return arr
}

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

func Mx(args ...int64) int64 {
	maxVal := MN_INT
	for _, v := range args {
		if v > maxVal {
			maxVal = v
		}
	}
	return maxVal
}

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

func Mn(args ...int64) int64 {
	minVal := MX_INT
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
	}
	return minVal
}

func LCM(a, b int64) int64 {
	return a / GCD(a, b) * b
}

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

func S(v interface{}) string {
	return fmt.Sprintf("%v", v)
}

// Author: sighduck
// URL: https://codeforces.com/contest/1256/problem/D

package main

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

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

func initArray(size int64) []string {
	arr := make([]string, size)
	for i := int64(0); i < size; i++ {
		arr[i] = "1"
	}
	return arr
}

func Solve(n int64, k int64, s string) string {
	answer := initArray(n)
	zeros := int64(0)
	for i := int64(0); i < n; i++ {
		if string(s[i]) == "0" {
			swaps := min(k, i-zeros)
			k -= swaps
			answer[i-swaps] = "0"
			zeros += 1
		}
	}
	return strings.Join(answer, "")
}

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

	var q int
	fmt.Fscanf(reader, "%d\n", &q)
	for i := 0; i < q; i++ {
		var n, k int64
		var s string
		fmt.Fscanf(reader, "%d %d\n%s\n", &n, &k, &s)
		fmt.Fprintf(writer, "%s\n", Solve(n, k, s))
	}
}

package main

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

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

func main() {
	//bs, _ := ioutil.ReadFile("in.txt")
	//treader := bytes.NewBuffer(bs)
	//reader := bufio.NewReader(treader)
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var q, n, k int64
	var bin []byte
	var str string
	_ , _= fmt.Fscanf(reader, "%d\n", &q)
	for  q > 0 {
		q--
		_, _ = fmt.Fscanf(reader, "%d %d\n", &n, &k)
		_, _ = fmt.Fscanf(reader, "%s\n", &str)
		bin = []byte(str)
		pos := int64(0)

		for i := pos; i < int64(len(bin)); i++ {
			if bin[i] == '0' {
				l := min(i - pos, k)
				bin[i], bin[i - l] = bin[i - l], bin[i]
				k -= l
				pos++
			}
		}
		_, _ = fmt.Fprintf(writer, "%s\n", bin)
	}
}

package main

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

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

	return s
}

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

	if e == 0 {
		return 1
	}

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

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

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

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

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

	return a
}

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

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

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

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

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

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

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

	return string(res)
}

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

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

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

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

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

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

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

var q int
var n, k int64
var S []rune

func main() {
	q = ReadInt()
	for tc := 0; tc < q; tc++ {
		n, k = ReadInt64_2()
		S = ReadRuneSlice()

		solve()
	}
}

func solve() {
	zeroNum := 0
	for i := int64(0); i < n; i++ {
		if S[i] == '0' {
			zeroNum++
		}
	}
	zeros := make([]int64, zeroNum)

	j := int64(0)
	for i := int64(0); i < n; i++ {
		if S[i] == '0' {
			zeros[j] = i - j
			j++
		}
	}

	zeroSums := make([]int64, zeroNum+1)
	for i := 0; i < zeroNum; i++ {
		zeroSums[i+1] = zeroSums[i] + zeros[i]
	}

	// c: 確実に先頭に持ってくることができる0の数
	c := 0
	for i := 0; i < len(zeroSums); i++ {
		if zeroSums[i] > k {
			break
		} else {
			c = i
		}
	}

	answers := []rune{}
	for i := 0; i < c; i++ {
		answers = append(answers, '0')
	}
	skipped := 0
	for i := int64(0); i < n; i++ {
		if S[i] == '0' && skipped < c {
			skipped++
		} else {
			answers = append(answers, S[i])
		}
	}

	if c == zeroNum {
		fmt.Println(string(answers))
		return
	}

	k -= zeroSums[c]
	for i := int64(c); i < n; i++ {
		if answers[i] == '0' {
			for j := i; j > 0; j-- {
				if answers[j-1] > answers[j] {
					answers[j], answers[j-1] = answers[j-1], answers[j]
					k--
				}
				if k == 0 {
					break
				}
			}

			fmt.Println(string(answers))
			return
		}
	}
}

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

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

package main

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

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

	return s
}

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

	if e == 0 {
		return 1
	}

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

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

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

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

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

	return a
}

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

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

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

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

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

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

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

	return string(res)
}

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

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

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

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

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

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

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

var q int
var n, k int64
var S []rune

func main() {
	q = ReadInt()
	for tc := 0; tc < q; tc++ {
		n, k = ReadInt64_2()
		S = ReadRuneSlice()

		solve()
	}
}

func solve() {
	zeroNum := 0
	for i := int64(0); i < n; i++ {
		if S[i] == '0' {
			zeroNum++
		}
	}
	zeros := make([]int64, zeroNum)

	j := int64(0)
	for i := int64(0); i < n; i++ {
		if S[i] == '0' {
			zeros[j] = i - j
			j++
		}
	}

	zeroSums := make([]int64, zeroNum+1)
	for i := 0; i < zeroNum; i++ {
		zeroSums[i+1] = zeroSums[i] + zeros[i]
	}

	// c: 確実に先頭に持ってくることができる0の数
	c := 0
	for i := 0; i < len(zeroSums); i++ {
		if zeroSums[i] > k {
			break
		} else {
			c = i
		}
	}
	fmt.Println(zeroSums)

	answers := []rune{}
	for i := 0; i < c; i++ {
		answers = append(answers, '0')
	}
	skipped := 0
	for i := int64(0); i < n; i++ {
		if S[i] == '0' && skipped < c {
			skipped++
		} else {
			answers = append(answers, S[i])
		}
	}

	if c == zeroNum {
		fmt.Println(string(answers))
		return
	}

	k -= zeroSums[c]
	for i := int64(c); i < n; i++ {
		if answers[i] == '0' {
			for j := i; j > 0; j-- {
				if k == 0 {
					break
				}
				if answers[j-1] > answers[j] {
					answers[j], answers[j-1] = answers[j-1], answers[j]
					k--
				}
			}

			fmt.Println(string(answers))
			return
		}
	}
}

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

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

// Author: sighduck
// URL: https://codeforces.com/contest/1256/problem/D

package main

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

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

func initArray(size int) []string {
	arr := make([]string, size)
	for i := 0; i < size; i++ {
		arr[i] = "1"
	}
	return arr
}

func Solve(n int, k int, s string) string {
	answer := initArray(n)
	zeros := 0
	for i, elem := range s {
		if string(elem) == "0" {
			swaps := min(k, i-zeros)
			k -= swaps
			answer[i-swaps] = "0"
			zeros += 1
		}
	}
	return strings.Join(answer, "")
}

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

	var q int
	fmt.Fscanf(reader, "%d\n", &q)
	for i := 0; i < q; i++ {
		var n, k int
		var s string
		fmt.Fscanf(reader, "%d %d\n%s\n", &n, &k, &s)
		fmt.Fprintf(writer, "%s\n", Solve(n, k, s))
	}
}

package main

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

func main() {
	// bs, _ := ioutil.ReadFile("in.txt")
	// treader := bytes.NewBuffer(bs)
	// reader := bufio.NewReader(treader)
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var q, n, k int64
	var bin []byte
	_ , _= fmt.Fscanf(reader, "%d\n", &q)
	for  q > 0 {
		q--
		_, _ = fmt.Fscanf(reader, "%d %d\n", &n, &k)
		bin, _, _ = reader.ReadLine()
		pos := int64(0)
		for k > 0 {
			remain0Pos := int64(-1)
			for i := pos;i < int64(len(bin));i++ {
				if bin[i] == '0' {
					remain0Pos = i
					break
				}
			}
			if remain0Pos == -1 {
				break
			}
			if k >= int64(remain0Pos - pos) {
				k -= int64(remain0Pos - pos)
				bin[pos], bin[remain0Pos] = bin[remain0Pos], bin[pos]
			} else {
				bin[remain0Pos], bin[remain0Pos - k] = bin[remain0Pos - k], bin[remain0Pos]
				k = 0
			}
			pos++
		}
		_, _ = fmt.Fprintf(writer, "%s\n", bin)
	}
}

package main

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

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

func main() {
	//bs, _ := ioutil.ReadFile("in.txt")
	 //treader := bytes.NewBuffer(bs)
	 //reader := bufio.NewReader(treader)
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var q, n, k int64
	var bin []byte
	_ , _= fmt.Fscanf(reader, "%d\n", &q)
	for  q > 0 {
		q--
		_, _ = fmt.Fscanf(reader, "%d %d\n", &n, &k)
		bin, _, _ = reader.ReadLine()
		pos := int64(0)

		for i := pos; i < int64(len(bin)); i++ {
			if bin[i] == '0' {
				l := min(i - pos, k)
				bin[i], bin[i - l] = bin[i - l], bin[i]
				k -= l
				pos++
			}
		}
		_, _ = fmt.Fprintf(writer, "%s\n", bin)
	}
}