After a hard day Vitaly got very hungry and he wants to eat his favorite potato pie. But it's not that simple. Vitaly is in the first room of the house with n room located in a line and numbered starting from one from left to right. You can go from the first room to the second room, from the second room to the third room and so on — you can go from the (n - 1)-th room to the n-th room. Thus, you can go to room x only from room x - 1.The potato pie is located in the n-th room and Vitaly needs to go there. Each pair of consecutive rooms has a door between them. In order to go to room x from room x - 1, you need to open the door between the rooms with the corresponding key. In total the house has several types of doors (represented by uppercase Latin letters) and several types of keys (represented by lowercase Latin letters). The key of type t can open the door of type T if and only if t and T are the same letter, written in different cases. For example, key f can open door F.Each of the first n - 1 rooms contains exactly one key of some type that Vitaly can use to get to next rooms. Once the door is open with some key, Vitaly won't get the key from the keyhole but he will immediately run into the next room. In other words, each key can open no more than one door.Vitaly realizes that he may end up in some room without the key that opens the door to the next room. Before the start his run for the potato pie Vitaly can buy any number of keys of any type that is guaranteed to get to room n.Given the plan of the house, Vitaly wants to know what is the minimum number of keys he needs to buy to surely get to the room n, which has a delicious potato pie. Write a program that will help Vitaly find out this number.

The first line of the input contains a positive integer n (2 ≤ n ≤ 105) — the number of rooms in the house. The second line of the input contains string s of length 2·n - 2. Let's number the elements of the string from left to right, starting from one.  The odd positions in the given string s contain lowercase Latin letters — the types of the keys that lie in the corresponding rooms. Thus, each odd position i of the given string s contains a lowercase Latin letter — the type of the key that lies in room number (i + 1) / 2. The even positions in the given string contain uppercase Latin letters — the types of doors between the rooms. Thus, each even position i of the given string s contains an uppercase letter — the type of the door that leads from room i / 2 to room i / 2 + 1.

Print the only integer — the minimum number of keys that Vitaly needs to buy to surely get from room one to room n.

package main

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

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

const ALPHAB int = 26

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

	var (
		n, key int
		s string
	)

	alph := make([]int, ALPHAB)

	scan(&n, &s)

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

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

	println(key)
}

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

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

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

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

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

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

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

package main

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

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

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

package main

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

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

	reader := bufio.NewReader(os.Stdin)
	re := regexp.MustCompile(`\r?\n`)
	if s, err := reader.ReadString('\n'); err != nil {
		if err != io.EOF {
			return
		}
	} else {
		s = re.ReplaceAllString(s, "")

		keys := map[byte]int{}
		buy_key := 0
		for i := 0; i < n-1; i++ {
			key := s[i*2]
			lock := s[i*2+1] + ('a' - 'A')
			if key == lock {
				continue
			} else {
				if _, exist := keys[lock]; exist && keys[lock] > 0 {
					keys[lock] = keys[lock] - 1
				} else {
					buy_key++
				}
				if _, exist := keys[key]; exist {
					keys[key] = keys[key] + 1
				} else {
					keys[key] = 1
				}
			}
		}

		fmt.Println(buy_key)
	}
}

package main

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

func main() {
	s := bufio.NewScanner(os.Stdin)
	s.Buffer([]byte(nil), 1e6)
	s.Split(bufio.ScanWords)
	_ = nextInt(s)
	s.Scan()
	str := s.Text()

	keys := make([]int, 26)
	doors := make([]int, 26)

	for i := 0; i < len(str); i++ {
		if isUpper(str[i]) && keys[toLower(str[i])-'a'] > 0 {
			keys[toLower(str[i])-'a']--
		} else if isUpper(str[i]) {
			doors[str[i]-'A']++
		} else {
			keys[str[i]-'a']++
		}
	}
	c := 0
	for i := 0; i < 26; i++ {
		if doors[i] > 0 {
			c += doors[i]
		}
	}
	fmt.Println(c)
}

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

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

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

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

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

package main

import (
	"fmt"
)

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

	var keys [26]int
	rooms := ""
	fmt.Scan(&rooms)

	ans := 0
	for i := 0; i < len(rooms); i += 2 {
		keys[rooms[i]-'a']++
		if keys[rooms[i+1]-'A'] == 0 {
			ans++
		} else {
			keys[rooms[i+1]-'A']--
		}
	}

	fmt.Println(ans)
}

package main

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

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

	reader := bufio.NewReader(os.Stdin)
	re := regexp.MustCompile(`\r?\n`)
	if s, err := reader.ReadString('\n'); err != nil {
		if err != io.EOF {
			return
		}
	} else {
		s = re.ReplaceAllString(s, "")

		keys := map[byte]int{}
		buy_key := 0
		for i := 0; i < n-1; i++ {
			key := s[i*2]
			lock := s[i*2+1] + ('a' - 'A')
			if key == lock {
				continue
			} else {
				if _, exist := keys[lock]; exist {
					keys[lock] = keys[lock] - 1
				} else {
					buy_key++
				}
				if _, exist := keys[key]; exist {
					keys[key] = keys[key] + 1
				} else {
					keys[key] = 1
				}
			}
		}

		fmt.Println(buy_key)
	}
}

package main

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

func main() {
	s := bufio.NewScanner(os.Stdin)
	s.Split(bufio.ScanWords)
	_ = nextInt(s)
	s.Scan()
	str := s.Text()

	keys := make([]int, 26)
	doors := make([]int, 26)

	for i := 0; i < len(str); i++ {
		if isUpper(str[i]) && keys[toLower(str[i])-'a'] > 0 {
			keys[toLower(str[i])-'a']--
		} else if isUpper(str[i]) {
			doors[str[i]-'A']++
		} else {
			keys[str[i]-'a']++
		}
	}
	c := 0
	for i := 0; i < 26; i++ {
		if doors[i] > 0 {
			c += doors[i]
		}
	}
	fmt.Println(c)
}

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

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

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

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

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


You are given a 0-1 rectangular matrix. What is the number of squares in it? A square is a solid square frame (border) with linewidth equal to 1. A square should be at least 2 × 2. We are only interested in two types of squares:   squares with each side parallel to a side of the matrix;  squares with each side parallel to a diagonal of the matrix. For example the following matrix contains only one square of the first type: 0000000 0111100 0100100 0100100 0111100The following matrix contains only one square of the second type:00000000010000010100000100000000000Regardless of type, a square must contain at least one 1 and can't touch (by side or corner) any foreign 1. Of course, the lengths of the sides of each square should be equal.How many squares are in the given matrix?

The first line contains integer t (1 ≤ t ≤ 10000), where t is the number of test cases in the input. Then test cases follow. Each case starts with a line containing integers n and m (2 ≤ n, m ≤ 250), where n is the number of rows and m is the number of columns. The following n lines contain m characters each (0 or 1). The total number of characters in all test cases doesn't exceed 106 for any input file.

You should output exactly t lines, with the answer to the i-th test case on the i-th line.

package main

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

type Group struct {
	maxCol, minCol                 int
	maxRow, minRow                 int
	maxRowMinusCol, minRowMinusCol int
	maxRowPlusCol, minRowPlusCol   int
	parallel, diagnal              bool
	area                           int
}

var n, m int
var matrix [255][255]int

func floodfill(g *Group, index, row, col int) {
	for drow := -1; drow <= 1; drow++ {
		for dcol := -1; dcol <= 1; dcol++ {
			r, c := row+drow, col+dcol
			if matrix[r][c] == 0 {
				g.area++
				matrix[r][c] = index
				if r > g.maxRow {
					g.maxRow = r
				}
				if r < g.minRow {
					g.minRow = r
				}
				if c > g.maxCol {
					g.maxCol = c
				}
				if c < g.minCol {
					g.minCol = c
				}
				if r+c > g.maxRowPlusCol {
					g.maxRowPlusCol = r + c
				}
				if r+c < g.minRowPlusCol {
					g.minRowPlusCol = r + c
				}
				if r-c > g.maxRowMinusCol {
					g.maxRowMinusCol = r - c
				}
				if r-c < g.minRowMinusCol {
					g.minRowMinusCol = r - c
				}
				floodfill(g, index, r, c)
			}
		}
	}
}

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

	for i := 0; i < t; i++ {
		fmt.Fscanf(reader, "%d %d\n", &n, &m)
		for j := 0; j < m+2; j++ {
			matrix[0][j] = -1
			matrix[n+1][j] = -1
		}
		for j := 0; j < n+2; j++ {
			matrix[j][0] = -1
			matrix[j][m+1] = -1
		}
		for j := 0; j < n; j++ {
			var line string
			fmt.Fscanf(reader, "%s\n", &line)
			for k := 0; k < m; k++ {
				if line[k] == '0' {
					matrix[j+1][k+1] = -1
				} else {
					matrix[j+1][k+1] = 0
				}
			}
		}
		groups := []*Group{}
		for r := 1; r <= n; r++ {
			for c := 1; c <= m; c++ {
				if matrix[r][c] == 0 {
					g := &Group{c, c, r, r, r - c, r - c, r + c, r + c, true, true, 1}
					groups = append(groups, g)
					matrix[r][c] = len(groups)
					floodfill(g, len(groups), r, c)
				}
			}
		}
		for r := 1; r <= n; r++ {
			for c := 1; c <= m; c++ {
				if matrix[r][c] > 0 {
					g := groups[matrix[r][c]-1]
					if r != g.maxRow && r != g.minRow &&
						c != g.maxCol && c != g.minCol {
						g.parallel = false
					}
					if r-c != g.maxRowMinusCol && r-c != g.minRowMinusCol &&
						r+c != g.maxRowPlusCol && r+c != g.minRowPlusCol {
						g.diagnal = false
					}
				}
			}
		}
		ans := 0
		for j := 0; j < len(groups); j++ {
			g := groups[j]
			if g.parallel && g.maxRow-g.minRow == g.maxCol-g.minCol && g.area > 1 &&
				g.area == 4*(g.maxRow-g.minRow) {
				ans++
				//	fmt.Printf("parallel: %+v\n", g)
			} else if g.diagnal && g.maxRowMinusCol-g.minRowMinusCol == g.maxRowPlusCol-g.minRowPlusCol &&
				(g.maxRowMinusCol-g.minRowMinusCol)%2 == 0 &&
				g.area == 2*(g.maxRowMinusCol-g.minRowMinusCol) && g.area > 1 {
				ans++
				//	fmt.Printf("diagnal: %+v\n", g)
			}
		}
		fmt.Printf("%d\n", ans)
	}
}

package main

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

type Group struct {
	maxCol, minCol                 int
	maxRow, minRow                 int
	maxRowMinusCol, minRowMinusCol int
	maxRowPlusCol, minRowPlusCol   int
	parallelArea, diagnalArea      int
}

var n, m int
var matrix [255][255]int

