Hanh lives in a shared apartment. There are $$$n$$$ people (including Hanh) living there, each has a private fridge. $$$n$$$ fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of $$$n$$$ people can open it.    For exampe, in the picture there are $$$n=4$$$ people and $$$5$$$ chains. The first person knows passcodes of two chains: $$$1-4$$$ and $$$1-2$$$. The fridge $$$1$$$ can be open by its owner (the person $$$1$$$), also two people $$$2$$$ and $$$4$$$ (acting together) can open it. The weights of these fridges are $$$a_1, a_2, \ldots, a_n$$$. To make a steel chain connecting fridges $$$u$$$ and $$$v$$$, you have to pay $$$a_u + a_v$$$ dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges. Hanh's apartment landlord asks you to create exactly $$$m$$$ steel chains so that all fridges are private. A fridge is private if and only if, among $$$n$$$ people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge $$$i$$$ is not private if there exists the person $$$j$$$ ($$$i \ne j$$$) that the person $$$j$$$ can open the fridge $$$i$$$.For example, in the picture all the fridges are private. On the other hand, if there are $$$n=2$$$ fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly $$$m$$$ chains, and if yes, output any solution that minimizes the total cost.

Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \le T \le 10$$$). Then the descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \le n \le 1000$$$, $$$1 \le m \le n$$$) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^4$$$) — weights of all fridges.

For each test case:   If there is no solution, print a single integer $$$-1$$$.  Otherwise, print a single integer $$$c$$$ — the minimum total cost. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$), meaning that the $$$i$$$-th steel chain connects fridges $$$u_i$$$ and $$$v_i$$$. An arbitrary number of chains can be between a pair of fridges.  If there are multiple answers, print any.

package main

import (
	"fmt"
)

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

	for fmt.Scan(&t); t > 0; t-- {
		fmt.Scan(&n, &m)
		a := make([]int, n)
		sum := 0
		for i := 0; i < n; i++ {
			fmt.Scan(&a[i])
			sum += 2 * a[i]
		}
		if (m < n || n == 2) {
			fmt.Println(-1)
		} else {
			fmt.Println(sum)
			for i := 0; i < n; i++ {
				fmt.Println(i+1, (i+1)%n+1)
			}
		}
	}
}

package main

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

var scanner *bufio.Scanner
var out *bufio.Writer

func init() {
	scanner = bufio.NewScanner(bufio.NewReader(os.Stdin))
	//scanner.Buffer(make([]byte, 311000), 311000)
	scanner.Split(bufio.ScanWords)
	out = bufio.NewWriterSize(os.Stdout, 10000)
}

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

func nextInt() int {
	txt := next()
	num, err := strconv.Atoi(txt)
	if err != nil {
		panic(fmt.Errorf("Atoi(%q) returned error: %v", txt, err))
	}
	return num
}

func nextLong() int64 {
	txt := next()
	num, err := strconv.ParseInt(txt, 10, 64)
	if err != nil {
		panic(fmt.Errorf("ParseInt(%q, 10, 64) returned error: %v", txt, err))
	}
	return num
}

// ========================================================

func main() {
	defer out.Flush()
	numCases := nextInt()
	for caseId := 1; caseId <= numCases; caseId++ {
		n, m := nextInt(), nextInt()
		var sum int64
		for i := 0; i < n; i++ {
			sum += int64(nextInt())
		}
		if m < n || n < 3 {
			fmt.Fprintln(out, "-1")
			continue
		}
		fmt.Fprintln(out, sum*2)
		fmt.Fprintln(out, n, 1)
		for i := 1; i < n; i++ {
			fmt.Fprintln(out, i, i+1)
		}

	}
}

package main

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

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

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

func solve(n, m int, costs []int) {
	if m < n || n <= 2 {
		printf("-1\n")
		return
	}

	sort.Ints(costs)
	sum := 0
	for _, v := range costs {
		sum += v
	}

	printf("%d\n", sum*2)
	for i := 1; i < n; i++ {
		printf("%d %d\n", i, i+1)
	}
	printf("%d %d\n", n, 1)
}

func main() {
	defer writer.Flush()
	tt := 0
	scanf("%d\n", &tt)
	for i := 0; i < tt; i++ {
		var n, m int
		scanf("%d %d\n", &n, &m)
		costs := make([]int, n)
		for j := 0; j < n; j++ {
			scanf("%d", &costs[j])
		}
		scanf("\n")
		solve(n, m, costs)
	}
}

package main

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

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

	return s
}

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

	if e == 0 {
		return 1
	}

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

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

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

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

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

	return a
}

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

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

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

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

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

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

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

	return string(res)
}

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

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

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

	return string(res)
}

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

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

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

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

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

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

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

var t int
var n, m int
var A []int

type Edge struct {
	key          int
	nodeId, cost int
}
type EdgeList []*Edge
type byKey struct {
	EdgeList
}

func (l EdgeList) Len() int {
	return len(l)
}
func (l EdgeList) Swap(i, j int) {
	l[i], l[j] = l[j], l[i]
}

func (l byKey) Less(i, j int) bool {
	return l.EdgeList[i].key < l.EdgeList[j].key
}

// how to use
// L := make(EdgeList, 0, 200000+5)
// L = append(L, &Edge{key: intValue})
// sort.Stable(byKey{ L })                // Stable ASC
// sort.Stable(sort.Reverse(byKey{ L }))  // Stable DESC

func main() {
	t = ReadInt()

	for tc := 0; tc < t; tc++ {
		n, m = ReadInt2()
		A = ReadIntSlice(n)

		solve()
	}
}

func solve() {
	if m < n {
		fmt.Println(-1)
		return
	}

	if n == 2 {
		fmt.Println(-1)
		return
	}

	sumA := Sum(A...)
	cost := 2 * sumA

	L := make(EdgeList, 0)
	for i := 0; i < n; i++ {
		L = append(L, &Edge{key: A[i], nodeId: i + 1, cost: A[i]})
	}
	sort.Stable(byKey{L})

	mm := m - n
	cost += mm * (L[0].cost + L[1].cost)

	answers := [][]int{}
	for i := 1; i <= n; i++ {
		if i == n {
			answers = append(answers, []int{i, 1})
		} else {
			answers = append(answers, []int{i, i + 1})
		}
	}
	for i := 0; i < mm; i++ {
		answers = append(answers, []int{L[0].nodeId, L[1].nodeId})
	}

	fmt.Println(cost)
	for i := 0; i < m; i++ {
		fmt.Printf("%d %d\n", answers[i][0], answers[i][1])
	}
}

/*
- MODは最後にとりましたか？
- ループを抜けた後も処理が必要じゃありませんか？
- 和・積・あまりを求められたらint64が必要ではありませんか？
*/

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

package main

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

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

	var t, n, m int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &m)
		a := make([]int, n)
		sum := 0
		for i := range a {
			Fscan(in, &a[i])
			sum += a[i] * 2
		}
		if n == 2 || n > m {
			Fprintln(out, -1)
			continue
		}
		Fprintln(out, sum)
		for i := 1; i < n; i++ {
			Fprintln(out, i, i+1)
		}
		Fprintln(out, n, 1)
	}
}

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

package main

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

type node struct {
	i, val int
}

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

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

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

		ns := make([]node, n)
		sum := 0
		for j := 0; j < n; j++ {
			ns[j].i = j + 1
			fmt.Fscan(in, &ns[j].val)
			sum += 2 * ns[j].val
		}

		if m < n || n == 2 {
			fmt.Fprintln(out, -1)
			continue
		}

		sort.Slice(ns, func(l, r int) bool { return ns[l].val < ns[r].val })
		sum += (m - n) * (ns[0].val + ns[1].val)

		fmt.Fprintln(out, sum)

		for j := 0; j < n-1; j++ {
			fmt.Fprintln(out, j+1, j+2)
		}
		fmt.Fprintln(out, 1, n)

		for j := m - n; j > 0; j-- {
			fmt.Fprintln(out, ns[0].i, ns[1].i)
		}

		/*
		for _, v := range ns {
			fmt.Println("Got:", v)
		}
		fmt.Println("End")
		*/
	}
}

