As a teacher, Riko Hakozaki often needs to help her students with problems from various subjects. Today, she is asked a programming task which goes as follows.You are given an undirected complete graph with $$$n$$$ nodes, where some edges are pre-assigned with a positive weight while the rest aren't. You need to assign all unassigned edges with non-negative weights so that in the resulting fully-assigned complete graph the XOR sum of all weights would be equal to $$$0$$$.Define the ugliness of a fully-assigned complete graph the weight of its minimum spanning tree, where the weight of a spanning tree equals the sum of weights of its edges. You need to assign the weights so that the ugliness of the resulting graph is as small as possible.As a reminder, an undirected complete graph with $$$n$$$ nodes contains all edges $$$(u, v)$$$ with $$$1 \le u < v \le n$$$; such a graph has $$$\frac{n(n-1)}{2}$$$ edges.She is not sure how to solve this problem, so she asks you to solve it for her.

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n \le 2 \cdot 10^5$$$, $$$0 \le m \le \min(2 \cdot 10^5, \frac{n(n-1)}{2} - 1)$$$)  — the number of nodes and the number of pre-assigned edges. The inputs are given so that there is at least one unassigned edge. The $$$i$$$-th of the following $$$m$$$ lines contains three integers $$$u_i$$$, $$$v_i$$$, and $$$w_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u \ne v$$$, $$$1 \le w_i < 2^{30}$$$), representing the edge from $$$u_i$$$ to $$$v_i$$$ has been pre-assigned with the weight $$$w_i$$$. No edge appears in the input more than once.

Print on one line one integer  — the minimum ugliness among all weight assignments with XOR sum equal to $$$0$$$.

NoteThe following image showcases the first test case. The black weights are pre-assigned from the statement, the red weights are assigned by us, and the minimum spanning tree is denoted by the blue edges.

package main

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

type dsu struct{
	p []int
}

func newDSU(n int) *dsu{
	ret := &dsu{}
	ret.p = make([]int, n)
	for i:=0;i<n;i++{
		ret.p[i]=i
	}
	return ret
}

func (d *dsu) find(u int) int {
	if d.p[u] == u {
		return u
	}
	d.p[u] = d.find(d.p[u])
	return d.p[u]
}

func (d *dsu) merge(u, v int) {
	d.p[d.find(u)]=d.find(v)
}

func (d *dsu) reset() {
	for i := 0; i < len(d.p); i++ {
		d.p[i] = i
	}
}

type edge struct{
	u,v,w int
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var n,e int
	fmt.Fscan(in, &n, &e)

	g := make([][]int, n)

	xr := 0
	ed := make([]edge, e)
	for i := 0;i < e; i++{
		fmt.Fscan(in, &ed[i].u,&ed[i].v,&ed[i].w)
		ed[i].u--
		ed[i].v--
		xr ^= ed[i].w
		g[ed[i].u]=append(g[ed[i].u], ed[i].v)
		g[ed[i].v]=append(g[ed[i].v], ed[i].u)
	}
	for _, adj := range g {
		sort.Ints(adj)
	}
	sort.Slice(ed, func(i,j int)bool{
		return ed[i].w < ed[j].w
	})
	cs := make([]int, n)
	d := newDSU(n)
	for i:=0;i<n;i++ {
		last := -1
		for j:=0;j<len(g[i]); j++ {
			if last + 1 < g[i][j] {
				d.merge(i, g[i][j]-1)
				cs[last+2]+=1
				cs[g[i][j]]-=1
			}
			last=g[i][j]
		}
		if last+2 < n {
			cs[last+2] += 1
		}
	}
	for i:=1;i<n;i++{
		cs[i]+=cs[i-1]
		if cs[i] > 0 {
			d.merge(i-1, i)
		}
	}
	mst := int64(0)
	for _, e := range ed {
		if d.find(e.u) == d.find(e.v) {
			continue
		}
		d.merge(e.u, e.v)
		mst += int64(e.w)
	}
	if int64(n)*int64(n-1)/2 > int64(e + n-1) || xr == 0 {
		fmt.Fprintln(out, mst)
		return
	}

	useful := []edge{}
	for i := 0; i < n; i++ {
		v := 0
		for j := 0; j<len(g[i]); j++ {
			for ; v <= g[i][j]; v++ {
				if v == g[i][j] || v == i {
					continue
				}
				if i < v {
					useful = append(useful, edge{
						u: i,
						v: v,
						w: 0,
					})
				}
			}
		}
		for ; v < n; v++ {
			if v == i {
				continue
			}
			if i < v {
				useful = append(useful, edge{
					u: i,
					v: v,
					w: 0,
				})
			}
		}
	}
	d.reset()
	for _, e := range ed {
		if d.find(e.u) == d.find(e.v) {
			continue
		}
		d.merge(e.u, e.v)
		useful = append(useful, e)
	}
	mst += int64(xr)
	for i := 0; i < len(useful); i++ {
		if useful[i].w != 0 {
			continue
		}
		cur := int64(0)
		d.reset()
		for j, e := range useful {
			if i == j {
				continue
			}
			if d.find(e.u) == d.find(e.v) {
				continue
			}
			d.merge(e.u, e.v)
			cur += int64(e.w)
		}
		if mst > cur {
			mst = cur
		}
	}
	fmt.Fprintln(out, mst)
}

package main

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

type dsu struct{
	p []int
}

func newDSU(n int) *dsu{
	ret := &dsu{}
	ret.p = make([]int, n)
	for i:=0;i<n;i++{
		ret.p[i]=i
	}
	return ret
}

func (d *dsu) find(u int) int {
	if d.p[u] == u {
		return u
	}
	d.p[u] = d.find(d.p[u])
	return d.p[u]
}

func (d *dsu) merge(u, v int) {
	d.p[d.find(u)]=d.find(v)
}

func (d *dsu) reset() {
	for i := 0; i < len(d.p); i++ {
		d.p[i] = i
	}
}

type edge struct{
	u,v,w int
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var n,e int
	fmt.Fscan(in, &n, &e)

	g := make([][]int, n)

	xr := 0
	ed := make([]edge, e)
	for i := 0;i < e; i++{
		fmt.Fscan(in, &ed[i].u,&ed[i].v,&ed[i].w)
		ed[i].u--
		ed[i].v--
		xr ^= ed[i].w
		g[ed[i].u]=append(g[ed[i].u], ed[i].v)
		g[ed[i].v]=append(g[ed[i].v], ed[i].u)
	}
	for _, adj := range g {
		sort.Ints(adj)
	}
	sort.Slice(ed, func(i,j int)bool{
		return ed[i].w < ed[j].w
	})
	cs := make([]int, n)
	d := newDSU(n)
	for i:=0;i<n;i++ {
		last := -1
		for j:=0;j<len(g[i]); j++ {
			if last + 1 < g[i][j] {
				d.merge(i, g[i][j]-1)
				cs[last+2]+=1
				cs[g[i][j]]-=1
			}
			last=g[i][j]
		}
		if last+2 < n {
			d.merge(i, n-1)
			if last + 2 < n {
				cs[last+2] += 1
			}
		}
	}
	for i:=1;i<n;i++{
		cs[i]+=cs[i-1]
		if cs[i] > 0 {
			d.merge(i-1, i)
		}
	}
	mst := int64(0)
	for _, e := range ed {
		if d.find(e.u) == d.find(e.v) {
			continue
		}
		d.merge(e.u, e.v)
		mst += int64(e.w)
	}
	if int64(n)*int64(n-1)/2 > int64(e + n-1) || xr == 0 {
		fmt.Fprintln(out, mst)
		return
	}

	useful := []edge{}
	for i := 0; i < n; i++ {
		v := 0
		for j := 0; j<len(g[i]); j++ {
			for ; v <= g[i][j]; v++ {
				if v == g[i][j] || v == i {
					continue
				}
				if i < v {
					useful = append(useful, edge{
						u: i,
						v: v,
						w: 0,
					})
				}
			}
		}
		for ; v < n; v++ {
			if v == i {
				continue
			}
			if i < v {
				useful = append(useful, edge{
					u: i,
					v: v,
					w: 0,
				})
			}
		}
	}
	d.reset()
	for _, e := range ed {
		if d.find(e.u) == d.find(e.v) {
			continue
		}
		d.merge(e.u, e.v)
		useful = append(useful, e)
	}
	mst += int64(xr)
	for i := 0; i < len(useful); i++ {
		if useful[i].w != 0 {
			continue
		}
		cur := int64(0)
		d.reset()
		for j, e := range useful {
			if i == j {
				continue
			}
			if d.find(e.u) == d.find(e.v) {
				continue
			}
			d.merge(e.u, e.v)
			cur += int64(e.w)
		}
		if mst > cur {
			mst = cur
		}
	}
	fmt.Fprintln(out, mst)
}

package main

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

type dsu struct{
	p []int
}

func newDSU(n int) *dsu{
	ret := &dsu{}
	ret.p = make([]int, n)
	for i:=0;i<n;i++{
		ret.p[i]=i
	}
	return ret
}

func (d *dsu) find(u int) int {
	if d.p[u] == u {
		return u
	}
	d.p[u] = d.find(d.p[u])
	return d.p[u]
}

func (d *dsu) merge(u, v int) {
	d.p[d.find(u)]=d.find(v)
}

func (d *dsu) reset() {
	for i := 0; i < len(d.p); i++ {
		d.p[i] = i
	}
}

type edge struct{
	u,v,w int
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var n,e int
	fmt.Fscan(in, &n, &e)

	g := make([][]int, n)

	xr := 0
	ed := make([]edge, e)
	for i := 0;i < e; i++{
		fmt.Fscan(in, &ed[i].u,&ed[i].v,&ed[i].w)
		ed[i].u--
		ed[i].v--
		xr ^= ed[i].w
		g[ed[i].u]=append(g[ed[i].u], ed[i].v)
		g[ed[i].v]=append(g[ed[i].v], ed[i].u)
	}
	for _, adj := range g {
		sort.Ints(adj)
	}
	sort.Slice(ed, func(i,j int)bool{
		return ed[i].w < ed[j].w
	})
	cs := make([]int, n)
	d := newDSU(n)
	for i:=0;i<n;i++ {
		last := -1
		for j:=0;j<len(g[i]); j++ {
			if last + 1 < g[i][j] {
				d.merge(i, g[i][j]-1)
				cs[last+2]+=1
				cs[g[i][j]]-=1
			}
			last=g[i][j]
		}
		if last+1 < n {
			d.merge(i, n-1)
			if last + 2 < n {
				cs[last+2] += 1
			}
		}
	}
	for i:=1;i<n;i++{
		cs[i]+=cs[i-1]
		if cs[i] > 0 {
			d.merge(i-1, i)
		}
	}
	mst := int64(0)
	for _, e := range ed {
		if d.find(e.u) == d.find(e.v) {
			continue
		}
		d.merge(e.u, e.v)
		mst += int64(e.w)
	}
	if int64(n)*int64(n-1)/2 > int64(e + n-1) || xr == 0 {
		fmt.Fprintln(out, mst)
		return
	}

	useful := []edge{}
	for i := 0; i < n; i++ {
		v := 0
		for j := 0; j<len(g[i]); j++ {
			for ; v <= g[i][j]; v++ {
				if v == g[i][j] || v == i {
					continue
				}
				if i < v {
					useful = append(useful, edge{
						u: i,
						v: v,
						w: 0,
					})
				}
			}
		}
		for ; v < n; v++ {
			if v == i {
				continue
			}
			if i < v {
				useful = append(useful, edge{
					u: i,
					v: v,
					w: 0,
				})
			}
		}
	}
	d.reset()
	for _, e := range ed {
		if d.find(e.u) == d.find(e.v) {
			continue
		}
		d.merge(e.u, e.v)
		useful = append(useful, e)
	}
	mst += int64(xr)
	for i := 0; i < len(useful); i++ {
		if useful[i].w != 0 {
			continue
		}
		cur := int64(0)
		d.reset()
		comp := n
		for j, e := range useful {
			if i == j {
				continue
			}
			if d.find(e.u) == d.find(e.v) {
				continue
			}
			comp--
			d.merge(e.u, e.v)
			cur += int64(e.w)
		}
		if comp == 1 && mst > cur {
			mst = cur
		}
	}
	fmt.Fprintln(out, mst)
}

package main

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