func floodfill(g *Group, index, row, col int) {
	for drow := -1; drow <= 1; drow++ {
		for dcol := -1; dcol <= 1; dcol++ {
			r, c := row+drow, col+dcol
			if matrix[r][c] == 0 {
				matrix[r][c] = index
				if r > g.maxRow {
					g.maxRow = r
				}
				if r < g.minRow {
					g.minRow = r
				}
				if c > g.maxCol {
					g.maxCol = c
				}
				if c < g.minCol {
					g.minCol = c
				}
				if r+c > g.maxRowPlusCol {
					g.maxRowPlusCol = r + c
				}
				if r+c < g.minRowPlusCol {
					g.minRowPlusCol = r + c
				}
				if r-c > g.maxRowMinusCol {
					g.maxRowMinusCol = r - c
				}
				if r-c < g.minRowMinusCol {
					g.minRowMinusCol = r - c
				}
				floodfill(g, index, r, c)
			}
		}
	}
}

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

	for i := 0; i < t; i++ {
		fmt.Fscanf(reader, "%d %d\n", &n, &m)
		for j := 0; j < m+2; j++ {
			matrix[0][j] = -1
			matrix[n+1][j] = -1
		}
		for j := 0; j < n+2; j++ {
			matrix[j][0] = -1
			matrix[j][m+1] = -1
		}
		for j := 0; j < n; j++ {
			var line string
			fmt.Fscanf(reader, "%s\n", &line)
			for k := 0; k < m; k++ {
				if line[k] == '0' {
					matrix[j+1][k+1] = -1
				} else {
					matrix[j+1][k+1] = 0
				}
			}
		}
		groups := []*Group{}
		for r := 1; r <= n; r++ {
			for c := 1; c <= m; c++ {
				if matrix[r][c] == 0 {
					g := &Group{c, c, r, r, r - c, r - c, r + c, r + c, 0, 0}
					groups = append(groups, g)
					matrix[r][c] = len(groups)
					floodfill(g, len(groups), r, c)
				}
			}
		}
		for r := 1; r <= n; r++ {
			for c := 1; c <= m; c++ {
				if matrix[r][c] > 0 {
					g := groups[matrix[r][c]-1]
					if r == g.maxRow || r == g.minRow ||
						c == g.maxCol || c == g.minCol {
						g.parallelArea++
					}
					if r-c == g.maxRowMinusCol || r-c == g.minRowMinusCol ||
						r+c == g.maxRowPlusCol || r+c == g.minRowPlusCol {
						g.diagnalArea++
					}
				}
			}
		}
		ans := 0
		for j := 0; j < len(groups); j++ {
			g := groups[j]
			if g.maxRow-g.minRow == g.maxCol-g.minCol && g.parallelArea > 1 &&
				g.parallelArea == 4*(g.maxRow-g.minRow) {
				ans++
				//				fmt.Printf("parallel: %+v\n", g)
			} else if g.maxRowMinusCol-g.minRowMinusCol == g.maxRowPlusCol-g.minRowPlusCol &&
				g.diagnalArea == 2*(g.maxRowMinusCol-g.minRowMinusCol) && g.diagnalArea > 1 {
				ans++
				//				fmt.Printf("diagnal: %+v\n", g)
			}
		}
		fmt.Printf("%d\n", ans)
	}
}

package main

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

type Group struct {
	maxCol, minCol                 int
	maxRow, minRow                 int
	maxRowMinusCol, minRowMinusCol int
	maxRowPlusCol, minRowPlusCol   int
	parallel, diagnal              bool
	area                           int
}

var n, m int
var matrix [255][255]int

func floodfill(g *Group, index, row, col int) {
	for drow := -1; drow <= 1; drow++ {
		for dcol := -1; dcol <= 1; dcol++ {
			r, c := row+drow, col+dcol
			if matrix[r][c] == 0 {
				g.area++
				matrix[r][c] = index
				if r > g.maxRow {
					g.maxRow = r
				}
				if r < g.minRow {
					g.minRow = r
				}
				if c > g.maxCol {
					g.maxCol = c
				}
				if c < g.minCol {
					g.minCol = c
				}
				if r+c > g.maxRowPlusCol {
					g.maxRowPlusCol = r + c
				}
				if r+c < g.minRowPlusCol {
					g.minRowPlusCol = r + c
				}
				if r-c > g.maxRowMinusCol {
					g.maxRowMinusCol = r - c
				}
				if r-c < g.minRowMinusCol {
					g.minRowMinusCol = r - c
				}
				floodfill(g, index, r, c)
			}
		}
	}
}

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

	for i := 0; i < t; i++ {
		fmt.Fscanf(reader, "%d %d\n", &n, &m)
		for j := 0; j < m+2; j++ {
			matrix[0][j] = -1
			matrix[n+1][j] = -1
		}
		for j := 0; j < n+2; j++ {
			matrix[j][0] = -1
			matrix[j][m+1] = -1
		}
		for j := 0; j < n; j++ {
			var line string
			fmt.Fscanf(reader, "%s\n", &line)
			for k := 0; k < m; k++ {
				if line[k] == '0' {
					matrix[j+1][k+1] = -1
				} else {
					matrix[j+1][k+1] = 0
				}
			}
		}
		groups := []*Group{}
		for r := 1; r <= n; r++ {
			for c := 1; c <= m; c++ {
				if matrix[r][c] == 0 {
					g := &Group{c, c, r, r, r - c, r - c, r + c, r + c, true, true, 1}
					groups = append(groups, g)
					matrix[r][c] = len(groups)
					floodfill(g, len(groups), r, c)
				}
			}
		}
		for r := 1; r <= n; r++ {
			for c := 1; c <= m; c++ {
				if matrix[r][c] > 0 {
					g := groups[matrix[r][c]-1]
					if r != g.maxRow && r != g.minRow &&
						c != g.maxCol && c != g.minCol {
						g.parallel = false
					}
					if r-c != g.maxRowMinusCol && r-c != g.minRowMinusCol &&
						r+c != g.maxRowPlusCol && r+c != g.minRowPlusCol {
						g.parallel = false
					}
				}
			}
		}
		ans := 0
		for j := 0; j < len(groups); j++ {
			g := groups[j]
			if g.maxRow-g.minRow == g.maxCol-g.minCol && g.area > 1 &&
				g.area == 4*(g.maxRow-g.minRow) {
				ans++
				//	fmt.Printf("parallel: %+v\n", g)
			} else if g.maxRowMinusCol-g.minRowMinusCol == g.maxRowPlusCol-g.minRowPlusCol &&
				(g.maxRowMinusCol-g.minRowMinusCol)%2 == 0 &&
				g.area == 2*(g.maxRowMinusCol-g.minRowMinusCol) && g.area > 1 {
				ans++
				//				fmt.Printf("diagnal: %+v\n", g)
			}
		}
		fmt.Printf("%d\n", ans)
	}
}

package main

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

type Group struct {
	maxCol, minCol                 int
	maxRow, minRow                 int
	maxRowMinusCol, minRowMinusCol int
	maxRowPlusCol, minRowPlusCol   int
	parallelArea, diagnalArea      int
}

var n, m int
var matrix [255][255]int

func floodfill(g *Group, index, row, col int) {
	for drow := -1; drow <= 1; drow++ {
		for dcol := -1; dcol <= 1; dcol++ {
			r, c := row+drow, col+dcol
			if matrix[r][c] == 0 {
				matrix[r][c] = index
				if r > g.maxRow {
					g.maxRow = r
				}
				if r < g.minRow {
					g.minRow = r
				}
				if c > g.maxCol {
					g.maxCol = c
				}
				if c < g.minCol {
					g.minCol = c
				}
				if r+c > g.maxRowPlusCol {
					g.maxRowPlusCol = r + c
				}
				if r+c < g.minRowPlusCol {
					g.minRowPlusCol = r + c
				}
				if r-c > g.maxRowMinusCol {
					g.maxRowMinusCol = r - c
				}
				if r-c < g.minRowMinusCol {
					g.minRowMinusCol = r - c
				}
				floodfill(g, index, r, c)
			}
		}
	}
}

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

	for i := 0; i < t; i++ {
		fmt.Fscanf(reader, "%d %d\n", &n, &m)
		for j := 0; j < m+2; j++ {
			matrix[0][j] = -1
			matrix[n+1][j] = -1
		}
		for j := 0; j < n+2; j++ {
			matrix[j][0] = -1
			matrix[j][m+1] = -1
		}
		for j := 0; j < n; j++ {
			var line string
			fmt.Fscanf(reader, "%s\n", &line)
			for k := 0; k < m; k++ {
				if line[k] == '0' {
					matrix[j+1][k+1] = -1
				} else {
					matrix[j+1][k+1] = 0
				}
			}
		}
		groups := []*Group{}
		for r := 1; r <= n; r++ {
			for c := 1; c <= m; c++ {
				if matrix[r][c] == 0 {
					g := &Group{c, c, r, r, r - c, r - c, r + c, r + c, 0, 0}
					groups = append(groups, g)
					matrix[r][c] = len(groups)
					floodfill(g, len(groups), r, c)
				}
			}
		}
		for r := 1; r <= n; r++ {
			for c := 1; c <= m; c++ {
				if matrix[r][c] > 0 {
					g := groups[matrix[r][c]-1]
					if r == g.maxRow || r == g.minRow ||
						c == g.maxCol || c == g.minCol {
						g.parallelArea++
					}
					if r-c == g.maxRowMinusCol || r-c == g.minRowMinusCol ||
						r+c == g.maxRowPlusCol || r+c == g.minRowPlusCol {
						g.diagnalArea++
					}
				}
			}
		}
		ans := 0
		for j := 0; j < len(groups); j++ {
			g := groups[j]
			if g.maxRow-g.minRow == g.maxCol-g.minCol && g.parallelArea > 1 &&
				g.parallelArea == 4*(g.maxRow-g.minRow) {
				ans++
				fmt.Printf("parallel: %+v\n", g)
			} else if g.maxRowMinusCol-g.minRowMinusCol == g.maxRowPlusCol-g.minRowPlusCol &&
				(g.maxRowMinusCol-g.minRowMinusCol)%2 == 0 &&
				g.diagnalArea == 2*(g.maxRowMinusCol-g.minRowMinusCol) && g.diagnalArea > 1 {
				ans++
				fmt.Printf("diagnal: %+v\n", g)
			}
		}
		fmt.Printf("%d\n", ans)
	}
}

package main

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

type Group struct {
	maxCol, minCol                 int
	maxRow, minRow                 int
	maxRowMinusCol, minRowMinusCol int
	maxRowPlusCol, minRowPlusCol   int
	area                           int
	parallel, diagonal             bool
}

var n, m int
var matrix [255][255]int

func floodfill(g *Group, index, row, col int) {
	for drow := -1; drow <= 1; drow++ {
		for dcol := -1; dcol <= 1; dcol++ {
			r, c := row+drow, col+dcol
			if matrix[r][c] == 0 {
				matrix[r][c] = index
				g.area++
				if r > g.maxRow {
					g.maxRow = r
				}
				if r < g.minRow {
					g.minRow = r
				}
				if c > g.maxCol {
					g.maxCol = c
				}
				if c < g.minCol {
					g.minCol = c
				}
				if r+c > g.maxRowPlusCol {
					g.maxRowPlusCol = r + c
				}
				if r+c < g.minRowPlusCol {
					g.minRowPlusCol = r + c
				}
				if r-c > g.maxRowMinusCol {
					g.maxRowMinusCol = r - c
				}
				if r-c < g.minRowMinusCol {
					g.minRowMinusCol = r - c
				}
				floodfill(g, index, r, c)
			}
		}
	}
}

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

	for i := 0; i < t; i++ {
		fmt.Fscanf(reader, "%d %d\n", &n, &m)
		for j := 0; j < m+2; j++ {
			matrix[0][j] = -1
			matrix[n+1][j] = -1
		}
		for j := 0; j < n+2; j++ {
			matrix[j][0] = -1
			matrix[j][m+1] = -1
		}
		for j := 0; j < n; j++ {
			var line string
			fmt.Fscanf(reader, "%s\n", &line)
			for k := 0; k < m; k++ {
				if line[k] == '0' {
					matrix[j+1][k+1] = -1
				} else {
					matrix[j+1][k+1] = 0
				}
			}
		}
		groups := []*Group{}
		for r := 1; r <= n; r++ {
			for c := 1; c <= m; c++ {
				if matrix[r][c] == 0 {
					g := &Group{c, c, r, r, r - c, r - c, r + c, r + c, 1, true, true}
					groups = append(groups, g)
					matrix[r][c] = len(groups)
					floodfill(g, len(groups), r, c)
				}
			}
		}
		for r := 1; r <= n; r++ {
			for c := 1; c <= m; c++ {
				if matrix[r][c] > 0 {
					g := groups[matrix[r][c]-1]
					if r != g.maxRow && r != g.minRow &&
						c != g.maxCol && c != g.minCol {
						g.parallel = false
					}
					if r-c != g.maxRowMinusCol && r-c != g.minRowMinusCol &&
						r+c != g.maxRowPlusCol && r+c != g.minRowPlusCol {
						g.diagonal = false
					}
				}
			}
		}
		ans := 0
		for j := 0; j < len(groups); j++ {
			g := groups[j]
			if g.area == 1 {
				continue
			} else if g.parallel && g.maxRow-g.minRow == g.maxCol-g.minCol &&
				g.area == 4*(g.maxRow-g.minRow) {
				ans++
			} else if g.diagonal && g.maxRowMinusCol-g.minRowMinusCol == g.maxRowPlusCol-g.minRowPlusCol &&
				g.area == 2*(g.maxRowMinusCol-g.minRowMinusCol) {
				ans++
			}
		}
		fmt.Printf("%d\n", ans)
	}
}