package main

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

type node struct {
	i, val int
}

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

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

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

		ns := make([]node, n)
		sum := 0
		for j := 0; j < n; j++ {
			ns[j].i = j + 1
			fmt.Fscan(in, &ns[j].val)
			sum += 2 * ns[j].val
		}

		if m < n {
			fmt.Fprintln(out, -1)
			continue
		}

		sort.Slice(ns, func(l, r int) bool { return ns[l].val < ns[r].val })
		sum += (m - n) * (ns[0].val + ns[1].val)

		fmt.Fprintln(out, sum)

		for j := 0; j < n-1; j++ {
			fmt.Fprintln(out, j+1, j+2)
		}
		fmt.Fprintln(out, 1, n)

		for j := m - n; j > 0; j-- {
			fmt.Fprintln(out, ns[0].i, ns[1].i)
		}

		/*
		for _, v := range ns {
			fmt.Println("Got:", v)
		}
		fmt.Println("End")
		*/
	}
}

package main

import (
	"fmt"
)

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

	for fmt.Scan(&t); t > 0; t-- {
		fmt.Scan(&n, &m)
		a := make([]int, n)
		sum := 0
		for i := 0; i < n; i++ {
			fmt.Scan(&a[i])
			sum += 2 * a[i]
		}
		fmt.Println(sum)
		for i := 0; i < n; i++ {
			fmt.Println(i+1, (i+1)%n+1)
		}
	}
}

package main

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

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

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

func solve(n, m int, costs []int) {
	if m < n-1 || n <= 2 {
		printf("-1\n")
		return
	}
	rst := 0
	for _, v := range costs {
		rst += v
	}
	printf("%d\n", rst * 2)
	for i := 1; i < n; i++ {
		printf("%d %d\n", i, i+1)
	}
	printf("%d %d\n", n, 1)
}

func main() {
	defer writer.Flush()
	tt := 0
	scanf("%d\n", &tt)
	for i := 0; i < tt; i++ {
		var n, m int
		scanf("%d %d\n", &n, &m)
		costs := make([]int, n)
		for j := 0; j < n; j++ {
			scanf("%d", &costs[j])
		}
		scanf("\n")
		solve(n, m, costs)
	}
}

package main

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

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

	return s
}

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

	if e == 0 {
		return 1
	}

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

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

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

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

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

	return a
}

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

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

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

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

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

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

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

	return string(res)
}

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

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

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

	return string(res)
}

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

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

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

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

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

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

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

var t int
var n, m int
var A []int

type Edge struct {
	key          int
	nodeId, cost int
}
type EdgeList []*Edge
type byKey struct {
	EdgeList
}

func (l EdgeList) Len() int {
	return len(l)
}
func (l EdgeList) Swap(i, j int) {
	l[i], l[j] = l[j], l[i]
}

func (l byKey) Less(i, j int) bool {
	return l.EdgeList[i].key < l.EdgeList[j].key
}

// how to use
// L := make(EdgeList, 0, 200000+5)
// L = append(L, &Edge{key: intValue})
// sort.Stable(byKey{ L })                // Stable ASC
// sort.Stable(sort.Reverse(byKey{ L }))  // Stable DESC

func main() {
	t = ReadInt()

	for tc := 0; tc < t; tc++ {
		n, m = ReadInt2()
		A = ReadIntSlice(n)

		solve()
	}
}

func solve() {
	if m < n {
		fmt.Println(-1)
		return
	}

	sumA := Sum(A...)
	cost := 2 * sumA

	L := make(EdgeList, 0)
	for i := 0; i < n; i++ {
		L = append(L, &Edge{key: A[i], nodeId: i + 1, cost: A[i]})
	}
	sort.Stable(byKey{L})

	mm := m - n
	cost += mm * (L[0].cost + L[1].cost)

	answers := [][]int{}
	for i := 1; i <= n; i++ {
		if i == n {
			answers = append(answers, []int{i, 1})
		} else {
			answers = append(answers, []int{i, i + 1})
		}
	}
	for i := 0; i < mm; i++ {
		answers = append(answers, []int{L[0].nodeId, L[1].nodeId})
	}

	fmt.Println(cost)
	for i := 0; i < m; i++ {
		fmt.Printf("%d %d\n", answers[i][0], answers[i][1])
	}
}

/*
- MODは最後にとりましたか？
- ループを抜けた後も処理が必要じゃありませんか？
- 和・積・あまりを求められたらint64が必要ではありませんか？
*/

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


High school student Vasya got a string of length n as a birthday present. This string consists of letters 'a' and 'b' only. Vasya denotes beauty of the string as the maximum length of a substring (consecutive subsequence) consisting of equal letters.Vasya can change no more than k characters of the original string. What is the maximum beauty of the string he can achieve?

The first line of the input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n) — the length of the string and the maximum number of characters to change. The second line contains the string, consisting of letters 'a' and 'b' only.

Print the only integer — the maximum beauty of the string Vasya can achieve by changing no more than k characters.

NoteIn the first sample, Vasya can obtain both strings "aaaa" and "bbbb".In the second sample, the optimal answer is obtained with the string "aaaaabaa" or with the string "aabaaaaa".

package main

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

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

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

func main() {
	defer writer.Flush()
	var n, c int
	scanf("%d %d\n", &n, &c)
	var s string
	scanf("%s\n", &s)
	//printf("%d %d %s\n", n, c, s)
	alphabet := [2]byte{'a', 'b'}
	ans := 0
	for i := 0; i < len(alphabet); i++ {
		bad := 0
		j := 0
		k := 0
		for k = 0; k < len(s); k++ {
			if s[k] != alphabet[i] {
				bad++
			}
			if bad > c {
				for j <= k && s[j] == alphabet[i] {
					j++
				}
				bad--
				j++
			}
			if k-j+1 > ans {
				ans = k - j + 1
			}
		}
	}
	printf("%d\n", ans)
}

package main

import (
	"fmt"
	"os"
)

func max(seqs []int, joins int) int {
	if joins >= len(seqs)-1 {
		sum := -1
		for i := 0; i < len(seqs); i++ {
			sum += 1 + seqs[i]
		}
		return sum
	}
	// joins < len(seqs)
	max := -1
	for i := 0; i < joins+1; i++ {
		max += 1 + seqs[i]
	}
	sum := max
	for i := joins + 1; i < len(seqs); i++ {
		sum += seqs[i] - seqs[i-joins-1]
		if sum > max {
			max = sum
		}
	}
	return max
}

func solve(s string, chg int) int {
	n := len(s)
	var soa, sob []int // sequences of a's / b's
	c, cPrev := byte(0), byte(0)
	for i := 0; i < n; i++ {
		cPrev, c = c, s[i]
		if c == cPrev {
			if c == 'a' {
				soa[len(soa)-1]++
				sob = append(sob, 0)
			} else {
				sob[len(sob)-1]++
				soa = append(soa, 0)
			}
		} else {
			if c == 'a' {
				soa = append(soa, 1)
			} else {
				sob = append(sob, 1)
			}
		}
	}
	if s[0] == 'a' {
		sob = append([]int{0}, sob...)
	} else {
		soa = append([]int{0}, soa...)
	}
	if c == 'a' {
		sob = append(sob, 0)
	} else {
		soa = append(soa, 0)
	}
	soaMax := max(soa, chg)
	sobMax := max(sob, chg)
	if soaMax > sobMax {
		return soaMax
	}
	return sobMax
}