// github.com/EndlessCheng/codeforces-go
func prim(g [][]int) (mst int64) {
	n := len(g)
	minW := make([]int, n)
	for i := range minW {
		minW[i] = 2e9
	}
	minW[0] = 0
	used := make([]bool, n)
	for {
		v := -1
		for i, u := range used {
			if !u && (v < 0 || minW[i] < minW[v]) {
				v = i
			}
		}
		if v < 0 {
			break
		}
		used[v] = true
		mst += int64(minW[v])
		for w, wt := range g[v] {
			if wt < minW[w] {
				minW[w] = wt
			}
		}
	}
	return
}

func CF1508C(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, m, v, w, wt, xor, maxV int
	Fscan(in, &n, &m)
	type edge struct{ v, w, wt int }
	es := make([]edge, m)
	for i := range es {
		Fscan(in, &v, &w, &wt)
		xor ^= wt
		es[i] = edge{v - 1, w - 1, wt}
	}

	if n < 1e3 && m >= (n-2)*(n-1)/2 {
		g := make([][]int, n)
		for i := range g {
			g[i] = make([]int, n)
		}
		for _, e := range es {
			g[e.v][e.w] = e.wt
			g[e.w][e.v] = e.wt
		}
		ans := int64(1e18)
		for i, r := range g {
			for j, wt := range r[:i] {
				if wt == 0 {
					g[i][j] = xor
					g[j][i] = xor
					if res := prim(g); res < ans {
						ans = res
					}
					g[i][j] = 0
					g[j][i] = 0
				}
			}
		}
		Fprint(out, ans)
		return
	}

	fa := make([]int, n)
	for i := range fa {
		fa[i] = i
	}
	var f func(int) int
	f = func(x int) int {
		if fa[x] != x {
			fa[x] = f(fa[x])
		}
		return fa[x]
	}
	g := make([][]int, n)
	for _, e := range es {
		g[e.v] = append(g[e.v], e.w)
		g[e.w] = append(g[e.w], e.v)
	}
	for v, ws := range g {
		if len(ws) < len(g[maxV]) {
			maxV = v
		}
	}
	mergeInv := func(v int) {
		has := map[int]bool{v: true}
		for _, w := range g[v] {
			has[w] = true
		}
		for i := range g {
			if !has[i] {
				fa[f(i)] = f(v)
			}
		}
	}
	mergeInv(maxV)
	for v := range g {
		if f(v) != f(maxV) {
			mergeInv(v)
		}
	}
	sort.Slice(es, func(i, j int) bool { return es[i].wt < es[j].wt })
	ans := int64(0)
	for _, e := range es {
		if v, w := f(e.v), f(e.w); v != w {
			fa[v] = w
			ans += int64(e.wt)
		}
	}
	Fprint(out, ans)
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func prim8(dist [][]int) (mst int64) {
	n := len(dist)
	minW := make([]int, n)
	for i := range minW {
		minW[i] = 2e9
	}
	minW[0] = 0
	used := make([]bool, n)
	for {
		v := -1
		for i, u := range used {
			if !u && (v < 0 || minW[i] < minW[v]) {
				v = i
			}
		}
		if v < 0 {
			break
		}
		used[v] = true
		mst += int64(minW[v])
		for w, wt := range dist[v] {
			minW[w] = min(minW[w], wt)
		}
	}
	return
}

func CF1508C(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, m, v, w, wt, xor, maxV int
	Fscan(in, &n, &m)
	fa := make([]int, n)
	var f func(int) int
	f = func(x int) int {
		if fa[x] != x {
			fa[x] = f(fa[x])
		}
		return fa[x]
	}
	type edge struct{ v, w, wt int }
	es := make([]edge, m)
	for i := range es {
		Fscan(in, &v, &w, &wt)
		xor ^= wt
		es[i] = edge{v - 1, w - 1, wt}
	}

	if n < 1e3 && m >= (n-2)*(n-1)/2 {
		g := make([][]bool, n)
		for i := range g {
			g[i] = make([]bool, n)
		}
		for _, e := range es {
			g[e.v][e.w] = true
			g[e.w][e.v] = true
		}
		for i := range fa {
			fa[i] = i
		}
		for v, ws := range g {
			for w, has := range ws {
				if has {
					continue
				}
				if v, w := f(v), f(w); v != w {
					fa[v] = w
					continue
				}
				dist := make([][]int, n)
				for i := range dist {
					dist[i] = make([]int, n)
				}
				for _, e := range es {
					dist[e.v][e.w] = e.wt
					dist[e.w][e.v] = e.wt
				}
				ans := int64(1e18)
				for i, r := range dist {
					for j, d := range r[:i] {
						if d == 0 {
							dist[i][j] = xor
							dist[j][i] = xor
							if res := prim8(dist); res < ans {
								ans = res
							}
							dist[i][j] = 0
							dist[j][i] = 0
						}
					}
				}
				Fprint(out, ans)
				return
			}
		}
	}

	for i := range fa {
		fa[i] = i
	}
	g := make([][]int, n)
	for _, e := range es {
		g[e.v] = append(g[e.v], e.w)
		g[e.w] = append(g[e.w], e.v)
	}
	for v, ws := range g {
		if len(ws) < len(g[maxV]) {
			maxV = v
		}
	}
	mergeInv := func(v int) {
		has := map[int]bool{}
		for _, w := range g[v] {
			has[w] = true
		}
		for i := range g {
			if i != v && !has[i] {
				fa[f(i)] = f(v)
			}
		}
	}
	mergeInv(maxV)
	for v := range g {
		if f(v) != f(maxV) {
			mergeInv(v)
		}
	}
	sort.Slice(es, func(i, j int) bool { return es[i].wt < es[j].wt })
	ans := int64(0)
	for _, e := range es {
		if v, w := f(e.v), f(e.w); v != w {
			fa[v] = w
			ans += int64(e.wt)
		}
	}
	Fprint(out, ans)
}

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

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

package main

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

// github.com/EndlessCheng/codeforces-go
func prim8(dist [][]int) (mst int64) {
	n := len(dist)
	minW := make([]int, n)
	for i := range minW {
		minW[i] = 2e9
	}
	minW[0] = 0
	used := make([]bool, n)
	for {
		v := -1
		for i, u := range used {
			if !u && (v < 0 || minW[i] < minW[v]) {
				v = i
			}
		}
		if v < 0 {
			break
		}
		used[v] = true
		mst += int64(minW[v])
		for w, wt := range dist[v] {
			minW[w] = min(minW[w], wt)
		}
	}
	return
}

func CF1508C(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, m, v, w, wt, xor, maxV int
	Fscan(in, &n, &m)
	fa := make([]int, n)
	var f func(int) int
	f = func(x int) int {
		if fa[x] != x {
			fa[x] = f(fa[x])
		}
		return fa[x]
	}
	type edge struct{ v, w, wt int }
	es := make([]edge, m)
	for i := range es {
		Fscan(in, &v, &w, &wt)
		xor ^= wt
		es[i] = edge{v - 1, w - 1, wt}
	}

	if n < 1e3 && m >= (n-2)*(n-1)/2 {
		g := make([][]bool, n)
		for i := range g {
			g[i] = make([]bool, n)
		}
		for _, e := range es {
			g[e.v][e.w] = true
			g[e.w][e.v] = true
		}
		for i := range fa {
			fa[i] = i
		}
		for v, ws := range g {
			for w, has := range ws {
				if has {
					continue
				}
				if v, w := f(v), f(w); v != w {
					fa[v] = w
					continue
				}
				dist := make([][]int, n)
				for i := range dist {
					dist[i] = make([]int, n)
				}
				for _, e := range es {
					dist[e.v][e.w] = e.wt
					dist[e.w][e.v] = e.wt
				}
				ans := int64(1e18)
				for i, r := range dist {
					for j, d := range r[:i] {
						if d == 0 {
							dist[i][j] = xor
							dist[j][i] = xor
							if res := prim8(dist); res < ans {
								ans = res
							}
							dist[i][j] = 0
							dist[j][i] = 0
						}
					}
				}
				Fprint(out, ans)
				return
			}
		}
	}

	for i := range fa {
		fa[i] = i
	}
	g := make([][]int, n)
	for _, e := range es {
		g[e.v] = append(g[e.v], e.w)
		g[e.w] = append(g[e.w], e.v)
	}
	for v, ws := range g {
		if len(ws) < len(g[maxV]) {
			maxV = v
		}
	}
	mergeInv := func(v int) {
		has := map[int]bool{v: true}
		for _, w := range g[v] {
			has[w] = true
		}
		for i := range g {
			if !has[i] {
				fa[f(i)] = f(v)
			}
		}
	}
	mergeInv(maxV)
	for v := range g {
		if f(v) != f(maxV) {
			mergeInv(v)
		}
	}
	sort.Slice(es, func(i, j int) bool { return es[i].wt < es[j].wt })
	ans := int64(0)
	for _, e := range es {
		if v, w := f(e.v), f(e.w); v != w {
			fa[v] = w
			ans += int64(e.wt)
		}
	}
	Fprint(out, ans)
}

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

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

package main

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

// github.com/EndlessCheng/codeforces-go
func prim(dist [][]int) (mst int64) {
	n := len(dist)
	minW := make([]int, n)
	for i := range minW {
		minW[i] = 2e9
	}
	minW[0] = 0
	used := make([]bool, n)
	for {
		v := -1
		for i, u := range used {
			if !u && (v < 0 || minW[i] < minW[v]) {
				v = i
			}
		}
		if v < 0 {
			break
		}
		used[v] = true
		mst += int64(minW[v])
		for w, wt := range dist[v] {
			minW[w] = min(minW[w], wt)
		}
	}
	return
}

func CF1508C(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, m, v, w, wt, xor, maxDegInvV int
	Fscan(in, &n, &m)
	fa := make([]int, n)
	var f func(int) int
	f = func(x int) int {
		if fa[x] != x {
			fa[x] = f(fa[x])
		}
		return fa[x]
	}
	type edge struct{ v, w, wt int }
	es := make([]edge, m)
	for i := range es {
		Fscan(in, &v, &w, &wt)
		xor ^= wt
		es[i] = edge{v - 1, w - 1, wt}
	}

	if n < 1e3 && m >= (n-2)*(n-1)/2 {
		g := make([]map[int]bool, n)
		for i := range g {
			g[i] = map[int]bool{}
		}
		for _, e := range es {
			g[e.v][e.w] = true
			g[e.w][e.v] = true
		}
		for i := range fa {
			fa[i] = i
		}
		for v, mp := range g {
			for w := range g {
				if mp[w] {
					continue
				}
				if v, w := f(v), f(w); v != w {
					fa[v] = w
					continue
				}
				dist := make([][]int, n)
				for i := range dist {
					dist[i] = make([]int, n)
				}
				for _, e := range es {
					dist[e.v][e.w] = e.wt
					dist[e.w][e.v] = e.wt
				}
				ans := int64(1e18)
				for i, r := range dist {
					for j, d := range r[:i] {
						if d == 0 {
							dist[i][j] = xor
							dist[j][i] = xor
							if res := prim(dist); res < ans {
								ans = res
							}
							dist[i][j] = 0
							dist[j][i] = 0
						}
					}
				}
				Fprint(out, ans)
				return
			}
		}
	}

	for i := range fa {
		fa[i] = i
	}
	g := make([][]int, n)
	for _, e := range es {
		g[e.v] = append(g[e.v], e.w)
		g[e.w] = append(g[e.w], e.v)
	}
	for v, ws := range g {
		if len(ws) < len(g[maxDegInvV]) {
			maxDegInvV = v
		}
	}
	mergeInv := func(v int) {
		has := map[int]bool{v: true}
		for _, w := range g[v] {
			has[w] = true
		}
		for i := range g {
			if !has[i] {
				fa[f(i)] = f(v)
			}
		}
	}
	mergeInv(maxDegInvV)
	for v := range g {
		if f(v) != f(maxDegInvV) {
			mergeInv(v)
		}
	}
	sort.Slice(es, func(i, j int) bool { return es[i].wt < es[j].wt })
	ans := int64(0)
	for _, e := range es {
		if v, w := f(e.v), f(e.w); v != w {
			fa[v] = w
			ans += int64(e.wt)
		}
	}
	Fprint(out, ans)
}

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

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

package main

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

// github.com/EndlessCheng/codeforces-go
func prim(dist [][]int) (mst int64) {
	n := len(dist)
	minW := make([]int, n)
	for i := range minW {
		minW[i] = 2e9
	}
	minW[0] = 0
	used := make([]bool, n)
	for {
		v := -1
		for i, u := range used {
			if !u && (v < 0 || minW[i] < minW[v]) {
				v = i
			}
		}
		if v < 0 {
			break
		}
		used[v] = true
		mst += int64(minW[v])
		for w, wt := range dist[v] {
			minW[w] = min(minW[w], wt)
		}
	}
	return
}

func CF1508C(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, m, v, w, wt, xor int
	Fscan(in, &n, &m)
	type edge struct{ v, w, wt int }
	es := make([]edge, m)
	for i := range es {
		Fscan(in, &v, &w, &wt)
		xor ^= wt
		es[i] = edge{v - 1, w - 1, wt}
	}

	fa := make([]int, n)
	var f func(int) int
	f = func(x int) int {
		if fa[x] != x {
			fa[x] = f(fa[x])
		}
		return fa[x]
	}

	if n < 1e3 && m >= (n-2)*(n-1)/2 {
		g := make([]map[int]bool, n)
		for i := range g {
			g[i] = map[int]bool{}
		}
		for _, e := range es {
			g[e.v][e.w] = true
			g[e.w][e.v] = true
		}
		for i := range fa {
			fa[i] = i
		}
		for v, mp := range g {
			for w := 0; w < n; w++ {
				if mp[w] {
					continue
				}
				if v, w := f(v), f(w); v != w {
					fa[v] = w
					continue
				}
				dist := make([][]int, n)
				for i := range dist {
					dist[i] = make([]int, n)
				}
				for _, e := range es {
					dist[e.v][e.w] = e.wt
					dist[e.w][e.v] = e.wt
				}
				ans := int64(1e18)
				for i, r := range dist {
					for j, d := range r[:i] {
						if d == 0 {
							dist[i][j] = xor
							dist[j][i] = xor
							if res := prim(dist); res < ans {
								ans = res
							}
							dist[i][j] = 0
							dist[j][i] = 0
						}
					}
				}
				Fprint(out, ans)
				return
			}
		}
	}

	g := make([][]int, n)
	for _, e := range es {
		g[e.v] = append(g[e.v], e.w)
		g[e.w] = append(g[e.w], e.v)
	}
	maxDegInv, maxDegInvV := 0, 0
	for v, edges := range g {
		if degInv := n - 1 - len(edges); degInv > maxDegInv {
			maxDegInv, maxDegInvV = degInv, v
		}
	}

	for i := range fa {
		fa[i] = i
	}
	mergeInv := func(v int, edges []int) {
		has := map[int]bool{v: true}
		for _, w := range edges {
			has[w] = true
		}
		for i := 0; i < n; i++ {
			if !has[i] {
				fa[f(i)] = f(v)
			}
		}
	}
	mergeInv(maxDegInvV, g[maxDegInvV])
	for v, edges := range g {
		if f(v) != f(maxDegInvV) {
			mergeInv(v, edges)
		}
	}

	sort.Slice(es, func(i, j int) bool { return es[i].wt < es[j].wt })
	ans := int64(0)
	for _, e := range es {
		if v, w := f(e.v), f(e.w); v != w {
			fa[v] = w
			ans += int64(e.wt)
		}
	}
	Fprint(out, ans)
}

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

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

package main

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

type dsu struct{
	p []int
}

func newDSU(n int) *dsu{
	ret := &dsu{}
	ret.p = make([]int, n)
	for i:=0;i<n;i++{
		ret.p[i]=i
	}
	return ret
}

func (d *dsu) find(u int) int {
	if d.p[u] == u {
		return u
	}
	d.p[u] = d.find(d.p[u])
	return d.p[u]
}

func (d *dsu) merge(u, v int) {
	d.p[d.find(u)]=d.find(v)
}

func (d *dsu) reset() {
	for i := 0; i < len(d.p); i++ {
		d.p[i] = i
	}
}

type edge struct{
	u,v,w int
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var n,e int
	fmt.Fscan(in, &n, &e)

	g := make([][]int, n)

	xr := 0
	ed := make([]edge, e)
	for i := 0;i < e; i++{
		fmt.Fscan(in, &ed[i].u,&ed[i].v,&ed[i].w)
		ed[i].u--
		ed[i].v--
		xr ^= ed[i].w
		g[ed[i].u]=append(g[ed[i].u], ed[i].v)
		g[ed[i].v]=append(g[ed[i].v], ed[i].u)
	}
	for _, adj := range g {
		sort.Ints(adj)
	}
	sort.Slice(ed, func(i,j int)bool{
		return ed[i].w < ed[j].w
	})
	cs := make([]int, n)
	d := newDSU(n)
	for i:=0;i<n;i++ {
		last := -1
		for j:=0;j<len(g[i]); j++ {
			if last + 1 < g[i][j] {
				d.merge(i, g[i][j]-1)
				cs[last+2]+=1
				cs[g[i][j]]-=1
			}
			last=g[i][j]
		}
		if last+1 < n {
			d.merge(i, n-1)
			if last + 2 < n {
				cs[last+2] += 1
			}
		}
	}
	for i:=1;i<n;i++{
		cs[i]+=cs[i-1]
		if cs[i] > 0 {
			d.merge(i-1, i)
		}
	}
	mst := int64(0)
	for _, e := range ed {
		if d.find(e.u) == d.find(e.v) {
			continue
		}
		d.merge(e.u, e.v)
		mst += int64(e.w)
	}
	if int64(n)*int64(n-1)/2 > int64(e + n-1) || xr == 0 {
		fmt.Fprintln(out, mst)
		return
	}

	useful := []edge{}
	for i := 0; i < n; i++ {
		v := 0
		for j := 0; j<len(g[i]); j++ {
			for ; v <= g[i][j]; v++ {
				if v == g[i][j] || v == i {
					continue
				}
				useful = append(useful, edge{
					u: i,
					v: v,
					w: 0,
				})
			}
		}
		for ; v < n; v++ {
			if v == i {
				continue
			}
			useful = append(useful, edge{
				u: i,
				v: v,
				w: 0,
			})
		}
	}
	d.reset()
	for _, e := range ed {
		if d.find(e.u) == d.find(e.v) {
			continue
		}
		d.merge(e.u, e.v)
		useful = append(useful, e)
	}
	mst += int64(xr)
	if n > 600 {
		fmt.Println(xr, mst)
	}
	for i := 0; i < len(useful); i++ {
		if useful[i].w != 0 {
			continue
		}
		cur := int64(0)
		d.reset()
		comp := n
		for j, e := range useful {
			if i == j {
				continue
			}
			if d.find(e.u) == d.find(e.v) {
				continue
			}
			comp--
			d.merge(e.u, e.v)
			cur += int64(e.w)
		}
		if comp == 1 && mst > cur {
			mst = cur
		}
	}
	fmt.Fprintln(out, mst)
}

package main

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

type dsu struct{
	p []int
}

func newDSU(n int) *dsu{
	ret := &dsu{}
	ret.p = make([]int, n)
	for i:=0;i<n;i++{
		ret.p[i]=i
	}
	return ret
}

func (d *dsu) find(u int) int {
	if d.p[u] == u {
		return u
	}
	d.p[u] = d.find(d.p[u])
	return d.p[u]
}

func (d *dsu) merge(u, v int) {
	d.p[d.find(u)]=d.find(v)
}

func (d *dsu) reset() {
	for i := 0; i < len(d.p); i++ {
		d.p[i] = i
	}
}

type edge struct{
	u,v,w int
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var n,e int
	fmt.Fscan(in, &n, &e)

	g := make([][]int, n)

	xr := 0
	ed := make([]edge, e)
	for i := 0;i < e; i++{
		fmt.Fscan(in, &ed[i].u,&ed[i].v,&ed[i].w)
		ed[i].u--
		ed[i].v--
		xr ^= ed[i].w
		g[ed[i].u]=append(g[ed[i].u], ed[i].v)
		g[ed[i].v]=append(g[ed[i].v], ed[i].u)
	}
	for _, adj := range g {
		sort.Ints(adj)
	}
	sort.Slice(ed, func(i,j int)bool{
		return ed[i].w < ed[j].w
	})
	cs := make([]int, n)
	d := newDSU(n)
	for i:=0;i<n;i++ {
		last := -1
		for j:=0;j<len(g[i]); j++ {
			if last + 1 < g[i][j] {
				d.merge(i, g[i][j]-1)
				cs[last+2]+=1
				cs[g[i][j]]-=1
			}
			last=g[i][j]
		}
		if last+1 < n {
			d.merge(i, n-1)
			if last + 2 < n {
				cs[last+2] += 1
			}
		}
	}
	for i:=1;i<n;i++{
		cs[i]+=cs[i-1]
		if cs[i] > 0 {
			d.merge(i-1, i)
		}
	}
	mst := int64(0)
	for _, e := range ed {
		if d.find(e.u) == d.find(e.v) {
			continue
		}
		d.merge(e.u, e.v)
		mst += int64(e.w)
	}
	if int64(n)*int64(n-1)/2 > int64(e + n-1) || xr == 0 {
		fmt.Fprintln(out, mst)
		return
	}

	useful := []edge{}
	for i := 0; i < n; i++ {
		v := 0
		for j := 0; j<len(g[i]); j++ {
			for ; v <= g[i][j]; v++ {
				if v == g[i][j] || v == i {
					continue
				}
				useful = append(useful, edge{
					u: i,
					v: v,
					w: 0,
				})
			}
		}
		for ; v < n; v++ {
			if v == i {
				continue
			}
			useful = append(useful, edge{
				u: i,
				v: v,
				w: 0,
			})
		}
	}
	d.reset()
	for _, e := range ed {
		if d.find(e.u) == d.find(e.v) {
			continue
		}
		d.merge(e.u, e.v)
		useful = append(useful, e)
	}
	mst += int64(xr)
	for i := 0; i < len(useful); i++ {
		if useful[i].w != 0 {
			continue
		}
		cur := int64(0)
		d.reset()
		comp := n
		for j, e := range useful {
			if i == j {
				continue
			}
			if d.find(e.u) == d.find(e.v) {
				continue
			}
			comp--
			d.merge(e.u, e.v)
			cur += int64(e.w)
		}
		if comp == 1 && mst > cur {
			mst = cur
		}
	}
	fmt.Fprintln(out, mst)
}

package main

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

type dsu struct{
	p []int
}

func newDSU(n int) *dsu{
	ret := &dsu{}
	ret.p = make([]int, n)
	for i:=0;i<n;i++{
		ret.p[i]=i
	}
	return ret
}

func (d *dsu) find(u int) int {
	if d.p[u] == u {
		return u
	}
	d.p[u] = d.find(d.p[u])
	return d.p[u]
}

func (d *dsu) merge(u, v int) {
	d.p[d.find(u)]=d.find(v)
}

func (d *dsu) reset() {
	for i := 0; i < len(d.p); i++ {
		d.p[i] = i
	}
}

type edge struct{
	u,v,w int
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var n,e int
	fmt.Fscan(in, &n, &e)

	g := make([][]int, n)

	xr := 0
	ed := make([]edge, e)
	for i := 0;i < e; i++{
		fmt.Fscan(in, &ed[i].u,&ed[i].v,&ed[i].w)
		ed[i].u--
		ed[i].v--
		xr ^= ed[i].w
		g[ed[i].u]=append(g[ed[i].u], ed[i].v)
		g[ed[i].v]=append(g[ed[i].v], ed[i].u)
	}
	for _, adj := range g {
		sort.Ints(adj)
	}
	sort.Slice(ed, func(i,j int)bool{
		return ed[i].w < ed[j].w
	})
	cs := make([]int, n)
	d := newDSU(n)
	for i:=0;i<n;i++ {
		last := -1
		for j:=0;j<len(g[i]); j++ {
			if last + 1 < g[i][j] {
				d.merge(i, g[i][j]-1)
				cs[last+2]+=1
				cs[g[i][j]]-=1
			}
			last=g[i][j]
		}
		if last+1 < n {
			d.merge(i, n-1)
			if last + 2 < n {
				cs[last+2] += 1
			}
		}
	}
	for i:=1;i<n;i++{
		cs[i]+=cs[i-1]
		if cs[i] > 0 {
			d.merge(i-1, i)
		}
	}
	mst := int64(0)
	for _, e := range ed {
		if d.find(e.u) == d.find(e.v) {
			continue
		}
		d.merge(e.u, e.v)
		mst += int64(e.w)
	}
	if int64(n)*int64(n-1)/2 > int64(e + n-1) || xr == 0 {
		fmt.Fprintln(out, mst)
		return
	}

	useful := []edge{}
	for i := 0; i < n; i++ {
		v := 0
		for j := 0; j<len(g[i]); j++ {
			for ; v <= g[i][j]; v++ {
				if v == g[i][j] || v == i {
					continue
				}
				useful = append(useful, edge{
					u: i,
					v: v,
					w: 0,
				})
			}
		}
		for ; v < n; v++ {
			if v == i {
				continue
			}
			useful = append(useful, edge{
				u: i,
				v: v,
				w: 0,
			})
		}
	}
	d.reset()
	for _, e := range ed {
		if d.find(e.u) == d.find(e.v) {
			continue
		}
		d.merge(e.u, e.v)
		useful = append(useful, e)
	}
	mst += int64(xr)
	for i := 0; i < len(useful); i++ {
		if useful[i].w != 0 {
			continue
		}
		cur := int64(0)
		d.reset()
		for j, e := range useful {
			if i == j {
				continue
			}
			if d.find(e.u) == d.find(e.v) {
				continue
			}
			d.merge(e.u, e.v)
			cur += int64(e.w)
		}
		if mst > cur {
			mst = cur
		}
	}
	fmt.Fprintln(out, mst)
}

package main

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

type dsu struct{
	p []int
}

func newDSU(n int) *dsu{
	ret := &dsu{}
	ret.p = make([]int, n)
	for i:=0;i<n;i++{
		ret.p[i]=i
	}
	return ret
}

func (d *dsu) find(u int) int {
	if d.p[u] == u {
		return u
	}
	d.p[u] = d.find(d.p[u])
	return d.p[u]
}

func (d *dsu) merge(u, v int) {
	d.p[d.find(u)]=d.find(v)
}

func (d *dsu) reset() {
	for i := 0; i < len(d.p); i++ {
		d.p[i] = i
	}
}

type edge struct{
	u,v,w int
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var n,e int
	fmt.Fscan(in, &n, &e)

	g := make([][]int, n)

	xr := 0
	ed := make([]edge, e)
	for i := 0;i < e; i++{
		fmt.Fscan(in, &ed[i].u,&ed[i].v,&ed[i].w)
		ed[i].u--
		ed[i].v--
		xr ^= ed[i].w
		g[ed[i].u]=append(g[ed[i].u], ed[i].v)
		g[ed[i].v]=append(g[ed[i].v], ed[i].u)
	}
	for _, adj := range g {
		sort.Ints(adj)
	}
	sort.Slice(ed, func(i,j int)bool{
		return ed[i].w < ed[j].w
	})
	cs := make([]int, n)
	d := newDSU(n)
	for i:=0;i<n;i++ {
		last := -1
		for j:=0;j<len(g[i]); j++ {
			if last + 1 < g[i][j] {
				d.merge(i, g[i][j]-1)
				cs[last+2]+=1
				cs[g[i][j]]-=1
			}
			last=g[i][j]
		}
		if last+1 < n {
			d.merge(i, n-1)
			if last + 2 < n {
				cs[last+2] += 1
			}
		}
	}
	for i:=1;i<n;i++{
		cs[i]+=cs[i-1]
		if cs[i] > 0 {
			d.merge(i-1, i)
		}
	}
	mst := int64(0)
	for _, e := range ed {
		if d.find(e.u) == d.find(e.v) {
			continue
		}
		d.merge(e.u, e.v)
		mst += int64(e.w)
	}
	if int64(n)*int64(n-1)/2 > int64(e + n-1) || xr == 0 {
		if n > 600 {
			fmt.Println("!")
		}
		fmt.Fprintln(out, mst)
		return
	}

	useful := []edge{}
	for i := 0; i < n; i++ {
		v := 0
		for j := 0; j<len(g[i]); j++ {
			for ; v <= g[i][j]; v++ {
				if v == g[i][j] || v == i {
					continue
				}
				useful = append(useful, edge{
					u: i,
					v: v,
					w: 0,
				})
			}
		}
		for ; v < n; v++ {
			if v == i {
				continue
			}
			useful = append(useful, edge{
				u: i,
				v: v,
				w: 0,
			})
		}
	}
	d.reset()
	for _, e := range ed {
		if d.find(e.u) == d.find(e.v) {
			continue
		}
		d.merge(e.u, e.v)
		useful = append(useful, e)
	}
	mst += int64(xr)
	for i := 0; i < len(useful); i++ {
		if useful[i].w != 0 {
			continue
		}
		cur := int64(xr)
		d.reset()
		for j, e := range useful {
			if i == j {
				continue
			}
			if d.find(e.u) == d.find(e.v) {
				continue
			}
			d.merge(e.u, e.v)
			cur += int64(e.w)
		}
		if mst > cur {
			mst = cur
		}
	}
	fmt.Fprintln(out, mst)
}

package main

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

type dsu struct{
	p []int
}

func newDSU(n int) *dsu{
	ret := &dsu{}
	ret.p = make([]int, n)
	for i:=0;i<n;i++{
		ret.p[i]=i
	}
	return ret
}

func (d *dsu) find(u int) int {
	if d.p[u] == u {
		return u
	}
	d.p[u] = d.find(d.p[u])
	return d.p[u]
}

func (d *dsu) merge(u, v int) {
	d.p[d.find(u)]=d.find(v)
}

func (d *dsu) reset() {
	for i := 0; i < len(d.p); i++ {
		d.p[i] = i
	}
}

type edge struct{
	u,v,w int
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var n,e int
	fmt.Fscan(in, &n, &e)

	g := make([][]int, n)

	xr := 0
	ed := make([]edge, e)
	for i := 0;i < e; i++{
		fmt.Fscan(in, &ed[i].u,&ed[i].v,&ed[i].w)
		ed[i].u--
		ed[i].v--
		xr ^= ed[i].w
		g[ed[i].u]=append(g[ed[i].u], ed[i].v)
		g[ed[i].v]=append(g[ed[i].v], ed[i].u)
	}
	for _, adj := range g {
		sort.Ints(adj)
	}
	sort.Slice(ed, func(i,j int)bool{
		return ed[i].w < ed[j].w
	})
	cs := make([]int, n)
	d := newDSU(n)
	for i:=0;i<n;i++ {
		last := -1
		for j:=0;j<len(g[i]); j++ {
			if last + 1 < g[i][j] {
				d.merge(i, g[i][j]-1)
				cs[last+2]+=1
				cs[g[i][j]]-=1
			}
			last=g[i][j]
		}
		if last+1 < n {
			d.merge(i, n-1)
			if last + 2 < n {
				cs[last+2] += 1
			}
		}
	}
	for i:=1;i<n;i++{
		cs[i]+=cs[i-1]
		if cs[i] > 0 {
			d.merge(i-1, i)
		}
	}
	mst := int64(0)
	for _, e := range ed {
		if d.find(e.u) == d.find(e.v) {
			continue
		}
		d.merge(e.u, e.v)
		mst += int64(e.w)
	}
	if int64(n)*int64(n-1)/2 > int64(e + n-1) || xr == 0 {
		fmt.Fprintln(out, mst)
		return
	}

	useful := []edge{}
	for i := 0; i < n; i++ {
		v := 0
		for j := 0; j<len(g[i]); j++ {
			for ; v <= g[i][j]; v++ {
				if v == g[i][j] || v == i {
					continue
				}
				useful = append(useful, edge{
					u: i,
					v: v,
					w: 0,
				})
			}
		}
		for ; v < n; v++ {
			if v == i {
				continue
			}
			useful = append(useful, edge{
				u: i,
				v: v,
				w: 0,
			})
		}
	}
	d.reset()
	for _, e := range ed {
		if d.find(e.u) == d.find(e.v) {
			continue
		}
		d.merge(e.u, e.v)
		useful = append(useful, e)
	}
	mst += int64(xr)
	for i := 0; i < len(useful); i++ {
		if useful[i].w != 0 {
			continue
		}
		cur := int64(xr)
		d.reset()
		for j, e := range useful {
			if i == j {
				continue
			}
			if d.find(e.u) == d.find(e.v) {
				continue
			}
			d.merge(e.u, e.v)
			cur += int64(e.w)
		}
		if mst > cur {
			mst = cur
		}
	}
	fmt.Fprintln(out, mst)
}

package main

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

type dsu struct{
	p []int
}

func newDSU(n int) *dsu{
	ret := &dsu{}
	ret.p = make([]int, n)
	for i:=0;i<n;i++{
		ret.p[i]=i
	}
	return ret
}

func (d *dsu) find(u int) int {
	if d.p[u] == u {
		return u
	}
	d.p[u] = d.find(d.p[u])
	return d.p[u]
}

func (d *dsu) merge(u, v int) {
	d.p[d.find(u)]=d.find(v)
}

func (d *dsu) reset() {
	for i := 0; i < len(d.p); i++ {
		d.p[i] = i
	}
}

type edge struct{
	u,v,w int
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var n,e int
	fmt.Fscan(in, &n, &e)

	g := make([][]int, n)

	xr := 0
	ed := make([]edge, e)
	for i := 0;i < e; i++{
		fmt.Fscan(in, &ed[i].u,&ed[i].v,&ed[i].w)
		ed[i].u--
		ed[i].v--
		xr ^= ed[i].w
		g[ed[i].u]=append(g[ed[i].u], ed[i].v)
		g[ed[i].v]=append(g[ed[i].v], ed[i].u)
	}
	for _, adj := range g {
		sort.Ints(adj)
	}
	sort.Slice(ed, func(i,j int)bool{
		return ed[i].w < ed[j].w
	})
	cs := make([]int, n)
	d := newDSU(n)
	for i:=0;i<n;i++ {
		last := -1
		for j:=0;j<len(g[i]); j++ {
			if last + 1 < g[i][j] {
				d.merge(i, g[i][j]-1)
				cs[last+2]+=1
				cs[g[i][j]]-=1
			}
			last=g[i][j]
		}
		if last+2 < n {
			d.merge(i, n-1)
			cs[last+2] += 1
		}
	}
	for i:=1;i<n;i++{
		cs[i]+=cs[i-1]
		if cs[i] > 0 {
			d.merge(i-1, i)
		}
	}
	mst := int64(0)
	for _, e := range ed {
		if d.find(e.u) == d.find(e.v) {
			continue
		}
		d.merge(e.u, e.v)
		mst += int64(e.w)
	}
	if int64(n)*int64(n-1)/2 > int64(e + n-1) || xr == 0 {
		fmt.Fprintln(out, mst)
		return
	}

	useful := []edge{}
	for i := 0; i < n; i++ {
		v := 0
		for j := 0; j<len(g[i]); j++ {
			for ; v <= g[i][j]; v++ {
				if v == g[i][j] || v == i {
					continue
				}
				useful = append(useful, edge{
					u: i,
					v: v,
					w: 0,
				})
			}
		}
		for ; v < n; v++ {
			if v == i {
				continue
			}
			useful = append(useful, edge{
				u: i,
				v: v,
				w: 0,
			})
		}
	}
	d.reset()
	for _, e := range ed {
		if d.find(e.u) == d.find(e.v) {
			continue
		}
		d.merge(e.u, e.v)
		useful = append(useful, e)
	}
	mst += int64(xr)
	for i := 0; i < len(useful); i++ {
		if useful[i].w != 0 {
			continue
		}
		cur := int64(xr)
		d.reset()
		for j, e := range useful {
			if i == j {
				continue
			}
			if d.find(e.u) == d.find(e.v) {
				continue
			}
			d.merge(e.u, e.v)
			cur += int64(e.w)
		}
		if mst > cur {
			mst = cur
		}
	}
	fmt.Fprintln(out, mst)
}

package main

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

type dsu struct{
	p []int
}

func newDSU(n int) *dsu{
	ret := &dsu{}
	ret.p = make([]int, n)
	for i:=0;i<n;i++{
		ret.p[i]=i
	}
	return ret
}

func (d *dsu) find(u int) int {
	if d.p[u] == u {
		return u
	}
	d.p[u] = d.find(d.p[u])
	return d.p[u]
}

func (d *dsu) merge(u, v int) {
	d.p[d.find(u)]=d.find(v)
}

func (d *dsu) reset() {
	for i := 0; i < len(d.p); i++ {
		d.p[i] = i
	}
}

type edge struct{
	u,v,w int
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var n,e int
	fmt.Fscan(in, &n, &e)

	g := make([][]int, n)

	xr := 0
	ed := make([]edge, e)
	for i := 0;i < e; i++{
		fmt.Fscan(in, &ed[i].u,&ed[i].v,&ed[i].w)
		ed[i].u--
		ed[i].v--
		xr ^= ed[i].w
		g[ed[i].u]=append(g[ed[i].u], ed[i].v)
		g[ed[i].v]=append(g[ed[i].v], ed[i].u)
	}
	for _, adj := range g {
		sort.Ints(adj)
	}
	sort.Slice(ed, func(i,j int)bool{
		return ed[i].w < ed[j].w
	})
	cs := make([]int, n)
	d := newDSU(n)
	for i:=0;i<n;i++ {
		last := -1
		for j:=0;j<len(g[i]); j++ {
			if last + 1 < g[i][j] {
				d.merge(i, g[i][j]-1)
				cs[last+2]+=1
				cs[g[i][j]]-=1
			}
			last=g[i][j]
		}
		if last+2 < n {
			cs[last+2] += 1
		}
	}
	for i:=1;i<n;i++{
		cs[i]+=cs[i-1]
		if cs[i] > 0 {
			d.merge(i-1, i)
		}
	}
	mst := int64(0)
	for _, e := range ed {
		if d.find(e.u) == d.find(e.v) {
			continue
		}
		d.merge(e.u, e.v)
		mst += int64(e.w)
	}
	if int64(n)*int64(n-1)/2 > int64(e + n-1) || xr == 0 {
		fmt.Fprintln(out, mst)
		return
	}

	useful := []edge{}
	for i := 0; i < n; i++ {
		v := 0
		for j := 0; j<len(g[i]); j++ {
			for ; v <= g[i][j]; v++ {
				if v == g[i][j] || v == i {
					continue
				}
				useful = append(useful, edge{
					u: i,
					v: v,
					w: 0,
				})
			}
		}
		for ; v < n; v++ {
			if v == i {
				continue
			}
			useful = append(useful, edge{
				u: i,
				v: v,
				w: 0,
			})
		}
	}
	d.reset()
	for _, e := range ed {
		if d.find(e.u) == d.find(e.v) {
			continue
		}
		d.merge(e.u, e.v)
		useful = append(useful, e)
	}
	mst = int64(1e18)
	for i := 0; i < len(useful); i++ {
		if useful[i].w != 0 {
			continue
		}
		cur := int64(xr)
		d.reset()
		for j, e := range useful {
			if i == j {
				continue
			}
			if d.find(e.u) == d.find(e.v) {
				continue
			}
			d.merge(e.u, e.v)
			cur += int64(e.w)
		}
		if mst > cur {
			mst = cur
		}
	}
	fmt.Fprintln(out, mst)
}

package main

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

type dsu struct{
	p []int
}

func newDSU(n int) *dsu{
	ret := &dsu{}
	ret.p = make([]int, n)
	for i:=0;i<n;i++{
		ret.p[i]=i
	}
	return ret
}

func (d *dsu) find(u int) int {
	if d.p[u] == u {
		return u
	}
	d.p[u] = d.find(d.p[u])
	return d.p[u]
}

func (d *dsu) merge(u, v int) {
	d.p[d.find(u)]=d.find(v)
}

func (d *dsu) reset() {
	for i := 0; i < len(d.p); i++ {
		d.p[i] = i
	}
}

type edge struct{
	u,v,w int
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var n,e int
	fmt.Fscan(in, &n, &e)

	g := make([][]int, n)

	xr := 0
	ed := make([]edge, e)
	for i := 0;i < e; i++{
		fmt.Fscan(in, &ed[i].u,&ed[i].v,&ed[i].w)
		ed[i].u--
		ed[i].v--
		xr ^= ed[i].w
		g[ed[i].u]=append(g[ed[i].u], ed[i].v)
		g[ed[i].v]=append(g[ed[i].v], ed[i].u)
	}
	for _, adj := range g {
		sort.Ints(adj)
	}
	sort.Slice(ed, func(i,j int)bool{
		return ed[i].w < ed[j].w
	})
	cs := make([]int, n)
	d := newDSU(n)
	for i:=0;i<n;i++ {
		last := -1
		for j:=0;j<len(g[i]); j++ {
			if last + 1 < g[i][j] {
				d.merge(i, g[i][j]-1)
				cs[last+2]+=1
				cs[g[i][j]]-=1
			}
			last=g[i][j]
		}
		if last+2 < n {
			d.merge(i, n-1)
			cs[last+2] += 1
		}
	}
	for i:=1;i<n;i++{
		cs[i]+=cs[i-1]
		if cs[i] > 0 {
			d.merge(i-1, i)
		}
	}
	mst := int64(0)
	for _, e := range ed {
		if d.find(e.u) == d.find(e.v) {
			continue
		}
		d.merge(e.u, e.v)
		mst += int64(e.w)
	}
	if int64(n)*int64(n-1)/2 > int64(e + n-1) || xr == 0 {
		fmt.Fprintln(out, mst)
		return
	}

	useful := []edge{}
	for i := 0; i < n; i++ {
		v := 0
		for j := 0; j<len(g[i]); j++ {
			for ; v <= g[i][j]; v++ {
				if v == g[i][j] || v == i {
					continue
				}
				useful = append(useful, edge{
					u: i,
					v: v,
					w: 0,
				})
			}
		}
		for ; v < n; v++ {
			if v == i {
				continue
			}
			useful = append(useful, edge{
				u: i,
				v: v,
				w: 0,
			})
		}
	}
	d.reset()
	for _, e := range ed {
		if d.find(e.u) == d.find(e.v) {
			continue
		}
		d.merge(e.u, e.v)
		useful = append(useful, e)
	}
	mst = int64(1e18)
	for i := 0; i < len(useful); i++ {
		if useful[i].w != 0 {
			continue
		}
		cur := int64(xr)
		d.reset()
		for j, e := range useful {
			if i == j {
				continue
			}
			if d.find(e.u) == d.find(e.v) {
				continue
			}
			d.merge(e.u, e.v)
			cur += int64(e.w)
		}
		if mst > cur {
			mst = cur
		}
	}
	fmt.Fprintln(out, mst)
}


Tokitsukaze has a permutation $$$p$$$. She performed the following operation to $$$p$$$ exactly $$$k$$$ times: in one operation, for each $$$i$$$ from $$$1$$$ to $$$n - 1$$$ in order, if $$$p_i$$$ > $$$p_{i+1}$$$, swap $$$p_i$$$, $$$p_{i+1}$$$. After exactly $$$k$$$ times of operations, Tokitsukaze got a new sequence $$$a$$$, obviously the sequence $$$a$$$ is also a permutation.After that, Tokitsukaze wrote down the value sequence $$$v$$$ of $$$a$$$ on paper. Denote the value sequence $$$v$$$ of the permutation $$$a$$$ of length $$$n$$$ as $$$v_i=\sum_{j=1}^{i-1}[a_i < a_j]$$$, where the value of $$$[a_i < a_j]$$$ define as if $$$a_i < a_j$$$, the value is $$$1$$$, otherwise is $$$0$$$ (in other words, $$$v_i$$$ is equal to the number of elements greater than $$$a_i$$$ that are to the left of position $$$i$$$). Then Tokitsukaze went out to work.There are three naughty cats in Tokitsukaze's house. When she came home, she found the paper with the value sequence $$$v$$$ to be bitten out by the cats, leaving several holes, so that the value of some positions could not be seen clearly. She forgot what the original permutation $$$p$$$ was. She wants to know how many different permutations $$$p$$$ there are, so that the value sequence $$$v$$$ of the new permutation $$$a$$$ after exactly $$$k$$$ operations is the same as the $$$v$$$ written on the paper (not taking into account the unclear positions).Since the answer may be too large, print it modulo $$$998\,244\,353$$$.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Each test case consists of two lines. The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 10^6$$$; $$$0 \leq k \leq n-1$$$) — the length of the permutation and the exactly number of operations. The second line contains $$$n$$$ integers $$$v_1, v_2, \dots, v_n$$$ ($$$-1 \leq v_i \leq i-1$$$) — the value sequence $$$v$$$. $$$v_i = -1$$$ means the $$$i$$$-th position of $$$v$$$ can't be seen clearly. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$.

For each test case, print a single integer — the number of different permutations modulo $$$998\,244\,353$$$.

package main

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

const (
	MOD = 998244353
	N   = 1000005
)

func solve(myio *MyIO) {
	var n int
	var k int
	//Fscan(in, &n, &k)
	n = myio.ReadInt()
	k = myio.ReadInt()

	var v int32
	var ans int64 = 1
	for i := 0; i < n; i++ {
		v = int32(myio.ReadInt())
		if i >= n-k {
			if v > 0 {
				ans = 0
			}
		} else if v == -1 {
			ans = ans * int64(i+k+1) % MOD
		} else if v == 0 {
			ans = ans * int64(k+1) % MOD
		}
	}
	if ans != 0 {
		for i := 1; i <= k; i++ {
			ans = ans * int64(i) % MOD
		}
	}

	myio.Println(ans)
}

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<<16),
	}
}

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 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 Fprintf(myio.out, format, a...)
}

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

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

