package main

import "fmt"
import "bufio"
import "os"

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

	var n, m, K int
	fmt.Fscanf(buf, "%d %d %d\n", &n, &m, &K)

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

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

	const inf = int64(1000000000000000)

	cost := func(i int, j int) int64 {
		if c[i] == 0 {
			return int64(p[i][j])
		} else if c[i] == j {
			return 0
		} else {
			return inf
		}
	}

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

	f := make([][][]int64, n+1)
	for i := 1; i <= n; i++ {
		f[i] = make([][]int64, m+1)
		for j := 1; j <= m; j++ {
			f[i][j] = make([]int64, K+1)
			for k := 1; k <= K; k++ {
				f[i][j][k] = inf
			}
		}
	}

	for j := 1; j <= m; j++ {
		f[1][j][1] = cost(1, j)
	}

	for i := 2; i <= n; i++ {
		for j := 1; j <= m; j++ {
			delta := cost(i, j)
			for k := 1; k <= K; k++ {
				f[i][j][k] = min(f[i][j][k], f[i-1][j][k]+delta)
				for c := 1; c <= m; c++ {
					if (c != j) && (k > 1) {
						f[i][j][k] = min(f[i][j][k], f[i-1][c][k-1]+delta)
					}
				}
			}
		}
	}

	best := f[n][1][K]
	for j := 2; j <= m; j++ {
		best = min(best, f[n][j][K])
	}
	if best >= inf {
		best = -1
	}

	fmt.Println(best)
}

package main

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

var scanner *bufio.Scanner

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

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

  n, m, k := getI(), getI(), getI()
  trees := make([]int, n)
  for i := range trees {
    trees[i] = getI()
  }
  allColors := make([]int, m)
  for i := range allColors {
    allColors[i] = i + 1
  }
  colorBeautyCosts := make([][]int64, m + 1)
  for i := 0; i <= m; i++ {
    colorBeautyCosts[i] = make([]int64, k + 1)
    for j := 0; j <= k; j++ {
      colorBeautyCosts[i][j] = -1
    }
  }
  colorBeautyCosts[0][0] = 0
  for _, tree := range trees {
    choices := allColors
    if tree != 0 {
      choices = []int{ tree }
    }
    paintCosts := make([]int64, m + 1)
    for i := 1; i <= m; i++ {
      paintCosts[i] = getI64()
    }
    newColorBeautyCosts := make([][]int64, m + 1)
    for i := 0; i <= m; i++ {
      newColorBeautyCosts[i] = make([]int64, k + 1)
      for j := 0; j <= k; j++ {
        newColorBeautyCosts[i][j] = -1
      }
    }
    for _, newColor := range choices {
      costDelta := paintCosts[newColor]
      if tree != 0 {
        costDelta = 0
      }
      newCosts := newColorBeautyCosts[newColor]
      for color, costs := range colorBeautyCosts {
        beautyDelta := 0
        if color != newColor {
          beautyDelta = 1
        }
        for beauty, cost := range costs {
          if cost == -1 {
            continue
          }
          newBeauty := beauty + beautyDelta
          if newBeauty > k {
            continue
          }
          newCost := cost + costDelta
          foundCost := newCosts[newBeauty]
          if foundCost == -1 || newCost < foundCost {
            newCosts[newBeauty] = newCost
          }
        }
      }
    }
    colorBeautyCosts = newColorBeautyCosts
  }
  result := int64(-1)
  for _, costs := range colorBeautyCosts {
    cost := costs[k]
    if cost == -1 {
      continue
    }
    if result == -1 || cost < result {
      result = cost
    }
  }
  writer.WriteString(fmt.Sprintf("%d\n", result))
}

// http://codeforces.com/contest/711/problem/C
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

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

	return b
}

func main () {
	var n, m, K int

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)

	fmt.Fscanln(input, &n, &m, &K)

	c := make([]int, n) // исходные цвета деревьев
	p := make([][]int, n)

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

	for i := 0; i < n; i++ {
		p[i] = make([]int, m + 1)
		for j := 1; j <= m; j++ {
			fmt.Fscan(input, &p[i][j])
		}
	}

	// Solve
	dp := [101][101][101]int64{}
	const inf = math.MaxInt64 - 1000000001

	for i := 0; i < n; i++ {
		for k := 0; k <= K; k++ {
			for color := 0; color <= 100; color++ {
				// Init inf
				dp[i][k][color] = inf
			}
		}
	}

	if c[0] == 0 {
		for color := 1; color <= m; color++ {
			dp[0][1][color] = int64(p[0][color])
		}
	} else {
		dp[0][1][c[0]] = 0
	}

	for i := 1; i < n; i++ {
		for k := 1; k <= K; k++ {

			if c[i] == 0 {
				// Если можно рисовать
				for color := 1; color <= m; color++ {
					for prev_color := 1; prev_color <= m; prev_color++ {
						if prev_color == color {
							dp[i][k][color] = min(dp[i - 1][k][prev_color] + int64(p[i][color]), dp[i][k][color])
						} else {
							dp[i][k][color] = min(dp[i - 1][k - 1][prev_color] + int64(p[i][color]), dp[i][k][color])
						}
					}
				}
			} else {
				// Если нельзя рисовать
				for prev_color := 1; prev_color <= m; prev_color++ {
					if prev_color == c[i] {
						dp[i][k][c[i]] = min(dp[i - 1][k][prev_color], dp[i][k][c[i]])
					} else {
						dp[i][k][c[i]] = min(dp[i - 1][k - 1][prev_color], dp[i][k][c[i]])
					}
				}
			}
		}
	}

	ans := int64(inf)
	for i := 1; i <= m; i++ {
		ans = min(ans, dp[n - 1][K][i])
	}

	if ans == inf {
		fmt.Println("-1")
		return
	}

	fmt.Println(ans)
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF711C(_r io.Reader, out io.Writer) {
	min := func(a, b int64) int64 {
		if a < b {
			return a
		}
		return b
	}
	in := bufio.NewReader(_r)
	var n, m, tar int
	Fscan(in, &n, &m, &tar)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	cost := make([][]int64, n)
	for i := range cost {
		cost[i] = make([]int64, m)
		for j := range cost[i] {
			Fscan(in, &cost[i][j])
		}
	}

	const inf int64 = 1e18
	ans := inf
	dp := make([][][]int64, n)
	for i := range dp {
		dp[i] = make([][]int64, n+1)
		for j := range dp[i] {
			dp[i][j] = make([]int64, m)
			for k := range dp[i][j] {
				dp[i][j][k] = inf
			}
		}
	}
	if c := a[0]; c > 0 {
		dp[0][1][c-1] = 0
	} else {
		dp[0][1] = cost[0]
	}
	for i := 1; i < n; i++ {
		for b := 1; b <= n; b++ {
			for c, cst := range cost[i] {
				if a[i] > 0 {
					if c != a[i]-1 {
						continue
					}
					cst = 0
				}
				for pc := 0; pc < m; pc++ {
					bb := b
					if c != pc {
						bb--
					}
					dp[i][b][c] = min(dp[i][b][c], dp[i-1][bb][pc]+cst)
				}
			}
		}
	}
	for _, v := range dp[n-1][tar] {
		ans = min(ans, v)
	}
	if ans == inf {
		ans = -1
	}
	Fprint(out, ans)
}

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