func main() {
	var n, k int
	nr, err := fmt.Fscanln(os.Stdin, &n, &k)
	if nr != 2 || err != nil {
		panic(err)
	}
	var s string
	nr, err = fmt.Fscanln(os.Stdin, &s)
	if nr != 1 || err != nil {
		panic(err)
	}
	res := solve(s, k)
	fmt.Println(res)
}


There are n cities numbered from 1 to n in Berland. Some of them are connected by two-way roads. Each road has its own length — an integer number from 1 to 1000. It is known that from each city it is possible to get to any other city by existing roads. Also for each pair of cities it is known the shortest distance between them. Berland Government plans to build k new roads. For each of the planned road it is known its length, and what cities it will connect. To control the correctness of the construction of new roads, after the opening of another road Berland government wants to check the sum of the shortest distances between all pairs of cities. Help them — for a given matrix of shortest distances on the old roads and plans of all new roads, find out how the sum of the shortest distances between all pairs of cities changes after construction of each road.

The first line contains integer n (2 ≤ n ≤ 300) — amount of cities in Berland. Then there follow n lines with n integer numbers each — the matrix of shortest distances. j-th integer in the i-th row — di, j, the shortest distance between cities i and j. It is guaranteed that di, i = 0, di, j = dj, i, and a given matrix is a matrix of shortest distances for some set of two-way roads with integer lengths from 1 to 1000, such that from each city it is possible to get to any other city using these roads. Next line contains integer k (1 ≤ k ≤ 300) — amount of planned roads. Following k lines contain the description of the planned roads. Each road is described by three space-separated integers ai, bi, ci (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ ci ≤ 1000) — ai and bi — pair of cities, which the road connects, ci — the length of the road. It can be several roads between a pair of cities, but no road connects the city with itself.

Output k space-separated integers qi (1 ≤ i ≤ k). qi should be equal to the sum of shortest distances between all pairs of cities after the construction of roads with indexes from 1 to i. Roads are numbered from 1 in the input order. Each pair of cities should be taken into account in the sum exactly once, i. e. we count unordered pairs.

//http://codeforces.com/contest/25/problem/C
//C. Roads in Berland

package main

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

var dist [301][301]int32

func main() {
	var (
		n, i, j, q, a, b, dab int32
		s                     int64
	)

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

	fmt.Fscan(in, &n)

	for i = 1; i <= n; i++ {
		for j = 1; j <= n; j++ {
			fmt.Fscan(in, &dist[i][j])
			s += int64(dist[i][j])
		}
	}
	s /= 2

	fmt.Fscan(in, &q)
	for ; q > 0; q-- {
		fmt.Fscan(in, &a)
		fmt.Fscan(in, &b)
		fmt.Fscan(in, &dab)
		if dab < dist[a][b] {
			s = 0
			for i = 1; i < n; i++ {
				for j = i + 1; j <= n; j++ {
					if dist[i][a]+dab+dist[b][j] < dist[i][j] {
						dist[i][j] = dist[i][a] + dab + dist[b][j]
						dist[j][i] = dist[i][j]
					}
					if dist[i][b]+dab+dist[a][j] < dist[i][j] {
						dist[i][j] = dist[i][b] + dab + dist[a][j]
						dist[j][i] = dist[i][j]
					}
					s += int64(dist[i][j])
				}
			}
		}
		fmt.Fprint(out, s)
		fmt.Fprint(out, " ")

	}
	fmt.Fprintln(out)
	out.Flush()

}

package main

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

var r *bufio.Reader = bufio.NewReader(os.Stdin)
var w *bufio.Writer = bufio.NewWriter(os.Stdout)

var n int
var mat [][]int

func fw() {
        var nc int
        for u := 0; u < n; u++ {
                for v := 0; v < n; v++ {
                        for k := 0; k < n; k++ {
                                nc = mat[u][v] + mat[v][k]
                                if nc < mat[u][k] {
                                        mat[k][u], mat[u][k] = nc, nc
                                }
                        }
                }
        }
}

func sum() int64 {
        var s int64
        for u := 0; u < n - 1; u++ {
                for v := u + 1; v < n; v++ {
                        s += int64(mat[u][v])
                }
        }
        return s
}

func main() {
        defer w.Flush()

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

        s := sum()
        var t int
        fmt.Fscanf(r, "%d\n", &t)
        var i, j, c int
        var nc int
        for ; t > 0; t-- {
                fmt.Fscanf(r, "%d %d %d\n", &i, &j, &c)
                i -= 1
                j -= 1
                if mat[i][j] > c {
                        mat[i][j] = c
                        mat[j][i] = c

                        for u := 0; u < n; u++ {
                                for v := 0; v < n; v++ {
                                        nc = mat[u][i] + c + mat[j][v]
                                        if mat[u][j] + c + mat[i][v] < nc {
                                                nc = mat[u][j] + c + mat[j][v]
                                        }
                                        if nc < mat[u][v] {
                                                mat[u][v], mat[v][u] = nc, nc
                                        }
                                }
                        }
                }

                s = sum()
                fmt.Fprintf(w, "%v ", s)
        }
}

//http://codeforces.com/contest/25/problem/C
//C. Roads in Berland

package main

import (
	"fmt"
)

var dist [301][301]int16

func main() {
	var (
		n, i, j, q, a, b, dab, s int16
	)

	fmt.Scan(&n)

	for i = 1; i <= n; i++ {
		for j = 1; j <= n; j++ {
			fmt.Scan(&dist[i][j])
			s += dist[i][j]
		}
	}
	s /= 2

	fmt.Scan(&q)
	for ; q > 0; q-- {
		fmt.Scan(&a)
		fmt.Scan(&b)
		fmt.Scan(&dab)
		if dab < dist[a][b] {
			s = 0
			for i = 1; i < n; i++ {
				for j = i + 1; j <= n; j++ {
					if dist[i][a]+dab+dist[b][j] < dist[i][j] {
						dist[i][j] = dist[i][a] + dab + dist[b][j]
						dist[j][i] = dist[i][j]
					}
					if dist[i][b]+dab+dist[a][j] < dist[i][j] {
						dist[i][j] = dist[i][b] + dab + dist[a][j]
						dist[j][i] = dist[i][j]
					}
					s += dist[i][j]
				}
			}
		}
		fmt.Print(s, " ")

	}
	fmt.Println()

}

//http://codeforces.com/contest/25/problem/C
//C. Roads in Berland

package main

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

var dist [301][301]int32

func main() {
	var (
		n, i, j, q, a, b, dab, s int32
	)

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

	fmt.Fscan(in, &n)

	for i = 1; i <= n; i++ {
		for j = 1; j <= n; j++ {
			fmt.Fscan(in, &dist[i][j])
			s += dist[i][j]
		}
	}
	s /= 2

	fmt.Fscan(in, &q)
	for ; q > 0; q-- {
		fmt.Fscan(in, &a)
		fmt.Fscan(in, &b)
		fmt.Fscan(in, &dab)
		if true || dab < dist[a][b] {
			s = 0
			for i = 1; i <= n; i++ {
				for j = 1; j <= n; j++ {
					if dist[i][a]+dab+dist[b][j] < dist[i][j] {
						dist[i][j] = dist[i][a] + dab + dist[b][j]
						dist[j][i] = dist[i][j]
					}
					if dist[i][b]+dab+dist[a][j] < dist[i][j] {
						dist[i][j] = dist[i][b] + dab + dist[a][j]
						dist[j][i] = dist[i][j]
					}
					s += dist[i][j]
				}
			}
		}
		fmt.Fprint(out, s/2)
		fmt.Fprint(out, " ")

	}
	fmt.Fprintln(out)
	out.Flush()

}