package main

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

type Group struct {
	maxCol, minCol                 int
	maxRow, minRow                 int
	maxRowMinusCol, minRowMinusCol int
	maxRowPlusCol, minRowPlusCol   int
	area                           int
	parallel, diagonal             bool
}

var n, m int
var matrix [255][255]int

func floodfill(g *Group, index, row, col int) {
	for drow := -1; drow <= 1; drow++ {
		for dcol := -1; dcol <= 1; dcol++ {
			r, c := row+drow, col+dcol
			if matrix[r][c] == 0 {
				matrix[r][c] = index
				g.area++
				if r > g.maxRow {
					g.maxRow = r
				}
				if r < g.minRow {
					g.minRow = r
				}
				if c > g.maxCol {
					g.maxCol = c
				}
				if c < g.minCol {
					g.minCol = c
				}
				if r+c > g.maxRowPlusCol {
					g.maxRowPlusCol = r + c
				}
				if r+c < g.minRowPlusCol {
					g.minRowPlusCol = r + c
				}
				if r-c > g.maxRowMinusCol {
					g.maxRowMinusCol = r - c
				}
				if r-c < g.minRowMinusCol {
					g.minRowMinusCol = r - c
				}
				floodfill(g, index, r, c)
			}
		}
	}
}

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

	for i := 0; i < t; i++ {
		fmt.Fscanf(reader, "%d %d\n", &n, &m)
		for j := 0; j < m+2; j++ {
			matrix[0][j] = -1
			matrix[n+1][j] = -1
		}
		for j := 0; j < n+2; j++ {
			matrix[j][0] = -1
			matrix[j][m+1] = -1
		}
		for j := 0; j < n; j++ {
			var line string
			fmt.Fscanf(reader, "%s\n", &line)
			for k := 0; k < m; k++ {
				if line[k] == '0' {
					matrix[j+1][k+1] = -1
				} else {
					matrix[j+1][k+1] = 0
				}
			}
		}
		groups := []*Group{}
		for r := 1; r <= n; r++ {
			for c := 1; c <= m; c++ {
				if matrix[r][c] == 0 {
					g := &Group{r, r, c, c, r - c, r - c, r + c, r + c, 1, true, true}
					groups = append(groups, g)
					matrix[r][c] = len(groups)
					floodfill(g, len(groups), r, c)
				}
			}
		}
		for r := 1; r <= n; r++ {
			for c := 1; c <= m; c++ {
				if matrix[r][c] > 0 {
					g := groups[matrix[r][c]-1]
					if r != g.maxRow && r != g.minRow &&
						c != g.maxCol && c != g.minCol {
						g.parallel = false
					}
					if r-c != g.maxRowMinusCol && r-c != g.minRowMinusCol &&
						r+c != g.maxRowPlusCol && r+c != g.minRowPlusCol {
						g.diagonal = false
					}
				}
			}
		}
		ans := 0
		for j := 0; j < len(groups); j++ {
			g := groups[j]
			if g.area == 1 {
				continue
			} else if g.parallel && g.maxRow-g.minRow == g.maxCol-g.minCol &&
				g.area == 4*(g.maxRow-g.minRow) {
				ans++
			} else if g.diagonal && g.maxRowMinusCol-g.minRowMinusCol == g.maxRowPlusCol-g.minRowPlusCol &&
				g.area == 2*(g.maxRowMinusCol-g.minRowMinusCol) {
				ans++
			}
		}
		fmt.Printf("%d\n", ans)
	}
}

package main

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

type Group struct {
	maxCol, minCol                 int
	maxRow, minRow                 int
	maxRowMinusCol, minRowMinusCol int
	maxRowPlusCol, minRowPlusCol   int
	parallelArea, diagnalArea      int
}

var n, m int
var matrix [255][255]int

func floodfill(g *Group, index, row, col int) {
	for drow := -1; drow <= 1; drow++ {
		for dcol := -1; dcol <= 1; dcol++ {
			r, c := row+drow, col+dcol
			if matrix[r][c] == 0 {
				matrix[r][c] = index
				if r > g.maxRow {
					g.maxRow = r
				}
				if r < g.minRow {
					g.minRow = r
				}
				if c > g.maxCol {
					g.maxCol = c
				}
				if c < g.minCol {
					g.minCol = c
				}
				if r+c > g.maxRowPlusCol {
					g.maxRowPlusCol = r + c
				}
				if r+c < g.minRowPlusCol {
					g.minRowPlusCol = r + c
				}
				if r-c > g.maxRowMinusCol {
					g.maxRowMinusCol = r - c
				}
				if r-c < g.minRowMinusCol {
					g.minRowMinusCol = r - c
				}
				floodfill(g, index, r, c)
			}
		}
	}
}

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

	for i := 0; i < t; i++ {
		fmt.Fscanf(reader, "%d %d\n", &n, &m)
		for j := 0; j < m+2; j++ {
			matrix[0][j] = -1
			matrix[n+1][j] = -1
		}
		for j := 0; j < n+2; j++ {
			matrix[j][0] = -1
			matrix[j][m+1] = -1
		}
		for j := 0; j < n; j++ {
			var line string
			fmt.Fscanf(reader, "%s\n", &line)
			if t == 50 && i == 31 {
				fmt.Printf("%s\n", line)
			}
			for k := 0; k < m; k++ {
				if line[k] == '0' {
					matrix[j+1][k+1] = -1
				} else {
					matrix[j+1][k+1] = 0
				}
			}
		}
		groups := []*Group{}
		for r := 1; r <= n; r++ {
			for c := 1; c <= m; c++ {
				if matrix[r][c] == 0 {
					g := &Group{c, c, r, r, r - c, r - c, r + c, r + c, 0, 0}
					groups = append(groups, g)
					matrix[r][c] = len(groups)
					floodfill(g, len(groups), r, c)
				}
			}
		}
		for r := 1; r <= n; r++ {
			for c := 1; c <= m; c++ {
				if matrix[r][c] > 0 {
					g := groups[matrix[r][c]-1]
					if r == g.maxRow || r == g.minRow ||
						c == g.maxCol || c == g.minCol {
						g.parallelArea++
					}
					if r-c == g.maxRowMinusCol || r-c == g.minRowMinusCol ||
						r+c == g.maxRowPlusCol || r+c == g.minRowPlusCol {
						g.diagnalArea++
					}
				}
			}
		}
		ans := 0
		for j := 0; j < len(groups); j++ {
			g := groups[j]
			if g.maxRow-g.minRow == g.maxCol-g.minCol && g.parallelArea > 1 &&
				g.parallelArea == 4*(g.maxRow-g.minRow) {
				ans++
				//				fmt.Printf("parallel: %+v\n", g)
			} else if g.maxRowMinusCol-g.minRowMinusCol == g.maxRowPlusCol-g.minRowPlusCol &&
				g.diagnalArea == 2*(g.maxRowMinusCol-g.minRowMinusCol) && g.diagnalArea > 1 {
				ans++
				//				fmt.Printf("diagnal: %+v\n", g)
			}
		}
		if t != 50 {
			fmt.Printf("%d\n", ans)
		}
	}
}

package main

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

type Group struct {
	maxCol, minCol                 int
	maxRow, minRow                 int
	maxRowMinusCol, minRowMinusCol int
	maxRowPlusCol, minRowPlusCol   int
	parallelArea, diagnalArea      int
}

var n, m int
var matrix [255][255]int

func floodfill(g *Group, index, row, col int) {
	for drow := -1; drow <= 1; drow++ {
		for dcol := -1; dcol <= 1; dcol++ {
			r, c := row+drow, col+dcol
			if matrix[r][c] == 0 {
				matrix[r][c] = index
				if r > g.maxRow {
					g.maxRow = r
				}
				if r < g.minRow {
					g.minRow = r
				}
				if c > g.maxCol {
					g.maxCol = c
				}
				if c < g.minCol {
					g.minCol = c
				}
				if r+c > g.maxRowPlusCol {
					g.maxRowPlusCol = r + c
				}
				if r+c < g.minRowPlusCol {
					g.minRowPlusCol = r + c
				}
				if r-c > g.maxRowMinusCol {
					g.maxRowMinusCol = r - c
				}
				if r-c < g.minRowMinusCol {
					g.minRowMinusCol = r - c
				}
				floodfill(g, index, r, c)
			}
		}
	}
}

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

	for i := 0; i < t; i++ {
		fmt.Fscanf(reader, "%d %d\n", &n, &m)
		for j := 0; j < m+2; j++ {
			matrix[0][j] = -1
			matrix[n+1][j] = -1
		}
		for j := 0; j < n+2; j++ {
			matrix[j][0] = -1
			matrix[j][m+1] = -1
		}
		for j := 0; j < n; j++ {
			var line string
			fmt.Fscanf(reader, "%s\n", &line)
			for k := 0; k < m; k++ {
				if line[k] == '0' {
					matrix[j+1][k+1] = -1
				} else {
					matrix[j+1][k+1] = 0
				}
			}
		}
		groups := []*Group{}
		for r := 1; r <= n; r++ {
			for c := 1; c <= m; c++ {
				if matrix[r][c] == 0 {
					g := &Group{c, c, r, r, r - c, r - c, r + c, r + c, 0, 0}
					groups = append(groups, g)
					matrix[r][c] = len(groups)
					floodfill(g, len(groups), r, c)
				}
			}
		}
		for r := 1; r <= n; r++ {
			for c := 1; c <= m; c++ {
				if matrix[r][c] > 0 {
					g := groups[matrix[r][c]-1]
					if r == g.maxRow || r == g.minRow ||
						c == g.maxCol || c == g.minCol {
						g.parallelArea++
					}
					if r-c == g.maxRowMinusCol || r-c == g.minRowMinusCol ||
						r+c == g.maxRowPlusCol || r+c == g.minRowPlusCol {
						g.diagnalArea++
					}
				}
			}
		}
		ans := 0
		for j := 0; j < len(groups); j++ {
			g := groups[j]
			if g.maxRow-g.minRow == g.maxCol-g.minCol && g.parallelArea > 1 &&
				g.parallelArea == 4*(g.maxRow-g.minRow) {
				ans++
				//	fmt.Printf("parallel: %+v\n", g)
			} else if g.maxRowMinusCol-g.minRowMinusCol == g.maxRowPlusCol-g.minRowPlusCol &&
				(g.maxRowMinusCol-g.minRowMinusCol)%2 == 0 &&
				g.diagnalArea == 2*(g.maxRowMinusCol-g.minRowMinusCol) && g.diagnalArea > 1 {
				ans++
				//				fmt.Printf("diagnal: %+v\n", g)
			}
		}
		fmt.Printf("%d\n", ans)
	}
}


There are n phone numbers in Polycarp's contacts on his phone. Each number is a 9-digit integer, starting with a digit different from 0. All the numbers are distinct.There is the latest version of Berdroid OS installed on Polycarp's phone. If some number is entered, is shows up all the numbers in the contacts for which there is a substring equal to the entered sequence of digits. For example, is there are three phone numbers in Polycarp's contacts: 123456789, 100000000 and 100123456, then:  if he enters 00 two numbers will show up: 100000000 and 100123456,  if he enters 123 two numbers will show up 123456789 and 100123456,  if he enters 01 there will be only one number 100123456. For each of the phone numbers in Polycarp's contacts, find the minimum in length sequence of digits such that if Polycarp enters this sequence, Berdroid shows this only phone number.