package main

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

const (
	maxNM = 110
	INF   = int64(1 << 40)
)

var N, M, K int
var C []int
var P [][]int
var dp [maxNM][maxNM][maxNM]int64

func main() {
	fsc := NewFastScanner()
	N, M, K = fsc.NextInt(), fsc.NextInt(), fsc.NextInt()
	C = make([]int, N)
	for i := 0; i < N; i++ {
		C[i] = fsc.NextInt()
	}
	P = make([][]int, N)
	for i := 0; i < N; i++ {
		P[i] = make([]int, M)
	}
	for i := 0; i < N; i++ {
		for j := 0; j < M; j++ {
			P[i][j] = fsc.NextInt()
		}
	}
	for i := 0; i < maxNM; i++ {
		for j := 0; j < maxNM; j++ {
			for k := 0; k < maxNM; k++ {
				dp[i][j][k] = INF
			}
		}
	}
	if C[0] == 0 {
		dp[0][0][0] = 0
	} else {
		dp[0][C[0]][1] = 0
	}

	for i := 0; i < N; i++ { //pos
		for j := 0; j <= M; j++ { //color
			for k := 0; k <= K; k++ { //cnt
				if dp[i][j][k] == INF {
					continue
				}
				if C[i] != 0 { //colored
					if j == C[i] {
						dp[i+1][C[i]][k] = Min(dp[i+1][C[i]][k], dp[i][j][k])
					} else {
						dp[i+1][C[i]][k+1] = Min(dp[i+1][C[i]][k+1], dp[i][j][k])
					}
				} else { //uncolored

					for c := 0; c < M; c++ {
						if c+1 == j { //same color
							dp[i+1][c+1][k] = Min(dp[i+1][c+1][k], dp[i][j][k]+int64(P[i][c]))
						} else {
							dp[i+1][c+1][k+1] = Min(dp[i+1][c+1][k+1], dp[i][j][k]+int64(P[i][c]))
						}
					}
				}
			}
		}
	}
	var ans int64
	ans = INF
	for i := 1; i <= M; i++ {
		ans = Min(ans, dp[N][i][K])
	}
	if ans == INF {
		ans = -1
	}
	fmt.Println(ans)
}

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

//template
type FastScanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewFastScanner() *FastScanner {
	rdr := bufio.NewReaderSize(os.Stdin, 2028)
	return &FastScanner{r: rdr}
}

func (s *FastScanner) 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 *FastScanner) NextInt() int {
	val, err := strconv.Atoi(s.Next())
	if err != nil {
		panic(err)
	}
	return val
}
func (s *FastScanner) NextInt64() int64 {
	val, err := strconv.ParseInt(s.Next(), 10, 64)
	if err != nil {
		panic(err)
	}
	return val
}
func (s *FastScanner) Pre() {
	if s.p >= len(s.buf) {
		s.ReadLine()
		s.p = 0
	}
}

func (s *FastScanner) ReadLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, err := s.r.ReadLine()
		if err != nil {
			panic(err.Error())
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

// http://codeforces.com/contest/711/problem/C
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

func main () {
	var n, m, K int

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)

	fmt.Fscanln(input, &n, &m, &K)

	c := make([]int, n) // исходные цвета деревьев
	p := make([][]int, n)

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

	for i := 0; i < n; i++ {
		p[i] = make([]int, m + 1)
		for j := 1; j <= m; j++ {
			fmt.Fscan(input, &p[i][j])
		}
	}

	// Solve
	dp := make([][]struct{
		litres int64
		k int
	}, n)

	for i := 0; i < n; i++ {
		dp[i] = make([]struct{
			litres int64
			k int
		}, m + 1)

		if c[i] == 0 {
			// Если можно рисовать
			for color := 1; color <= m; color++ {

				if i == 0 {
					dp[i][color].k = 1
					dp[i][color].litres = int64(p[i][color])
					continue
				}

				for prev_color := 1; prev_color <= m; prev_color++ {
					if prev_color == color {
						if dp[i - 1][prev_color].k > 0 && (dp[i][color].k == 0 || dp[i][color].litres > dp[i - 1][prev_color].litres + int64(p[i][color])) {
							dp[i][color].litres = dp[i - 1][prev_color].litres + int64(p[i][color])
							dp[i][color].k = dp[i - 1][prev_color].k
						}
					} else {
						if dp[i - 1][prev_color].k > 0 && (dp[i][color].k == 0 || dp[i][color].litres > dp[i - 1][prev_color].litres + int64(p[i][color])) {
							if dp[i - 1][prev_color].k + 1 <= K {
								dp[i][color].litres = dp[i - 1][prev_color].litres + int64(p[i][color])
								dp[i][color].k = dp[i - 1][prev_color].k + 1
							}
						}
					}
				}
			}

		} else {
			// Если нельзя рисовать
			if i == 0 {
				dp[i][c[i]].k = 1
				dp[i][c[i]].litres = 0
				continue
			}

			for prev_color := 1; prev_color <= m; prev_color++ {
				if prev_color == c[i] {
					if dp[i - 1][prev_color].k > 0 && (dp[i][c[i]].k == 0 || dp[i][c[i]].litres > dp[i - 1][prev_color].litres) {
						dp[i][c[i]].litres = dp[i - 1][prev_color].litres
						dp[i][c[i]].k = dp[i - 1][prev_color].k
					}
				} else {
					if dp[i - 1][prev_color].k > 0 && (dp[i][c[i]].k == 0 || dp[i][c[i]].litres > dp[i - 1][prev_color].litres) {
						if dp[i - 1][prev_color].k + 1 <= K {
							dp[i][c[i]].litres = dp[i - 1][prev_color].litres
							dp[i][c[i]].k = dp[i - 1][prev_color].k + 1
						}
					}
				}
			}
		}

	}

	ans := dp[n - 1][1]
	ans.litres = math.MaxInt64
	for i := 1; i <= m; i++ {
		fmt.Println(dp[n - 1][i])
		if dp[n - 1][i].k == K && ans.litres > dp[n - 1][i].litres {
			ans = dp[n - 1][i]
		}
	}

	if ans.k != K {
		fmt.Println("-1")
		return
	}

	fmt.Println(ans.litres)
}