//http://codeforces.com/contest/25/problem/C
//C. Roads in Berland

package main

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

var dist [301][301]int32

func main() {
	var (
		n, i, j, q, a, b, dab, s int32
	)

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

	fmt.Fscan(in, &n)

	for i = 1; i <= n; i++ {
		for j = 1; j <= n; j++ {
			fmt.Fscan(in, &dist[i][j])
			s += dist[i][j]
		}
	}
	s /= 2

	fmt.Fscan(in, &q)
	for ; q > 0; q-- {
		fmt.Fscan(in, &a)
		fmt.Fscan(in, &b)
		fmt.Fscan(in, &dab)
		s = 0
		for i = 1; i < n; i++ {
			for j = i + 1; j <= n; j++ {
				if dist[i][a]+dab+dist[b][j] < dist[i][j] {
					dist[i][j] = dist[i][a] + dab + dist[b][j]
					dist[j][i] = dist[i][j]
				}
				if dist[i][b]+dab+dist[a][j] < dist[i][j] {
					dist[i][j] = dist[i][b] + dab + dist[a][j]
					dist[j][i] = dist[i][j]
				}
				s += dist[i][j]
			}
		}

		fmt.Fprint(out, s)
		fmt.Fprint(out, " ")

	}
	fmt.Fprintln(out)
	out.Flush()

}

//http://codeforces.com/contest/25/problem/C
//C. Roads in Berland

package main

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

var dist [301][301]int16

func main() {
	var (
		n, i, j, q, a, b, dab int16
		s                     int32 // int16 overflow :)
	)

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

	fmt.Fscan(in, &n)

	for i = 1; i <= n; i++ {
		for j = 1; j <= n; j++ {
			fmt.Fscan(in, &dist[i][j])
			s += int32(dist[i][j])
		}
	}
	s /= 2

	fmt.Fscan(in, &q)
	for ; q > 0; q-- {
		fmt.Fscan(in, &a)
		fmt.Fscan(in, &b)
		fmt.Fscan(in, &dab)
		if dab < dist[a][b] {
			s = 0
			for i = 1; i < n; i++ {
				for j = i + 1; j <= n; j++ {
					if dist[i][a]+dab+dist[b][j] < dist[i][j] {
						dist[i][j] = dist[i][a] + dab + dist[b][j]
						dist[j][i] = dist[i][j]
					}
					if dist[i][b]+dab+dist[a][j] < dist[i][j] {
						dist[i][j] = dist[i][b] + dab + dist[a][j]
						dist[j][i] = dist[i][j]
					}
					s += int32(dist[i][j])
				}
			}
		}
		fmt.Fprint(out, s)
		fmt.Fprint(out, " ")

	}
	fmt.Fprintln(out)
	out.Flush()

}

//http://codeforces.com/contest/25/problem/C
//C. Roads in Berland

package main

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

var dist [301][301]int32

func main() {
	var (
		n, i, j, q, a, b, dab, s int32
	)

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

	fmt.Fscan(in, &n)

	for i = 1; i <= n; i++ {
		for j = 1; j <= n; j++ {
			fmt.Fscan(in, &dist[i][j])
			s += dist[i][j]
		}
	}
	s /= 2

	fmt.Fscan(in, &q)
	for ; q > 0; q-- {
		fmt.Fscan(in, &a)
		fmt.Fscan(in, &b)
		fmt.Fscan(in, &dab)
		if dab >= dist[a][b] {
			continue
		}
		s = 0
		for i = 1; i < n; i++ {
			for j = i + 1; j <= n; j++ {
				if dist[i][a]+dab+dist[b][j] < dist[i][j] {
					dist[i][j] = dist[i][a] + dab + dist[b][j]
					dist[j][i] = dist[i][j]
				}
				if dist[i][b]+dab+dist[a][j] < dist[i][j] {
					dist[i][j] = dist[i][b] + dab + dist[a][j]
					dist[j][i] = dist[i][j]
				}
				s += dist[i][j]
			}
		}

		fmt.Fprint(out, s)
		fmt.Fprint(out, " ")

	}
	fmt.Fprintln(out)
	out.Flush()

}

//http://codeforces.com/contest/25/problem/C
//C. Roads in Berland

package main

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

var dist [301][301]int32

func main() {
	var (
		n, i, j, q, a, b, dab, s int32
	)

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

	fmt.Fscan(in, &n)

	for i = 1; i <= n; i++ {
		for j = 1; j <= n; j++ {
			fmt.Fscan(in, &dist[i][j])
			s += dist[i][j]
		}
	}
	s /= 2

	fmt.Fscan(in, &q)
	for ; q > 0; q-- {
		fmt.Fscan(in, &a)
		fmt.Fscan(in, &b)
		fmt.Fscan(in, &dab)
		if dab < dist[a][b] {
			s = 0
			for i = 1; i < n; i++ {
				for j = i + 1; j <= n; j++ {
					if dist[i][a]+dab+dist[b][j] < dist[i][j] {
						dist[i][j] = dist[i][a] + dab + dist[b][j]
						dist[j][i] = dist[i][j]
					}
					if dist[i][b]+dab+dist[a][j] < dist[i][j] {
						dist[i][j] = dist[i][b] + dab + dist[a][j]
						dist[j][i] = dist[i][j]
					}
					s += dist[i][j]
				}
			}
		}
		fmt.Fprint(out, s)
		fmt.Fprint(out, " ")

	}
	fmt.Fprintln(out)
	out.Flush()

}

package main

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

var r *bufio.Reader = bufio.NewReader(os.Stdin)
var w *bufio.Writer = bufio.NewWriter(os.Stdout)

var n int
var mat [][]int

func fw(mat[][]int) int {
        var nc int
        for u := 0; u < n; u++ {
                for v := 0; v < n; v++ {
                        for k := 0; k < n; k++ {
                                nc = mat[u][v] + mat[v][k]
                                if nc < mat[u][k] {
                                        mat[k][u], mat[u][k] = nc, nc
                                }
                        }
                }
        }

        s := 0
        for u := 0; u < n - 1; u++ {
                for v := u + 1; v < n; v++ {
                        s += mat[u][v]
                }
        }
        return s
}

func main() {
        defer w.Flush()

        fmt.Fscanf(r, "%d\n", &n)
        mat = make([][]int, n)
        for i := 0; i < n; i++ {
                mat[i] = make([]int, n)
                for j := 0; j < n - 1; j++ {
                        fmt.Fscanf(r, "%d ", &mat[i][j])
                }
                fmt.Fscanf(r, "%d\n", &mat[i][n-1])
        }
        s := fw(mat)

        var t int
        fmt.Fscanf(r, "%d\n", &t)
        var u, v, c int
        var nc int
        for ; t > 0; t-- {
                fmt.Fscanf(r, "%d %d %d\n", &u, &v, &c)
                u -= 1
                v -= 1
                if mat[u][v] > c {
                        s -= mat[u][v] - c
                        mat[u][v] = c
                        mat[v][u] = c
                        for k := 0; k < n; k++ {
                                nc = mat[u][v] + mat[v][k]
                                if nc < mat[u][k] {
                                        s -= mat[u][k] - nc
                                        mat[k][u], mat[u][k] = nc, nc
                                }

                                nc = mat[v][u] + mat[u][k]
                                if nc < mat[v][k] {
                                        s -= mat[v][k] - nc
                                        mat[k][v], mat[v][k] = nc, nc
                                }
                        }
                }

                fmt.Fprintf(w, "%v ", s)
        }
}