func main() {
	//in := bufio.NewReader(os.Stdin)
	//out := bufio.NewWriter(os.Stdout)
	//defer out.Flush()
	myio := NewIO(os.Stdin, os.Stdout)
	defer myio.Flush()
	var T int
	for T = myio.ReadInt(); T > 0; T-- {
		solve(myio)
	}
}

package main

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

const (
	MOD = 998244353
	N   = 1000005
)

func solve(in io.Reader, out io.Writer) {
	var n int
	var k int
	Fscan(in, &n, &k)

	var v int32
	var ans int64 = 1
	for i := 0; i < n; i++ {
		Fscan(in, &v)
		if i >= n-k {
			if v > 0 {
				ans = 0
			}
		} else if v == -1 {
			ans = ans * int64(i+k+1) % MOD
		} else if v == 0 {
			ans = ans * int64(k+1) % MOD
		}
	}
	if ans != 0 {
		for i := 1; i <= k; i++ {
			ans = ans * int64(i) % MOD
		}
	}

	Fprintln(out, ans)

}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var T int
	for Fscan(in, &T); T > 0; T-- {
		solve(in, out)
	}
}

package main

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

const (
	MOD = 998244353
	N   = 1000005
)

var fac []int64

func initialize() {
	fac = make([]int64, N)
	fac[0] = 1
	for i := 1; i < N; i++ {
		fac[i] = fac[i-1] * int64(i) % MOD
	}
}
func solve(in io.Reader, out io.Writer) {
	var n int
	var k int
	Fscan(in, &n, &k)

	var v int32
	ans := fac[k]
	for i := 0; i < n; i++ {
		Fscan(in, &v)
		if i >= n-k {
			if v > 0 {
				ans = 0
			}
		} else if v == -1 {
			ans = ans * int64(i+k+1) % MOD
		} else if v == 0 {
			ans = ans * int64(k+1) % MOD
		}
	}

	Fprintln(out, ans)

}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	initialize()
	var T int
	for Fscan(in, &T); T > 0; T-- {
		solve(in, out)
	}
}