// http://codeforces.com/contest/711/problem/C
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

func main () {
	var n, m, K int

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)

	fmt.Fscanln(input, &n, &m, &K)

	c := make([]int, n) // исходные цвета деревьев
	p := make([][]int, n)

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

	for i := 0; i < n; i++ {
		p[i] = make([]int, m + 1)
		for j := 1; j <= m; j++ {
			fmt.Fscan(input, &p[i][j])
		}
	}

	// Solve
	dp := make([][]struct{
		litres int64
		k int
	}, n)

	for i := 0; i < n; i++ {
		dp[i] = make([]struct{
			litres int64
			k int
		}, m + 1)

		if c[i] == 0 {
			// Если можно рисовать
			for color := 1; color <= m; color++ {

				if i == 0 {
					dp[i][color].k = 1
					dp[i][color].litres = int64(p[i][color])
					continue
				}

				for prev_color := 1; prev_color <= m; prev_color++ {
					if prev_color == color {
						if dp[i - 1][prev_color].k > 0 && (dp[i][color].k == 0 || dp[i][color].litres > dp[i - 1][prev_color].litres + int64(p[i][color])) {
							dp[i][color].litres = dp[i - 1][prev_color].litres + int64(p[i][color])
							dp[i][color].k = dp[i - 1][prev_color].k
						}
					} else {
						if dp[i - 1][prev_color].k > 0 && (dp[i][color].k == 0 || dp[i][color].litres > dp[i - 1][prev_color].litres + int64(p[i][color])) {
							if dp[i - 1][prev_color].k + 1 <= K {
								dp[i][color].litres = dp[i - 1][prev_color].litres + int64(p[i][color])
								dp[i][color].k = dp[i - 1][prev_color].k + 1
							}
						}
					}
				}
			}

		} else {
			// Если нельзя рисовать
			if i == 0 {
				dp[i][c[i]].k = 1
				dp[i][c[i]].litres = 0
				continue
			}

			for prev_color := 1; prev_color <= m; prev_color++ {
				if prev_color == c[i] {
					if dp[i - 1][prev_color].k > 0 && (dp[i][c[i]].k == 0 || dp[i][c[i]].litres > dp[i - 1][prev_color].litres) {
						dp[i][c[i]].litres = dp[i - 1][prev_color].litres
						dp[i][c[i]].k = dp[i - 1][prev_color].k
					}
				} else {
					if dp[i - 1][prev_color].k > 0 && (dp[i][c[i]].k == 0 || dp[i][c[i]].litres > dp[i - 1][prev_color].litres) {
						if dp[i - 1][prev_color].k + 1 <= K {
							dp[i][c[i]].litres = dp[i - 1][prev_color].litres
							dp[i][c[i]].k = dp[i - 1][prev_color].k + 1
						}
					}
				}
			}
		}

	}

	ans := dp[n - 1][1]
	ans.litres = math.MaxInt64
	for i := 1; i <= m; i++ {
		if dp[n - 1][i].k == K && ans.litres > dp[n - 1][i].litres {
			ans = dp[n - 1][i]
		}
	}

	if ans.k != K {
		fmt.Println("-1")
		return
	}

	fmt.Println(ans.litres)
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF711C(_r io.Reader, out io.Writer) {
	min := func(a, b int) int {
		if a < b {
			return a
		}
		return b
	}
	in := bufio.NewReader(_r)
	var n, m, tar int
	Fscan(in, &n, &m, &tar)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	cost := make([][]int, n)
	for i := range cost {
		cost[i] = make([]int, m)
		for j := range cost[i] {
			Fscan(in, &cost[i][j])
		}
	}

	const inf int = 1e9
	ans := inf
	dp := make([][][]int, n)
	for i := range dp {
		dp[i] = make([][]int, n+1)
		for j := range dp[i] {
			dp[i][j] = make([]int, m)
			for k := range dp[i][j] {
				dp[i][j][k] = inf
			}
		}
	}
	if c := a[0]; c > 0 {
		dp[0][1][c-1] = 0
	} else {
		dp[0][1] = cost[0]
	}
	for i := 1; i < n; i++ {
		for b := 1; b <= n; b++ {
			for c, cst := range cost[i] {
				if a[i] > 0 {
					if c != a[i]-1 {
						continue
					}
					cst = 0
				}
				for pc := 0; pc < m; pc++ {
					bb := b
					if c != pc {
						bb--
					}
					dp[i][b][c] = min(dp[i][b][c], dp[i-1][bb][pc]+cst)
				}
			}
		}
	}
	for _, v := range dp[n-1][tar] {
		ans = min(ans, v)
	}
	if ans == inf {
		ans = -1
	}
	Fprint(out, ans)
}

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