package main

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

var r *bufio.Reader = bufio.NewReader(os.Stdin)
var w *bufio.Writer = bufio.NewWriter(os.Stdout)

var n int
var mat [][]int

func fw() {
        var nc int
        for u := 0; u < n; u++ {
                for v := 0; v < n; v++ {
                        for k := 0; k < n; k++ {
                                nc = mat[u][v] + mat[v][k]
                                if nc < mat[u][k] {
                                        mat[k][u], mat[u][k] = nc, nc
                                }
                        }
                }
        }
}

func sum() int {
        s := 0
        for u := 0; u < n - 1; u++ {
                for v := u + 1; v < n; v++ {
                        s += mat[u][v]
                }
        }
        return s
}

func main() {
        defer w.Flush()

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

        s := sum()
        var t int
        fmt.Fscanf(r, "%d\n", &t)
        var i, j, c int
        var nc int
        for ; t > 0; t-- {
                fmt.Fscanf(r, "%d %d %d\n", &i, &j, &c)
                i -= 1
                j -= 1
                if mat[i][j] > c {
                        mat[i][j] = c
                        mat[j][i] = c

                        for u := 0; u < n; u++ {
                                for v := 0; v < n; v++ {
                                        nc = mat[u][i] + c + mat[j][v]
                                        if mat[u][j] + c + mat[i][v] < nc {
                                                nc = mat[u][j] + c + mat[j][v]
                                        }
                                        if nc < mat[u][v] {
                                                mat[u][v], mat[v][u] = nc, nc
                                        }
                                }
                        }
                }

                s = sum()
                fmt.Fprintf(w, "%v ", s)
        }
}

package main

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

var r *bufio.Reader = bufio.NewReader(os.Stdin)
var w *bufio.Writer = bufio.NewWriter(os.Stdout)

var n int
var mat [][]int

func fw(mat[][]int) int {
        var nc int
        for u := 0; u < n; u++ {
                for v := 0; v < n; v++ {
                        for k := 0; k < n; k++ {
                                nc = mat[u][v] + mat[v][k]
                                if nc < mat[u][k] {
                                        mat[k][u], mat[u][k] = nc, nc
                                }
                        }
                }
        }

        s := 0
        for u := 0; u < n - 1; u++ {
                for v := u + 1; v < n; v++ {
                        s += mat[u][v]
                }
        }
        return s
}

func main() {
        defer w.Flush()

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

        var t int
        var s int
        fmt.Fscanf(r, "%d\n", &t)
        var u, v, c int
        for ; t > 0; t-- {
                fmt.Fscanf(r, "%d %d %d\n", &u, &v, &c)
                u -= 1
                v -= 1
                if mat[u][v] > c {
                        s -= mat[u][v] - c
                        mat[u][v] = c
                        mat[v][u] = c
                        s = fw(mat)
                }

                fmt.Fprintf(w, "%v ", s)
        }
}

package main

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

var r *bufio.Reader = bufio.NewReader(os.Stdin)
var w *bufio.Writer = bufio.NewWriter(os.Stdout)

var n int
var mat [][]int

func fw() int {
        var nc int
        for u := 0; u < n; u++ {
                for v := 0; v < n; v++ {
                        for k := 0; k < n; k++ {
                                nc = mat[u][v] + mat[v][k]
                                if nc < mat[u][k] {
                                        mat[k][u], mat[u][k] = nc, nc
                                }
                        }
                }
        }

        s := 0
        for u := 0; u < n - 1; u++ {
                for v := u + 1; v < n; v++ {
                        s += mat[u][v]
                }
        }
        return s
}

func main() {
        defer w.Flush()

        fmt.Fscanf(r, "%d\n", &n)
        mat = make([][]int, n)
        for i := 0; i < n; i++ {
                mat[i] = make([]int, n)
                for j := 0; j < n - 1; j++ {
                        fmt.Fscanf(r, "%d ", &mat[i][j])
                }
                fmt.Fscanf(r, "%d\n", &mat[i][n-1])
        }
        s := fw()

        var t int
        fmt.Fscanf(r, "%d\n", &t)
        var u, v, c int
        var nc int
        for ; t > 0; t-- {
                fmt.Fscanf(r, "%d %d %d\n", &u, &v, &c)
                u -= 1
                v -= 1
                if mat[u][v] > c {
                        s -= mat[u][v] - c
                        mat[u][v] = c
                        mat[v][u] = c

                        for k := 0; k < n; k++ {
                                nc = mat[u][v] + mat[v][k]
                                if nc < mat[u][k] {
                                        s -= mat[u][k] - nc
                                        mat[u][k], mat[k][u] = nc, nc
                                }

                                nc = mat[v][u] + mat[u][k]
                                if nc < mat[v][k] {
                                        s -= mat[v][k] - nc
                                        mat[v][k], mat[k][v] = nc, nc
                                }
                        }
                }

                fmt.Fprintf(w, "%v ", s)
        }
}


You are given a binary string of length $$$n$$$. You have exactly $$$k$$$ moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped ($$$0$$$ becomes $$$1$$$, $$$1$$$ becomes $$$0$$$). You need to output the lexicographically largest string that you can get after using all $$$k$$$ moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length, if and only if the following holds:   in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ contains a $$$1$$$, and the string $$$b$$$ contains a $$$0$$$.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has two lines. The first line has two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq k \leq 10^9$$$). The second line has a binary string of length $$$n$$$, each character is either $$$0$$$ or $$$1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, output two lines. The first line should contain the lexicographically largest string you can obtain. The second line should contain $$$n$$$ integers $$$f_1, f_2, \ldots, f_n$$$, where $$$f_i$$$ is the number of times the $$$i$$$-th bit is selected. The sum of all the integers must be equal to $$$k$$$.

NoteHere is the explanation for the first testcase. Each step shows how the binary string changes in a move.  Choose bit $$$1$$$: $$$\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}$$$.  Choose bit $$$4$$$: $$$\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}$$$.  Choose bit $$$4$$$: $$$\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}$$$.  The final string is $$$111110$$$ and this is the lexicographically largest string we can get.

package main

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

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

func printAnswer(m map[int]int, bitsCount int) {
	for i := 0; i < bitsCount; i++ {
		if val, ok := m[i]; ok {
			fmt.Fprint(writer, val)
			fmt.Fprint(writer, " ")
		} else {
			fmt.Fprint(writer, "0 ")
		}
	}
	fmt.Fprintln(writer)
}

func flipFirstOne(bits []rune) int {
	i := 0

	for i+1 < len(bits) {
		if bits[i] == '1' {
			break
		}

		i += 1
	}

	if bits[i] == '1' {
		bits[i] = '0'
	} else {
		bits[i] = '1'
	}

	return i
}