package main

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

const (
	MOD = 998244353
	N   = 1000005
)

var fac []int64

func initialize() {
	fac = make([]int64, N)
	fac[0] = 1
	for i := 1; i < N; i++ {
		fac[i] = fac[i-1] * int64(i) % MOD
	}
}
func solve(in io.Reader, out io.Writer) {
	var n int
	var k int
	Fscan(in, &n, &k)

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

	for i := n - 1; i >= n-k; i-- {
		if v[i] > 0 {
			Fprintln(out, 0)
			return
		}
	}

	ans := fac[k]
	for i := 0; i < n-k; i++ {
		if v[i] == -1 {
			ans = ans * int64(i+k+1) % MOD
		} else if v[i] == 0 {
			ans = ans * int64(k+1) % MOD
		}
	}
	Fprintln(out, ans)

}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	initialize()
	var T int
	for Fscan(in, &T); T > 0; T-- {
		solve(in, out)
	}
}

package main

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

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<<16),
	}
}

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

const mod = 998244353

func main() {
	myio := NewIO(os.Stdin, os.Stdout)
	defer myio.Flush()

	t := myio.ReadInt()
	sol := func() {
		n := myio.ReadInt()
		k := myio.ReadInt()
		var ans int64 = 1
		for i := 1; i <= k; i++ {
			ans = ans * (int64)(i) % mod
		}
		for j := 0; j < n; j++ {
			vj := myio.ReadInt()
			i := j + k
			if i >= n {
				if vj > 0 {
					ans = 0
				}
			} else {
				if vj == -1 {
					ans = ans * (int64)(i+1) % mod //
				} else if vj == 0 {
					ans = ans * (int64)(k+1) % mod // 0-k
				}
			}
		}
		myio.Println(ans)
	}
	for ; t > 0; t-- {
		sol()
	}
}