The first line contains single integer n (1 ≤ n ≤ 70000) — the total number of phone contacts in Polycarp's contacts. The phone numbers follow, one in each line. Each number is a positive 9-digit integer starting with a digit from 1 to 9. All the numbers are distinct.

Print exactly n lines: the i-th of them should contain the shortest non-empty sequence of digits, such that if Polycarp enters it, the Berdroid OS shows up only the i-th number from the contacts. If there are several such sequences, print any of them.

package main

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

var in = bufio.NewScanner(os.Stdin)

func readInt() int {
	if !in.Scan() {
		panic(in.Err())
	}
	res := 0
	for _, b := range in.Bytes() {
		res = res*10 + int(b-'0')
	}
	return res
}

func format(str, le int) string {
	b := make([]byte, le)
	for i := le - 1; i >= 0; i, str = i-1, str/10 {
		b[i] = '0' + byte(str%10)
	}
	return string(b)
}

func main() {
	in.Split(bufio.ScanWords)

	n := readInt()
	num := make([]int, n)
	for i := range num {
		num[i] = readInt()
	}
	ans := make([]string, n)
	for le := 1; le <= 9; le++ {
		good := make(map[int]bool)
		idx := make(map[int]int)
		for i, num := range num {
			mod := 10
			for x := 1; x < le; x++ {
				mod *= 10
			}
			for j := 1; ; j *= 10 {
				str := num / j % mod
				if ii, ok := idx[str]; !ok {
					idx[str] = i
					good[str] = true
				} else if ii != i {
					delete(good, str)
				}
				if j*mod == 1e9 {
					break
				}
			}
		}
		for str := range good {
			i := idx[str]
			if len(ans[i]) == 0 {
				ans[i] = format(str, le)
			}
		}
	}

	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	for _, a := range ans {
		fmt.Fprintln(out, a)
	}
}


As we communicate, we learn much new information. However, the process of communication takes too much time. It becomes clear if we look at the words we use in our everyday speech.We can list many simple words consisting of many letters: "information", "technologies", "university", "construction", "conservatoire", "refrigerator", "stopwatch", "windowsill", "electricity", "government" and so on. Of course, we can continue listing those words ad infinitum. Fortunately, the solution for that problem has been found. To make our speech clear and brief, we should replace the initial words with those that resemble them but are much shorter. This idea hasn't been brought into life yet, that's why you are chosen to improve the situation. Let's consider the following formal model of transforming words: we shall assume that one can use n words in a chat. For each words we shall introduce a notion of its shorter variant. We shall define shorter variant of an arbitrary word s as such word t, that meets the following conditions:  it occurs in s as a subsequence,  its length ranges from one to four characters. In other words, the word t consists at least of one and at most of four characters that occur in the same order in the word s. Note that those characters do not necessarily follow in s immediately one after another. You are allowed not to shorten the initial word if its length does not exceed four characters.You are given a list of n different words. Your task is to find a set of their shortened variants. The shortened variants of all words from the list should be different.

The first line of the input file contains the only integer n (1 ≤ n ≤ 200). Then n lines contain a set of different non-empty words that consist of lowercase Latin letters. The length of each word does not exceed 10 characters.

If the solution exists, print in the output file exactly n lines, where the i-th line represents the shortened variant of the i-th word from the initial set. If there are several variants to solve the problem, print any of them. If there is no solution, print -1.

6
privet
spasibo
codeforces
java
marmelad
normalno

package main

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

// github.com/EndlessCheng/codeforces-go
func CF120H(_r io.Reader, _w io.Writer) {
	var _ss []string
	var _gen func(s, sub string)
	_gen = func(s, sub string) {
		_ss = append(_ss, sub)
		if len(sub) < 4 {
			for i := range s {
				_gen(s[i+1:], sub+string(s[i]))
			}
		}
	}
	genSubStrs := func(s string) []string {
		_ss = []string{}
		_gen(s, "")
		a := _ss[1:]
		n := len(a)
		sort.Strings(a)
		res := make([]string, 1, n)
		res[0] = a[0]
		for i := 1; i < n; i++ {
			if a[i] != a[i-1] {
				res = append(res, a[i])
			}
		}
		return res
	}
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n int
	Fscan(in, &n)
	strID := map[string]int{}
	idToStr := []string{}
	g := make([][]int, n)
	for i := range g {
		var s string
		Fscan(in, &s)
		ss := genSubStrs(s)
		for _, s := range ss {
			id, ok := strID[s]
			if !ok {
				id = len(strID)
				strID[s] = id
				idToStr = append(idToStr, s)
			}
			g[i] = append(g[i], id)
		}
	}

	matchL := make([]int, n)
	matchR := make([]int, len(strID))
	for i := range matchR {
		matchR[i] = -1
	}
	var used []bool
	var f func(v int) bool
	f = func(v int) bool {
		used[v] = true
		for _, w := range g[v] {
			if lv := matchR[w]; lv == -1 || !used[lv] && f(lv) {
				matchR[w] = v
				matchL[v] = w
				return true
			}
		}
		return false
	}
	cnt := 0
	for v := range g {
		used = make([]bool, n)
		if f(v) {
			cnt++
		}
	}
	if cnt < n {
		Fprint(out, -1)
		return
	}
	for _, id := range matchL {
		Fprintln(out, idToStr[id])
	}
}

func main() {
	r, _ := os.Open("input.txt")
	w, _ := os.Create("output.txt")
	CF120H(r, w)
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF120H(_r io.Reader, _w io.Writer) {
	var _ss []string
	var _gen func(s, sub string)
	_gen = func(s, sub string) {
		_ss = append(_ss, sub)
		if len(sub) < 4 {
			for i := range s {
				_gen(s[i+1:], sub+string(s[i]))
			}
		}
	}
	genSubStrs := func(s string) []string {
		_ss = []string{}
		_gen(s, "")
		a := _ss[1:]
		n := len(a)
		sort.Strings(a)
		res := make([]string, 1, n)
		res[0] = a[0]
		for i := 1; i < n; i++ {
			if a[i] != a[i-1] {
				res = append(res, a[i])
			}
		}
		return res
	}
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n int
	Fscan(in, &n)
	strID := map[string]int{}
	idToStr := []string{}
	g := make([][]int, n)
	for i := range g {
		var s string
		Fscan(in, &s)
		ss := genSubStrs(s)
		for _, s := range ss {
			id, ok := strID[s]
			if !ok {
				id = len(strID)
				strID[s] = id
				idToStr = append(idToStr, s)
			}
			g[i] = append(g[i], id)
		}
	}

	matchL := make([]int, n)
	matchR := make([]int, len(strID))
	for i := range matchR {
		matchR[i] = -1
	}
	var used []bool
	var f func(v int) bool
	f = func(v int) bool {
		if !used[v] {
			used[v] = true
			for _, w := range g[v] {
				if matchR[w] == -1 || f(matchR[w]) {
					matchR[w] = v
					matchL[v] = w
					return true
				}
			}
		}
		return false
	}
	cnt := 0
	for v := range g {
		used = make([]bool, n)
		if f(v) {
			cnt++
		}
	}
	if cnt < n {
		Fprint(out, -1)
		return
	}
	for _, id := range matchL {
		Fprintln(out, idToStr[id])
	}
}

func main() {
	r, _ := os.Open("input.txt")
	w, _ := os.Create("output.txt")
	CF120H(r, w)
}

package main

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

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

	genSubStrs := func(s string) []string {
		a := []string{}
		var f func(s, sub string)
		f = func(s, sub string) {
			a = append(a, sub)
			if len(sub) < 4 {
				for i, b := range s {
					f(s[i+1:], sub+string(b))
				}
			}
		}
		f(s, "")
		a = a[1:]
		sort.Strings(a)
		j := 0
		for i := 1; i < len(a); i++ {
			if a[j] != a[i] {
				j++
				a[j] = a[i]
			}
		}
		return a[:j+1]
	}
	var n int
	var s string
	Fscan(in, &n)
	sid := map[string]int{}
	strs := []string{}
	g := make([][]int, n)
	for i := range g {
		Fscan(in, &s)
		a := genSubStrs(s)
		for _, s := range a {
			id, ok := sid[s]
			if !ok {
				id = len(sid)
				sid[s] = id
				strs = append(strs, s)
			}
			g[i] = append(g[i], id)
		}
	}

	matchL := make([]int, n)
	matchR := make([]int, len(sid))
	for i := range matchR {
		matchR[i] = -1
	}
	var used []bool
	var f func(int) bool
	f = func(v int) bool {
		used[v] = true
		for _, w := range g[v] {
			if lv := matchR[w]; lv == -1 || !used[lv] && f(lv) {
				matchR[w] = v
				matchL[v] = w
				return true
			}
		}
		return false
	}
	cnt := 0
	for v := range g {
		used = make([]bool, n)
		if f(v) {
			cnt++
		}
	}
	if cnt < n {
		Fprint(out, -1)
		return
	}
	for _, id := range matchL {
		Fprintln(out, strs[id])
	}
}

func main() { r, _ := os.Open("input.txt"); w, _ := os.Create("output.txt"); CF120H(r, w) }

package main

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

// github.com/EndlessCheng/codeforces-go
func CF120H(_r io.Reader, _w io.Writer) {
	var _ss []string
	var _gen func(s, sub string)
	_gen = func(s, sub string) {
		_ss = append(_ss, sub)
		if len(sub) < 4 {
			for i := range s {
				_gen(s[i+1:], sub+string(s[i]))
			}
		}
	}
	genSubStrs := func(s string) []string {
		_ss = []string{}
		_gen(s, "")
		a := _ss[1:]
		n := len(a)
		sort.Strings(a)
		res := make([]string, 1, n)
		res[0] = a[0]
		for i := 1; i < n; i++ {
			if a[i] != a[i-1] {
				res = append(res, a[i])
			}
		}
		return res
	}
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n int
	Fscan(in, &n)
	strID := map[string]int{}
	idToStr := []string{}
	g := make([][]int, n)
	for i := range g {
		var s string
		Fscan(in, &s)
		ss := genSubStrs(s)
		for _, s := range ss {
			id, ok := strID[s]
			if !ok {
				id = len(strID)
				strID[s] = id
				idToStr = append(idToStr, s)
			}
			g[i] = append(g[i], id)
		}
	}

	matchL := make([]int, n)
	matchR := make([]int, len(strID))
	for i := range matchR {
		matchR[i] = -1
	}
	var used []bool
	var f func(v int) bool
	f = func(v int) bool {
		if !used[v] {
			used[v] = true
			for _, w := range g[v] {
				if matchR[w] == -1 || f(matchR[w]) {
					matchR[w] = v
					matchL[v] = w
					return true
				}
			}
		}
		return false
	}
	cnt := 0
	for v := range g {
		used = make([]bool, n)
		if f(v) {
			cnt++
		}
	}
	if cnt < n {
		Fprint(out, -1)
		return
	}
	for _, id := range matchL {
		Fprintln(out, idToStr[id])
	}
}