package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
)

type Edge struct {
	from, to, next int
}

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewReader(bs)
	var n, m, k int
	fmt.Fscanf(reader, "%d %d %d\n", &n, &m, &k)

	firstEdge := make([]int, n)
	edges := make([]Edge, 0, 2*m)
	dist := make([]int, 2*m)
	from := make([]int, 2*m)
	for i := 0; i < n; i++ {
		firstEdge[i] = -1
	}
	for i := 0; i < 2*m; i++ {
		dist[i], from[i] = -1, -1
	}

	for i := 0; i < m; i++ {
		var x, y int
		fmt.Fscanf(reader, "%d %d\n", &x, &y)
		x, y = x-1, y-1
		edges, firstEdge[x] = append(edges, Edge{x, y, firstEdge[x]}), i*2
		edges, firstEdge[y] = append(edges, Edge{y, x, firstEdge[y]}), i*2+1
	}

	tabu := map[uint64]bool{}

	for i := 0; i < k; i++ {
		var a, b, c uint64
		fmt.Fscanf(reader, "%d %d %d\n", &a, &b, &c)
		a, b, c = a-1, b-1, c-1
		tabu[(a<<24)+(b<<12)+c] = true
	}

	queue := []int{}
	for e := firstEdge[0]; e != -1; e = edges[e].next {
		dist[e] = 1
		queue = append(queue, e)
	}

	for len(queue) > 0 {
		start := edges[queue[0]]
		mask := (uint64(start.from) << 24) + (uint64(start.to) << 12)
		for e := firstEdge[start.to]; e != -1; e = edges[e].next {
			if _, ok := tabu[mask+uint64(edges[e].to)]; !ok && (dist[e] == -1 || dist[e] > dist[queue[0]]+1) {
				dist[e] = dist[queue[0]] + 1
				from[e] = queue[0]
				queue = append(queue, e)
			}
		}
		queue = queue[1:]
	}

	best, beste := 9999999, -1

	for i := 0; i < 2*m; i++ {
		if edges[i].to == n-1 && dist[i] != -1 && dist[i] < best {
			best, beste = dist[i], i
		}
	}
	if beste == -1 {
		fmt.Printf("-1")
	} else {
		fmt.Printf("%d\n", best)
		path := []int{n - 1}
		for e := beste; e != -1; e = from[e] {
			path = append(path, edges[e].from)
		}
		for i := len(path) - 1; i >= 0; i-- {
			fmt.Printf("%d ", path[i]+1)
		}
	}

}

package main

import (
	"bufio"
	"fmt"
	"math"
	"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...)
	}
}

type tri struct {
	a, b, c int
}

type ij struct {
	i, j int
}

var (
	n, m, k int

	adj  [][]int
	forb = make(map[tri]bool)

	visit  []ij
	dists  = make(map[ij]int)
	prevIJ = make(map[ij]int)
)

func main() {
	defer writer.Flush()

	// Read input.
	Scanf("%d %d %d\n", &n, &m, &k)

	adj = make([][]int, n+1)
	for i := 0; i < m; i++ {
		var xi, yi int
		Scanf("%d %d\n", &xi, &yi)
		adj[xi] = append(adj[xi], yi)
		adj[yi] = append(adj[yi], xi)
	}

	for i := 0; i < k; i++ {
		var ai, bi, ci int
		Scanf("%d %d %d\n", &ai, &bi, &ci)
		forb[tri{ai, bi, ci}] = true
	}

	// Dijkstra, visit starting from visit starting from city 1.
	visit = []ij{{1, 1}}
	dists[ij{1, 1}] = 0
	dijkstra()

	// Find the way back.
	minDist := math.MaxInt32
	var minIJ ij
	for curIJ, d := range dists {
		if curIJ.j == n {
			if d < minDist {
				minDist = d
				minIJ = curIJ
			}
		}
	}

	if minDist == math.MaxInt32 {
		Printf("-1\n")
		return
	}

	path := []int{n}
	curIJ := minIJ
	for curIJ.j != 1 {
		path = append(path, curIJ.i)
		curIJ = ij{prevIJ[curIJ], curIJ.i}
	}
	Printf("%d\n", len(path)-1)
	for i := len(path) - 1; i >= 0; i-- {
		Printf("%d ", path[i])
	}
}

func dijkstra() {
	for len(visit) > 0 {
		// cur is j before i.
		cur := visit[0]
		visit = visit[1:]

		for _, nex := range adj[cur.j] {
			jNex := ij{cur.j, nex}
			if _, ok := dists[jNex]; ok {
				// d(j,nex) has already been calculated.
				continue
			}
			if forb[tri{cur.i, cur.j, nex}] {
				// i, j, nex is forbidden.
				continue
			}
			dists[jNex] = dists[cur] + 1
			prevIJ[jNex] = cur.i
			visit = append(visit, jNex)
		}
	}
}

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
)