func solve(movesCount int, bits []rune) {
	defer writer.Flush()
	answer := make(map[int]int)
	one := '1'

	if movesCount%2 == 1 {
		movesCount -= 1
		flippedPosition := flipFirstOne(bits)
		one = '0'
		answer[flippedPosition] = 1
	}

	i := 0
	for movesCount != 0 && i < len(bits) {
		/** Looking for the first zero */
		for i < len(bits) && bits[i] == one {
			fmt.Fprint(writer, "1")
			i += 1
		}

		if i >= len(bits) {
			break
		} else {
			if _, ok := answer[i]; ok {
				answer[i] += 1
			} else {
				answer[i] = 1
			}

			if i+1 == len(bits) {
				fmt.Fprint(writer, "0")
				if _, ok := answer[i]; ok {
					answer[i] += 1
				} else {
					answer[i] = 1
				}
				i += 1
				movesCount -= 2
				break
			} else {
				fmt.Fprint(writer, "1")
			}
		}

		i += 1

		/** Looking for the second zero */
		for i < len(bits) && bits[i] == one {
			if i+1 == len(bits) {
				break
			}

			fmt.Fprint(writer, "1")
			i += 1
		}

		if i >= len(bits) {
			break
		} else if i+1 == len(bits) {
			if bits[i] == one {
				fmt.Fprint(writer, "0")
			} else {
				fmt.Fprint(writer, "1")
			}
		} else {
			fmt.Fprint(writer, "1")
		}

		if _, ok := answer[i]; ok {
			answer[i] += 1
		} else {
			answer[i] = 1
		}

		i += 1
		movesCount -= 2
	}

	if i >= len(bits) {
		if _, ok := answer[len(bits)-1]; ok {
			answer[len(bits)-1] += movesCount
		} else {
			answer[len(bits)-1] = movesCount
		}
	} else {
		for i < len(bits) {
			if bits[i] == one {
				fmt.Fprint(writer, "1")
			} else {
				fmt.Fprint(writer, "0")
			}

			i += 1
		}
	}

	fmt.Fprintln(writer)
	printAnswer(answer, len(bits))
}

func main() {
	var t, n, k int
	var bitsString string
	var bits []rune
	fmt.Scan(&t)

	for i := 0; i < t; i++ {
		fmt.Fscan(reader, &n, &k)
		fmt.Fscan(reader, &bitsString)

		bits = []rune(bitsString)
		solve(k, bits)
	}
}

package main

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

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

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

	var T int
	var n int
	var k int
	var x string

	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &k, &x)
		ans := []byte{}
		se := make([]int, n)
		if k%2 == 0 {
			for i, v := range x {
				if v == '0' {
					if k == 0 {
						ans = append(ans, x[i:]...)
						break
					}
					if k > 0 {
						k--
						se[i]++
					}
				}
				ans = append(ans, '1')
			}
		} else {
			for i, v := range x {
				if k == 0 {
					if v == '1' {
						ans = append(ans, '0')
					} else {
						ans = append(ans, '1')
					}
				}
				if k > 0 {
					if v == '1' {
						se[i]++
						k--
					}
					ans = append(ans, '1')
				}
			}
		}

		se[0] += k - k%2
		if k > 0 && k%2 == 1 {
			se[n-1]++
			ans[n-1] = '0'
		}

		Fprintln(out, string(ans))
		for i, v := range se {
			if i > 0 {
				Fprintf(out, " ")
			}
			Fprintf(out, "%d", v)
		}
		Fprintln(out, "")
	}
}

package main

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

func main_div2_782_B() {
	_in, _out := os.Stdin, os.Stdout
	in, out := bufio.NewReader(_in), bufio.NewWriter(_out)
	defer out.Flush()
	var cas int = 1
	Fscan(in, &cas)
	var sol func()
	sol = func() {
		var n, k int
		Fscan(in, &n, &k)
		var s1 string
		Fscan(in, &s1)
		s := []byte(s1)
		ans := make([]int, n)
		if k == 0 {
			Fprintln(out, string(s))
			for i := 0; i < n; i++ {
				Fprint(out, ans[i], " ")
			}
			Fprintln(out)
			return
		}
		cnt := 0
		//var p = -1
		for i := 0; i < n; i++ {
			if cnt >= k {
				//	p = i
				break
			}
			if s[i] == '0' {
				if k&1 == 0 {
					ans[i] = 1
					cnt++

				}
				//	s[i] = '1'
			} else {
				if k&1 != 0 {
					ans[i] = 1
					cnt++
				}
			}
		}
		//	Fprintln(out, ans, k-cnt)
		if cnt < k {

			ans[n-1] += (k - cnt)

		}
		for i := 0; i < n; i++ {
			if (k-ans[i])&1 != 0 {
				if s[i] == '0' {
					s[i] = '1'
				} else {
					s[i] = '0'
				}
			}
		}
		//for i := 0; i < n; i++ {
		Fprintln(out, string(s))
		//}
		for i := 0; i < n; i++ {
			Fprint(out, ans[i], " ")
		}
		Fprintln(out)
		return
	}
	for i := 0; i < cas; i++ {
		sol()
	}
}

func main() {
	main_div2_782_B()
}

package main

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

const (
	INF = 3e5 + 10
	L0  = int64(0)
	L1  = int64(1)
)

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, m int
		Fscan(in, &n, &m)
		canUse := m

		var s string
		Fscan(in, &s)

		ans := make([]byte, n)
		ans2 := make([]int, n)
		for index, ch := range s {
			flag := m & 1
			v := int(ch - '0')
			if index != n-1 {
				if v^flag == 0 && canUse > 0 {
					ans2[index] += 1
					canUse -= 1
					ans[index] = '1'
				} else if v^flag == 1 {
					ans[index] = '1'
				} else if v^flag == 0 {
					ans[index] = '0'
				}
			} else {
				ans2[n-1] += canUse
				flag = (m - canUse) & 1
				v = v ^ flag
				ans[n-1] = byte('0' + v)
			}
		}
		Fprintln(out, string(ans))
		for i := 0; i < n; i++ {
			if i != n-1 {
				Fprint(out, ans2[i], " ")
			} else {
				Fprintln(out, ans2[i])
			}
		}
	}

}

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

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

package main

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

func main() {
	in, out := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)

	for ii := 0; ii < t; ii++ {
		var n, k int
		var s string
		fmt.Fscan(in, &n, &k, &s)
		ans := make([]int, n)
		temp := k
		for i := 0; i < n && temp > 0; i++ {
			if k&1 == 1 {
				if s[i] == '1' {
					ans[i] = 1
					temp--
				} else {
					ans[i] = 0
				}
			} else {
				if s[i] == '1' {
					ans[i] = 0
				} else {
					ans[i] = 1
					temp--
				}
			}
		}
		if temp > 0 {
			ans[n-1] += temp
		}
		sb := strings.Builder{}
		for i := 0; i < n; i++ {
			if (k-ans[i])&1 == 0 {
				sb.WriteByte(s[i])
			} else {
				if s[i] == '0' {
					sb.WriteByte('1')
				} else {
					sb.WriteByte('0')
				}
			}
		}
		fmt.Fprintln(out, sb.String())
		for i := 0; i < n; i++ {
			fmt.Fprint(out, ans[i])
			if i == n-1 {
				fmt.Fprintln(out)
			} else {
				fmt.Fprint(out, " ")
			}
		}
	}

}

package main

import (
	"fmt"
	"strings"
)

func main() {
	var t int
	fmt.Scanln(&t)

	for test := 0; test < t; test++ {
		var n, k int
		fmt.Scanf("%d %d\n", &n, &k)

		var str string
		fmt.Scanln(&str)

		s := []byte(str)

		f := make([]int, n)
		remainK := k
		for i := 0; i < n && remainK > 0; i++ {
			if (k%2 == 0 && s[i] == '0') || (k%2 == 1 && s[i] == '1') {
				f[i] = 1
				remainK--
			}
		}

		f[n-1] += remainK

		for i := 0; i < n; i++ {
			if (k-f[i])%2 == 1 {
				if s[i] == '1' {
					s[i] = '0'
				} else {
					s[i] = '1'
				}
			}
		}

		fmt.Println(string(s))

		output := fmt.Sprint(f)
		output = strings.Replace(output, "[", "", -1)
		output = strings.Replace(output, "]", "", -1)
		fmt.Println(output)
	}
}