func main() {
	r, _ := os.Open("input.txt")
	w, _ := os.OpenFile("output.txt", os.O_WRONLY|os.O_CREATE, 0666)
	CF120H(r, w)
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF120H(_r io.Reader, _w io.Writer) {
	var _ss []string
	var _gen func(s, sub string)
	_gen = func(s, sub string) {
		_ss = append(_ss, sub)
		if len(sub) < 4 {
			for i := range s {
				_gen(s[i+1:], sub+string(s[i]))
			}
		}
	}
	genSubStrs := func(s string) []string {
		_ss = []string{}
		_gen(s, "")
		a := _ss[1:]
		n := len(a)
		sort.Strings(a)
		res := make([]string, 1, n)
		res[0] = a[0]
		for i := 1; i < n; i++ {
			if a[i] != a[i-1] {
				res = append(res, a[i])
			}
		}
		return res
	}
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n int
	Fscan(in, &n)
	strID := map[string]int{}
	idToStr := []string{}
	g := make([][]int, n)
	for i := range g {
		var s string
		Fscan(in, &s)
		ss := genSubStrs(s)
		for _, s := range ss {
			id, ok := strID[s]
			if !ok {
				id = len(strID)
				strID[s] = id
				idToStr = append(idToStr, s)
			}
			g[i] = append(g[i], id)
		}
	}

	matchL := make([]int, n)
	matchR := make([]int, len(strID))
	for i := range matchR {
		matchR[i] = -1
	}
	var used []bool
	var f func(v int) bool
	f = func(v int) bool {
		if !used[v] {
			used[v] = true
			for _, w := range g[v] {
				if matchR[w] == -1 || f(matchR[w]) {
					matchR[w] = v
					matchL[v] = w
					return true
				}
			}
		}
		return false
	}
	cnt := 0
	for v := range g {
		used = make([]bool, n)
		if f(v) {
			cnt++
		}
	}
	if cnt < n {
		Fprint(out, -1)
		return
	}
	for _, id := range matchL {
		Fprintln(out, idToStr[id])
	}
}

func main() {
	r, _ := os.Open("input.txt")
	w, _ := os.Open("output.txt")
	CF120H(r, w)
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF120H(_r io.Reader, _w io.Writer) {
	var _ss []string
	var _gen func(s, sub string)
	_gen = func(s, sub string) {
		_ss = append(_ss, sub)
		if len(sub) < 4 {
			for i := range s {
				_gen(s[i+1:], sub+string(s[i]))
			}
		}
	}
	genSubStrs := func(s string) []string {
		_ss = []string{}
		_gen(s, "")
		a := _ss[1:]
		n := len(a)
		sort.Strings(a)
		res := make([]string, 1, n)
		res[0] = a[0]
		for i := 1; i < n; i++ {
			if a[i] != a[i-1] {
				res = append(res, a[i])
			}
		}
		return res
	}
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n int
	Fscan(in, &n)
	strID := map[string]int{}
	idToStr := []string{}
	g := make([][]int, n)
	for i := range g {
		var s string
		Fscan(in, &s)
		ss := genSubStrs(s)
		for _, s := range ss {
			id, ok := strID[s]
			if !ok {
				id = len(strID)
				strID[s] = id
				idToStr = append(idToStr, s)
			}
			g[i] = append(g[i], id)
		}
	}

	matchL := make([]int, n)
	matchR := make([]int, len(strID))
	for i := range matchR {
		matchR[i] = -1
	}
	var used []bool
	var f func(v int) bool
	f = func(v int) bool {
		if !used[v] {
			used[v] = true
			for _, w := range g[v] {
				if matchR[w] == -1 || f(matchR[w]) {
					matchR[w] = v
					matchL[v] = w
					return true
				}
			}
		}
		return false
	}
	cnt := 0
	for v := range g {
		used = make([]bool, n)
		if f(v) {
			cnt++
		}
	}
	if cnt < n {
		Fprint(out, -1)
		return
	}
	for _, id := range matchL {
		Fprintln(out, idToStr[id])
	}
}

func main() {
	r, _ := os.Open("input.txt")
	w, _ := os.OpenFile("output.txt", os.O_WRONLY, 0)
	CF120H(r, w)
}


Gerald has n younger brothers and their number happens to be even. One day he bought n2 candy bags. One bag has one candy, one bag has two candies, one bag has three candies and so on. In fact, for each integer k from 1 to n2 he has exactly one bag with k candies. Help him give n bags of candies to each brother so that all brothers got the same number of candies.

The single line contains a single integer n (n is even, 2 ≤ n ≤ 100) — the number of Gerald's brothers.

Let's assume that Gerald indexes his brothers with numbers from 1 to n. You need to print n lines, on the i-th line print n integers — the numbers of candies in the bags for the i-th brother. Naturally, all these numbers should be distinct and be within limits from 1 to n2. You can print the numbers in the lines in any order.  It is guaranteed that the solution exists at the given limits.

NoteThe sample shows Gerald's actions if he has two brothers. In this case, his bags contain 1, 2, 3 and 4 candies. He can give the bags with 1 and 4 candies to one brother and the bags with 2 and 3 to the other brother.

package main
import (
	"fmt"
)
func main() {
	var masuk,t1,t2,hasil int
	fmt.Scan(&masuk)
	for a:=1; a<=masuk; a++ {
		for b:=0; b<masuk; b++ {
			t1=a+b*(masuk+1)
			t2=(b+1)*masuk
			if t1>t2 {
				hasil=a+(b-1)*(masuk +1)+1
				fmt.Printf("%d ",hasil)
			} else {
				hasil= a+b*(masuk+1)
				fmt.Printf("%d ",hasil)
			}

		}
		fmt.Println()
	}
}

package main

import "fmt"

func main() {
	var n int

	fmt.Scan(&n)

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

package main

import (
  "fmt"
)

var n int

func input() {
  fmt.Scanf("%d", &n)
}

func solve() {
  for i := 1; i <= n; i ++ {
    for x := 0; x < n; x ++ {
      if i + x * (n + 1) > (x + 1) * n {
        fmt.Print(i + (x - 1) * (n + 1) + 1, ` `)
      } else {
        fmt.Print(i + x * (n + 1), ` `)
      }
    }
    fmt.Print("\n")
  }
}

func main() {
  input()
  solve()
}

package main

import "fmt"

func main() {
    var n int
    fmt.Scan(&n)
    for i := 0; i < n; i++ {
        for j := 0; j < n; j++ {
            fmt.Print((j*n)+1+(i+j)%n)
            if j == (n-1) { fmt.Println() } else { fmt.Print(" ") }
        }
    }
}

package main
import 	"fmt"
func main() {
	var n int
	_,_=fmt.Scanf("%d", &n)
	var tempn, k = n*n, 1
	for i :=1; i<=n;i++{
		for j:=1; j<=n/2;j++{
			fmt.Printf("%d %d ", k, tempn)
			tempn--
			k++
		}
		fmt.Println("")
	}
}

package main

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

type Scanner struct {
  r            io.Reader
  split        bufio.SplitFunc
  maxTokenSize int
  token, buf   []byte
  start, end   int
}

func NewScanner(r io.Reader) *Scanner {
  return &Scanner{
    r:            r,
    split:        bufio.ScanWords,
    maxTokenSize: 16777216,
    buf:          make([]byte, 4096),
  }
}

func (s *Scanner) advance(n int) bool {
  if n < 0 || n > s.end-s.start {
    return false
  }
  s.start += n
  return true
}

func (s *Scanner) Text() string {
  return string(s.token)
}

func (s *Scanner) Scan() bool {
  for {
    if s.end > s.start {
      advance, token, err := s.split(s.buf[s.start:s.end], false)
      if err != nil || !s.advance(advance) {
        return false
      }
      s.token = token
      if token != nil {
        return true
      }
    }
    if s.start > 0 && (s.end == len(s.buf) || s.start > len(s.buf)/2) {
      copy(s.buf, s.buf[s.start:s.end])
      s.end -= s.start
      s.start = 0
    }
    if s.end == len(s.buf) {
      if len(s.buf) >= s.maxTokenSize {
        return false
      }
      newSize := len(s.buf) * 2
      if newSize > s.maxTokenSize {
        newSize = s.maxTokenSize
      }
      newBuf := make([]byte, newSize)
      copy(newBuf, s.buf[s.start:s.end])
      s.buf = newBuf
      s.end -= s.start
      s.start = 0
      continue
    }
    for loop := 0; ; {
      n, _ := s.r.Read(s.buf[s.end:len(s.buf)])
      s.end += n
      if n > 0 {
        break
      }
      loop++
      if loop > 100 {
        break
      }
    }
  }
}

func (s *Scanner) Split(split bufio.SplitFunc) {
  s.split = split
}

var w *bufio.Writer = bufio.NewWriter(os.Stdout)
var sc *Scanner = NewScanner(os.Stdin)

func next() string { sc.Scan(); return sc.Text() }
func nc() byte     { sc.Scan(); return sc.Text()[0] }

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

func ni64() int64 {
  sc.Scan()
  n, _ := strconv.ParseInt(sc.Text(), 10, 64)
  return n
}

func pri32(i ...int) {
  for _, ii := range i {
    w.WriteString(strconv.Itoa(ii))
    sp()
  }
}
func pri64(i ...int64) {
  for _, ii := range i {
    w.WriteString(strconv.FormatInt(ii, 10))
    sp()
  }
}
func sp()   { w.WriteByte(' ') }
func endl() { w.WriteByte('\n') }

func main() {
  n := ni32()
  cnt := 0
  for i := 0; i < n*n/2; i++ {
    pri32(i+1, n*n-i)
    cnt++
    if cnt == n/2 {
      cnt = 0
      endl()
    }

  }
  w.Flush()
}

package main

import (
	"fmt"
)

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

	cur := 1
	for i := 0; i < n; i++ {
		for j := 0; j < n/2; j++ {
			fmt.Printf("%d %d ", cur, n*n-cur+1)
			cur++
		}
		fmt.Println()
	}
}

// mic-334A
package main

import (
    "fmt"
)

func main() {
    var n, k, i,s,m int
    fmt.Scan(&n)
    change := false
    k = n * n
    ans1 := make([]int, k/2)
    ans2 := make([]int, k/2)
    i = 1
    for i < k {
        if !change {
            ans1[s] = i
            ans1[s+1] = k
            change = true
            s+=2
        } else if change {
            ans2[m] = i
            ans2[m+1] = k
            change = false
            m+=2
        }
        i++
        k--
    }
    for i := range ans1 {
        fmt.Print(ans1[i])
        if i < len(ans1)-1 {
            fmt.Print(" ")
        } else {
            fmt.Println("")
        }
    }
        for i := range ans2 {
        fmt.Print(ans2[i])
        if i < len(ans2)-1 {
            fmt.Print(" ")
        } else {
            fmt.Println("")
        }
    }
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			fmt.Print((j * n) + 1 + (i+j)%n)
			if j == (n - 1) {
				fmt.Println()
			} else {
				fmt.Print(" ")
			}
		}
	}
}

// cf334q project main.go
package main

import (
	"fmt"
)

// 1 8 9  16
// 2 7 10 15
// 3 6 11 14
// 4 5 12 13
func main() {
	var n int
	fmt.Scanf("%d", &n)

	for i := 0; i < n; i++ {
		for j := 1; j <= n; j++ {
			var u int
			if j%2 == 0 {
				u = n*j - i
			} else {
				u = n*(j-1) + i + 1
			}
			fmt.Printf("%d ", u)
		}
		fmt.Println("")
	}

}

// cf334q project main.go
package main

import (
	"fmt"
)

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

	for i := 1; i <= n; i++ {
		fmt.Printf("%d %d\n", i, 2*n+1-i)
	}

}


You are given two arrays of length $$$n$$$: $$$a_1, a_2, \dots, a_n$$$ and $$$b_1, b_2, \dots, b_n$$$.You can perform the following operation any number of times:   Choose integer index $$$i$$$ ($$$1 \le i \le n$$$);  Swap $$$a_i$$$ and $$$b_i$$$. What is the minimum possible sum $$$|a_1 - a_2| + |a_2 - a_3| + \dots + |a_{n-1} - a_n|$$$ $$$+$$$ $$$|b_1 - b_2| + |b_2 - b_3| + \dots + |b_{n-1} - b_n|$$$ (in other words, $$$\sum\limits_{i=1}^{n - 1}{\left(|a_i - a_{i+1}| + |b_i - b_{i+1}|\right)}$$$) you can achieve after performing several (possibly, zero) operations?

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 4000$$$) — the number of test cases. Then, $$$t$$$ test cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$2 \le n \le 25$$$) — the length of arrays $$$a$$$ and $$$b$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the array $$$a$$$. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le 10^9$$$) — the array $$$b$$$.

For each test case, print one integer — the minimum possible sum $$$\sum\limits_{i=1}^{n-1}{\left(|a_i - a_{i+1}| + |b_i - b_{i+1}|\right)}$$$.