type Edge struct {
	from, to, next int
}

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewReader(bs)
	var n, m, k int
	fmt.Fscanf(reader, "%d %d %d\n", &n, &m, &k)

	firstEdge := make([]int, n)
	edges := make([]Edge, 0, 2*m)
	dist := make([]int, 2*m)
	from := make([]int, 2*m)
	for i := 0; i < n; i++ {
		firstEdge[i] = -1
	}
	for i := 0; i < 2*m; i++ {
		dist[i], from[i] = -1, -1
	}

	for i := 0; i < m; i++ {
		var x, y int
		fmt.Fscanf(reader, "%d %d\n", &x, &y)
		x, y = x-1, y-1
		edges, firstEdge[x] = append(edges, Edge{x, y, firstEdge[x]}), i*2
		edges, firstEdge[y] = append(edges, Edge{y, x, firstEdge[y]}), i*2+1
	}

	tabu := map[uint64]bool{}

	for i := 0; i < k; i++ {
		var a, b, c uint64
		fmt.Fscanf(reader, "%d %d %d\n", &a, &b, &c)
		a, b, c = a-1, b-1, c-1
		tabu[(a<<24)+(b<<12)+c] = true
	}

	queue := []int{}
	for e := firstEdge[0]; e != -1; e = edges[e].next {
		dist[e] = 1
		queue = append(queue, e)
	}

	for len(queue) > 0 {
		start := edges[queue[0]]
		mask := (uint64(start.from) << 24) + (uint64(start.to) << 12)
		for e := firstEdge[start.to]; e != -1; e = edges[e].next {
			if _, ok := tabu[mask+uint64(edges[e].to)]; !ok && (dist[e] == -1 || dist[e] > dist[queue[0]]+1) {
				dist[e] = dist[queue[0]] + 1
				from[e] = queue[0]
				queue = append(queue, e)
			}
		}
		queue = queue[1:]
	}

	best, beste := 9999999, -1

	for i := 0; i < 2*m; i++ {
		if edges[i].next == n-1 && dist[i] != -1 && dist[i] < best {
			best, beste = dist[i], i
		}
	}
	if beste == -1 {
		fmt.Printf("-1")
	} else {
		fmt.Printf("%d\n", best)
		path := []int{n - 1}
		for e := beste; e != -1; e = from[e] {
			path = append(path, edges[e].from)
		}
		for i := len(path) - 1; i >= 0; i-- {
			fmt.Printf("%d ", path[i]+1)
		}
	}

}


package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	bf := make([]byte, 1024*1024)
	sc.Buffer(bf, 10*1024*1024)
	sc.Split(bufio.ScanWords)

	sudoku := make([][]byte, 9)

	ini := func () int {
		sc.Scan()
		i, _ := strconv.Atoi(sc.Text())
		return i
	}

	ins := func () []byte {
		sc.Scan()
		return sc.Bytes()
	}

	for t := ini(); t > 0 ; t-- {
		for i := 0; i < 9; i++ {
			sudoku[i] = ins()
		}

		sudoku[0][0] = sudoku[0][1]
		sudoku[1][3] = sudoku[1][4]
		sudoku[2][6] = sudoku[2][7]

		sudoku[3][1] = sudoku[3][2]
		sudoku[4][4] = sudoku[4][5]
		sudoku[5][7] = sudoku[5][8]

		sudoku[6][2] = sudoku[6][1]
		sudoku[7][5] = sudoku[7][4]
		sudoku[8][8] = sudoku[8][7]

		for i := range sudoku {
			fmt.Println(string(sudoku[i]))
		}
	}
}

package main

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

type BufferedWriter interface {
	Printf(format string, a ...interface{})
	Flush()
}

type writerImpl struct {
	*bufio.Writer
}

func NewBufferedWriter(writer io.Writer) BufferedWriter {
	return &writerImpl{Writer: bufio.NewWriter(writer)}
}

func (impl *writerImpl) Printf(f string, a ...interface{}) {
	fmt.Fprintf(impl.Writer, f, a...)
}

func (impl *writerImpl) Flush() {
	impl.Writer.Flush()
}

type WordScanner interface {
	NextInt() int
	NextInt64() int64
	NextString() string
}

type wordScannerImpl struct {
	*bufio.Scanner
}

func NewWordScanner(reader io.Reader) WordScanner {
	s := bufio.NewScanner(reader)
	s.Split(bufio.ScanWords)
	// adjust the following size as needed
	// 2 << 17 is good enough to scan strings of len 2*10^5
	// it could go as high as 2 << 26 before cf judge breaks
	size := 2 << 20
	buf := make([]byte, size)
	s.Buffer(buf, size)
	return &wordScannerImpl{Scanner: s}
}

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

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

func (impl *wordScannerImpl) NextString() string {
	impl.Scan()
	return impl.Text()
}

var reader = NewWordScanner(os.Stdin)
var writer = NewBufferedWriter(os.Stdout)

func main() {
	defer writer.Flush()
	t := reader.NextInt64()
	for ; t > 0; t-- {
		board := [][]rune{}
		for i := 0; i < 9; i++ {
			row := reader.NextString()
			board = append(board, []rune(row))
		}
		anti(board)
        //check(board)
		for _, row := range board {
			writer.Printf("%s\n", string(row))
		}
	}
}

func anti(b [][]rune) {
    for _, p := range [][2]int{
        {0, 0},
        {1, 3},
        {2, 6},
        {3, 1},
        {4, 4},
        {5, 7},
        {6, 2},
        {7, 5},
        {8, 8},
    }{
        b[p[0]][p[1]] += 1
        if b[p[0]][p[1]] > '9' {
            b[p[0]][p[1]] -= 9
        }
    }
}

func check(b [][]rune) {
	for i := 0; i < 9; i++ {
		m := map[rune]int{}
		for j := 0; j < 9; j++ {
			m[b[i][j]]++
		}
		writer.Printf("%d\n", len(m)) // should be 8
	}
	for i := 0; i < 9; i++ {
		m := map[rune]int{}
		for j := 0; j < 9; j++ {
			m[b[j][i]]++
		}
		writer.Printf("%d\n", len(m)) // should be 8
	}
}

package main

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

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

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }
func scanIntArr() ([]int, error) {
	str, err := reader.ReadString('\n')
	if err != nil {
		return nil, err
	}
	str = strings.TrimRight(str, "\n\r")
	strArr := strings.Split(str, " ")

	arr := make([]int, len(strArr))
	for i, strA := range strArr {
		arr[i], err = strconv.Atoi(strA)
		if err != nil {
			return nil, err
		}
	}

	return arr, nil
}

func main() {
	defer writer.Flush()

	var t int
	scanf("%d\n", &t)

	for i := 0; i < t; i++ {
		exec()
	}
}

var toBe = []int{0, 3, 6, 1, 4, 7, 2, 5, 8}