package main

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

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

const mod = 998244353

func main() {
	myio := NewIO(os.Stdin, os.Stdout)
	defer myio.Flush()

	t := myio.ReadInt()
	sol := func() {
		n := myio.ReadInt()
		k := myio.ReadInt()
		var ans int64 = 1
		for i := 1; i <= k; i++ {
			ans = ans * (int64)(i) % mod
		}
		for j := 0; j < n; j++ {
			vj := myio.ReadInt()
			i := j + k
			if i >= n {
				if vj > 0 {
					ans = 0
				}
			} else {
				if vj == -1 {
					ans = ans * (int64)(i+1) % mod
				} else if vj == 0 {
					ans = ans * (int64)(k+1) % mod
				}
			}
		}
		myio.Println(ans)
	}
	for ; t > 0; t-- {
		sol()
	}
}

package main

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

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

const mod = 998244353

func main() {
	myio := NewIO(os.Stdin, os.Stdout)
	defer myio.Flush()

	t := myio.ReadInt()
	sol := func() {
		n := myio.ReadInt()
		k := myio.ReadInt()
		var ans int64 = 1
		for i := 1; i <= k; i++ {
			ans = ans * (int64)(i) % mod
		}
		for j := 0; j < n; j++ {
			vj := myio.ReadInt()
			i := j + k
			if i >= n {
				if vj > 0 {
					ans = 0
				}
			} else {
				if vj == -1 {
					ans = ans * (int64)(i+1) % mod //
				} else if vj == 0 {
					ans = ans * (int64)(k+1) % mod // 0-k
				}
			}
		}
		myio.Println(ans)
	}
	for ; t > 0; t-- {
		sol()
	}
}