NoteIn the first test case, we can, for example, swap $$$a_3$$$ with $$$b_3$$$ and $$$a_4$$$ with $$$b_4$$$. We'll get arrays $$$a = [3, 3, 3, 3]$$$ and $$$b = [10, 10, 10, 10]$$$ with sum $$$3 \cdot |3 - 3| + 3 \cdot |10 - 10| = 0$$$.In the second test case, arrays already have minimum sum (described above) equal to $$$|1 - 2| + \dots + |4 - 5| + |6 - 7| + \dots + |9 - 10|$$$ $$$= 4 + 4 = 8$$$.In the third test case, we can, for example, swap $$$a_5$$$ and $$$b_5$$$.

package main

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

func abs(n int64) int64 {
	if n > 0 {
		return n
	}
	return -n
}

func find_sum(a, b []int64, m int) int64 {
	var sum int64
	for j := 0; j < m; j++ {
		if a[j] < b[j] {
			a[j], b[j] = b[j], a[j]
		}

		if j > 0 {
			sum += abs(a[j] - a[j-1])
			sum += abs(b[j] - b[j-1])
		}
	}

	return sum
}

func read_ab(s *bufio.Scanner, a, b []int64) int {
	s.Scan()
	m, _ := strconv.Atoi(s.Text())

	for j := 0; j < m; j++ {
		s.Scan()
		a[j],_ = strconv.ParseInt(s.Text(), 10, 64)
	}

	for j := 0; j < m; j++ {
		s.Scan()
		b[j],_ = strconv.ParseInt(s.Text(), 10, 64)
	}

	return m
}

func main() {
	a := make([]int64, 25)
	b := make([]int64, 25)

	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	output := bufio.NewWriter(os.Stdout)
	defer output.Flush()

	scanner.Scan()
	n,_ := strconv.Atoi(scanner.Text())

	for i := 0; i < n; i++ {
		m := read_ab(scanner, a, b)
		sum := find_sum(a, b, m)

		b := strconv.AppendInt(nil, sum, 10)
		b = append(b, '\n')
		output.Write(b)
	}
}

package main

import (
	"flag"
	"fmt"
	"log"
	"os"
	"bufio"
	"strconv"

	"runtime/pprof"
)

var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")

func abs(n int64) int64 {
	if n > 0 {
		return n
	}
	return -n
}

func find_sum(a, b []int64, m int) int64 {
	var sum int64
	for j := 0; j < m; j++ {
		if a[j] < b[j] {
			a[j], b[j] = b[j], a[j]
		}

		if j > 0 {
			sum += abs(a[j] - a[j-1])
			sum += abs(b[j] - b[j-1])
		}
	}

	return sum
}

func read_ab(s *bufio.Scanner, a, b []int64) int {
	s.Scan()
	m, _ := strconv.ParseInt(s.Text(), 10, 64)

	for j := 0; j < int(m); j++ {
		s.Scan()
		a[j],_ = strconv.ParseInt(s.Text(), 10, 64)
	}

	for j := 0; j < int(m); j++ {
		s.Scan()
		b[j],_ = strconv.ParseInt(s.Text(), 10, 64)
	}

	return int(m)
}

func main() {

	flag.Parse()
	if *cpuprofile != "" {
		f, err := os.Create(*cpuprofile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	a := make([]int64, 25)
	b := make([]int64, 25)

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

	scanner.Scan()
	n,_ := strconv.ParseInt(scanner.Text(), 10, 64)

	for i := 0; i < int(n); i++ {
		m := read_ab(scanner, a, b)
		sum := find_sum(a, b, m)

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

package main

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

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

func init() {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 4096), 1e9)
}

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

func _int() int {
	s := _string()
	i := 0
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int(r - '0')
	}
	return i
}

func _int64() int64 {
	s := _string()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

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

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

func sol(a, b []int64) int64 {
	for i := range a {
		if b[i] < a[i] {
			a[i], b[i] = b[i], a[i]
		}
	}
	ret := int64(0)
	for i := range a {
		if i == 0 {
			continue
		}
		ret += abs(a[i] - a[i-1])
		ret += abs(b[i] - b[i-1])
	}
	return ret
}

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

package main

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

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

func init() {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 4096), 1e9)
}

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

func _int() int {
	s := _string()
	i := 0
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int(r - '0')
	}
	return i
}

func _int64() int64 {
	s := _string()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

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

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

func sol(a, b []int64) int64 {
	for i := range a {
		if i == 0 {
			continue
		}
		noswap := abs(b[i]-b[i-1]) + abs(a[i]-a[i-1])
		swap := abs(b[i]-a[i-1]) + abs(a[i]-b[i-1])
		if swap < noswap {
			a[i], b[i] = b[i], a[i]
		}
	}
	ret := int64(0)
	for i := range a {
		if i == 0 {
			continue
		}
		ret += abs(a[i] - a[i-1])
		ret += abs(b[i] - b[i-1])
	}
	return ret
}

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

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n := 0
	for scanner.Scan() {
		n, _ = strconv.Atoi(scanner.Text())
		break
	}

	answers := make([]int64, n)

	for i := 0; i < n; i++ {
		scanner.Scan()
		scanner.Text()

		scanner.Scan()
		sarr1 := strings.Split(scanner.Text(), " ")
		scanner.Scan()
		sarr2 := strings.Split(scanner.Text(), " ")

		arr1, arr2 := make([]int64, len(sarr1)), make([]int64, len(sarr2))
		for j := 0; j < len(arr1); j++ {
			arr1[j], _ = strconv.ParseInt(sarr1[j], 10, 64)
			arr2[j], _ = strconv.ParseInt(sarr2[j], 10, 64)
		}

		sum := float64(0)

		for j := 0; j < len(arr1)-1; j++ {
			if math.Abs(float64(arr1[j]-arr1[j+1]))+math.Abs(float64(arr2[j]-arr2[j+1])) < math.Abs(float64(arr1[j]-arr2[j+1]))+math.Abs(float64(arr2[j]-arr1[j+1])) {
				sum += math.Abs(float64(arr1[j]-arr1[j+1])) + math.Abs(float64(arr2[j]-arr2[j+1]))
				continue
			}

			sum += math.Abs(float64(arr1[j]-arr2[j+1])) + math.Abs(float64(arr2[j]-arr1[j+1]))
			arr1[j+1], arr2[j+1] = arr2[j+1], arr1[j+1]
		}

		answers[i] = int64(sum)
	}

	for _, answer := range answers {
		fmt.Println(answer)
	}
}

package main

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

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

func A(_r io.Reader, _w io.Writer) {
	in, out := bufio.NewReader(_r), bufio.NewWriterSize(_w, 102400000)
	defer out.Flush()

	var c int
	var n int
	Fscan(in, &c)
	for t := 0; t < c; t++ {
		Fscan(in, &n)
		a, b := make([]int64, n), make([]int64, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		for i := range b {
			Fscan(in, &b[i])
		}
		ans := int64(0)
		for i := 1; i < n; i++ {
			old := abs(a[i]-a[i-1]) + abs(b[i]-b[i-1])
			swap := abs(b[i]-a[i-1]) + abs(a[i]-b[i-1])
			if swap < old {
				ans += swap
				a[i], b[i] = b[i], a[i]
			} else {
				ans += old
			}
		}
		Fprintln(out, ans)
	}
}

func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"runtime/debug"
)

/* begin of MyIO */
const eof = 0

type any = interface{}

type MyIO struct {
	in  *os.File
	out *bufio.Writer
	_i  int
	_n  int
	buf []byte
}

func NewIO(in *os.File, out *os.File) *MyIO {
	return &MyIO{
		in,
		bufio.NewWriter(out),
		0, 0, make([]byte, 1<<12),
	}
}

func (myio *MyIO) rc() byte {
	if myio._i == myio._n {
		myio._n, _ = myio.in.Read(myio.buf)
		if myio._n == 0 { // EOF
			return eof
		}
		myio._i = 0
	}
	b := myio.buf[myio._i]
	myio._i++
	return b
}

func (myio *MyIO) ReadInt() (x int) {
	rc := myio.rc
	b := rc()
	neg := false
	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
		if b == '-' {
			neg = true
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int(b&15)
	}
	if neg {
		return -x
	}
	return
}

func (myio *MyIO) ReadInt64() (x int64) {
	rc := myio.rc
	b := rc()
	neg := false

	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
		if b == '-' {
			neg = true
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int64(b&15)
	}
	if neg {
		return -x
	}
	return x
}

func (myio *MyIO) ReadBytes() (s []byte) {
	rc := myio.rc
	b := rc()
	for ; b <= ' '; b = rc() {
	}
	for ; b > ' '; b = rc() {
		s = append(s, b)
	}
	return
}

func (myio *MyIO) ReadStr() (s string) {
	return string(myio.ReadBytes())
}

func (myio *MyIO) Flush() {
	myio.out.Flush()
}

func (myio *MyIO) Printf(format string, a ...any) (n int, err error) {
	return fmt.Fprintf(myio.out, format, a...)
}

func (myio *MyIO) Println(a ...any) (n int, err error) {
	return fmt.Fprintln(myio.out, a...)
}

func (myio *MyIO) Print(a ...any) (n int, err error) {
	return fmt.Fprint(myio.out, a...)
}

var myio *MyIO

func ri() int {
	return myio.ReadInt()
}

func ri64() int64 {
	return myio.ReadInt64()
}

func ria(_n int) []int {
	a := make([]int, _n)
	for i := range a {
		a[i] = ri()
	}
	return a
}

/* End of MyIO */

func abs(a int) int {
	if a > 0 {
		return a
	}
	return -a
}

func sol() {
	n := ri()
	a := ria(n)
	b := ria(n)

	for i := 0; i < n; i++ {
		if a[i] > b[i] {
			a[i], b[i] = b[i], a[i]
		}
	}
	var res int64 = 0
	for i := 1; i < n; i++ {
		res += int64(abs(a[i]-a[i-1])) + int64(abs(b[i]-b[i-1]))
	}

	fmt.Println(res)
}

func main() {
	debug.SetGCPercent(-1)
	in, out := os.Stdin, os.Stdout
	myio = NewIO(in, out)
	defer myio.Flush()

	var t = 1
	t = ri()
	for ; t > 0; t-- {
		sol()
	}
}

package main

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

func ss() {
	n := NextInt()
	a := make([]int64, n)
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = NextInt64()
	}
	for i := 0; i < n; i++ {
		b[i] = NextInt64()
	}
	ans := int64(0)
	for i := 1; i < n; i++ {
		ans += MinInt64(AbsInt64(b[i-1]-a[i])+AbsInt64(a[i-1]-b[i]), AbsInt64(a[i-1]-a[i])+AbsInt64(b[i-1]-b[i]))
	}
	Out(ans, "\n")
}

func Solve() {
	for T := NextInt(); T > 0; T-- {
		ss()
	}
}

// util
var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func In(a ...interface{}) {
	_, _ = fmt.Fscan(in, a...)
}

func main() {
	Solve()
	out.Flush()
}

func NextInt() int {
	var x int
	_, _ = fmt.Fscan(in, &x)
	return x
}

func NextInt64() int64 {
	var x int64
	_, _ = fmt.Fscan(in, &x)
	return x
}

func Out(a ...interface{}) {
	_, _ = fmt.Fprint(out, a...)
}

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