func exec() {
	field := make([][]int, 9)
	for i := 0; i < 9; i++ {
		field[i] = make([]int, 9)
		str, err := reader.ReadString('\n')
		if err != nil {
			fmt.Println(err.Error())
			panic(err)
		}
		str = strings.TrimRight(str, "\n\r")
		for j, s := range []byte(str) {
			if toBe[i] != j {
				printf("%s", string(s))
				continue
			}
			num, err := strconv.ParseInt(string(s), 10, 64)
			if err != nil {
				fmt.Println(err.Error())
				panic(err)
			}
			num -= 9
			if num == 0 {
				num = 1
			}
			printf("%d", int(math.Abs(float64(num))))
		}
		printf("\n")
	}
}

package main

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

var scanner *bufio.Scanner

func solve() {
	input := make([][]byte, 9)
	for i := 0; i < 9; i++ {
		input[i] = []byte(getString())
	}

	for i := 0; i < 9; i++ {
		for j := 0; j < 9; j++ {
			if input[i][j] == '1' {
				input[i][j] ='2'
			}
		}
	}
	for i := 0; i < 9; i++{
		fmt.Println(string(input[i]))
	}
}

func main() {

	f, err := os.Open("input.txt")
	if err == nil {
		os.Stdin = f
	}
	defer f.Close()

	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	t := fs()

	for i := 0; i < t; i++ {
		solve()
	}
}

func fs() int {
	if !scanner.Scan() {
		os.Exit(1)
	}
	i, _ := strconv.Atoi(scanner.Text())
	return i
}

func getString() string {
	if !scanner.Scan() {
		os.Exit(1)
	}
	return scanner.Text()
}

/*
URL:
https://codeforces.com/problemset/problem/1335/D
*/

package main

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

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

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

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

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

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

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

const (
	// General purpose
	MOD          = 1000000000 + 7
	ALPHABET_NUM = 26
	INF_INT64    = math.MaxInt64
	INF_BIT60    = 1 << 60
	INF_INT32    = math.MaxInt32
	INF_BIT30    = 1 << 30
	NIL          = -1

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

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

var (
	t int

	S    [][]rune
	memo [][2]int
)

func main() {
	memo = [][2]int{
		[2]int{0, 0}, [2]int{1, 3}, [2]int{2, 6},
		[2]int{3, 1}, [2]int{4, 4}, [2]int{5, 7},
		[2]int{6, 2}, [2]int{7, 5}, [2]int{8, 8},
	}

	t = ReadInt()
	for i := 0; i < t; i++ {
		S = [][]rune{}
		for j := 0; j < 9; j++ {
			row := ReadRuneSlice()
			S = append(S, row)
		}

		solve()
	}
}

func solve() {
	for _, c := range memo {
		i, j := c[0], c[1]
		if S[i][j] == '9' {
			S[i][j] = rune(1 + '0')
		} else {
			S[i][j]++
		}
	}

	for i := 0; i < 9; i++ {
		fmt.Println(string(S[i]))
	}
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

	return string(res)
}

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

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

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

	return string(res)
}

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

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

package main

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

func main() {
	proj := make([][]int, 9)
	for i := range proj {
		proj[i] = []int{i, ((3 * i) + (i / 3)) % 9}
	}

	var truncator string
	if runtime.GOOS == "windows" {
		truncator = "\r\n"
	} else {
		truncator = "\n"
	}

	reader := bufio.NewReader(os.Stdin)
	text, _ := reader.ReadString('\n')
	text = strings.TrimSuffix(text, truncator)
	testcase, _ := strconv.ParseInt(text, 10, 64)

	output := make([]string, testcase)
	var i int64
	sudoku := make([][]byte, 9)
	for i := range sudoku {
		sudoku[i] = make([]byte, 9)
	}

	for i = 0; i < testcase; i++ {
		for j := range sudoku {
			text, _ = reader.ReadString('\n')
			text = strings.TrimSuffix(text, truncator)
			inputSlice := []byte(text)
			copy(sudoku[j], inputSlice)
		}
		for _, v := range proj {
			sudoku[v[0]][v[1]] = next(sudoku[v[0]][v[1]])
		}
		output[i] = printsd(sudoku)
	}

	result := strings.Join(output, "\n")
	fmt.Printf(result)
	fmt.Printf("\n")
}

func next(a byte) byte {
	if a == '9' {
		a = '1'
		return a
	}
	a++
	return a
}

func printsd(a [][]byte) string {
	b := make([]string, len(a))
	for i, v := range a {
		b[i] = string(v)
	}
	c := strings.Join(b, "\n")
	return c
}

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

package main

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

func Solve(sudoku [][]rune) [][]rune {
	for i := 0; i < 9; i++ {
		for j := 0; j < 9; j++ {
			if sudoku[i][j] == '1' {
				sudoku[i][j] = '2'
			}
		}
	}
	return sudoku
}

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

	var t int
	fmt.Fscanf(reader, "%d\n", &t)
	for i := 0; i < t; i++ {
		sudoku := make([][]rune, 9)
		for i := 0; i < 9; i++ {
			var row string
			fmt.Fscanf(reader, "%s\n", &row)
			sudoku[i] = []rune(row)
		}
		ans := Solve(sudoku)
		for _, row := range ans {
			fmt.Fprintf(writer, "%s\n", string(row))
		}
	}
}

package main

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

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

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }
func scanIntArr() ([]int, error) {
	str, err := reader.ReadString('\n')
	if err != nil {
		return nil, err
	}
	str = strings.TrimRight(str, "\n\r")
	strArr := strings.Split(str, " ")

	arr := make([]int, len(strArr))
	for i, strA := range strArr {
		arr[i], err = strconv.Atoi(strA)
		if err != nil {
			return nil, err
		}
	}

	return arr, nil
}

func main() {
	defer writer.Flush()

	var t int
	scanf("%d\n", &t)

	for i := 0; i < t; i++ {
		exec()
	}
}