package main

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

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

const mod = 998244353

func main() {
	myio := NewIO(os.Stdin, os.Stdout)
	defer myio.Flush()

	t := myio.ReadInt()
	sol := func() {
		n := myio.ReadInt()
		k := myio.ReadInt()
		v := make([]int, n)
		var ans int64 = 1
		for i := 1; i <= k; i++ {
			ans = ans * (int64)(i) % mod
		}
		for i := 0; i < n; i++ {
			v[i] = myio.ReadInt()
		}
		for i := k; i < n; i++ {
			j := i - k
			if v[j] == -1 {
				ans = ans * (int64)(i+1) % mod //
			} else if v[j] == 0 {
				ans = ans * (int64)(k+1) % mod // 0-k
			}
		}
		for i := n - k; i < n; i++ {
			if v[i] > 0 {
				ans = 0
				break
			}
		}
		myio.Println(ans)
	}
	for ; t > 0; t-- {
		sol()
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)
var wrtr = bufio.NewWriterSize(os.Stdout, 10_000_000)
var rdr = bufio.NewScanner(os.Stdin)
func gs() string  { rdr.Scan(); return rdr.Text() }
func gi() int     { i,e := strconv.Atoi(gs()); if e != nil {panic(e)}; return i }
func gis(n int) []int  { res := make([]int,n); for i:=0;i<n;i++ { res[i] = gi() }; return res }
const MOD int64 = 998244353
func main() {
	//f1, _ := os.Create("cpu.prof"); pprof.StartCPUProfile(f1); defer pprof.StopCPUProfile()
	defer wrtr.Flush()
	infn := ""; if infn == "" && len(os.Args) > 1 {	infn = os.Args[1] }
	if infn != "" {	f, e := os.Open(infn); if e != nil { panic(e) }; rdr = bufio.NewScanner(f) }
	rdr.Split(bufio.ScanWords); rdr.Buffer(make([]byte,1024),1_000_000_000)
	// PROGRAM STARTS HERE
	T := gi()
	for tt:=1;tt<=T;tt++ {
		N,K := gi(),gi()
		V := gis(N)
		bad := false; ans := int64(1)
		// First, the last K entries should be the last entries of P in sorted order, so Vi should be 0 or -1
		for i:=1;i<=K;i++ { ans *= int64(i); ans %= MOD }
		for i,v := range V {
			if i < N-K {
				if v == -1 { ans *= int64(i+K+1); ans %= MOD }
				if v == 0  { ans *= int64(K+1); ans %= MOD }
			} else {
				if v != -1 && v != 0 { bad = true }
			}
		}
		if bad { ans = 0 }
		fmt.Fprintln(wrtr,ans)
	}
}

package main

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

const (
	MOD = 998244353
	N   = 1000005
)

var fac []int64

func initialize() {
	fac = make([]int64, N)
	fac[0] = 1
	for i := 1; i < N; i++ {
		fac[i] = fac[i-1] * int64(i) % MOD
	}
}
func solve(in io.Reader, out io.Writer) {
	var n int
	var k int
	Fscan(in, &n, &k)

	var v int32
	ans := fac[k]
	for i := 0; i < n; i++ {
		Fscan(in, &v)
		if i >= n-k {
			if v > 0 {
				ans = 0
			}
		} else if v == 0 {
			ans = ans * int64(i+k+1) % MOD
		} else if v == -1 {
			ans = ans * int64(k+1) % MOD
		}
	}

	Fprintln(out, ans)

}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	initialize()
	var T int
	for Fscan(in, &T); T > 0; T-- {
		solve(in, out)
	}
}


You are given an array $$$a$$$ consisting of $$$n$$$ integers.Your task is to determine if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome.Recall that an array $$$b$$$ is called a subsequence of the array $$$a$$$ if $$$b$$$ can be obtained by removing some (possibly, zero) elements from $$$a$$$ (not necessarily consecutive) without changing the order of remaining elements. For example, $$$[2]$$$, $$$[1, 2, 1, 3]$$$ and $$$[2, 3]$$$ are subsequences of $$$[1, 2, 1, 3]$$$, but $$$[1, 1, 2]$$$ and $$$[4]$$$ are not.Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $$$a$$$ of length $$$n$$$ is the palindrome if $$$a_i = a_{n - i - 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. For example, arrays $$$[1234]$$$, $$$[1, 2, 1]$$$, $$$[1, 3, 2, 2, 3, 1]$$$ and $$$[10, 100, 10]$$$ are palindromes, but arrays $$$[1, 2]$$$ and $$$[1, 2, 3, 1]$$$ are not.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$3 \le n \le 5000$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$ ($$$\sum n \le 5000$$$).

For each test case, print the answer — "YES" (without quotes) if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome and "NO" otherwise.

NoteIn the first test case of the example, the array $$$a$$$ has a subsequence $$$[1, 2, 1]$$$ which is a palindrome.In the second test case of the example, the array $$$a$$$ has two subsequences of length $$$3$$$ which are palindromes: $$$[2, 3, 2]$$$ and $$$[2, 2, 2]$$$.In the third test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes.In the fourth test case of the example, the array $$$a$$$ has one subsequence of length $$$4$$$ which is a palindrome: $$$[1, 2, 2, 1]$$$ (and has two subsequences of length $$$3$$$ which are palindromes: both are $$$[1, 2, 1]$$$).In the fifth test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes.

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	var t int
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		var n, a, prev int
		fmt.Fscan(in, &n)
		d := make(map[int]bool, n)
		fmt.Fscan(in, &prev)
		result := "NO"
		for i := 1; i < n; i++ {
			fmt.Fscan(in, &a)
			if d[a] {
				result = "YES"
			}
			d[prev] = true
			prev = a
		}
		fmt.Fprintln(out, result)
	}
	out.Flush()
}

package main

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

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

	var T, n, e int
	fmt.Fscan(r, &T)
	for t := 0; t < T; t++ {
		fmt.Fscan(r, &n)
		m := make(map[int]int)
		suc := false
		for i := 0; i < n; i++{
			fmt.Fscan(r, &e)
			v, ok := m[e]
			if ok && i-v > 1 {
				suc = true
			}
			if !ok {
				m[e] = i
			}
		}
		if suc {
			fmt.Fprint(w, "YES\n")
		}else {
			fmt.Fprint(w, "NO\n")
		}
	}

}