package main

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

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

	var T, n, k int
	var s []byte
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &k, &s)
		pos := [2][]int{}
		for i, b := range s {
			pos[b&1] = append(pos[b&1], i)
		}

		ans := make([]int, n)
		p := pos[k&1]
		m := len(p)
		for i := 1; i < m && k > k&1; i += 2 {
			s[p[i-1]] ^= 1
			s[p[i]] ^= 1
			ans[p[i-1]]++
			ans[p[i]]++
			k -= 2
		}

		if k&1 > 0 {
			i := bytes.IndexByte(s, '1')
			if i < 0 {
				i = n - 1
			}
			for j := range s {
				if j != i {
					s[j] ^= 1
				}
			}
			ans[i]++
			k--
		} else if k > 0 && m&1 > 0 {
			s[p[m-1]] ^= 1
			s[n-1] ^= 1
			ans[p[m-1]]++
			ans[n-1]++
			k -= 2
		}
		ans[0] += k

		Fprintf(out, "%s\n", s)
		for _, v := range ans {
			Fprint(out, v, " ")
		}
		Fprintln(out)
	}
}

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

package main

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

/*
1
6 3
100001
---
111110
1 0 0 2 0 0

100001 -> 111110 -> 000001 -> 111110
=========
1
6 1
111001
---
100110
1 0 0 0 0 0
*/

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

	bits := make([]bool, n)

	for i, r := range s {
		if r == '1' {
			bits[i] = true
		}
	}

	flips := make([]int, n)

	flipsLeftCnt := k

	for i, bit := range bits {
		if flipsLeftCnt > 0 {
			if !bit && k%2 == 0 || bit && k%2 != 0 {
				flips[i]++
				flipsLeftCnt--
			}
		} else {
			break
		}
	}

	flips[len(flips)-1] += flipsLeftCnt

	for i, cnt := range flips {
		if bits[i] == ((k-cnt)%2 == 0) {
			fmt.Fprintf(writer, "1")
		} else {
			fmt.Fprintf(writer, "0")
		}
	}
	fmt.Fprintln(writer)

	for _, cnt := range flips {
		fmt.Fprintf(writer, "%d ", cnt)
	}
	fmt.Fprintln(writer)
}

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

//goland:noinspection GoUnhandledErrorResult
func factorize(num int) []int {
	factors := []int{1}

	sqrtA := int(math.Sqrt(float64(num)))

	for ftr := 2; ftr <= sqrtA; ftr++ {
		for num%ftr == 0 {
			num /= ftr
			factors = append(factors, ftr)
		}
	}

	if num > sqrtA {
		factors = append(factors, num)
	}

	return factors
}

package main

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

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, k int
		var s string
		fmt.Fscan(in, &n, &k, &s)

		z := k
		m := make([]int, n)
		var flipped int
		fn := func(ch byte) int {
			n := int(ch - '0')
			if flipped%2 != 0 {
				return n ^ 1
			}
			return n
		}
		var pos int
		for pos < n {
			if z <= 0 {
				break
			}
			if z%2 == 0 && fn(s[pos]) == 0 {
				m[pos]++
				z--
				flipped++
			} else if z%2 != 0 && fn(s[pos]) == 1 {
				m[pos]++
				z--
				flipped++
			}

			pos++
		}

		m[n-1] += z
		for i, ch := range s {
			z := k - m[i]
			if z%2 != 0 && ch == '1' {
				fmt.Fprint(out, 0)
			} else if z%2 != 0 && ch == '0' {
				fmt.Fprint(out, 1)
			} else {
				fmt.Fprint(out, string(ch))
			}
		}
		fmt.Fprintln(out)
		for _, v := range m {
			fmt.Fprintf(out, "%d ", v)
		}
		fmt.Fprintln(out)
	}
}

package main

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

func main() {
	var reader *bufio.Reader = bufio.NewReader(os.Stdin)
	var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var T int
	fmt.Scan(&T)
	var n int
	var k int
	var s string
	for t := 0; t < T; t++ {
		fmt.Fscan(reader, &n, &k, &s)
		var ans []byte
		var flips []int
		flip := false
		for i := 0; i < n; i++ {
			val := s[i]
			if flip {
				if val == '1' {
					val = '0'
				} else {
					val = '1'
				}
			}
			if k == 0 || i == n-1 {
				ans = append(ans, val)
				flips = append(flips, k)
			} else {
				if k%2 == 0 {
					if val == '1' {
						// we are 1, want to keep it: dont flip!
						ans = append(ans, '1')
						flips = append(flips, 0)
					} else {
						// we are 0, want to flip it: flip!
						ans = append(ans, '1')
						k--
						flip = !flip
						flips = append(flips, 1)
					}
				} else {
					if val == '1' {
						// we are 1, want to keep it: flip!
						ans = append(ans, '1')
						k--
						flip = !flip
						flips = append(flips, 1)
					} else {
						// we are 0, want to flip it: don't flip!
						ans = append(ans, '1')
						flips = append(flips, 0)
					}
				}
			}
		}
		fmt.Fprintln(writer, string(ans))
		var flipstrings []string
		for _, j := range flips {
			flipstrings = append(flipstrings, strconv.Itoa(j))
		}
		fmt.Fprintln(writer, strings.Join(flipstrings, " "))
		// fmt.Fprintln(writer, ans)
	}
}

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()
		k := _int()
		s := _string()
		r1, r2 := sol(n, k, s)
		printf("%s\n", r1)
		for _, r := range r2 {
			printf("%d ", r)
		}
		printf("\n")
	}
}

func sol(n, k int, s string) (string, []int) {
	r1 := make([]byte, n)
	r2 := make([]int, n)
	kc := k
	if k&1 == 1 {
		for i := range s {
			if kc > 0 {
				if s[i] == '1' {
					r1[i] = '1'
					r2[i]++
					kc--
				} else {
					r1[i] = '1'
				}
			} else {
				if s[i] == '1' {
					r1[i] = '0'
				} else {
					r1[i] = '1'
				}
			}
		}
	} else {
		for i := range s {
			if kc > 0 {
				if s[i] == '0' {
					r1[i] = '1'
					r2[i]++
					kc--
				} else {
					r1[i] = '1'
				}
			} else {
				if s[i] == '1' {
					r1[i] = '1'
				} else {
					r1[i] = '0'
				}
			}
		}
	}
	// clean up last digit
	r2[len(r2)-1] += kc
	if kc&1 == 1 {
		if r1[len(r1)-1] == '0' {
			r1[len(r1)-1] = '1'
		} else {
			r1[len(r1)-1] = '0'
		}
	}
	return string(r1), r2
}

package main

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

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

func printAnswer(m map[int]int, bitsCount int) {
	for i := 0; i < bitsCount; i++ {
		if val, ok := m[i]; ok {
			fmt.Fprint(writer, val)
			fmt.Fprint(writer, " ")
		} else {
			fmt.Fprint(writer, "0 ")
		}
	}
	fmt.Fprintln(writer)
}

func flipFirstOne(bits []rune) int {
	i := 0

	for i+1 < len(bits) {
		if bits[i] == '1' {
			break
		}

		i += 1
	}

	if bits[i] == '1' {
		bits[i] = '0'
	} else {
		bits[i] = '1'
	}

	return i
}