func exec() {
	field := make([][]int, 9)
	for i := 0; i < 9; i++ {
		field[i] = make([]int, 9)
		str, err := reader.ReadString('\n')
		if err != nil {
			fmt.Println(err.Error())
			panic(err)
		}
		str = strings.TrimRight(str, "\n\r")
		for j, s := range []byte(str) {
			if j != i {
				printf("%s", string(s))
				continue
			}
			num, err := strconv.ParseInt(string(s), 10, 64)
			if err != nil {
				fmt.Println(err.Error())
				panic(err)
			}
			num -= 9
			if num == 0 {
				num = 1
			}
			printf("%d", int(math.Abs(float64(num))))
		}
		printf("\n")
	}
}

package main

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

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

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }
func scanIntArr() ([]int, error) {
	str, err := reader.ReadString('\n')
	if err != nil {
		return nil, err
	}
	str = strings.TrimRight(str, "\n\r")
	strArr := strings.Split(str, " ")

	arr := make([]int, len(strArr))
	for i, strA := range strArr {
		arr[i], err = strconv.Atoi(strA)
		if err != nil {
			return nil, err
		}
	}

	return arr, nil
}

func main() {
	defer writer.Flush()

	var t int
	scanf("%d\n", &t)

	for i := 0; i < t; i++ {
		exec()
	}
}

func exec() {
	field := make([][]int, 9)
	for i := 0; i < 9; i++ {
		field[i] = make([]int, 9)
		str, err := reader.ReadString('\n')
		if err != nil {
			fmt.Println(err.Error())
			panic(err)
		}
		str = strings.TrimRight(str, "\n\r")
		for j, s := range []byte(str) {
			if j != i {
				printf("%s", string(s))
				continue
			}
			num, err := strconv.ParseInt(string(s), 10, 64)
			if err != nil {
				fmt.Println(err.Error())
				panic(err)
			}
			num -= 9
			if num == 0 {
				num = 1
			}
			printf("%d", int(math.Abs(float64(num-9))))
		}
		printf("\n")
	}
}

package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	bf := make([]byte, 1024*1024)
	sc.Buffer(bf, 10*1024*1024)
	sc.Split(bufio.ScanWords)

	sudoku := make([][]byte, 9)

	ini := func () int {
		sc.Scan()
		i, _ := strconv.Atoi(sc.Text())
		return i
	}

	ins := func () []byte {
		sc.Scan()
		return sc.Bytes()
	}

	for t := ini(); t > 0 ; t-- {
		for i := 0; i < 9; i++ {
			sudoku[i] = ins()
		}

		sudoku[0][0] = sudoku[0][1]
		sudoku[1][3] = sudoku[1][4]
		sudoku[2][5] = sudoku[2][6]

		sudoku[3][1] = sudoku[3][2]
		sudoku[4][4] = sudoku[4][5]
		sudoku[5][7] = sudoku[5][8]

		sudoku[6][2] = sudoku[6][1]
		sudoku[7][5] = sudoku[7][4]
		sudoku[8][8] = sudoku[8][7]

		for i := range sudoku {
			fmt.Println(string(sudoku[i]))
		}
	}
}

/*
URL:
https://codeforces.com/problemset/problem/1335/D
*/

package main

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

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

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

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

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

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

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

const (
	// General purpose
	MOD          = 1000000000 + 7
	ALPHABET_NUM = 26
	INF_INT64    = math.MaxInt64
	INF_BIT60    = 1 << 60
	INF_INT32    = math.MaxInt32
	INF_BIT30    = 1 << 30
	NIL          = -1

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

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

var (
	t int

	S [][]rune
)

func main() {
	t = ReadInt()
	for i := 0; i < t; i++ {
		S = [][]rune{}
		for j := 0; j < 9; j++ {
			row := ReadRuneSlice()
			S = append(S, row)
		}

		solve()
	}
}

func solve() {
	for i := 0; i < 9; i++ {
		j := i
		if S[i][j] == '9' {
			S[i][j] = rune(1 + '0')
		} else {
			S[i][j]++
		}
	}

	for i := 0; i < 9; i++ {
		fmt.Println(string(S[i]))
	}
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

	return string(res)
}

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

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

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

	return string(res)
}

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

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

package main

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

type BufferedWriter interface {
	Printf(format string, a ...interface{})
	Flush()
}

type writerImpl struct {
	*bufio.Writer
}

func NewBufferedWriter(writer io.Writer) BufferedWriter {
	return &writerImpl{Writer: bufio.NewWriter(writer)}
}

func (impl *writerImpl) Printf(f string, a ...interface{}) {
	fmt.Fprintf(impl.Writer, f, a...)
}

func (impl *writerImpl) Flush() {
	impl.Writer.Flush()
}

type WordScanner interface {
	NextInt() int
	NextInt64() int64
	NextString() string
}

type wordScannerImpl struct {
	*bufio.Scanner
}

func NewWordScanner(reader io.Reader) WordScanner {
	s := bufio.NewScanner(reader)
	s.Split(bufio.ScanWords)
	// adjust the following size as needed
	// 2 << 17 is good enough to scan strings of len 2*10^5
	// it could go as high as 2 << 26 before cf judge breaks
	size := 2 << 20
	buf := make([]byte, size)
	s.Buffer(buf, size)
	return &wordScannerImpl{Scanner: s}
}

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

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

func (impl *wordScannerImpl) NextString() string {
	impl.Scan()
	return impl.Text()
}

var reader = NewWordScanner(os.Stdin)
var writer = NewBufferedWriter(os.Stdout)

func main() {
	defer writer.Flush()
	t := reader.NextInt64()
	for ; t > 0; t-- {
		board := [][]rune{}
		for i := 0; i < 9; i++ {
			row := reader.NextString()
			board = append(board, []rune(row))
		}
		sol(board)
        //check(board)
		for _, row := range board {
			writer.Printf("%s\n", string(row))
		}
	}
}

func sol(b [][]rune) {
	b[0][0], b[1][3], b[2][6],
		b[3][1], b[4][4], b[5][7],
		b[6][2], b[7][5], b[8][8] = b[1][3], b[2][6],
		b[3][1], b[4][4], b[5][7],
		b[6][2], b[7][5], b[8][8], b[0][0]
}