package main

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

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

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

	t := readInt(scanner)
	for i := 0; i < t; i++ {
		n := readInt(scanner)
		idx := make([]int, n+1)

		for j := range(idx) {
			idx[j] = -1
		}

		found := false
		for j := 0; j < n; j++ {
			x := readInt(scanner)
			if idx[x] == -1 {
				idx[x] = j
			} else if j - idx[x] > 1 {
				found = true
			}
		}

		if found {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import "fmt"

func main(){

	var t,n int
	fmt.Scan(&t)

	for T := 0 ; T < t ; T ++ {
		fmt.Scan(&n)
		a := make([]int , n)

		for i := 0 ; i < n ; i ++ {
			fmt.Scan(&a[i])
		}

		m := make(map[int]int)
		q := 0
		for i := 0 ; i < n ; i ++ {
			val , ok := m[a[i]]
			if ok == true {
				if  (i - val) > 1 {
					q = 1
					break
				}
			} else {
				m[a[i]] = i
			}
		}

		if q == 1 {
			fmt.Println("YES")
		} else {
			fmt.Println( "NO")
		}
	}
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var cases int
	fmt.Scanf("%d\n", &cases)
	for i := 0; i < cases; i++ {
		line := readLine(reader)
		n, _ := strconv.Atoi(string(line))
		line = readLine(reader)
		ans := strings.Split(string(line), " ")
		m := make(map[int]int)
		an := make([]int, n)
		flag := false
		for i := 0; i < n; i++ {
			x, _ := strconv.Atoi(ans[i])
			an[i] = x
			if i>=1 {
				v := m[x]
				v++
				m[x] = v
			}
		}
		// fmt.Println(m)
		for i := 1; i < n-1; i++ {
			if v := m[an[i]]; v==1 {
				delete(m, an[i])
			} else {
				m[an[i]] = v-1
			}
			if _, ok := m[an[i-1]]; ok {
				flag = true
				break
			}
		}
		if flag {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

func readLine(reader *bufio.Reader) []byte {
	result := make([]byte, 0)
	for {
		s, isPrefix, err := reader.ReadLine()
		if err != nil {
			fmt.Printf("error=%+v\n", err)
			break
		}
		result = append(result, s...)
		if !isPrefix {
			break
		}
	}
	return result
}

package main

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

const N int = 5005

var y [N]int
var t int

func null() {
	for i := 0; i < N; i++ {
		y[i] = 0
	}
}

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)

	fmt.Fscan(input, &t)

	for o := 0; o < t; o++ {
		null()
		var n int
		fmt.Fscan(input, &n)

		ok := false
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(input, &arr[i])
			y[arr[i]]++

			if y[arr[i]] == 3 {
				ok = true
			}

			if i > 0 && y[arr[i]] == 2 && arr[i] != arr[i-1] {
				ok = true
			}
		}

		if ok == true {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

var inf int = 0x3f3f3f3f
var reader *bufio.Reader

func init() {
	stdin := os.Stdin
	// stdin, _ = os.Open("1.in")
	reader = bufio.NewReaderSize(stdin, 1<<20)
}

var t, n int

func main() {
	fmt.Fscan(reader, &t)
	for ; t != 0; t-- {
		fmt.Fscan(reader, &n)
		m := make(map[int][]int)
		for i := 0; i < n; i++ {
			var a int
			fmt.Fscan(reader, &a)
			m[a] = append(m[a], i)
		}
		ans := "NO"
		for _, v := range m {
			if len(v) == 1 {
				continue
			}
			for i := 1; i < len(v); i++ {
				if v[i] != v[i-1]+1 {
					ans = "YES"
					break
				}
				if i > 1 && v[i] == v[i-1]+1 && v[i] == v[i-2]+2 {
					ans = "YES"
					break
				}
			}
			if ans == "YES" {
				break
			}
		}
		fmt.Println(ans)
	}
}

package main

import (
	"fmt"
	"math"
)

// Node
type N struct {
	count    int
	position []int
}

func main() {
	var t, n, x int
	fmt.Scan(&t)
	for t != 0 {
		fmt.Scan(&n)
		var count map[int]*N = make(map[int]*N)
		for i := 0; i < n; i++ {
			fmt.Scan(&x)
			if count[x] == nil {
				count[x] = new(N)
			}
			count[x].count++
			count[x].position = append(count[x].position, i)
		}
		// fmt.Println(count)
		ans := false
		for _, v := range count {
			if v.count >= 3 {
				ans = true
				break
			} else if v.count == 2 {
				if int(math.Abs(float64(v.position[0]-v.position[1]))) != 1 {
					ans = true
					break
				}
			}
		}
		if ans == true {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
		t--
	}
}

package main

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

func main() {
   for t := ScanInt(); t > 0; t-- {
      n := ScanInt()
      a := ScanIntSlice(n)
      r := false
   outer:
      for i := 0; i < n; i++ {
         for k := i + 2; k < n; k++ {
            if a[i] == a[k] {
               r = true
               break outer
            }
         }
      }
      if r {
         P("YES")
      } else {
         P("NO")
      }
   }

   close(O)
   oWg.Wait()
}

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

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

func init() {
   //P = fmt.Println

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

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

   writer := bufio.NewWriter(os.Stdout)
   go func() {
      defer oWg.Done()
      defer writer.Flush()

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

func ScanWord() string { in.Scan(); return in.Text() }
func ScanInt() int     { i, _ := strconv.Atoi(ScanWord()); return i }
func ScanInt64() int64 { i, _ := strconv.ParseInt(ScanWord(), 10, 64); return i }
func ScanStringSlice(n int) []string {
   s := make([]string, n)
   for i := 0; i < n; i++ {
      s[i] = ScanWord()
   }
   return s
}
func ScanIntSlice(n int) []int {
   arr := make([]int, n)
   for i := 0; i < n; i++ {
      arr[i] = ScanInt()
   }
   return arr
}
func ScanInt64Slice(n int) []int64 {
   arr := make([]int64, n)
   for i := 0; i < n; i++ {
      arr[i] = ScanInt64()
   }
   return arr
}

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

func S(v interface{}) string { return fmt.Sprintf("%v", v) }
func SArr(a []int) string {
   sb := strings.Builder{}
   sb.Grow(len(a) * 9)
   for i := range a {
      sb.WriteString(strconv.Itoa(a[i]) + " ")
   }
   return sb.String()
}
func SArr64(a []int64) string {
   sb := strings.Builder{}
   sb.Grow(len(a) * 9)
   for i := range a {
      sb.WriteString(strconv.FormatInt(a[i], 10) + " ")
   }
   return sb.String()
}
func MnMx(args ...int64) (int64, int64) {
   minVal, maxVal := int64(math.MaxInt64), int64(math.MinInt64)
   for _, v := range args {
      if v < minVal {
         minVal = v
      }
      if v > maxVal {
         maxVal = v
      }
   }
   return minVal, maxVal
}
func Mn(args ...int64) int64 { mn, _ := MnMx(args...); return mn }
func Mx(args ...int64) int64 { _, mx := MnMx(args...); return mx }
func LCM(a, b int64) int64   { return a / GCD(a, b) * b }
func GCD(a, b int64) int64 {
   if a == 0 {
      return b
   }
   return GCD(b%a, a)
}
func sortInt64Slice(a []int64) { sort.Slice(a, func(i, j int) bool { return a[i] < a[j] }) }
func abs(x int64) int64 {
   if x < 0 {
      return -x
   }
   return x
}

package main

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

func main() {
	r := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscan(r, &t)
	for tc := 0; tc < t; tc++ {
		var n int
		fmt.Fscan(r, &n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(r, &a[i])
		}
		rls := solve(a)
		if rls {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

func solve(a []int) bool {
	m := make(map[int]int)
	for i, v := range a {
		idx, ok := m[v]
		if ok {
			if idx+1 != i {
				return true
			}
		} else {
			m[v] = i
		}
	}
	return false
}

package main

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

type heap struct {
	array []int
}

func (h *heap) push(val int) {
	h.array = append(h.array, val)
	cur := len(h.array) - 1
	for cur > 0 {
		var parent int
		if (cur & 1) == 1 {
			parent = (cur - 1) / 2
		} else {
			parent = (cur - 2) / 2
		}
		if h.array[cur] < h.array[parent] {
			h.array[cur], h.array[parent] = h.array[parent], h.array[cur]
			cur = parent
		} else {
			break
		}
	}
}

func (h *heap) pop() int {
	res := h.array[0]
	h.array[0], h.array[len(h.array)-1] = h.array[len(h.array)-1], h.array[0]
	h.array = h.array[:len(h.array)-1]
	cur := 0
	for cur < len(h.array) {
		smallest := cur
		if cur*2+1 < len(h.array) && h.array[smallest] > h.array[cur*2+1] {
			smallest = cur*2 + 1
		}
		if cur*2+2 < len(h.array) && h.array[smallest] > h.array[cur*2+2] {
			smallest = cur*2 + 2
		}
		if smallest != cur {
			h.array[smallest], h.array[cur] = h.array[cur], h.array[smallest]
			cur = smallest
		} else {
			break
		}
	}
	return res
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var T int
	Fscan(in, &T)
	for T > 0 {
		T--
		var n, tmp int
		Fscan(in, &n)
		cnt := make(map[int][]int)
		for i := 0; i < n; i++ {
			Fscan(in, &tmp)
			cnt[tmp] = append(cnt[tmp], i)
		}
		flag := false
		for _, arr := range cnt {
			if len(arr) > 2{
				flag = true
				break
			}
			for i := 1; i < len(arr); i++ {
				if arr[i] != arr[i-1]+1 {
					flag = true
					break
				}
			}
		}
		if flag {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
}

package main

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

var (
	reader Reader = NewReader()
	writer Writer = NewWriter()
)

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

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

type readerImpl struct {
	*bufio.Scanner
}

func NewReader() Reader {
	s := bufio.NewScanner(os.Stdin)
	s.Split(bufio.ScanWords)
	return &readerImpl{Scanner: s}
}

func (i *readerImpl) NextInt() int {
	i.Scan()
	ret, _ := strconv.Atoi(i.Text())
	return ret
}

func (i *readerImpl) NextInt64() int64 {
	i.Scan()
	ret, _ := strconv.ParseInt(i.Text(), 10, 64)
	return ret
}

func (i *readerImpl) NextString() string {
	i.Scan()
	return i.Text()
}

type writerImpl struct {
	*bufio.Writer
}

func NewWriter() Writer {
	return &writerImpl{Writer: bufio.NewWriter(os.Stdout)}
}

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

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

func main() {
	defer writer.Flush()
	t := reader.NextInt()
	for ; t > 0; t-- {
		n := reader.NextInt()
		tmp := []int{}
		for ; n > 0; n-- {
			tmp = append(tmp, reader.NextInt())
		}
		writer.Printf("%s\n", sol(tmp))
	}
}

func sol(aa []int) string {
	if len(aa) < 3 {
		return "NO"
	}
	lmap := map[int]int{}
	rmap := map[int]int{}
	for i := 0; i < len(aa); i++ {
		rmap[aa[i]]++
	}
	for i := 0; i < len(aa); i++ {
		rmap[aa[i]]--
		for k, v := range lmap {
			if v > 0 && rmap[k] > 0 {
				return "YES"
			}
		}
		lmap[aa[i]]++
	}
	return "NO"
}

package main

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

var (
	reader Reader = NewReader()
	writer Writer = NewWriter()
)

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

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

type readerImpl struct {
	*bufio.Scanner
}

func NewReader() Reader {
	s := bufio.NewScanner(os.Stdin)
	s.Split(bufio.ScanWords)
	return &readerImpl{Scanner: s}
}

func (i *readerImpl) NextInt() int {
	i.Scan()
	ret, _ := strconv.Atoi(i.Text())
	return ret
}

func (i *readerImpl) NextInt64() int64 {
	i.Scan()
	ret, _ := strconv.ParseInt(i.Text(), 10, 64)
	return ret
}

func (i *readerImpl) NextString() string {
	i.Scan()
	return i.Text()
}

type writerImpl struct {
	*bufio.Writer
}

func NewWriter() Writer {
	return &writerImpl{Writer: bufio.NewWriter(os.Stdout)}
}

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

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

func main() {
	defer writer.Flush()
	t := reader.NextInt()
	for ; t > 0; t-- {
		n := reader.NextInt()
		tmp := map[int][]int{}
		for i := 0; i < n; i++ {
			a := reader.NextInt()
			tmp[a] = append(tmp[a], i)
		}
		writer.Printf("%s\n", sol(tmp))
	}
}

func sol(aa map[int][]int) string {
    //writer.Printf("%v\n", aa)
	for _, pp := range aa {
        if len(pp) >= 3 {
            return "YES"
        }
        if (len(pp) == 2) && pp[1] - pp[0] > 1 {
            return "YES"
        }
	}
	return "NO"
}

package main

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

func solve(in *bufio.Reader) string {
	var n, a, prev int
	fmt.Fscan(in, &n)
	d := make(map[int]bool)
	fmt.Fscan(in, &prev)
	for i := 1; i < n; i++ {
		fmt.Fscan(in, &a)
		if d[a] {
			return "YES"
		}
		d[prev] = true
		prev = a
	}
	return "NO"
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	var t int
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		fmt.Fprintln(out, solve(in))
	}
	out.Flush()
}

package main

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

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

	var T, n, e int
	fmt.Fscan(r, &T)
	for t := 0; t < T; t++ {
		fmt.Fscan(r, &n)
		m := make(map[int]int)
		suc := false
		for i := 0; i < n; i++{
			fmt.Fscan(r, &e)
			if v, ok := m[e]; ok && i-v > 1 {
				suc = true
			}
			m[e] = i
		}
		if suc {
			fmt.Fprint(w, "YES\n")
		}else {
			fmt.Fprint(w, "NO\n")
		}
	}

}

package main

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

const N int = 5005

var y [N]int
var t int

func null() {
	for i := 0; i < N; i++ {
		y[i] = 0
	}
}

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)

	fmt.Fscan(input, &t)

	for o := 0; o < t; o++ {
		null()
		var n int
		fmt.Fscan(input, &n)

		ok := false
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(input, &arr[i])
			y[arr[i]]++
			if i > 0 && y[arr[i]] >= 2 && arr[i] != arr[i-1] {
				ok = true
			}
		}

		if ok == true {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

var t int

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)

	fmt.Fscan(input, &t)

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

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

		ok := false
		for i := 1; i < (n - 1); i++ {
			if arr[i-1] == arr[i+1] {
				ok = true
				break
			}

			if (i + 2) < n {
				if arr[i-1] == arr[i+2] && arr[i] == arr[i+1] {
					ok = true
					break
				}
			}
		}

		if ok == true {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

var t int

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)

	fmt.Fscan(input, &t)

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

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

		if n == 4 {
			if arr[0] == arr[3] && arr[1] == arr[2] {
				fmt.Println("YES")
			} else {
				fmt.Println("NO")
			}
			continue
		}

		ok := false
		for i := 1; i < (n - 1); i++ {
			if arr[i-1] == arr[i+1] {
				ok = true
				break
			}
		}

		if ok == true {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

var inf int = 0x3f3f3f3f
var reader *bufio.Reader

func init() {
	stdin := os.Stdin
	// stdin, _ = os.Open("1.in")
	reader = bufio.NewReaderSize(stdin, 1<<20)
}

var t, n int

func main() {
	fmt.Fscan(reader, &t)
	for ; t != 0; t-- {
		fmt.Fscan(reader, &n)
		m := make(map[int][]int)
		for i := 0; i < n; i++ {
			var a int
			fmt.Fscan(reader, &a)
			m[a] = append(m[a], i)
		}
		ans := "NO"
		for _, v := range m {
			if len(v) == 1 {
				continue
			}
			for i := 1; i < len(v); i++ {
				if v[i] != v[i-1]+1 {
					ans = "YES"
					break
				}
			}
			if ans == "YES" {
				break
			}
		}
		fmt.Println(ans)
	}
}

package main

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

type heap struct {
	array []int
}

func (h *heap) push(val int) {
	h.array = append(h.array, val)
	cur := len(h.array) - 1
	for cur > 0 {
		var parent int
		if (cur & 1) == 1 {
			parent = (cur - 1) / 2
		} else {
			parent = (cur - 2) / 2
		}
		if h.array[cur] < h.array[parent] {
			h.array[cur], h.array[parent] = h.array[parent], h.array[cur]
			cur = parent
		} else {
			break
		}
	}
}

func (h *heap) pop() int {
	res := h.array[0]
	h.array[0], h.array[len(h.array)-1] = h.array[len(h.array)-1], h.array[0]
	h.array = h.array[:len(h.array)-1]
	cur := 0
	for cur < len(h.array) {
		smallest := cur
		if cur*2+1 < len(h.array) && h.array[smallest] > h.array[cur*2+1] {
			smallest = cur*2 + 1
		}
		if cur*2+2 < len(h.array) && h.array[smallest] > h.array[cur*2+2] {
			smallest = cur*2 + 2
		}
		if smallest != cur {
			h.array[smallest], h.array[cur] = h.array[cur], h.array[smallest]
			cur = smallest
		} else {
			break
		}
	}
	return res
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var T int
	Fscan(in, &T)
	for T > 0 {
		T--
		var n, tmp int
		Fscan(in, &n)
		cnt := make(map[int][]int)
		for i := 0; i < n; i++ {
			Fscan(in, &tmp)
			cnt[tmp] = append(cnt[tmp], i)
		}
		flag := false
		for _, arr := range cnt {
			for i := 1; i < len(arr); i++ {
				if arr[i] != arr[i-1]+1 {
					flag = true
					break
				}
			}
		}
		if flag {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
}

package main

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

var (
	reader Reader = NewReader()
	writer Writer = NewWriter()
)

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

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

type readerImpl struct {
	*bufio.Scanner
}

func NewReader() Reader {
	s := bufio.NewScanner(os.Stdin)
	s.Split(bufio.ScanWords)
	return &readerImpl{Scanner: s}
}

func (i *readerImpl) NextInt() int {
	i.Scan()
	ret, _ := strconv.Atoi(i.Text())
	return ret
}

func (i *readerImpl) NextInt64() int64 {
	i.Scan()
	ret, _ := strconv.ParseInt(i.Text(), 10, 64)
	return ret
}

func (i *readerImpl) NextString() string {
	i.Scan()
	return i.Text()
}

type writerImpl struct {
	*bufio.Writer
}

func NewWriter() Writer {
	return &writerImpl{Writer: bufio.NewWriter(os.Stdout)}
}

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

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

func main() {
	defer writer.Flush()
	t := reader.NextInt()
	for ; t > 0; t-- {
		n := reader.NextInt()
		tmp := map[int][]int{}
		for i := 0; i < n; i++ {
			a := reader.NextInt()
			tmp[a] = append(tmp[a], i)
		}
		writer.Printf("%s\n", sol(tmp))
	}
}

func sol(aa map[int][]int) string {
	for _, pp := range aa {
		for k := range pp {
			if k-1 >= 0 && pp[k]-pp[k-1] > 1 {
				return "YES"
			}
			if k+1 < len(pp) && pp[k+1]-pp[k] > 1 {
				return "YES"
			}
		}
	}
	return "NO"
}


Recently, Tokitsukaze found an interesting game. Tokitsukaze had $$$n$$$ items at the beginning of this game. However, she thought there were too many items, so now she wants to discard $$$m$$$ ($$$1 \le m \le n$$$) special items of them.These $$$n$$$ items are marked with indices from $$$1$$$ to $$$n$$$. In the beginning, the item with index $$$i$$$ is placed on the $$$i$$$-th position. Items are divided into several pages orderly, such that each page contains exactly $$$k$$$ positions and the last positions on the last page may be left empty.Tokitsukaze would do the following operation: focus on the first special page that contains at least one special item, and at one time, Tokitsukaze would discard all special items on this page. After an item is discarded or moved, its old position would be empty, and then the item below it, if exists, would move up to this empty position. The movement may bring many items forward and even into previous pages, so Tokitsukaze would keep waiting until all the items stop moving, and then do the operation (i.e. check the special page and discard the special items) repeatedly until there is no item need to be discarded.    Consider the first example from the statement: $$$n=10$$$, $$$m=4$$$, $$$k=5$$$, $$$p=[3, 5, 7, 10]$$$. The are two pages. Initially, the first page is special (since it is the first page containing a special item). So Tokitsukaze discards the special items with indices $$$3$$$ and $$$5$$$. After, the first page remains to be special. It contains $$$[1, 2, 4, 6, 7]$$$, Tokitsukaze discards the special item with index $$$7$$$. After, the second page is special (since it is the first page containing a special item). It contains $$$[9, 10]$$$, Tokitsukaze discards the special item with index $$$10$$$. Tokitsukaze wants to know the number of operations she would do in total.

The first line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$1 \le n \le 10^{18}$$$, $$$1 \le m \le 10^5$$$, $$$1 \le m, k \le n$$$) — the number of items, the number of special items to be discarded and the number of positions in each page. The second line contains $$$m$$$ distinct integers $$$p_1, p_2, \ldots, p_m$$$ ($$$1 \le p_1 < p_2 < \ldots < p_m \le n$$$) — the indices of special items which should be discarded.

Print a single integer — the number of operations that Tokitsukaze would do in total.

NoteFor the first example:  In the first operation, Tokitsukaze would focus on the first page $$$[1, 2, 3, 4, 5]$$$ and discard items with indices $$$3$$$ and $$$5$$$;  In the second operation, Tokitsukaze would focus on the first page $$$[1, 2, 4, 6, 7]$$$ and discard item with index $$$7$$$;  In the third operation, Tokitsukaze would focus on the second page $$$[9, 10]$$$ and discard item with index $$$10$$$. For the second example, Tokitsukaze would focus on the second page $$$[6, 7, 8, 9, 10]$$$ and discard all special items at once.

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, m, t, k int64
	scanf("%d %d %d\n", &n, &m, &k)
	p := []int64{}

	for i := 0; i < int(m); i++ {
		scanf("%d", &t)
		p = append(p, t)
	}
	pos := 0
	deleted := int64(0)
	op := 0
	for {
		window := (p[pos] - deleted - 1) / k
		var ps int
		for ps = pos; ps < len(p); ps++ {
			windo := (p[ps] - deleted - 1) / k
			if windo != window {
				deleted = int64(ps-pos) + deleted
				op++
				pos = ps
				break
			}
		}
		// fmt.Println(pos, deleted)
		if ps == len(p) {
			op++
			break
		}
	}

	printf("%d", op)

}

package main

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

func main() {
	sc := NewScanner()
	_, M, K := sc.NextInt(), sc.NextInt64(), sc.NextInt64()
	P := sc.NextInt64Array()

	a := int64(0)
	page := int64(1)
	cnt := 0
	geta := int64(0)
	for a < M {
		//fmt.Println(page)
		if P[a]-geta > K*page {
			page = 1 + (P[a]-geta)/K
			if (P[a]-geta)%K == 0 {
				page--
			}
			continue
		}
		for b := a; b < M; b++ {
			if P[b]-geta > K*page {
				cnt++
				geta += b - 1 - a + 1
				a = b
				break
			}
			if b == M-1 {
				geta += b - a + 1
				a = b + 1
				cnt++
			}
		}
		//fmt.Println(a, geta, page)
	}
	fmt.Println(cnt)
}

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

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

func (s *Scanner) NextFloat() float64 {
	v, _ := strconv.ParseFloat(s.Next(), 64)
	return v
}

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

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

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

	return mp
}

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

// Author: sighduck
// URL: https://codeforces.com/contest/1191/problem/C

package main

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

func ceilDiv(n int64, d int64) int64 {
	return (n + d - 1) / d
}

func Solve(n int64, m int64, k int64, p []int64) int64 {
	removed := int64(0)
	moves := int64(0)
	for len(p) > 0 {
		specialPage := ceilDiv(p[0]-removed, k)
		count := int64(0)
		for len(p) > 0 && k*(specialPage-1) < (p[0]-removed) && (p[0]-removed) <= specialPage*k {
			p = p[1:]
			count += 1
		}
		removed += count
		moves += 1
	}
	return moves
}

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

	var n, m, k int64
	fmt.Fscanf(reader, "%d %d %d\n", &n, &m, &k)
	p := make([]int64, m)
	for i := int64(0); i < m; i++ {
		fmt.Fscanf(reader, "%d ", &p[i])
	}

	fmt.Fprintf(writer, "%d\n", Solve(n, m, k, p))
}

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, m, t, k int64
	scanf("%d %d %d\n", &n, &m, &k)
	p := []int64{}

	for i := 0; i < int(m); i++ {
		scanf("%d", &t)
		p = append(p, t)
	}
	pos := 0
	deleted := int64(0)
	op := 0
	for {
		window := (p[pos] - deleted) / k
		var ps int
		for ps = pos; ps < len(p); ps++ {
			windo := (p[ps] - deleted - 1) / k
			if windo != window {
				deleted = int64(ps-pos) + deleted
				op++
				pos = ps
			}
		}
		if ps == len(p) {
			op++
			break
		}
	}

	printf("%d", op)

}

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, m, t, k int64
	scanf("%d %d %d\n", &n, &m, &k)
	p := []int64{}

	for i := 0; i < int(m); i++ {
		scanf("%d", &t)
		p = append(p, t)
	}
	pos := 0
	deleted := int64(0)
	op := 0
	for {
		window := (p[pos] - deleted - 1) / k
		var ps int
		for ps = pos; ps < len(p); ps++ {
			windo := (p[ps] - deleted - 1) / k
			if windo != window {
				deleted = int64(ps-pos) + deleted
				op++
				pos = ps
			}
		}
		// fmt.Println(pos, deleted)
		if ps == len(p) {
			op++
			break
		}
	}

	printf("%d", op)

}

package main

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

func main() {
	sc := NewScanner()
	_, M, K := sc.NextInt(), sc.NextInt64(), sc.NextInt64()
	P := sc.NextInt64Array()

	a := int64(0)
	page := int64(1)
	cnt := 0
	geta := int64(0)
	for a < M {
		//fmt.Println(page)
		if P[a]-geta > K*page {
			page = 1 + (P[a]-geta)/K
			continue
		}
		for b := a; b < M; b++ {
			if P[b]-geta > K*page {
				cnt++
				geta += b - 1 - a + 1
				a = b
				break
			}
			if b == M-1 {
				geta += b - a + 1
				a = b + 1
				cnt++
			}
		}
		//fmt.Println(a, geta, page)
	}
	fmt.Println(cnt)
}

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

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

func (s *Scanner) NextFloat() float64 {
	v, _ := strconv.ParseFloat(s.Next(), 64)
	return v
}

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

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

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

	return mp
}

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

package main

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

func main() {
	sc := NewScanner()
	_, M, K := sc.NextInt(), sc.NextInt64(), sc.NextInt64()
	P := sc.NextInt64Array()

	a := int64(0)
	page := int64(1)
	cnt := 0
	geta := int64(0)
	for a < M {
		//fmt.Println(page)
		if P[a]-geta > K*page {
			W := 1 + (P[a]-geta)/K
			if (P[a]-geta)%page > 0 {
				W++
			}
			page = W
			continue
		}
		for b := a; b < M; b++ {
			if P[b]-geta > K*page {
				cnt++
				geta += b - 1 - a + 1
				a = b
				break
			}
			if b == M-1 {
				geta += b - a + 1
				a = b + 1
				cnt++
			}
		}
		//fmt.Println(a, geta, page)
	}
	fmt.Println(cnt)
}

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

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

func (s *Scanner) NextFloat() float64 {
	v, _ := strconv.ParseFloat(s.Next(), 64)
	return v
}

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

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

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

	return mp
}

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

package main

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

func main() {
	sc := NewScanner()
	_, M, K := sc.NextInt(), sc.NextInt64(), sc.NextInt64()
	P := sc.NextInt64Array()

	a := int64(0)
	page := int64(1)
	cnt := 0
	geta := int64(0)
	for a < M {
		//fmt.Println(page)
		if P[a]-geta > K*page {
			page = 1 + (P[a]-geta)/K
			if (P[a]-geta)%page > 0 {
				page++
			}
			continue
		}
		for b := a; b < M; b++ {
			if P[b]-geta > K*page {
				cnt++
				geta += b - 1 - a + 1
				a = b
				break
			}
			if b == M-1 {
				geta += b - a + 1
				a = b + 1
				cnt++
			}
		}
		//fmt.Println(a, geta, page)
	}
	fmt.Println(cnt)
}

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

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

func (s *Scanner) NextFloat() float64 {
	v, _ := strconv.ParseFloat(s.Next(), 64)
	return v
}

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

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

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

	return mp
}

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

package main

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

func main() {
	sc := NewScanner()
	_, M, K := sc.NextInt(), sc.NextInt(), sc.NextInt()
	P := sc.NextIntArray()

	a := 0
	page := 1
	cnt := 0
	geta := 0
	for a < M {
		//fmt.Println(page)
		if P[a]-geta > K*page {
			page = 1 + (P[a]-geta)/K
			continue
		}
		for b := a; b < M; b++ {
			if P[b]-geta > K*page {
				cnt++
				geta += b - 1 - a + 1
				a = b
				break
			}
			if b == M-1 {
				geta += b - a + 1
				a = b + 1
				cnt++
			}
		}
		//fmt.Println(a, geta, page)
	}
	fmt.Println(cnt)
}

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

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

func (s *Scanner) NextFloat() float64 {
	v, _ := strconv.ParseFloat(s.Next(), 64)
	return v
}

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

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

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

	return mp
}

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