func solve(movesCount int, bits []rune) {
	defer writer.Flush()
	answer := make(map[int]int)
	one := '1'

	if movesCount%2 == 1 {
		movesCount -= 1
		flippedPosition := flipFirstOne(bits)
		one = '0'
		answer[flippedPosition] = 1
	}

	i := 0
	for movesCount != 0 && i < len(bits) {
		/** Looking for the first zero */
		for i < len(bits) && bits[i] == one {
			fmt.Fprint(writer, "1")
			i += 1
		}

		if i >= len(bits) {
			break
		} else {
			if _, ok := answer[i]; ok {
				answer[i] += 1
			} else {
				answer[i] = 1
			}

			if i+1 == len(bits) {
				fmt.Fprint(writer, "0")
				if _, ok := answer[i]; ok {
					answer[i] += 1
				} else {
					answer[i] = 1
				}
				i += 1
				movesCount -= 2
				break
			} else {
				fmt.Fprint(writer, "1")
			}
		}

		i += 1

		/** Looking for the second zero */
		for i < len(bits) && bits[i] == one {
			if i+1 == len(bits) {
				break
			}

			fmt.Fprint(writer, "1")
			i += 1
		}

		if i >= len(bits) {
			break
		} else if i+1 == len(bits) {
			fmt.Fprint(writer, "0")
		} else {
			fmt.Fprint(writer, "1")
		}

		if _, ok := answer[i]; ok {
			answer[i] += 1
		} else {
			answer[i] = 1
		}
		i += 1
		movesCount -= 2
	}

	if i >= len(bits) {
		if _, ok := answer[len(bits)-1]; ok {
			answer[len(bits)-1] += movesCount
		} else {
			answer[len(bits)-1] = movesCount
		}
	} else {
		for i < len(bits) {
			if bits[i] == one {
				fmt.Fprint(writer, "1")
			} else {
				fmt.Fprint(writer, "0")
			}

			i += 1
		}
	}

	fmt.Fprintln(writer)
	printAnswer(answer, len(bits))
}

func main() {
	var t, n, k int
	var bitsString string
	var bits []rune
	fmt.Scan(&t)

	for i := 0; i < t; i++ {
		fmt.Fscan(reader, &n, &k)
		fmt.Fscan(reader, &bitsString)

		bits = []rune(bitsString)
		solve(k, bits)
	}
}

package main

import "fmt"

func printArray(arr []int) {
	for _, num := range arr {
		fmt.Printf("%d ", num)
	}
	fmt.Println()
}

func flipFirstOne(bits []rune) int {
	i := 0

	for i+1 < len(bits) {
		if bits[i] == '1' {
			break
		}

		i += 1
	}

	if bits[i] == '1' {
		bits[i] = '0'
	} else {
		bits[i] = '1'
	}

	return i
}

func solve(movesCount int, bits []rune) {
	answer := make([]int, len(bits))
	one := '1'

	if movesCount%2 == 1 {
		movesCount -= 1
		flippedPosition := flipFirstOne(bits)
		one = '0'
		answer[flippedPosition] += 1
	}

	i := 0
	for movesCount != 0 && i < len(bits) {
		/** Looking for the first zero */
		for i < len(bits) && bits[i] == one {
			fmt.Print("1")
			i += 1
		}

		if i >= len(bits) {
			break
		} else {
			answer[i] += 1

			if i+1 == len(bits) {
				fmt.Print("0")
				answer[i] += 1
				i += 1
				movesCount -= 2
				break
			} else {
				fmt.Print("1")
			}
		}

		i += 1

		/** Looking for the second zero */
		for i < len(bits) && bits[i] == one {
			if i+1 == len(bits) {
				break
			}

			fmt.Print("1")
			i += 1
		}

		if i >= len(bits) {
			break
		} else if i+1 == len(bits) {
			fmt.Print("0")
		} else {
			fmt.Print("1")
		}

		answer[i] += 1
		i += 1
		movesCount -= 2
	}

	if i >= len(bits) {
		answer[len(bits)-1] += movesCount
	} else {
		for i < len(bits) {
			if bits[i] == one {
				fmt.Print("1")
			} else {
				fmt.Print("0")
			}

			i += 1
		}
	}

	fmt.Println()
	printArray(answer)
}

func main() {
	var t, n, k int
	var bitsString string
	var bits []rune
	fmt.Scanf("%d", &t)

	for i := 0; i < t; i++ {
		fmt.Scanf("%d %d", &n, &k)
		fmt.Scanf("%s", &bitsString)

		bits = []rune(bitsString)
		solve(k, bits)
	}
}

package main

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

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

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

	var T int
	var n int
	var k int
	var x string

	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &k, &x)
		ans := []byte{}
		se := make([]int, n)
		if k%2 == 0 {
			for i, v := range x {
				if v == '0' {
					if k > 0 {
						k--
						se[i]++
					}
					if k == 0 {
						ans = append(ans, x[i:]...)
						break
					}
				}
				ans = append(ans, '1')
			}
		} else {
			for i, v := range x {
				if k == 0 {
					if v == '1' {
						ans = append(ans, '0')
					} else {
						ans = append(ans, '1')
					}
				}
				if k > 0 {
					if v == '1' {
						se[i]++
						k--
					}
					ans = append(ans, '1')
				}
			}
		}

		se[0] += k - k%2
		if k > 0 && k%2 == 1 {
			se[len(ans)-1]++
			ans[len(ans)-1] = '0'
		}

		Fprintln(out, string(ans))
		for i, v := range se {
			if i > 0 {
				Fprintf(out, " ")
			}
			Fprintf(out, "%d", v)
		}
		Fprintln(out, "")
	}
}

package main

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

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

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

	var T int
	var n int
	var k int
	var x string

	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &k, &x)
		ans := []byte{}
		se := make([]int, n)
		if k%2 == 0 {
			for i, v := range x {
				if v == '0' {
					if k > 0 {
						k--
						se[i]++
					}
					if k == 0 {
						ans = append(ans, x[i:]...)
						break
					}
				}
				ans = append(ans, '1')
			}
		} else {
			for i, v := range x {
				if k == 0 {
					if v == '1' {
						ans = append(ans, '0')
					} else {
						ans = append(ans, '1')
					}
				}
				if k > 0 {
					if v == '1' {
						se[i]++
						k--
					}
					ans = append(ans, '1')
				}
			}
		}

		se[0] += (k - k%2)
		if k > 0 && k%2 == 1 {
			se[len(ans)-1]++
			ans[len(ans)-1] = '0'
		}

		Fprintln(out, string(ans))
		for _, v := range se {
			Fprintf(out, "%d ", v)
		}
		Fprintln(out, "")
	}
}

package main

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

func main_div2_782_B() {
	_in, _out := os.Stdin, os.Stdout
	in, out := bufio.NewReader(_in), bufio.NewWriter(_out)
	defer out.Flush()
	var cas int = 1
	Fscan(in, &cas)
	var sol func()
	sol = func() {
		var n, k int
		Fscan(in, &n, &k)
		var s1 string
		Fscan(in, &s1)
		s := []byte(s1)
		ans := make([]int, n)
		if k == 0 {
			Fprintln(out, string(s))
			for i := 0; i < n; i++ {
				Fprint(out, ans[i], " ")
			}
			Fprintln(out)
			return
		}
		cnt := 0
		//var p = -1
		for i := 0; i < n; i++ {
			if cnt > k {
				//	p = i
				break
			}
			if s[i] == '0' {
				if k&1 == 0 {
					ans[i] = 1
					cnt++

				}
				//	s[i] = '1'
			} else {
				if k&1 != 0 {
					ans[i] = 1
					cnt++
				}
			}
		}
	//	Fprintln(out, ans, k-cnt)
		if cnt < k {

			ans[n-1] += k - cnt

		}
		for i := 0; i < n; i++ {
			if (k-ans[i])&1 != 0 {
				if s[i] == '0' {
					s[i] = '1'
				} else {
					s[i] = '0'
				}
			}
		}
		//for i := 0; i < n; i++ {
		Fprintln(out, string(s))
		//}
		for i := 0; i < n; i++ {
			Fprint(out, ans[i], " ")
		}
		Fprintln(out)
		return
	}
	for i := 0; i < cas; i++ {
		sol()
	}
}

func main() {
	main_div2_782_B()
}