func check(b [][]rune) {
    for i := 0; i < 9; i++ {
        m := map[rune]int{}
        for j := 0; j < 9; j++ {
            m[b[i][j]]++
        }
        writer.Printf("%d\n", len(m)) // should be 8
    }
    for i := 0; i < 9; i++ {
        m := map[rune]int{}
        for j := 0; j < 9; j++ {
            m[b[j][i]]++
        }
        writer.Printf("%d\n", len(m)) // should be 8
    }
}


package main
import (
	"bufio"
	."fmt"
	"os"
	."strings"
)
func main() {
	in := bufio.NewReader(os.Stdin)
	const P, M = 998244353, 3e5 + 1
	f := [M]int64{0, 0, 1}
	for i := 3; i < M; i++ {
		f[i] = ((3*f[i-1]-4*f[i-3])%P+P)%P
	}
	p := [M]int64{1}
	for i := 1; i < M; i++ {
		p[i] = p[i-1]*2%P
	}
	var n, m, w int
	Fscan(in, &n, &m)
	a := make([]string, n)
	for i := range a {
		Fscan(in, &a[i])
		w += Count(a[i], "o")
	}
	c := func(s string) (S int64) {
		for _, s := range Split(s, "*") {
			S = (S + f[len(s)]*p[w-len(s)]) % P
		}
		return
	}
	S := int64(0)
	for _, r := range a {
		S += c(r)
	}
	for j := range a[0] {
		s := []byte{}
		for _, r := range a {
			s = append(s, r[j])
		}
		S += c(string(s))
	}
	Print(S % P)
}

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	const P, M = 998244353, 3e5
	dp := [M + 1]int64{0, 0, 1}
	for i := 3; i <= M; i++ {
		dp[i] = ((3*dp[i-1]-4*dp[i-3])%P + P) % P
	}
	p2 := [M + 1]int64{1}
	for i := 1; i <= M; i++ {
		p2[i] = p2[i-1] << 1 % P
	}

	var n, m, w int
	Fscan(in, &n, &m)
	a := make([][]byte, n)
	for i := range a {
		Fscan(in, &a[i])
		w += bytes.Count(a[i], []byte{'o'})
	}
	f := func(s []byte) (S int64) {
		for i, n := 0, len(s); i < n; {
			if s[i] == '*' {
				i++
				continue
			}
			j := i
			for ; i < n && s[i] == 'o'; i++ {
			}
			S = (S + dp[i-j]*p2[w-i+j]) % P
		}
		return
	}
	S := int64(0)
	for _, r := range a {
		S += f(r)
	}
	for j := range a[0] {
		s := []byte{}
		for _, r := range a {
			s = append(s, r[j])
		}
		S += f(s)
	}
	Print(S % P)
}

package main

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

// github.com/EndlessCheng/codeforces-go
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	const mod = 998244353
	const mx int = 3e5
	dp := [mx + 1]int64{0, 0, 1}
	for i := 3; i <= mx; i++ {
		dp[i] = ((3*dp[i-1]-4*dp[i-3])%mod + mod) % mod
	}
	p2 := [mx + 1]int64{1}
	for i := 1; i <= mx; i++ {
		p2[i] = p2[i-1] << 1 % mod
	}

	var n, m, cw int
	Fscan(in, &n, &m)
	a := make([][]byte, n)
	for i := range a {
		Fscan(in, &a[i])
		cw += bytes.Count(a[i], []byte{'o'})
	}
	f := func(s []byte) (res int64) {
		for i, n := 0, len(s); i < n; {
			if s[i] == '*' {
				i++
				continue
			}
			st := i
			for ; i < n && s[i] == 'o'; i++ {
			}
			res = (res + dp[i-st]*p2[cw-(i-st)]) % mod
		}
		return
	}
	ans := int64(0)
	for _, s := range a {
		ans += f(s)
	}
	for j := range a[0] {
		s := make([]byte, 0, n)
		for _, r := range a {
			s = append(s, r[j])
		}
		ans += f(s)
	}
	Fprint(out, ans%mod)
}

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

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	const P, M = 998244353, 3e5 + 1
	f := [M]int64{0, 0, 1}
	for i := 3; i < M; i++ {
		f[i] = ((3*f[i-1]-4*f[i-3])%P+P)%P
	}
	p := [M]int64{1}
	for i := 1; i < M; i++ {
		p[i] = p[i-1]*2%P
	}
	var n, m, w int
	Fscan(in, &n, &m)
	a := make([]string, n)
	for i := range a {
		Fscan(in, &a[i])
		w += Count(a[i], "o")
	}
	c := func(s string) (S int64) {
		for _, s := range Split(s, "*") {
			S = (S + f[len(s)]*p[w-len(s)]) % P
		}
		return
	}
	S := int64(0)
	for _, r := range a {
		S += c(r)
	}
	for j := range a[0] {
		s := []byte{}
		for _, r := range a {
			s = append(s, r[j])
		}
		S += c(string(s))
	}
	Print(S % P)
}

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	const P, M = 998244353, 3e5+1
	f := [M]int64{0, 0, 1}
	for i := 3; i < M; i++ {
		f[i] = 3*f[i-1]-4*f[i-3]
	}
	p := [M]int64{1}
	for i := 1; i < M; i++ {
		p[i] = p[i-1]*2%P
	}
	var n, m, w int
	Fscan(in, &n, &m)
	a := make([]string, n)
	for i := range a {
		Fscan(in, &a[i])
		w += Count(a[i], "o")
	}
	c := func(s string) (S int64) {
		for _, s := range Split(s, "*") {
			S = (S+f[len(s)]*p[w-len(s)])%P
		}
		return
	}
	S := int64(0)
	for _, r := range a {
		S += c(r)
	}
	for j := range a[0] {
		s := []byte{}
		for _, r := range a {
			s = append(s, r[j])
		}
		S += c(string(s))
	}
	Print((S%P+P)%P)
}