func AbsInt64(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

package main

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

func main() {

	reader := bufio.NewReader(os.Stdin)
	writter := bufio.NewWriter(os.Stdout)
	defer writter.Flush()

	t := 0
	fmt.Fscan(reader, &t)

	for t > 0 {
		n := 0
		fmt.Fscan(reader, &n)

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

		for i := range a {
			fmt.Fscan(reader, &a[i])
		}

		for i := range b {
			fmt.Fscan(reader, &b[i])
		}

		// for i := range result {
		// 	fmt.Fprintf(writter, "%d ", result[i])
		// }
		// fmt.Fprintln(writter)

		fmt.Fprintln(writter, solve(n, a, b))

		t--
	}

	// n := 6
	// a := []int{72, 101, 108, 108, 111, 44}
	// b := []int{10, 87, 111, 114, 108, 100}

	// fmt.Println(solve(n, a, b))

	//ethereum.Ethereum()

}

func solve(n int, a, b []int) int64 {

	sum := int64(0)

	for i := range a {
		if a[i] > b[i] {
			a[i], b[i] = b[i], a[i]
		}
	}

	// fmt.Println(a)
	// fmt.Println(b)

	for i := 1; i < n; i++ {
		sum += int64(math.Abs(float64(a[i] - a[i-1])))
	}

	for i := 1; i < n; i++ {
		sum += int64(math.Abs(float64(b[i] - b[i-1])))
	}

	return sum
}

package main

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

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

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

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

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

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

/**
Run solve the problem CF1661A
Date: 19/05/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1661/A
Problem: CF1661A
**/
func (in *CF1661A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		a := make([]int, n)
		for i := 0; i < n; i++ {
			a[i] = in.NextInt()
		}

		b := make([]int, n)
		for i := 0; i < n; i++ {
			b[i] = in.NextInt()
		}

		sum := int64(0)
		for i := 0; i < n; i++ {
			if a[i] > b[i] {
				a[i], b[i] = b[i], a[i]
			}

		}

		for i:=1;i<n;i++{
			sum += int64(math.Abs(float64(a[i-1])-float64(a[i])))
			sum += int64(math.Abs(float64(b[i-1])-float64(b[i])))
		}
		fmt.Println(sum)
	}
}

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

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

package main

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

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}

func main() {
	defer out.Flush()

	var t int64
	fmt.Fscan(in, &t)

	for ; t > 0; t-- {
		var n int
		fmt.Fscan(in, &n)

		a := make([]int64, n)
		b := make([]int64, n)

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

		var ans int64 = 0

		for i := 1; i < n; i++ {
			if abs(a[i-1]-a[i])+abs(b[i-1]-b[i]) <= abs(a[i-1]-b[i])+abs(b[i-1]-a[i]) {
				ans += abs(a[i-1]-a[i]) + abs(b[i-1]-b[i])
			} else {
				ans += abs(a[i-1]-b[i]) + abs(b[i-1]-a[i])
			}
		}

		fmt.Fprintln(out, ans)
	}
}

package main

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

const (
	INF = 3e5 + 10
)

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

	var t int
	Fscan(in, &t)

	for k := 0; k < t; k++ {
		var n int
		Fscan(in, &n)

		a := make([]int64, n)
		b := make([]int64, n)

		for i := 0; i < n; i++ {
			Fscan(in, &a[i])
		}
		for i := 0; i < n; i++ {
			Fscan(in, &b[i])
		}

		dp := make([][]int64, n)

		for i := 0; i < n; i++ {
			dp[i] = make([]int64, 2)
		}

		for i := 1; i < n; i++ {
			dp[i][0] = min(dp[i-1][0]+abs(a[i]-a[i-1])+abs(b[i]-b[i-1]), dp[i-1][1]+abs(a[i]-b[i-1])+abs(b[i]-a[i-1]))

			dp[i][1] = min(dp[i-1][0]+abs(b[i]-a[i-1])+abs(a[i]-b[i-1]), dp[i-1][1]+abs(b[i]-b[i-1])+abs(a[i]-a[i-1]))
		}

		Fprintln(out, min(dp[n-1][0], dp[n-1][1]))
	}

}

func abs(a int64) int64 {
	if a > 0 {
		return a
	}
	return -a

}

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

}

package main

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

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}
func readInt64(in *bufio.Reader) int64 {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.ParseInt(nStr, 10, 64)
	return n
}
func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}
func readArrInt64(in *bufio.Reader) []int64 {
	numbs := readLineNumbs(in)
	arr := make([]int64, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.ParseInt(n, 10, 64)
		arr[i] = val
	}
	return arr
}

func main() {
	ip := bufio.NewReader(os.Stdin)
	t := readInt(ip)
	for t > 0 {
		t--
		_ = readInt(ip)
		a := readArrInt64(ip)
		b := readArrInt64(ip)
		ans := int64(0)
		for i := range a {
			if i == 0 {
				continue
			}
			ans += min(abs(a[i]-a[i-1])+abs(b[i]-b[i-1]), abs(a[i]-b[i-1])+abs(b[i]-a[i-1]))
		}
		fmt.Println(ans)
	}
}

func min(i, j int64) int64 {
	if i > j {
		return j
	}
	return i
}

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

package main

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

var reader = bufio.NewReader(os.Stdin)

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

var writer = bufio.NewWriter(os.Stdout)

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

func redirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

func main() {
	defer writer.Flush()
	// redirectInputToFile(reader, "input.txt")

	T := 1
	scan(&T)
	for t := 0; t < T; t++ {
		solve()
	}
}

func solve() {
	var n int
	scan(&n)
	var a = make([]int, n)
	for i := range a {
		scan(&a[i])
	}
	var b = make([]int, n)
	for i := range b {
		scan(&b[i])
	}
	for i := 1; i < n; i++ {
		if abs(a[i]-a[i-1])+abs(b[i]-b[i-1]) > abs(b[i]-a[i-1])+abs(a[i]-b[i-1]) {
			a[i], b[i] = b[i], a[i]
		}
	}
	var ans int64
	for i := 1; i < n; i++ {
		ans += int64(abs(a[i] - a[i-1]))
	}
	for i := 1; i < n; i++ {
		ans += int64(abs(b[i] - b[i-1]))
	}
	printf("%v\n", ans)
}

func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

package main

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

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

	var T, n int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		a := make([]int64, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		b := make([]int64, n)
		for i := range b {
			Fscan(in, &b[i])
		}
		ans := int64(0)
		for i := 1; i < n; i++ {
			ans += min(abs(a[i]-a[i-1])+abs(b[i]-b[i-1]), abs(a[i]-b[i-1])+abs(b[i]-a[i-1]))
		}
		Fprintln(out, ans)
	}
}

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

func min(a, b int64) int64 {
	if a > b {
		return b
	}
	return a
}
func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}

package main

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

/*
2
4
50 100 50 50
1 50 1 1
2
1 10
10 1
---
198
0
*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n int
	scanf("%d\n", &n)

	a := make([]int, n)

	for i := range a {
		scanf("%d", &a[i])
	}
	scanf("\n")

	b := make([]int, n)

	for i := range b {
		scanf("%d", &b[i])
	}
	scanf("\n")

	sum := int64(0)

	for i := 0; i < n-1; i++ {
		if abs(a[i]-a[i+1])+abs(b[i]-b[i+1]) < abs(b[i]-a[i+1])+abs(a[i]-b[i+1]) {
			sum += int64(abs(a[i]-a[i+1]) + abs(b[i]-b[i+1]))
		} else {
			sum += int64(abs(b[i]-a[i+1]) + abs(a[i]-b[i+1]))
		}
	}

	fmt.Fprintln(writer, sum)
}

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

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

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int) int {
	return nums[minIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func minIdx(nums ...int) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	return nums[maxIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func maxIdx(nums ...int) int {
	maxNumIdx := 0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = i
		}
	}

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := 5; i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

package main

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

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

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

	var T int
	fmt.Fscan(in, &T)

	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

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

		b := make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &b[i])
		}

		var tot int64
		for i := 1; i < n; i++ {
			d := abs(a[i-1]-a[i]) + abs(b[i-1]-b[i])
			if abs(a[i-1]-b[i])+abs(b[i-1]-a[i]) < d {
				d = abs(a[i-1]-b[i]) + abs(b[i-1]-a[i])
			}
			tot += d
		}
		fmt.Fprintln(out, tot)
	}
}

package main

import (
	"fmt"
)

func abs(n int64) int64 {
	if n > 0 {
		return n
	}
	return -n
}

func min(n, m int64) int64 {
	if m > n {
		return n
	}

	return m
}

func main() {
	var n int
	var a, b [25]int64

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

	for i := 0; i < n; i++ {
		var sum int64
		var m int

		fmt.Scanf("%d\n", &m)

		for j := 0; j < m; j++ {
			fmt.Scanf("%d", &a[j])
		}

		fmt.Scanf("\n")

		for j := 0; j < m; j++ {
			fmt.Scanf("%d", &b[j])
		}

		fmt.Scanf("\n")

		for j := 0; j < m-1; j++ {
			mid := abs(a[i + 1] - a[i])
			midd := abs(b[i + 1] - b[i])
			middd := abs(a[i + 1] - b[i])
			mi := abs(b[i + 1] - a[i])

			sum += min(mi + middd, midd + mid)
		}

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

package main

import (
	"fmt"
	"math"
)

func main() {
	var n int

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

	for i:=0; i<n; i++ {
		var m, sum int

		fmt.Scanf("%d\n", &m)

		a := make([]int, m)
		b := make([]int, m)

		for j:=0; j < m; j++ {
			fmt.Scanf("%d", &a[j])
		}

		fmt.Scanf("\n")

		for j:=0; j < m; j++ {
			fmt.Scanf("%d", &b[j])
		}

		fmt.Scanf("\n")

		for j:=0; j < m; j++ {
			if a[j] < b[j] {
				a[j], b[j] = b[j], a[j]
			}
		}

		for j:=0; j < m-1; j++ {
			sum += int(math.Abs(float64(a[j]-a[j+1])))
			sum += int(math.Abs(float64(b[j]-b[j+1])))
		}

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

package main

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

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

func init() {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 4096), 1e9)
}

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

func _int() int {
	s := _string()
	i := 0
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int(r - '0')
	}
	return i
}

func _int64() int64 {
	s := _string()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

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

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

func sol(a, b []int64) int64 {
	for i := range a {
		if i == 0 {
			continue
		}
		if b[i] < a[i] {
			a[i], b[i] = b[i], a[i]
		}
	}
	ret := int64(0)
	for i := range a {
		if i == 0 {
			continue
		}
		ret += abs(a[i] - a[i-1])
		ret += abs(b[i] - b[i-1])
	}
	return ret
}

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

package main

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

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

func init() {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 4096), 1e9)
}

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

func _int() int {
	s := _string()
	i := 0
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int(r - '0')
	}
	return i
}

func _int64() int64 {
	s := _string()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

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

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

func sol(a, b []int) int {
	for i := range a {
		if i == 0 {
			continue
		}
		noswap := abs(b[i]-b[i-1]) + abs(a[i]-a[i-1])
		swap := abs(b[i]-a[i-1]) + abs(a[i]-b[i-1])
		if swap < noswap {
			a[i], b[i] = b[i], a[i]
		}
	}
	ret := 0
	for i := range a {
		if i == 0 {
			continue
		}
		ret += abs(a[i] - a[i-1])
		ret += abs(b[i] - b[i-1])
	}
	return ret
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

package main

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

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

func init() {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 4096), 1e9)
}

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

func _int() int {
	s := _string()
	i := 0
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int(r - '0')
	}
	return i
}

func _int64() int64 {
	s := _string()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

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

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

func sol(a, b []int) int {
	for i := range a {
		if b[i] < a[i] {
			a[i], b[i] = b[i], a[i]
		}
	}
	ret := 0
	for i := 0; i < len(a)-1; i++ {
		ret += abs(a[i+1] - a[i])
		ret += abs(b[i+1] - b[i])
	}
	return ret
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n := 0
	for scanner.Scan() {
		n, _ = strconv.Atoi(scanner.Text())
		break
	}

	answers := make([]int64, n)

	for i := 0; i < n; i++ {
		scanner.Scan()
		scanner.Text()

		scanner.Scan()
		sarr1 := strings.Split(scanner.Text(), " ")
		scanner.Scan()
		sarr2 := strings.Split(scanner.Text(), " ")

		arr1, arr2 := make([]int64, len(sarr1)), make([]int64, len(sarr2))
		for j := 0; j < len(arr1); j++ {
			arr1[j], _ = strconv.ParseInt(sarr1[j], 10, 64)
			arr2[j], _ = strconv.ParseInt(sarr2[j], 10, 64)
		}

		sum := float64(0)

		for j := 0; j < len(arr1)-1; j++ {
			if math.Abs(float64(arr1[j]-arr2[j+1])) < math.Abs(float64(arr1[j]-arr1[j+1])) {
				sum += math.Abs(float64(arr1[j]-arr2[j+1])) + math.Abs(float64(arr2[j]-arr1[j+1]))
				// arr1[j+1], arr2[j+1] = arr2[j+1], arr1[j+1]
			} else {
				sum += math.Abs(float64(arr1[j]-arr1[j+1])) + math.Abs(float64(arr2[j]-arr2[j+1]))
			}
		}

		answers[i] = int64(sum)
	}

	for _, answer := range answers {
		fmt.Println(answer)
	}
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n := 0
	for scanner.Scan() {
		n, _ = strconv.Atoi(scanner.Text())
		break
	}

	answers := make([]int, n)

	for i := 0; i < n; i++ {
		scanner.Scan()
		scanner.Text()

		scanner.Scan()
		sarr1 := strings.Split(scanner.Text(), " ")
		scanner.Scan()
		sarr2 := strings.Split(scanner.Text(), " ")

		arr1, arr2 := make([]int, len(sarr1)), make([]int, len(sarr2))
		for j := 0; j < len(arr1); j++ {
			arr1[j], _ = strconv.Atoi(sarr1[j])
			arr2[j], _ = strconv.Atoi(sarr2[j])
		}

		sum := float64(0)

		for j := 0; j < len(arr1)-1; j++ {
			if math.Abs(float64(arr1[j]-arr2[j+1])) < math.Abs(float64(arr1[j]-arr1[j+1])) {
				sum += math.Abs(float64(arr1[j]-arr2[j+1])) + math.Abs(float64(arr2[j]-arr1[j+1]))
				// arr1[j+1], arr2[j+1] = arr2[j+1], arr1[j+1]
			} else {
				sum += math.Abs(float64(arr1[j]-arr1[j+1])) + math.Abs(float64(arr2[j]-arr2[j+1]))
			}
		}

		answers[i] = int(sum)
	}

	for _, answer := range answers {
		fmt.Println(answer)
	}
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n := 0
	for scanner.Scan() {
		n, _ = strconv.Atoi(scanner.Text())
		break
	}

	answers := make([]int, n)

	for i := 0; i < n; i++ {
		scanner.Scan()
		scanner.Text()

		scanner.Scan()
		sarr1 := strings.Split(scanner.Text(), " ")
		scanner.Scan()
		sarr2 := strings.Split(scanner.Text(), " ")

		arr1, arr2 := make([]int, len(sarr1)), make([]int, len(sarr2))
		for j := 0; j < len(arr1); j++ {
			arr1[j], _ = strconv.Atoi(sarr1[j])
			arr2[j], _ = strconv.Atoi(sarr2[j])
		}

		sum := float64(0)

		for j := 0; j < len(arr1)-1; j++ {
			if math.Abs(float64(arr1[j]-arr2[j+1])) < math.Abs(float64(arr1[j]-arr1[j+1])) {
				arr1[j+1], arr2[j+1] = arr2[j+1], arr1[j+1]
			}

			sum += math.Abs(float64(arr1[j]-arr1[j+1])) + math.Abs(float64(arr2[j]-arr2[j+1]))
		}

		answers[i] = int(sum)
	}

	for _, answer := range answers {
		fmt.Println(answer)
	}
}

package main

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

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

func A(_r io.Reader, _w io.Writer) {
	in, out := bufio.NewReader(_r), bufio.NewWriterSize(_w, 102400000)
	defer out.Flush()

	var c int
	var n int
	Fscan(in, &c)
	for t := 0; t < c; t++ {
		Fscan(in, &n)
		a, b := make([]int, n), make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		for i := range b {
			Fscan(in, &b[i])
		}
		ans := 0
		for i := 1; i < n; i++ {
			old := abs(a[i]-a[i-1]) + abs(b[i]-b[i-1])
			swap := abs(b[i]-a[i-1]) + abs(a[i]-b[i-1])
			if swap < old {
				ans += swap
				a[i], b[i] = b[i], a[i]
			} else {
				ans += old
			}
		}
		Fprintln(out, ans)
	}
}

func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

package main

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

func ss() {
	n := NextInt()
	a := make([]int, n)
	b := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = NextInt()
	}
	for i := 0; i < n; i++ {
		b[i] = NextInt()
	}
	ans := 0
	for i := 1; i < n; i++ {
		ans += MinInt(AbsInt(b[i-1]-a[i])+AbsInt(a[i-1]-b[i]), AbsInt(a[i-1]-a[i])+AbsInt(b[i-1]-b[i]))
	}
	Out(ans, "\n")
}

func Solve() {
	for T := NextInt(); T > 0; T-- {
		ss()
	}
}

// util
var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func In(a ...interface{}) {
	_, _ = fmt.Fscan(in, a...)
}

func main() {
	Solve()
	out.Flush()
}

func NextInt() int {
	var x int
	_, _ = fmt.Fscan(in, &x)
	return x
}

func Out(a ...interface{}) {
	_, _ = fmt.Fprint(out, a...)
}

func Printf(format string, a ...interface{}) {
	_, _ = fmt.Fprintf(out, format, a...)
}

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

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

func AbsInt(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

package main

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

func main() {

	reader := bufio.NewReader(os.Stdin)
	writter := bufio.NewWriter(os.Stdout)
	defer writter.Flush()

	t := 0
	fmt.Fscan(reader, &t)

	for t > 0 {
		n := 0
		fmt.Fscan(reader, &n)

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

		for i := range a {
			fmt.Fscan(reader, &a[i])
		}

		for i := range b {
			fmt.Fscan(reader, &b[i])
		}

		// for i := range result {
		// 	fmt.Fprintf(writter, "%d ", result[i])
		// }
		// fmt.Fprintln(writter)

		fmt.Fprintln(writter, solve(n, a, b))

		t--
	}

	// n := 6
	// a := []int{72, 101, 108, 108, 111, 44}
	// b := []int{10, 87, 111, 114, 108, 100}

	// fmt.Println(solve(n, a, b))

	//ethereum.Ethereum()

}

func solve(n int, a, b []int) int {

	sum := 0

	for i := range a {
		if a[i] > b[i] {
			a[i], b[i] = b[i], a[i]
		}
	}

	// fmt.Println(a)
	// fmt.Println(b)

	for i := 1; i < n; i++ {
		sum += int(math.Abs(float64(a[i] - a[i-1])))
	}

	for i := 1; i < n; i++ {
		sum += int(math.Abs(float64(b[i] - b[i-1])))
	}

	return sum
}

package main

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

func main() {

	reader := bufio.NewReader(os.Stdin)
	writter := bufio.NewWriter(os.Stdout)
	defer writter.Flush()

	t := 0
	fmt.Fscan(reader, &t)

	for t > 0 {
		n := 0
		fmt.Fscan(reader, &n)

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

		for i := range a {
			fmt.Fscan(reader, &a[i])
		}

		for i := range b {
			fmt.Fscan(reader, &b[i])
		}

		// for i := range result {
		// 	fmt.Fprintf(writter, "%d ", result[i])
		// }
		// fmt.Fprintln(writter)

		fmt.Fprintln(writter, solve(n, a, b))

		t--
	}

	// n := 6
	// a := []int{72, 101, 108, 108, 111, 44}
	// b := []int{10, 87, 111, 114, 108, 100}

	// fmt.Println(solve(n, a, b))

	//ethereum.Ethereum()

}

func solve(n int, a, b []int) float64 {

	sum := float64(0)

	for i := range a {
		if a[i] > b[i] {
			a[i], b[i] = b[i], a[i]
		}
	}

	// fmt.Println(a)
	// fmt.Println(b)

	for i := 1; i < n; i++ {
		sum += math.Abs(float64(a[i] - a[i-1]))
	}

	for i := 1; i < n; i++ {
		sum += math.Abs(float64(b[i] - b[i-1]))
	}

	return sum
}

package main

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

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

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

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

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

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

/**
Run solve the problem CF1661A
Date: 19/05/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1661/A
Problem: CF1661A
**/
func (in *CF1661A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		a := make([]int, n)
		for i := 0; i < n; i++ {
			a[i] = in.NextInt()
		}

		b := make([]int, n)
		for i := 0; i < n; i++ {
			b[i] = in.NextInt()
		}

		sum := 0
		for i := 0; i < n; i++ {
			if a[i] > b[i] {
				a[i], b[i] = b[i], a[i]
			}

		}

		for i:=1;i<n;i++{
			sum += int(math.Abs(float64(a[i-1])-float64(a[i])))
			sum += int(math.Abs(float64(b[i-1])-float64(b[i])))
		}
		fmt.Println(sum)
	}
}

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

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

package main

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

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

func main() {
	defer out.Flush()

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

	for ; t > 0; t-- {
		var n int
		fmt.Fscan(in, &n)

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

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

		ans := 0

		for i := 1; i < n; i++ {
			if abs(a[i-1]-a[i])+abs(b[i-1]-b[i]) <= abs(a[i-1]-b[i])+abs(b[i-1]-a[i]) {
				ans += abs(a[i-1]-a[i]) + abs(b[i-1]-b[i])
			} else {
				ans += abs(a[i-1]-b[i]) + abs(b[i-1]-a[i])
			}
		}

		fmt.Fprintln(out, ans)
	}
}

package main

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

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}
func readInt64(in *bufio.Reader) int64 {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.ParseInt(nStr, 10, 64)
	return n
}
func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}
func readArrInt64(in *bufio.Reader) []int64 {
	numbs := readLineNumbs(in)
	arr := make([]int64, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.ParseInt(n, 10, 64)
		arr[i] = val
	}
	return arr
}

func main() {
	ip := bufio.NewReader(os.Stdin)
	t := readInt(ip)
	for t > 0 {
		t--
		_ = readInt(ip)
		a := readArrInt(ip)
		b := readArrInt(ip)
		ans := 0
		for i := range a {
			if i == 0 {
				continue
			}
			ans += min(abs(a[i]-a[i-1])+abs(b[i]-b[i-1]), abs(a[i]-b[i-1])+abs(b[i]-a[i-1]))
		}
		fmt.Println(ans)
	}
}

func max(i, j int) int {
	if i > j {
		return i
	}
	return j
}

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

func reverseSlice(_slice []int) {
	n := len(_slice)
	for i := 0; i < n/2; i++ {
		_slice[i], _slice[n-i-1] = _slice[n-i-1], _slice[i]
	}
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}


Given a simple graph, output the number of simple cycles in it. A simple cycle is a cycle with no repeated vertices or edges.

The first line of input contains two integers n and m (1 ≤ n ≤ 19, 0 ≤ m) – respectively the number of vertices and edges of the graph. Each of the subsequent m lines contains two integers a and b, (1 ≤ a, b ≤ n, a ≠ b) indicating that vertices a and b are connected by an undirected edge. There is no more than one edge connecting any pair of vertices.

Output the number of cycles in the given graph.

NoteThe example graph is a clique and contains four cycles of length 3 and three cycles of length 4.

package main

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

func findStart(state int) int {
	for i := 0; ; i++ {
		if state&1 == 1 {
			return i
		} else {
			state = state >> 1
		}
	}
}

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

	var n, m int

	fmt.Fscanf(reader, "%d %d\n", &n, &m)

	graph := [19][19]bool{}

	state := 1 << uint(n)
	dp := [19][1 << 19]uint64{}

	for i := 0; i < m; i++ {
		var v0, v1 int
		fmt.Fscanf(reader, "%d %d\n", &v0, &v1)
		graph[v0-1][v1-1] = true
		graph[v1-1][v0-1] = true
	}
	for i := 0; i < n; i++ {
		dp[i][1<<uint(i)] = 1
	}
	var result uint64

	for i := 1; i < state; i++ {
		start := findStart(i)
		for j := start; j < n; j++ {
			if (i>>uint(j))&1 == 0 {
				continue
			}
			if graph[j][start] {
				result = result + dp[j][i]
			}
			for k := start + 1; k < n; k++ {
				if !graph[j][k] || j == k || ((i>>uint(k))&1) == 1 {
					continue
				}
				dp[k][i|(1<<uint(k))] += dp[j][i]
			}
		}
	}
	fmt.Printf("%d", (result-uint64(m))/2)
}