Gildong was hiking a mountain, walking by millions of trees. Inspired by them, he suddenly came up with an interesting idea for trees in data structures: What if we add another edge in a tree?Then he found that such tree-like graphs are called 1-trees. Since Gildong was bored of solving too many tree problems, he wanted to see if similar techniques in trees can be used in 1-trees as well. Instead of solving it by himself, he's going to test you by providing queries on 1-trees.First, he'll provide you a tree (not 1-tree) with $$$n$$$ vertices, then he will ask you $$$q$$$ queries. Each query contains $$$5$$$ integers: $$$x$$$, $$$y$$$, $$$a$$$, $$$b$$$, and $$$k$$$. This means you're asked to determine if there exists a path from vertex $$$a$$$ to $$$b$$$ that contains exactly $$$k$$$ edges after adding a bidirectional edge between vertices $$$x$$$ and $$$y$$$. A path can contain the same vertices and same edges multiple times. All queries are independent of each other; i.e. the added edge in a query is removed in the next query.

The first line contains an integer $$$n$$$ ($$$3 \le n \le 10^5$$$), the number of vertices of the tree. Next $$$n-1$$$ lines contain two integers $$$u$$$ and $$$v$$$ ($$$1 \le u,v \le n$$$, $$$u \ne v$$$) each, which means there is an edge between vertex $$$u$$$ and $$$v$$$. All edges are bidirectional and distinct. Next line contains an integer $$$q$$$ ($$$1 \le q \le 10^5$$$), the number of queries Gildong wants to ask. Next $$$q$$$ lines contain five integers $$$x$$$, $$$y$$$, $$$a$$$, $$$b$$$, and $$$k$$$ each ($$$1 \le x,y,a,b \le n$$$, $$$x \ne y$$$, $$$1 \le k \le 10^9$$$) – the integers explained in the description. It is guaranteed that the edge between $$$x$$$ and $$$y$$$ does not exist in the original tree.

For each query, print "YES" if there exists a path that contains exactly $$$k$$$ edges from vertex $$$a$$$ to $$$b$$$ after adding an edge between vertices $$$x$$$ and $$$y$$$. Otherwise, print "NO". You can print each letter in any case (upper or lower).

NoteThe image below describes the tree (circles and solid lines) and the added edges for each query (dotted lines).  Possible paths for the queries with "YES" answers are:   $$$1$$$-st query: $$$1$$$ – $$$3$$$ – $$$2$$$  $$$2$$$-nd query: $$$1$$$ – $$$2$$$ – $$$3$$$  $$$4$$$-th query: $$$3$$$ – $$$4$$$ – $$$2$$$ – $$$3$$$ – $$$4$$$ – $$$2$$$ – $$$3$$$ – $$$4$$$ – $$$2$$$ – $$$3$$$

package main

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

var reader io.Reader
var scanner *bufio.Scanner

func Init(io io.Reader, max_token_size int) {
	reader = bufio.NewReader(io)
	scanner = bufio.NewScanner(reader)
	// max token size
	scanner.Split(bufio.ScanWords)
	scanner.Buffer([]byte{}, max_token_size)
}

func stderr(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
}

func die(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
	os.Exit(1)
}

func NextInt() int {
	bytes := NextBytes()
	i, err := Atoi(bytes)
	if err != nil {
		die(err)
	}
	return i
}

func NextBytes() []byte {
	if !scanner.Scan() {
		die(scanner.Err())
	}
	bytes := scanner.Bytes()
	copied := make([]byte, len(bytes))
	copy(copied, bytes)
	return copied
}

func Atoi(b []byte) (int, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	n := 0
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func ExtendByteArray(a []byte, size int) []byte {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]byte, size-len(a))...)
	}
}

func ExtendIntArray(a []int, size int) []int {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]int, size-len(a))...)
	}
}

type Graph struct {
	n     int
	m     int
	edges int
	fst   []int
	nxt   []int
	end   []int
}

func (g *Graph) Add(a, b int) {
	g.nxt[g.edges] = g.fst[a]
	g.fst[a] = g.edges
	g.end[g.edges] = b
	g.edges++
}

func (g *Graph) Adjacent(a int, fn func(b int)) {
	for u := g.fst[a]; u != -1; u = g.nxt[u] {
		fn(g.end[u])
	}
}

func NewGraph(n, m int) *Graph {
	g := &Graph{n, m, 0, make([]int, n), make([]int, m), make([]int, m)}
	for i := 0; i < n; i++ {
		g.fst[i] = -1
	}
	return g
}

type LCATree struct {
	g            *Graph
	maxLogHeight int
	root         int
	parent       []int
	up           [][]int
	h            []int
	entrance     []int
	exit         []int
}

func (t *LCATree) GoDown(v int, par int, time int) int {
	t.parent[v] = par
	t.up[v][0] = par
	t.entrance[v] = time
	time++
	for i := 1; i < t.maxLogHeight; i++ {
		t.up[v][i] = t.up[t.up[v][i-1]][i-1]
	}
	t.g.Adjacent(v, func(to int) {
		if to == par {
			return
		}
		t.h[to] = t.h[v] + 1
		time = t.GoDown(to, v, time)
	})
	t.exit[v] = time
	time++
	// stderr("vertex", v, "entrance", t.entrance[v], "exit", t.exit[v])
	return time
}

func (t *LCATree) Prepare() {
	t.GoDown(t.root, t.root, 0)
}

func (t *LCATree) IsParent(u, v int) bool {
	return t.entrance[u] <= t.entrance[v] && t.exit[u] >= t.exit[v]
}

func (t *LCATree) LCA(u, v int) int {
	if t.IsParent(u, v) {
		return u
	}
	for h := t.maxLogHeight - 1; h >= 0; {
		for h >= 0 && t.IsParent(t.up[u][h], v) {
			h--
		}
		if h >= 0 {
			u = t.up[u][h]
		}
	}
	return t.up[u][0]
}

func (t *LCATree) Distance(u, v int) int {
	lca := t.LCA(u, v)
	// stderr("lca of", u, v, "is", lca)
	return t.h[u] + t.h[v] - 2*t.h[lca]
}

func NewLCATree(g *Graph) *LCATree {
	parent := make([]int, g.n)
	var mh = 18
	up := make([][]int, g.n)
	for i := 0; i < g.n; i++ {
		up[i] = make([]int, mh)
	}
	h := make([]int, g.n)
	entrance := make([]int, g.n)
	exit := make([]int, g.n)
	return &LCATree{g, mh, 0, parent, up, h, entrance, exit}
}

func check(d, k int) bool {
	return d <= k && (d&1) == (k&1)
}

func main() {
	inf := os.Stdin
	out := os.Stdout
	// f, err := os.Open("a.in")
	// if err != nil {
	// 	die(err)
	// }
	// out, err := os.Create("a.out")
	// if err != nil {
	// 	die(err)
	// }
	// defer out.Close()
	Init(inf, 1000010)

	n := NextInt()
	g := NewGraph(n, 2*(n-1))

	for i := 0; i < n-1; i++ {
		u, v := NextInt(), NextInt()
		u--
		v--
		g.Add(u, v)
		g.Add(v, u)
	}
	t := NewLCATree(g)
	t.Prepare()
	q := NextInt()

	for i := 0; i < q; i++ {
		x, y, a, b, k := NextInt(), NextInt(), NextInt(), NextInt(), NextInt()
		x--
		y--
		a--
		b--
		// stderr("query", x, y, a, b, k)
		dab := t.Distance(a, b)
		dax := t.Distance(a, x)
		day := t.Distance(a, y)
		dbx := t.Distance(b, x)
		dby := t.Distance(b, y)
		// stderr("dab", dab, "ax ay", dax, dby, "ay bx", day, dbx)
		if check(dab, k) || check(dax+dby+1, k) || check(day+dbx+1, k) {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

package main

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

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

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

	g := make(map[int][]int)
	for i := 0; i < n-1; i++ {
		var u, v int
		fmt.Fscanf(reader, "%d %d\n", &u, &v)
		g[u] = append(g[u], v)
		g[v] = append(g[v], u)
	}

	t := newTree(g, n)

	var q int
	fmt.Fscanf(reader, "%d\n", &q)

	for i := 0; i < q; i++ {
		var x, y, a, b, k int
		fmt.Fscanf(reader, "%d %d %d %d %d\n", &x, &y, &a, &b, &k)
		fmt.Fprintf(writer, "%s\n", can(t, x, y, a, b, k))
	}
}

func can(t *Tree, x, y, a, b, k int) string {
	d1 := t.path(a, b)
	if isOk(d1, k) {
		return "YES"
	}

	d2 := t.path(a, x) + 1 + t.path(y, b)
	if isOk(d2, k) {
		return "YES"
	}

	d3 := t.path(a, y) + 1 + t.path(x, b)
	if isOk(d3, k) {
		return "YES"
	}

	return "NO"
}

func isOk(d, k int) bool {
	return d <= k && (k-d)%2 == 0
}

type Tree struct {
	n   int
	t   map[int][]int
	par map[int]int
	lv  map[int]int
	anc [][]int
}

func newTree(g map[int][]int, n int) *Tree {
	t := new(Tree)
	t.t = g
	t.n = n
	t.par = make(map[int]int, n)
	t.lv = make(map[int]int, n)

	t.dfs(1, 0, 0)
	t.makeLcaTable()

	return t
}

func (t *Tree) dfs(r, l, p int) {
	t.par[r] = p
	t.lv[r] = l

	for _, ch := range t.t[r] {
		if ch == p {
			continue
		}
		t.dfs(ch, l+1, r)
	}
}

const Lv = 21

func (t *Tree) makeLcaTable() {
	t.anc = make([][]int, Lv)
	for j := 0; j < Lv; j++ {
		t.anc[j] = make([]int, t.n+1)
	}

	for i := 1; i <= t.n; i++ {
		t.anc[0][i] = t.par[i]
	}

	for j := 1; j < Lv; j++ {
		for i := 1; i <= t.n; i++ {
			t.anc[j][i] = t.anc[j-1][t.anc[j-1][i]]
		}
	}
}

func (t *Tree) lca(u, v int) int {
	if t.lv[u] > t.lv[v] {
		u, v = v, u
	}

	for j := Lv - 1; j >= 0; j-- {
		a := t.anc[j][v]
		if a != 0 && t.lv[a] >= t.lv[u] {
			v = a
		}
	}

	if t.lv[u] != t.lv[v] {
		panic(fmt.Sprintf("Expect Lv(%d) = Lv(%d)", u, v))
	}

	if u == v {
		return u
	}

	for j := Lv - 1; j >= 0; j-- {
		if t.anc[j][u] != t.anc[j][v] {
			u = t.anc[j][u]
			v = t.anc[j][v]
		}
	}

	return t.par[u]
}

func (t *Tree) path(u, v int) int {
	lca := t.lca(u, v)
	return t.lv[u] + t.lv[v] - 2*t.lv[lca]
}

package main

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

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

	var n, q, v, w, x, y, a, b, k int
	Fscan(in, &n)
	g := make([][]int, n)
	for i := 0; i < n-1; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	const mx = 17
	pa := make([][mx]int, n)
	dep := make([]int, n)
	var dfs func(v, p, d int)
	dfs = func(v, p, d int) {
		pa[v][0] = p
		dep[v] = d
		for _, w := range g[v] {
			if w != p {
				dfs(w, v, d+1)
			}
		}
	}
	dfs(0, -1, 0)
	for k := 0; k+1 < mx; k++ {
		for v := range pa {
			if p := pa[v][k]; p == -1 {
				pa[v][k+1] = -1
			} else {
				pa[v][k+1] = pa[p][k]
			}
		}
	}
	_lca := func(v, w int) int {
		if dep[v] > dep[w] {
			v, w = w, v
		}
		for k := uint(0); k < mx; k++ {
			if (dep[w]-dep[v])>>k&1 == 1 {
				w = pa[w][k]
			}
		}
		if v == w {
			return v
		}
		for k := mx - 1; k >= 0; k-- {
			if pa[v][k] != pa[w][k] {
				v, w = pa[v][k], pa[w][k]
			}
		}
		return pa[v][0]
	}
	_d := func(v, w int) int { return dep[v] + dep[w] - dep[_lca(v, w)]<<1 }

_q:
	for Fscan(in, &q); q > 0; q-- {
		Fscan(in, &x, &y, &a, &b, &k)
		x--
		y--
		a--
		b--
		for _, d := range []int{_d(a, b), _d(a, x) + _d(b, y) + 1, _d(a, y) + _d(b, x) + 1} {
			if d <= k && d&1 == k&1 {
				Fprintln(out, "YES")
				continue _q
			}
		}
		Fprintln(out, "NO")
	}
}

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

package main

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

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

	var n, q, v, w, x, y, a, b, k int
	Fscan(in, &n)
	g := make([][]int, n)
	for i := 0; i < n-1; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	const mx = 18
	pa := [mx][1e5]int{}
	dep := make([]int, n)
	var dfs func(v, p, d int)
	dfs = func(v, p, d int) {
		pa[0][v] = p
		dep[v] = d
		for _, w := range g[v] {
			if w != p {
				dfs(w, v, d+1)
			}
		}
	}
	dfs(0, -1, 0)
	for k := 0; k+1 < mx; k++ {
		for v := 0; v < n; v++ {
			if p := pa[k][v]; p == -1 {
				pa[k+1][v] = -1
			} else {
				pa[k+1][v] = pa[k][p]
			}
		}
	}
	_lca := func(v, w int) int {
		if dep[v] > dep[w] {
			v, w = w, v
		}
		for k := uint(0); k < mx; k++ {
			if (dep[w]-dep[v])>>k&1 == 1 {
				w = pa[k][w]
			}
		}
		if v == w {
			return v
		}
		for k := mx - 1; k >= 0; k-- {
			if pa[k][v] != pa[k][w] {
				v, w = pa[k][v], pa[k][w]
			}
		}
		return pa[0][v]
	}
	_d := func(v, w int) int { return dep[v] + dep[w] - dep[_lca(v, w)]<<1 }

_q:
	for Fscan(in, &q); q > 0; q-- {
		Fscan(in, &x, &y, &a, &b, &k)
		x--
		y--
		a--
		b--
		for _, d := range []int{_d(a, b), _d(a, x) + _d(b, y) + 1, _d(a, y) + _d(b, x) + 1} {
			if d <= k && d&1 == k&1 {
				Fprintln(out, "YES")
				continue _q
			}
		}
		Fprintln(out, "NO")
	}
}

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

package main

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

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

	var n, q, v, w, x, y, a, b, k int
	Fscan(in, &n)
	g := make([][]int, n)
	for i := 0; i < n-1; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	const mx = 18
	pa := [1e5][mx]int{}
	dep := make([]int, n)
	var dfs func(v, p, d int)
	dfs = func(v, p, d int) {
		pa[v][0] = p
		dep[v] = d
		for _, w := range g[v] {
			if w != p {
				dfs(w, v, d+1)
			}
		}
	}
	dfs(0, -1, 0)
	for k := 0; k+1 < mx; k++ {
		for v := range pa {
			if p := pa[v][k]; p == -1 {
				pa[v][k+1] = -1
			} else {
				pa[v][k+1] = pa[p][k]
			}
		}
	}
	_lca := func(v, w int) int {
		if dep[v] > dep[w] {
			v, w = w, v
		}
		for k := uint(0); k < mx; k++ {
			if (dep[w]-dep[v])>>k&1 == 1 {
				w = pa[w][k]
			}
		}
		if v == w {
			return v
		}
		for k := mx - 1; k >= 0; k-- {
			if pa[v][k] != pa[w][k] {
				v, w = pa[v][k], pa[w][k]
			}
		}
		return pa[v][0]
	}
	_d := func(v, w int) int { return dep[v] + dep[w] - dep[_lca(v, w)]<<1 }

_q:
	for Fscan(in, &q); q > 0; q-- {
		Fscan(in, &x, &y, &a, &b, &k)
		x--
		y--
		a--
		b--
		for _, d := range []int{_d(a, b), _d(a, x) + _d(b, y) + 1, _d(a, y) + _d(b, x) + 1} {
			if d <= k && d&1 == k&1 {
				Fprintln(out, "YES")
				continue _q
			}
		}
		Fprintln(out, "NO")
	}
}

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

package main

import (
	"bufio"
	. "fmt"
	"io"
	"math/bits"
	"os"
)

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

	var n, q, v, w, x, y, a, b, k int
	Fscan(in, &n)
	g := make([][]int, n)
	for i := 0; i < n-1; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	vs := make([]int, 0, 2*n-1)
	pos := make([]int, n)
	depths := make([]int, 0, 2*n-1)
	dis := make([]int, n)
	var dfs func(v, fa, d int)
	dfs = func(v, fa, d int) {
		pos[v] = len(vs)
		vs = append(vs, v)
		depths = append(depths, d)
		dis[v] = d
		for _, w := range g[v] {
			if w != fa {
				dfs(w, v, d+1)
				vs = append(vs, v)
				depths = append(depths, d)
			}
		}
	}
	dfs(0, -1, 0)

	type pair struct{ v, i int }
	var st [][18]pair
	stInit := func(a []int) {
		n := len(a)
		st = make([][18]pair, n)
		for i := range st {
			st[i][0] = pair{a[i], i}
		}
		for j := uint(1); 1<<j <= n; j++ {
			for i := 0; i+(1<<j)-1 < n; i++ {
				st0, st1 := st[i][j-1], st[i+(1<<(j-1))][j-1]
				if st0.v < st1.v {
					st[i][j] = st0
				} else {
					st[i][j] = st1
				}
			}
		}
	}
	stInit(depths)
	stQuery := func(l, r int) int {
		k := uint(bits.Len(uint(r-l+1)) - 1)
		st0, st1 := st[l][k], st[r-(1<<k)+1][k]
		if st0.v < st1.v {
			return st0.i
		}
		return st1.i
	}
	lca := func(v, w int) int {
		pv, pw := pos[v], pos[w]
		if pv > pw {
			pv, pw = pw, pv
		}
		return vs[stQuery(pv, pw)]
	}
	_d := func(v, w int) int { return dis[v] + dis[w] - dis[lca(v, w)]<<1 }

_q:
	for Fscan(in, &q); q > 0; q-- {
		Fscan(in, &x, &y, &a, &b, &k)
		x--
		y--
		a--
		b--
		for _, d := range []int{_d(a, b), _d(a, x) + _d(b, y) + 1, _d(a, y) + _d(b, x) + 1} {
			if d <= k && d&1 == k&1 {
				Fprintln(out, "YES")
				continue _q
			}
		}
		Fprintln(out, "NO")
	}
}

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

package main

import (
	"bufio"
	. "fmt"
	"io"
	"math/bits"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1304E(_r io.Reader, _w io.Writer) {
	in := bufio.NewScanner(_r)
	in.Split(bufio.ScanWords)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	read := func() (x int) {
		in.Scan()
		for _, b := range in.Bytes() {
			x = x*10 + int(b-'0')
		}
		return
	}

	n := read()
	g := make([][]int, n)
	for i := 0; i < n-1; i++ {
		v, w := read()-1, read()-1
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	vs := make([]int, 0, 2*n-1)
	pos := make([]int, n)
	depths := make([]int, 0, 2*n-1)
	dis := make([]int, n)
	var dfs func(v, fa, d int)
	dfs = func(v, fa, d int) {
		pos[v] = len(vs)
		vs = append(vs, v)
		depths = append(depths, d)
		dis[v] = d
		for _, w := range g[v] {
			if w != fa {
				dfs(w, v, d+1)
				vs = append(vs, v)
				depths = append(depths, d)
			}
		}
	}
	dfs(0, -1, 0)

	type pair struct{ v, i int }
	var st [][18]pair
	stInit := func(a []int) {
		n := len(a)
		st = make([][18]pair, n)
		for i := range st {
			st[i][0] = pair{a[i], i}
		}
		for j := uint(1); 1<<j <= n; j++ {
			for i := 0; i+(1<<j)-1 < n; i++ {
				st0, st1 := st[i][j-1], st[i+(1<<(j-1))][j-1]
				if st0.v < st1.v {
					st[i][j] = st0
				} else {
					st[i][j] = st1
				}
			}
		}
	}
	stInit(depths)
	stQuery := func(l, r int) int {
		k := uint(bits.Len(uint(r-l+1)) - 1)
		a, b := st[l][k], st[r-(1<<k)+1][k]
		if a.v < b.v {
			return a.i
		}
		return b.i
	}
	lca := func(v, w int) int {
		pv, pw := pos[v], pos[w]
		if pv > pw {
			pv, pw = pw, pv
		}
		return vs[stQuery(pv, pw)]
	}
	_d := func(v, w int) int { return dis[v] + dis[w] - dis[lca(v, w)]<<1 }

_q:
	for q := read(); q > 0; q-- {
		x, y, a, b, k := read()-1, read()-1, read()-1, read()-1, read()
		for _, d := range []int{_d(a, b), _d(a, x) + _d(b, y) + 1, _d(a, y) + _d(b, x) + 1} {
			if d <= k && d&1 == k&1 {
				Fprintln(out, "YES")
				continue _q
			}
		}
		Fprintln(out, "NO")
	}
}

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

package main

import (
	"bufio"
	. "fmt"
	"io"
	"math/bits"
	"os"
)

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

	var n, q, v, w, x, y, a, b, k int
	Fscan(in, &n)
	g := make([][]int, n)
	for i := 0; i < n-1; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	vs := make([]int, 0, 2*n-1)
	pos := make([]int, n)
	depths := make([]int, 0, 2*n-1)
	dis := make([]int, n)
	var dfs func(v, fa, d int)
	dfs = func(v, fa, d int) {
		pos[v] = len(vs)
		vs = append(vs, v)
		depths = append(depths, d)
		dis[v] = d
		for _, w := range g[v] {
			if w != fa {
				dfs(w, v, d+1)
				vs = append(vs, v)
				depths = append(depths, d)
			}
		}
	}
	dfs(0, -1, 0)

	type pair struct{ v, i int }
	var st [][18]pair
	stInit := func(a []int) {
		n := len(a)
		st = make([][18]pair, n)
		for i := range st {
			st[i][0] = pair{a[i], i}
		}
		for j := uint(1); 1<<j <= n; j++ {
			for i := 0; i+(1<<j)-1 < n; i++ {
				st0, st1 := st[i][j-1], st[i+(1<<(j-1))][j-1]
				if st0.v < st1.v {
					st[i][j] = st0
				} else {
					st[i][j] = st1
				}
			}
		}
	}
	stInit(depths)
	stQuery := func(l, r int) int {
		k := uint(bits.Len(uint(r-l+1)) - 1)
		st0, st1 := st[l][k], st[r-(1<<k)+1][k]
		if st0.v < st1.v {
			return st0.i
		}
		return st1.i
	}
	calcLCA := func(v, w int) int {
		pv, pw := pos[v], pos[w]
		if pv > pw {
			pv, pw = pw, pv
		}
		return vs[stQuery(pv, pw)]
	}
	calcDis := func(v, w int) int { return dis[v] + dis[w] - dis[calcLCA(v, w)]<<1 }

	solve := func() bool {
		if d := calcDis(a, b); d <= k && d&1 == k&1 {
			return true
		}
		if d := calcDis(a, x) + calcDis(b, y) + 1; d <= k && d&1 == k&1 {
			return true
		}
		d := calcDis(a, y) + calcDis(b, x) + 1
		return d <= k && d&1 == k&1
	}
	Fscan(in, &q)
	for _case := 1; _case <= q; _case++ {
		Fscan(in, &x, &y, &a, &b, &k)
		x--
		y--
		a--
		b--
		if solve() {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
}

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

package main

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

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

	var n, q, v, w, x, y, a, b, k int
	Fscan(in, &n)
	g := make([][]int, n)
	for i := 0; i < n-1; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	const mx = 18
	pa := make([][mx]int, n)
	dep := make([]int, n)
	var dfs func(v, p, d int)
	dfs = func(v, p, d int) {
		pa[v][0] = p
		dep[v] = d
		for _, w := range g[v] {
			if w != p {
				dfs(w, v, d+1)
			}
		}
	}
	dfs(0, -1, 0)
	for k := 0; k+1 < mx; k++ {
		for v := range pa {
			if p := pa[v][k]; p == -1 {
				pa[v][k+1] = -1
			} else {
				pa[v][k+1] = pa[p][k]
			}
		}
	}
	_lca := func(v, w int) int {
		if dep[v] > dep[w] {
			v, w = w, v
		}
		for k := uint(0); k < mx; k++ {
			if (dep[w]-dep[v])>>k&1 == 1 {
				w = pa[w][k]
			}
		}
		if v == w {
			return v
		}
		for k := mx - 1; k >= 0; k-- {
			if pa[v][k] != pa[w][k] {
				v, w = pa[v][k], pa[w][k]
			}
		}
		return pa[v][0]
	}
	_d := func(v, w int) int { return dep[v] + dep[w] - dep[_lca(v, w)]<<1 }

_q:
	for Fscan(in, &q); q > 0; q-- {
		Fscan(in, &x, &y, &a, &b, &k)
		x--
		y--
		a--
		b--
		for _, d := range []int{_d(a, b), _d(a, x) + _d(b, y) + 1, _d(a, y) + _d(b, x) + 1} {
			if d <= k && d&1 == k&1 {
				Fprintln(out, "YES")
				continue _q
			}
		}
		Fprintln(out, "NO")
	}
}

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

package main

import (
	"bufio"
	. "fmt"
	"io"
	"math/bits"
	"os"
)

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

	var n, q, v, w, x, y, a, b, k int
	Fscan(in, &n)
	g := make([][]int, n)
	for i := 0; i < n-1; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	vs := make([]int, 0, 2*n-1)
	pos := make([]int, n)
	depths := make([]int, 0, 2*n-1)
	dis := make([]int, n)
	var dfs func(v, fa, d int)
	dfs = func(v, fa, d int) {
		pos[v] = len(vs)
		vs = append(vs, v)
		depths = append(depths, d)
		dis[v] = d
		for _, w := range g[v] {
			if w != fa {
				dfs(w, v, d+1)
				vs = append(vs, v)
				depths = append(depths, d)
			}
		}
	}
	dfs(0, -1, 0)

	type pair struct{ v, i int }
	var st [][18]pair
	stInit := func(a []int) {
		n := len(a)
		st = make([][18]pair, n)
		for i := range st {
			st[i][0] = pair{a[i], i}
		}
		for j := uint(1); 1<<j <= n; j++ {
			for i := 0; i+(1<<j)-1 < n; i++ {
				st0, st1 := st[i][j-1], st[i+(1<<(j-1))][j-1]
				if st0.v < st1.v {
					st[i][j] = st0
				} else {
					st[i][j] = st1
				}
			}
		}
	}
	stInit(depths)
	stQuery := func(l, r int) int {
		k := uint(bits.Len(uint(r-l+1)) - 1)
		st0, st1 := st[l][k], st[r-(1<<k)+1][k]
		if st0.v < st1.v {
			return st0.i
		}
		return st1.i
	}
	calcLCA := func(v, w int) int {
		pv, pw := pos[v], pos[w]
		if pv > pw {
			pv, pw = pw, pv
		}
		return vs[stQuery(pv, pw)]
	}
	calcDis := func(v, w int) int { return dis[v] + dis[w] - 2*dis[calcLCA(v, w)] }

	for Fscan(in, &q); q > 0; q-- {
		Fscan(in, &x, &y, &a, &b, &k)
		x--
		y--
		a--
		b--
		loop := calcDis(x, y) + 1
		if dab := calcDis(a, b); k >= dab && (k-dab)%loop == 0 {
			Fprintln(out, "YES")
			continue
		}
		if lcaXA := calcLCA(x, a); lcaXA != x && lcaXA != a {
			x, y = y, x
		}
		if dab := calcDis(x, a) + calcDis(y, b) + 1; k >= dab && (k-dab)%loop == 0 {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
}

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

package main

import (
	"bufio"
	. "fmt"
	"io"
	"math/bits"
	"os"
)

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

	var n, q, v, w, x, y, a, b, k int
	Fscan(in, &n)
	g := make([][]int, n)
	for i := 0; i < n-1; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	vs := make([]int, 0, 2*n-1)
	pos := make([]int, n)
	depths := make([]int, 0, 2*n-1)
	dis := make([]int, n)
	var dfs func(v, fa, d int)
	dfs = func(v, fa, d int) {
		pos[v] = len(vs)
		vs = append(vs, v)
		depths = append(depths, d)
		dis[v] = d
		for _, w := range g[v] {
			if w != fa {
				dfs(w, v, d+1)
				vs = append(vs, v)
				depths = append(depths, d)
			}
		}
	}
	dfs(0, -1, 0)

	type pair struct{ v, i int }
	var st [][18]pair
	stInit := func(a []int) {
		n := len(a)
		st = make([][18]pair, n)
		for i := range st {
			st[i][0] = pair{a[i], i}
		}
		for j := uint(1); 1<<j <= n; j++ {
			for i := 0; i+(1<<j)-1 < n; i++ {
				st0, st1 := st[i][j-1], st[i+(1<<(j-1))][j-1]
				if st0.v < st1.v {
					st[i][j] = st0
				} else {
					st[i][j] = st1
				}
			}
		}
	}
	stInit(depths)
	stQuery := func(l, r int) int {
		k := uint(bits.Len(uint(r-l+1)) - 1)
		st0, st1 := st[l][k], st[r-(1<<k)+1][k]
		if st0.v < st1.v {
			return st0.i
		}
		return st1.i
	}
	calcLCA := func(v, w int) int {
		pv, pw := pos[v], pos[w]
		if pv > pw {
			pv, pw = pw, pv
		}
		return vs[stQuery(pv, pw)]
	}
	calcDis := func(v, w int) int { return dis[v] + dis[w] - dis[calcLCA(v, w)]<<1 }

	solve := func() bool {
		dab := calcDis(a, b)
		if dab <= k && dab&1 == k&1 {
			return true
		}
		lcaAB, lcaXY := calcLCA(a, b), calcLCA(x, y)
		if lcaAB == lcaXY {
			if calcDis(a, x) > calcDis(a, y) {
				x, y = y, x
			}
			if calcDis(b, y) > calcDis(b, x) {
				x, y = y, x
			}
			dab2 := calcDis(x, a) + calcDis(y, b) + 1
			if dab2 <= k && dab2&1 == k&1 {
				return true
			}
			if dxy := calcDis(x, y); dxy&1 == 0 {
				if dab2 < dab {
					dab = dab2
				}
				return dab+dxy+1 <= k
			}
			return false
		}
		if lcaAB == b {
			a, b = b, a
		}
		if lcaXY == y {
			x, y = y, x
		}
		if dxy := calcDis(x, y); dxy&1 == 0 {
			dax := calcDis(a, x)
			return dab+2*dax+dxy+1 <= k
		}
		return false
	}
	Fscan(in, &q)
	for _case := 1; _case <= q; _case++ {
		Fscan(in, &x, &y, &a, &b, &k)
		x--
		y--
		a--
		b--
		//  expected YES, found NO [83rd token]
		if _case == 83 {
			Fprintln(out, Sprintf("%d,%d,%d,%d,%d", x,y,a,b,k))
			return
		}
		if solve() {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
}

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

package main

import (
	"bufio"
	. "fmt"
	"io"
	"math/bits"
	"os"
)

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

	var n, q, v, w, x, y, a, b, k int
	Fscan(in, &n)
	g := make([][]int, n)
	for i := 0; i < n-1; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	vs := make([]int, 0, 2*n-1)
	pos := make([]int, n)
	depths := make([]int, 0, 2*n-1)
	dis := make([]int, n)
	var dfs func(v, fa, d int)
	dfs = func(v, fa, d int) {
		pos[v] = len(vs)
		vs = append(vs, v)
		depths = append(depths, d)
		dis[v] = d
		for _, w := range g[v] {
			if w != fa {
				dfs(w, v, d+1)
				vs = append(vs, v)
				depths = append(depths, d)
			}
		}
	}
	dfs(0, -1, 0)

	type pair struct{ v, i int }
	var st [][18]pair
	stInit := func(a []int) {
		n := len(a)
		st = make([][18]pair, n)
		for i := range st {
			st[i][0] = pair{a[i], i}
		}
		for j := uint(1); 1<<j <= n; j++ {
			for i := 0; i+(1<<j)-1 < n; i++ {
				st0, st1 := st[i][j-1], st[i+(1<<(j-1))][j-1]
				if st0.v < st1.v {
					st[i][j] = st0
				} else {
					st[i][j] = st1
				}
			}
		}
	}
	stInit(depths)
	stQuery := func(l, r int) int {
		k := uint(bits.Len(uint(r-l+1)) - 1)
		st0, st1 := st[l][k], st[r-(1<<k)+1][k]
		if st0.v < st1.v {
			return st0.i
		}
		return st1.i
	}
	calcLCA := func(v, w int) int {
		pv, pw := pos[v], pos[w]
		if pv > pw {
			pv, pw = pw, pv
		}
		return vs[stQuery(pv, pw)]
	}
	calcDis := func(v, w int) int { return dis[v] + dis[w] - dis[calcLCA(v, w)]<<1 }

	solve := func() bool {
		dab := calcDis(a, b)
		if dab <= k && dab&1 == k&1 {
			return true
		}
		lcaAB, lcaXY := calcLCA(a, b), calcLCA(x, y)
		if lcaAB == lcaXY {
			if lcaAX := calcLCA(a, x); lcaAX != a && lcaAX != x {
				x, y = y, x
			}
			dab2 := calcDis(x, a) + calcDis(y, b) + 1
			if dab2 <= k && dab2&1 == k&1 {
				return true
			}
			if dxy := calcDis(x, y); dxy&1 == 0 {
				if dab2 < dab {
					dab = dab2
				}
				return dab+dxy+1 <= k
			}
			return false
		}
		if lcaAB == b {
			a, b = b, a
		}
		if lcaXY == y {
			x, y = y, x
		}
		if dxy := calcDis(x, y); dxy&1 == 0 {
			dax := calcDis(a, x)
			return dab+2*dax+dxy+1 <= k
		}
		return false
	}
	for Fscan(in, &q); q > 0; q-- {
		Fscan(in, &x, &y, &a, &b, &k)
		x--
		y--
		a--
		b--
		if solve() {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
}

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

package main

import (
	"bufio"
	. "fmt"
	"io"
	"math/bits"
	"os"
)

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

	var n, q, v, w, x, y, a, b, k int
	Fscan(in, &n)
	g := make([][]int, n)
	for i := 0; i < n-1; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	vs := make([]int, 0, 2*n-1)
	pos := make([]int, n)
	depths := make([]int, 0, 2*n-1)
	dis := make([]int, n)
	var dfs func(v, fa, d int)
	dfs = func(v, fa, d int) {
		pos[v] = len(vs)
		vs = append(vs, v)
		depths = append(depths, d)
		dis[v] = d
		for _, w := range g[v] {
			if w != fa {
				dfs(w, v, d+1)
				vs = append(vs, v)
				depths = append(depths, d)
			}
		}
	}
	dfs(0, -1, 0)

	type pair struct{ v, i int }
	var st [][18]pair
	stInit := func(a []int) {
		n := len(a)
		st = make([][18]pair, n)
		for i := range st {
			st[i][0] = pair{a[i], i}
		}
		for j := uint(1); 1<<j <= n; j++ {
			for i := 0; i+(1<<j)-1 < n; i++ {
				st0, st1 := st[i][j-1], st[i+(1<<(j-1))][j-1]
				if st0.v < st1.v {
					st[i][j] = st0
				} else {
					st[i][j] = st1
				}
			}
		}
	}
	stInit(depths)
	stQuery := func(l, r int) int {
		k := uint(bits.Len(uint(r-l+1)) - 1)
		st0, st1 := st[l][k], st[r-(1<<k)+1][k]
		if st0.v < st1.v {
			return st0.i
		}
		return st1.i
	}
	calcLCA := func(v, w int) int {
		pv, pw := pos[v], pos[w]
		if pv > pw {
			pv, pw = pw, pv
		}
		return vs[stQuery(pv, pw)]
	}
	calcDis := func(v, w int) int { return dis[v] + dis[w] - dis[calcLCA(v, w)]<<1 }

	solve := func() bool {
		dab := calcDis(a, b)
		if dab <= k && dab&1 == k&1 {
			return true
		}
		lcaAB, lcaXY := calcLCA(a, b), calcLCA(x, y)
		if lcaAB == lcaXY {
			if calcDis(a, x) > calcDis(a, y) {
				x, y = y, x
			}
			dab2 := calcDis(x, a) + calcDis(y, b) + 1
			if dab2 <= k && dab2&1 == k&1 {
				return true
			}
			if dxy := calcDis(x, y); dxy&1 == 0 {
				if dab2 < dab {
					dab = dab2
				}
				return dab+dxy+1 <= k
			}
			return false
		}
		if lcaAB == b {
			a, b = b, a
		}
		if lcaXY == y {
			x, y = y, x
		}
		if dxy := calcDis(x, y); dxy&1 == 0 {
			dax := calcDis(a, x)
			return dab+2*dax+dxy+1 <= k
		}
		return false
	}
	Fscan(in, &q)
	for _case := 1; _case <= q; _case++ {
		Fscan(in, &x, &y, &a, &b, &k)
		x--
		y--
		a--
		b--
		if _case == 62 {
			Fprintln(out, x,y,a,b,k)
			return
		}
		if solve() {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
}

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

package main

import (
	"bufio"
	. "fmt"
	"io"
	"math/bits"
	"os"
)

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

	var n, q, v, w, x, y, a, b, k int
	Fscan(in, &n)
	g := make([][]int, n)
	for i := 0; i < n-1; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	vs := make([]int, 0, 2*n-1)
	pos := make([]int, n)
	depths := make([]int, 0, 2*n-1)
	dis := make([]int, n)
	var dfs func(v, fa, d int)
	dfs = func(v, fa, d int) {
		pos[v] = len(vs)
		vs = append(vs, v)
		depths = append(depths, d)
		dis[v] = d
		for _, w := range g[v] {
			if w != fa {
				dfs(w, v, d+1)
				vs = append(vs, v)
				depths = append(depths, d)
			}
		}
	}
	dfs(0, -1, 0)

	type pair struct{ v, i int }
	var st [][18]pair
	stInit := func(a []int) {
		n := len(a)
		st = make([][18]pair, n)
		for i := range st {
			st[i][0] = pair{a[i], i}
		}
		for j := uint(1); 1<<j <= n; j++ {
			for i := 0; i+(1<<j)-1 < n; i++ {
				st0, st1 := st[i][j-1], st[i+(1<<(j-1))][j-1]
				if st0.v < st1.v {
					st[i][j] = st0
				} else {
					st[i][j] = st1
				}
			}
		}
	}
	stInit(depths)
	stQuery := func(l, r int) int {
		k := uint(bits.Len(uint(r-l+1)) - 1)
		st0, st1 := st[l][k], st[r-(1<<k)+1][k]
		if st0.v < st1.v {
			return st0.i
		}
		return st1.i
	}
	calcLCA := func(v, w int) int {
		pv, pw := pos[v], pos[w]
		if pv > pw {
			pv, pw = pw, pv
		}
		return vs[stQuery(pv, pw)]
	}
	calcDis := func(v, w int) int { return dis[v] + dis[w] - dis[calcLCA(v, w)]<<1 }

	solve := func() bool {
		dab := calcDis(a, b)
		if dab <= k && dab&1 == k&1 {
			return true
		}
		lcaAB, lcaXY := calcLCA(a, b), calcLCA(x, y)
		if lcaAB == lcaXY {
			if calcDis(a, x) > calcDis(a, y) {
				x, y = y, x
			}
			dab2 := calcDis(x, a) + calcDis(y, b) + 1
			if dab2 <= k && dab2&1 == k&1 {
				return true
			}
			if dxy := calcDis(x, y); dxy&1 == 0 {
				if dab2 < dab {
					dab = dab2
				}
				return dab+dxy+1 <= k
			}
			return false
		}
		if lcaAB == b {
			a, b = b, a
		}
		if lcaXY == y {
			x, y = y, x
		}
		if dxy := calcDis(x, y); dxy&1 == 0 {
			dax := calcDis(a, x)
			return dab+2*dax+dxy+1 <= k
		}
		return false
	}
	Fscan(in, &q)
	for _case := 1; _case <= q; _case++ {
		Fscan(in, &x, &y, &a, &b, &k)
		x--
		y--
		a--
		b--
		if solve() {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
}

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

package main

import (
	"bufio"
	. "fmt"
	"io"
	"math/bits"
	"os"
)

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

	var n, q, v, w, x, y, a, b, k int
	Fscan(in, &n)
	g := make([][]int, n)
	for i := 0; i < n-1; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	vs := make([]int, 0, 2*n-1)
	pos := make([]int, n)
	depths := make([]int, 0, 2*n-1)
	dis := make([]int, n)
	var dfs func(v, fa, d int)
	dfs = func(v, fa, d int) {
		pos[v] = len(vs)
		vs = append(vs, v)
		depths = append(depths, d)
		dis[v] = d
		for _, w := range g[v] {
			if w != fa {
				dfs(w, v, d+1)
				vs = append(vs, v)
				depths = append(depths, d)
			}
		}
	}
	dfs(0, -1, 0)

	type pair struct{ v, i int }
	var st [][18]pair
	stInit := func(a []int) {
		n := len(a)
		st = make([][18]pair, n)
		for i := range st {
			st[i][0] = pair{a[i], i}
		}
		for j := uint(1); 1<<j <= n; j++ {
			for i := 0; i+(1<<j)-1 < n; i++ {
				st0, st1 := st[i][j-1], st[i+(1<<(j-1))][j-1]
				if st0.v < st1.v {
					st[i][j] = st0
				} else {
					st[i][j] = st1
				}
			}
		}
	}
	stInit(depths)
	stQuery := func(l, r int) int {
		k := uint(bits.Len(uint(r-l+1)) - 1)
		st0, st1 := st[l][k], st[r-(1<<k)+1][k]
		if st0.v < st1.v {
			return st0.i
		}
		return st1.i
	}
	calcLCA := func(v, w int) int {
		pv, pw := pos[v], pos[w]
		if pv > pw {
			pv, pw = pw, pv
		}
		return vs[stQuery(pv, pw)]
	}
	calcDis := func(v, w int) int { return dis[v] + dis[w] - dis[calcLCA(v, w)]<<1 }

	solve := func() bool {
		dab := calcDis(a, b)
		if dab <= k && dab&1 == k&1 {
			return true
		}
		lcaAB, lcaXY := calcLCA(a, b), calcLCA(x, y)
		if lcaAB == lcaXY {
			if calcDis(a, x) > calcDis(a, y) {
				x, y = y, x
			}
			if calcDis(b, y) > calcDis(b, x) {
				x, y = y, x
			}
			dab2 := calcDis(x, a) + calcDis(y, b) + 1
			if dab2 <= k && dab2&1 == k&1 {
				return true
			}
			if dxy := calcDis(x, y); dxy&1 == 0 {
				if dab2 < dab {
					dab = dab2
				}
				return dab+dxy+1 <= k
			}
			return false
		}
		if lcaAB == b {
			a, b = b, a
		}
		if lcaXY == y {
			x, y = y, x
		}
		if dxy := calcDis(x, y); dxy&1 == 0 {
			dax := calcDis(a, x)
			return dab+2*dax+dxy+1 <= k
		}
		return false
	}
	Fscan(in, &q)
	for _case := 1; _case <= q; _case++ {
		Fscan(in, &x, &y, &a, &b, &k)
		x--
		y--
		a--
		b--
		if solve() {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
}

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

package main

import (
	"bufio"
	. "fmt"
	"io"
	"math/bits"
	"os"
)

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

	var n, q, v, w, x, y, a, b, k int
	Fscan(in, &n)
	g := make([][]int, n)
	for i := 0; i < n-1; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	vs := make([]int, 0, 2*n-1)
	pos := make([]int, n)
	depths := make([]int, 0, 2*n-1)
	dis := make([]int, n)
	var dfs func(v, fa, d int)
	dfs = func(v, fa, d int) {
		pos[v] = len(vs)
		vs = append(vs, v)
		depths = append(depths, d)
		dis[v] = d
		for _, w := range g[v] {
			if w != fa {
				dfs(w, v, d+1)
				vs = append(vs, v)
				depths = append(depths, d)
			}
		}
	}
	dfs(0, -1, 0)

	type pair struct{ v, i int }
	var st [][18]pair
	stInit := func(a []int) {
		n := len(a)
		st = make([][18]pair, n)
		for i := range st {
			st[i][0] = pair{a[i], i}
		}
		for j := uint(1); 1<<j <= n; j++ {
			for i := 0; i+(1<<j)-1 < n; i++ {
				st0, st1 := st[i][j-1], st[i+(1<<(j-1))][j-1]
				if st0.v < st1.v {
					st[i][j] = st0
				} else {
					st[i][j] = st1
				}
			}
		}
	}
	stInit(depths)
	stQuery := func(l, r int) int {
		k := uint(bits.Len(uint(r-l+1)) - 1)
		st0, st1 := st[l][k], st[r-(1<<k)+1][k]
		if st0.v < st1.v {
			return st0.i
		}
		return st1.i
	}
	calcLCA := func(v, w int) int {
		pv, pw := pos[v], pos[w]
		if pv > pw {
			pv, pw = pw, pv
		}
		return vs[stQuery(pv, pw)]
	}
	calcDis := func(v, w int) int { return dis[v] + dis[w] - dis[calcLCA(v, w)]<<1 }

	solve := func() bool {
		dab := calcDis(a, b)
		if dab <= k && dab&1 == k&1 {
			return true
		}
		lcaAB, lcaXY := calcLCA(a, b), calcLCA(x, y)
		if lcaAB == lcaXY {
			if calcDis(a, x) > calcDis(a, y) {
				x, y = y, x
			}
			dab2 := calcDis(x, a) + calcDis(y, b) + 1
			if dab2 <= k && dab2&1 == k&1 {
				return true
			}
			if dxy := calcDis(x, y); dxy&1 == 0 {
				if dab2 < dab {
					dab = dab2
				}
				return dab+dxy+1 <= k
			}
			return false
		}
		if lcaAB == b {
			a, b = b, a
		}
		if lcaXY == y {
			x, y = y, x
		}
		if dxy := calcDis(x, y); dxy&1 == 0 {
			dax := calcDis(a, x)
			return dab+2*dax+dxy+1 <= k
		}
		return false
	}
	Fscan(in, &q)
	for _case := 1; _case <= q; _case++ {
		Fscan(in, &x, &y, &a, &b, &k)
		x--
		y--
		a--
		b--
		if _case == 62 {
			Fprintln(out, Sprintf("%d,%d,%d,%d,%d", x,y,a,b,k))
			return
		}
		if solve() {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
}

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

package main

import (
	"bufio"
	. "fmt"
	"io"
	"math/bits"
	"os"
)

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

	var n, q, v, w, x, y, a, b, k int
	Fscan(in, &n)
	g := make([][]int, n)
	for i := 0; i < n-1; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	vs := make([]int, 0, 2*n-1)
	pos := make([]int, n)
	depths := make([]int, 0, 2*n-1)
	dis := make([]int, n)
	var dfs func(v, fa, d int)
	dfs = func(v, fa, d int) {
		pos[v] = len(vs)
		vs = append(vs, v)
		depths = append(depths, d)
		dis[v] = d
		for _, w := range g[v] {
			if w != fa {
				dfs(w, v, d+1)
				vs = append(vs, v)
				depths = append(depths, d)
			}
		}
	}
	dfs(0, -1, 0)

	type pair struct{ v, i int }
	var st [][18]pair
	stInit := func(a []int) {
		n := len(a)
		st = make([][18]pair, n)
		for i := range st {
			st[i][0] = pair{a[i], i}
		}
		for j := uint(1); 1<<j <= n; j++ {
			for i := 0; i+(1<<j)-1 < n; i++ {
				st0, st1 := st[i][j-1], st[i+(1<<(j-1))][j-1]
				if st0.v < st1.v {
					st[i][j] = st0
				} else {
					st[i][j] = st1
				}
			}
		}
	}
	stInit(depths)
	stQuery := func(l, r int) int {
		k := uint(bits.Len(uint(r-l+1)) - 1)
		st0, st1 := st[l][k], st[r-(1<<k)+1][k]
		if st0.v < st1.v {
			return st0.i
		}
		return st1.i
	}
	calcLCA := func(v, w int) int {
		pv, pw := pos[v], pos[w]
		if pv > pw {
			pv, pw = pw, pv
		}
		return vs[stQuery(pv, pw)]
	}
	calcDis := func(v, w int) int { return dis[v] + dis[w] - dis[calcLCA(v, w)]<<1 }

	solve := func() bool {
		dab := calcDis(a, b)
		if dab <= k && dab&1 == k&1 {
			return true
		}
		lcaAB, lcaXY := calcLCA(a, b), calcLCA(x, y)
		if lcaAB == lcaXY {
			if calcDis(a, x) > calcDis(a, y) {
				x, y = y, x
			}
			if calcDis(b, y) > calcDis(a, x) {
				x, y = y, x
			}
			dab2 := calcDis(x, a) + calcDis(y, b) + 1
			if dab2 <= k && dab2&1 == k&1 {
				return true
			}
			if dxy := calcDis(x, y); dxy&1 == 0 {
				if dab2 < dab {
					dab = dab2
				}
				return dab+dxy+1 <= k
			}
			return false
		}
		if lcaAB == b {
			a, b = b, a
		}
		if lcaXY == y {
			x, y = y, x
		}
		if dxy := calcDis(x, y); dxy&1 == 0 {
			dax := calcDis(a, x)
			return dab+2*dax+dxy+1 <= k
		}
		return false
	}
	Fscan(in, &q)
	for _case := 1; _case <= q; _case++ {
		Fscan(in, &x, &y, &a, &b, &k)
		x--
		y--
		a--
		b--
		if solve() {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
}

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


$$$n$$$ people gathered to hold a jury meeting of the upcoming competition, the $$$i$$$-th member of the jury came up with $$$a_i$$$ tasks, which they want to share with each other.First, the jury decides on the order which they will follow while describing the tasks. Let that be a permutation $$$p$$$ of numbers from $$$1$$$ to $$$n$$$ (an array of size $$$n$$$ where each integer from $$$1$$$ to $$$n$$$ occurs exactly once).Then the discussion goes as follows:  If a jury member $$$p_1$$$ has some tasks left to tell, then they tell one task to others. Otherwise, they are skipped.  If a jury member $$$p_2$$$ has some tasks left to tell, then they tell one task to others. Otherwise, they are skipped.  ...  If a jury member $$$p_n$$$ has some tasks left to tell, then they tell one task to others. Otherwise, they are skipped.  If there are still members with tasks left, then the process repeats from the start. Otherwise, the discussion ends. A permutation $$$p$$$ is nice if none of the jury members tell two or more of their own tasks in a row. Count the number of nice permutations. The answer may be really large, so print it modulo $$$998\,244\,353$$$.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — number of jury members. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the number of problems that the $$$i$$$-th member of the jury came up with. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, print one integer — the number of nice permutations, taken modulo $$$998\,244\,353$$$.

NoteExplanation of the first test case from the example:There are two possible permutations, $$$p = [1, 2]$$$ and $$$p = [2, 1]$$$. For $$$p = [1, 2]$$$, the process is the following:  the first jury member tells a task;  the second jury member tells a task;  the first jury member doesn't have any tasks left to tell, so they are skipped;  the second jury member tells a task. So, the second jury member has told two tasks in a row (in succession), so the permutation is not nice.For $$$p = [2, 1]$$$, the process is the following:  the second jury member tells a task;  the first jury member tells a task;  the second jury member tells a task. So, this permutation is nice.

package main

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

const mod = 998244353

func add(a int, b int) int{
	a+=b
	if a>=mod{ a-=mod}
	return a
}

func sub(a int, b int) int{
	a-=b
	if (a<0){a+=mod}
	return a
}

func mul(a int, b int) int{
	res := int64(a) * int64(b) % mod
	return int(res)
}

func pow(a int , b int) int{
	if b==0{
		return 1;
	}
	var t int = pow(a, b >> 1)
	t = mul(t,t)
	if (b&1) > 0{
		t = mul(t, a)
	}
	return t
}

func choosen (k int , n int) int{
	if (k > n) {
		return 0
	}
	return mul(fact[n], mul(ifact[k], ifact[n-k]))
}

func inv(a int) int{
	return pow(a, mod-2)
}

const N = 2e5
var fact[N+1] int
var ifact[N+1] int
var a = make([]int, N)

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

func readInt() int{
	var n int
	fmt.Fscan(in, &n)
	return n
}

func Solve(){
	var n int = readInt()
	var M1 int = 0
	var M2 int = 0
	var cnt1 int = 0
	var cnt2 int = 0
	for i := 0; i < n; i++{
		a[i] = readInt()
		if (a[i] >= M1) {
			if (a[i] > M1){
				cnt2 = cnt1
				M2 = M1
				cnt1 = 0
				M1 = a[i]
			}
			cnt1++
		} else if (a[i] >= M2){
			if (a[i] > M2){
				cnt2 = 0
				M2 = a[i]
			}
			cnt2++
		}
	}
	//fmt.Println(M1, M2, cnt1, cnt2)
	if (cnt1 > 1){
		fmt.Println(fact[n])
		return
	}
	if (M1 - M2 > 1){
		fmt.Println(0)
		return
	}
	var ans int = 0
	for right := 1; right <= n - 1; right++{
		var t = mul(mul(fact[right], fact[n - 1 - right]), sub(choosen(right, n-1), choosen(right, n-1-cnt2)))
		ans = add(ans, t)
	}
	fmt.Println(ans)
}

func main(){
	fact[0] = 1
	ifact[0] = 1
	for i := 1; i <= N; i++{
		fact[i] = mul(fact[i-1], i)
		ifact[i] = mul(ifact[i-1], inv(i))
	}
	//defer out.Flush()
	for T:=readInt();T>0;T--{
		Solve()
	}
}

package main

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

const mod = 998244353

func add(a int, b int) int{
	a+=b
	if a>=mod{ a-=mod}
	return a
}

func sub(a int, b int) int{
	a-=b
	if (a<0){a+=mod}
	return a
}

func mul(a int, b int) int{
	res := int64(a) * int64(b) % mod
	return int(res)
}

func pow(a int , b int) int{
	if b==0{
		return 1;
	}
	var t int = pow(a, b >> 1)
	t = mul(t,t)
	if (b&1) > 0{
		t = mul(t, a)
	}
	return t
}

func choosen (k int , n int) int{
	if (k > n) {
		return 0
	}
	return mul(fact[n], mul(ifact[k], ifact[n-k]))
}

func inv(a int) int{
	return pow(a, mod-2)
}

const N = 2e5
var fact[N+1] int
var ifact[N+1] int

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

func readInt() int{
	var n int
	fmt.Fscan(in, &n)
	return n
}

func Solve(){
	var n int = readInt()
	var M1 int = 0
	var M2 int = 0
	var cnt1 int = 0
	var cnt2 int = 0
	var a = make([]int, n)
	for i := 0; i < n; i++{
		a[i] = readInt()
		if (a[i] >= M1) {
			if (a[i] > M1){
				cnt2 = cnt1
				M2 = M1
				cnt1 = 0
				M1 = a[i]
			}
			cnt1++
		} else if (a[i] >= M2){
			if (a[i] > M2){
				cnt2 = 0
				M2 = a[i]
			}
			cnt2++
		}
	}
	//fmt.Println(M1, M2, cnt1, cnt2)
	if (cnt1 > 1){
		fmt.Println(fact[n])
		return
	}
	if (M1 - M2 > 1){
		fmt.Println(0)
		return
	}
	var ans int = 0
	for right := 1; right <= n - 1; right++{
		var t = mul(mul(fact[right], fact[n - 1 - right]), sub(choosen(right, n-1), choosen(right, n-1-cnt2)))
		ans = add(ans, t)
	}
	fmt.Println(ans)
}

func main(){
	fact[0] = 1
	ifact[0] = 1
	for i := 1; i <= N; i++{
		fact[i] = mul(fact[i-1], i)
		ifact[i] = mul(ifact[i-1], inv(i))
	}
	//defer out.Flush()
	for T:=readInt();T>0;T--{
		Solve()
	}
}

package main

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

const mod = 998244353

func add(a int, b int) int{
	a+=b
	if a>=mod{ a-=mod}
	return a
}

func sub(a int, b int) int{
	a-=b
	if (a<0){a+=mod}
	return a
}

func mul(a int, b int) int{
	res := int64(a) * int64(b) % mod
	return int(res)
}

func pow(a int , b int) int{
	if b==0{
		return 1;
	}
	var t int = pow(a, b >> 1)
	t = mul(t,t)
	if (b&1) > 0{
		t = mul(t, a)
	}
	return t
}

func choosen (k int , n int) int{
	if (k > n) {
		return 0
	}
	return mul(fact[n], mul(ifact[k], ifact[n-k]))
}

func inv(a int) int{
	return pow(a, mod-2)
}

const N = 2e5
var fact[N+1] int
var ifact[N+1] int

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

func readInt() int{
	var n int
	fmt.Fscan(in, &n)
	return n
}

func Solve(){
	var n int = readInt()
	var M1 int = 0
	var M2 int = 0
	var cnt1 int = 0
	var cnt2 int = 0
	var a = make([]int, n)
	for i := 0; i < n; i++{
		a[i] = readInt()
		if (a[i] >= M1) {
			if (a[i] > M1){
				cnt2 = cnt1
				M2 = M1
				cnt1 = 0
				M1 = a[i]
			}
			cnt1++
		} else if (a[i] >= M2){
			if (a[i] > M2){
				cnt2 = 0
				M2 = a[i]
			}
			cnt2++
		}
	}
	//fmt.Println(M1, M2, cnt1, cnt2)
	if (cnt1 > 1){
		fmt.Println(fact[n])
		return
	}
	if (M1 - M2 > 1){
		fmt.Println(0)
		return
	}
	var ans int = 0
	for right := 1; right <= n - 1; right++{
		var t = mul(mul(fact[right], fact[n - 1 - right]), sub(choosen(right, n-1), choosen(right, n-1-cnt2)))
		ans = add(ans, t)
	}
	fmt.Println(ans)
}

func main(){
	fact[0] = 1
	ifact[0] = 1
	for i := 1; i <= N; i++{
		fact[i] = mul(fact[i-1], i)
		ifact[i] = mul(ifact[i-1], inv(i))
	}
	defer out.Flush()
	for T:=readInt();T>0;T--{
		Solve()
	}
}

package main

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

// var m []int
// err = json.Unmarshal([]byte(r.FormValue("example")), &m)

// for i := 0; i < len(m); i++ {
// 	fmt.Println(m[i])
// }

// sort.Slice(arr, func(i, j int) bool {
// 	return arr[i] > arr[j]
// })

func MIN(x, y int64) int64 {
	if x > y {
		return y
	} else {
		return x
	}
}

func MINN(x, y int) int {
	if x > y {
		return y
	} else {
		return x
	}
}

func MKO(x, y int64) int64 {
	if x < y {
		return 0
	} else {
		return x - y + 1
	}
}

func MAX(x, y int64) int64 {
	if x > y {
		return x
	} else {
		return y
	}
}

func MAXX(x, y int) int {
	if x > y {
		return x
	} else {
		return y
	}
}

func LCM(x, y int) int {
	if x >= y {
		initial := x
		for i := 1; i < y+2; i++ {
			if initial%y == 0 {
				break
			} else {
				initial = initial + x
			}

		}
		return initial
	} else {
		initial := y
		for i := 1; i < x+2; i++ {
			if initial%x == 0 {
				break
			} else {
				initial = initial + y
			}

		}
		return initial
	}
}

func GCD(x, y int64) int64 {
	if x*y == 0 {
		return MAX(x, y)
	} else {
		var l, q int64
		l = MAX(x, y)
		q = MIN(x, y)
		var m int64
		for l*q > 0 {
			a := l % q
			b := l - a
			cc := int64(b / q)
			remain := l - cc*q
			l = MAX(remain, q)
			q = MIN(remain, q)
			if l*q == 0 {
				m = l + q
				break
			}

		}
		return m
	}
}

func IsItPrime(x int) bool {
	if x < 10 {
		if x == 2 || x == 3 || x == 5 || x == 7 {
			return true
		} else {
			return false
		}
	} else {
		j := 0
		k := math.Sqrt(float64(x))
		for m := 3; m <= int(k); {
			if x%m == 0 {
				j = 1
				break
			}
			m = m + 2
		}
		if j == 1 {
			return false
		} else {
			return true
		}
	}
}

func PrimeFactorization(n int) (pfs map[int]int) {
	pfs = make(map[int]int)

	// Get the number of 2s that divide n
	for n%2 == 0 {
		if _, ok := pfs[2]; ok {
			pfs[2] += 1
		} else {
			pfs[2] = 1
		}
		n = n / 2
	}

	// n must be odd at this point. so we can skip one element
	// (note i = i + 2)
	for i := 3; i*i <= n; i = i + 2 {
		// while i divides n, append i and divide n
		for n%i == 0 {
			if _, ok := pfs[i]; ok {
				pfs[i] += 1
			} else {
				pfs[i] = 1
			}
			n = n / i
		}
	}

	// This condition is to handle the case when n is a prime number
	// greater than 2
	if n > 2 {
		pfs[n] = 1
	}

	return
}

func NumberOfDivisors(n int) int {
	pfs := PrimeFactorization(n)

	num := 1
	for _, exponents := range pfs {
		num *= (exponents + 1)
	}

	return num
}

func KthPrime(x int) (y int) {
	if x == 1 {
		y = 2
	}
	if x == 2 {
		y = 3
	}
	if x > 2 {
		var sum int = 2
		begin := 5
		for sum < x+1 {
			if IsItPrime(begin) == true {
				sum++
			}
			if sum == x {
				y = begin
				break
			} else {
				if begin%6 == 1 {
					begin = begin + 4
				} else {
					begin = begin + 2
				}
			}
		}
	}
	return
}

func MODULE(x, y int) int {
	if x > y {
		return x - y
	} else {
		return y - x
	}
}

func BITAND(x, y int64) int64 {
	var ans int64
	begin := x
	sec := y
	var count int64 = 1
	for 1 == 1 {
		if (begin%2)*(sec%2) == 1 {
			ans = ans + count
			count = count * 2
			begin = int64(begin / 2)
			sec = int64(sec / 2)
			if begin*sec == 0 {
				break
			}

		} else {
			count = count * 2
			begin = int64(begin / 2)
			sec = int64(sec / 2)
			if begin*sec == 0 {
				break
			}
		}
	}
	return ans
}

func Sum(n, m int) int {
	var ans int

	if n == 1 {
		return 0
	} else {
		for i := 2; i < n; i++ {
			ans = (ans + Sum(i, m)) % m
		}
		for j := 2; j < n+1; j++ {
			ans = (ans + Sum(n/j, m)) % m
		}
		return ans
	}
}

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)

		ARRAY := make([]int, n)

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

		}

		sort.Ints(ARRAY)

		q := ARRAY[n-1]

		if ARRAY[n-1]-ARRAY[n-2] > 1 {
			fmt.Println(0)
		} else {
			var m int64
			var tt int64
			var olol bool
			if q == ARRAY[n-2] {
				m = 1
				tt = 1
				olol = true
			} else {
				ans := 0
				for i := n - 2; i > -1; i-- {
					if ARRAY[i] == q-1 {
						ans++
					} else {
						break
					}
				}
				m = int64(ans)
				tt = int64(ans)
				olol = false
			}
			for i := 2; i < n+1; i++ {
				if int64(i) != tt+1 {
					m = m * int64(i)
					m = m % 998244353
				}
			}
			if olol == true {
				m = m * 2
				m = m % 998244353
				fmt.Println(m)
			} else {
				fmt.Println(m)
			}

		}

	}
}

package main

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

const mod = 998244353

var f [2 * 100001]int

func mul(arr ...int) int {
	res := 1
	for _, v := range arr {
		res = int((int64(res) * int64(v)) % mod)
	}
	return res
}

func fact() {
	f[0] = 1
	for i := 1; i < len(f); i++ {
		f[i] = mul(f[i-1], i)
	}
}

func pow(a, p int) int {
	if p == 0 {
		return 0
	}
	if p == 1 {
		return a
	}
	x := pow(a, p/2)
	x = mul(x, x)
	if p%2 == 1 {
		x = mul(x, a)
	}
	return x
}

func inv(n int) int {
	return pow(f[n], mod-2)
}

func main() {
	fact()

	r := bufio.NewReaderSize(os.Stdin, 1024*1024)
	var T int
	fmt.Fscanf(r, "%d\n", &T)

	for t := 0; t < T; t++ {
		var n int
		fmt.Fscanf(r, "%d\n", &n)
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscanf(r, "%d", &arr[i])
		}
		fmt.Fscanf(r, "\n")

		sort.Ints(arr)

		m, p := 0, 0
		for i := 0; i < n; i++ {
			if arr[i] == arr[n-1] {
				m++
			}
			if arr[i] == arr[n-2] {
				p++
			}
		}

		//1 2 3 3 3 4
		// 3 3 3 4
		// 3 3 3 X 4 X
		//

		if m > 1 {
			fmt.Println(f[n])
		} else if arr[n-1]-arr[n-2] > 1 {
			fmt.Println(0)
		} else {
			res := int64(f[n])

			for i := p + 1; i <= n; i++ {
				v := n - m - p
				k := i - p - 1
				res -= int64(mul(f[n-i], f[i-1], f[v], inv(k), inv(v-k)))
				if res < 0 {
					res += mod
				}
			}

			fmt.Println(res)
		}
	}
}

package main

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

const MOD int64 = 998_244_353

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

		ar := make([]int, n)
		for i := range ar {
			fmt.Fscan(in, &ar[i])
		}

		max := 0
		for i := range ar {
			if ar[i] > ar[max] {
				max = i
			}
		}

		c := 0
		k := 0
		for i := range ar {
			if ar[i] == ar[max] {
				c++
			}
			if ar[i] == ar[max]-1 {
				k++
			}
		}

		var fact int64 = 1
		var except int64 = 1
		for i := int64(1); i <= n; i++ {
			fact *= i
			fact %= MOD
			if i != int64(k+1) {
				except *= i
				except %= MOD
			}
		}

		if c == 1 {
			fact = (fact - except + MOD) % MOD
		}

		fmt.Fprintln(out, fact)
	}
}

package main

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

var in = bufio.NewReader(os.Stdin)

const kMod int64 = 998_244_353

func factorialWithSkip(rawX, rawSkip int) int64 {
	x, skip := int64(rawX), int64(rawSkip)
	result := int64(1)
	for i := int64(1); i <= x; i++ {
		if i == skip {
			result *= i - 1
		} else {
			result *= i
		}
		result %= kMod
	}
	return result
}

func solve() {
	var n int
	fmt.Fscan(in, &n)

	aCnt := make(map[int]int)
	maxV := 0
	for i := 0; i < n; i++ {
		var ai int
		fmt.Fscan(in, &ai)

		aCnt[ai]++
		if ai > maxV {
			maxV = ai
		}
	}

	if aCnt[maxV] > 1 {
		fmt.Println(factorialWithSkip(n, 0))
		return
	}
	if aCnt[maxV] == 1 {
		secondFreq, ok := aCnt[maxV-1]
		if !ok {
			fmt.Println(0)
			return
		}
		fmt.Println(factorialWithSkip(n, secondFreq+1))
	} else {
		panic("impossible")
	}
}

func main() {
	var t int
	fmt.Fscan(in, &t)

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

package main

import "fmt"

const mod = 998244353

func add(a int, b int) int{
	a+=b
	if a>=mod{ a-=mod}
	return a
}

func sub(a int, b int) int{
	a-=b
	if (a<0){a+=mod}
	return a
}

func mul(a int, b int) int{
	res := int64(a) * int64(b) % mod
	return int(res)
}

func pow(a int , b int) int{
	if b==0{
		return 1;
	}
	var t int = pow(a, b >> 1)
	t = mul(t,t)
	if (b&1) > 0{
		t = mul(t, a)
	}
	return t
}

func choosen (k int , n int) int{
	if (k > n) {
		return 0
	}
	return mul(fact[n], mul(ifact[k], ifact[n-k]))
}

func inv(a int) int{
	return pow(a, mod-2)
}

const N = 2e5
var fact[N+1] int
var ifact[N+1] int

func readInt() int{
	var n int
	fmt.Scanf("%d\n", &n)
	return n
}

func readArrayInt(n int) [] int{
	res := make([]int, n)
	for i := 0; i < n; i++{
		fmt.Scanf("%d", &res[i])
	}
	return res
}

func Solve(){
	var n int = readInt()
	var M1 int = 0
	var M2 int = 0
	var cnt1 int = 0
	var cnt2 int = 0
	var a = readArrayInt(n)
	for i := 0; i < n; i++{
		fmt.Print(a[i])
		if (a[i] >= M1) {
			if (a[i] > M1){
				cnt2 = cnt1
				M2 = M1
				cnt1 = 0
				M1 = a[i]
			}
			cnt1++
		} else if (a[i] >= M2){
			if (a[i] > M2){
				cnt2 = 0
				M2 = a[i]
			}
			cnt2++
		}
	}
	//fmt.Println(M1, M2, cnt1, cnt2)
	if (cnt1 > 1){
		fmt.Println(fact[n])
		return
	}
	if (M1 - M2 > 1){
		fmt.Println(0)
		return
	}
	var ans int = 0
	for right := 1; right <= n - 1; right++{
		var t = mul(mul(fact[right], fact[n - 1 - right]), sub(choosen(right, n-1), choosen(right, n-1-cnt2)))
		ans = add(ans, t)
	}
	fmt.Println(ans)
}

func main(){
	fact[0] = 1
	ifact[0] = 1
	for i := 1; i <= N; i++{
		fact[i] = mul(fact[i-1], i)
		ifact[i] = mul(ifact[i-1], inv(i))
	}
	for T:=readInt();T>0;T--{
		Solve()
	}
}

package main

import "fmt"

const mod = 998244353

func add(a int, b int) int{
	a+=b
	if a>=mod{ a-=mod}
	return a
}

func sub(a int, b int) int{
	a-=b
	if (a<0){a+=mod}
	return a
}

func mul(a int, b int) int{
	res := int64(a) * int64(b) % mod
	return int(res)
}

func pow(a int , b int) int{
	if b==0{
		return 1;
	}
	var t int = pow(a, b >> 1)
	t = mul(t,t)
	if (b&1) > 0{
		t = mul(t, a)
	}
	return t
}

func choosen (k int , n int) int{
	if (k > n) {
		return 0
	}
	return mul(fact[n], mul(ifact[k], ifact[n-k]))
}

func inv(a int) int{
	return pow(a, mod-2)
}

const N = 2e5
var fact[N+1] int
var ifact[N+1] int

func readInt() int{
	var n int
	fmt.Scanf("%d\n", &n)
	return n
}

func readArrayInt(n int) [] int{
	res := make([]int, n)
	for i := 0; i < n; i++{
		fmt.Scanf("%d", &res[i])
	}
	return res
}

func Solve(){
	var n int = readInt()
	var M1 int = 0
	var M2 int = 0
	var cnt1 int = 0
	var cnt2 int = 0
	var a = readArrayInt(n)
	for i := 0; i < n; i++{
		if (a[i] >= M1) {
			if (a[i] > M1){
				cnt2 = cnt1
				M2 = M1
				cnt1 = 0
				M1 = a[i]
			}
			cnt1++
		} else if (a[i] >= M2){
			if (a[i] > M2){
				cnt2 = 0
				M2 = a[i]
			}
			cnt2++
		}
	}
	//fmt.Println(M1, M2, cnt1, cnt2)
	if (cnt1 > 1){
		fmt.Println(fact[n])
		return
	}
	if (M1 - M2 > 1){
		fmt.Println(0)
		return
	}
	var ans int = 0
	for right := 1; right <= n - 1; right++{
		var t = mul(mul(fact[right], fact[n - 1 - right]), sub(choosen(right, n-1), choosen(right, n-1-cnt2)))
		ans = add(ans, t)
	}
	fmt.Println(ans)
}

func main(){
	fact[0] = 1
	ifact[0] = 1
	for i := 1; i <= N; i++{
		fact[i] = mul(fact[i-1], i)
		ifact[i] = mul(ifact[i-1], inv(i))
	}
	for T:=readInt();T>0;T--{
		Solve()
	}
}

package main

import "fmt"

const mod = 998244353

func add(a int, b int) int{
	a+=b
	if a>=mod{ a-=mod}
	return a
}

func sub(a int, b int) int{
	a-=b
	if (a<0){a+=mod}
	return a
}

func mul(a int, b int) int{
	res := int64(a) * int64(b) % mod
	return int(res)
}

func pow(a int , b int) int{
	if b==0{
		return 1;
	}
	var t int = pow(a, b >> 1)
	t = mul(t,t)
	if (b&1) > 0{
		t = mul(t, a)
	}
	return t
}

func choosen (k int , n int) int{
	if (k > n) {
		return 0
	}
	return mul(fact[n], mul(ifact[k], ifact[n-k]))
}

func inv(a int) int{
	return pow(a, mod-2)
}

const N = 2e5
var fact[N+1] int
var ifact[N+1] int

func readInt() int{
	var n int
	fmt.Scanf("%d", &n)
	return n
}

func Solve(){
	var n int = readInt()
	var M1 int = 0
	var M2 int = 0
	var cnt1 int = 0
	var cnt2 int = 0
	var a = make([]int, n)
	for i := 0; i < n; i++{
		a[i] = readInt()
		if (a[i] >= M1) {
			if (a[i] > M1){
				cnt2 = cnt1
				M2 = M1
				cnt1 = 0
				M1 = a[i]
			}
			cnt1++
		} else if (a[i] >= M2){
			if (a[i] > M2){
				cnt2 = 0
				M2 = a[i]
			}
			cnt2++
		}
	}
	//fmt.Println(M1, M2, cnt1, cnt2)
	if (cnt1 > 1){
		fmt.Println(fact[n])
		return
	}
	if (M1 - M2 > 1){
		fmt.Println(0)
		return
	}
	var ans int = 0
	for right := 1; right <= n - 1; right++{
		var t = mul(mul(fact[right], fact[n - 1 - right]), sub(choosen(right, n-1), choosen(right, n-1-cnt2)))
		ans = add(ans, t)
	}
	fmt.Println(ans)
}

func main(){
	fact[0] = 1
	ifact[0] = 1
	for i := 1; i <= N; i++{
		fact[i] = mul(fact[i-1], i);
		ifact[i] = mul(ifact[i-1], inv(i));
	}
	for T:=readInt();T>0;T--{
		Solve()
	}
}

package main

import "fmt"

const mod = 998244353

func add(a int, b int) int{
	a+=b
	if a>=mod{ a-=mod}
	return a
}

func sub(a int, b int) int{
	a-=b
	if (a<0){a+=mod}
	return a
}

func mul(a int, b int) int{
	res := int64(a) * int64(b) % mod
	return int(res)
}

func pow(a int , b int) int{
	if b==0{
		return 1;
	}
	var t int = pow(a, b >> 1)
	t = mul(t,t)
	if (b&1) > 0{
		t = mul(t, a)
	}
	return t
}

func choosen (k int , n int) int{
	if (k > n) {
		return 0
	}
	return mul(fact[n], mul(ifact[k], ifact[n-k]))
}

func inv(a int) int{
	return pow(a, mod-2)
}

const N = 2e5
var fact[N+1] int
var ifact[N+1] int

func readInt() int{
	var n int
	fmt.Scanf("%d", &n)
	return n
}

func Solve(){
	var n int = readInt()
	var M1 int = 0
	var M2 int = 0
	var cnt1 int = 0
	var cnt2 int = 0
	var a = make([]int, n)
	for i := 0; i < n; i++{
		a[i] = readInt()
		if (a[i] >= M1) {
			if (a[i] > M1){
				cnt2 = cnt1
				M2 = M1
				cnt1 = 0
				M1 = a[i]
			}
			cnt1++
		} else if (a[i] >= M2){
			if (a[i] > M2){
				cnt2 = 0
				M2 = a[i]
			}
			cnt2++
		}
	}
	if (cnt1 > 1){
		fmt.Println(fact[n])
		return
	}
	if (M1 - M2 > 1){
		fmt.Println(0)
		return
	}
	var ans int = 0
	for right := 1; right <= n - 1; right++{
		var t = mul(mul(fact[right], fact[n - 1 - right]), sub(choosen(right, n-1), choosen(right, n-1-cnt2)))
		ans = add(ans, t)
	}
	fmt.Println(ans)
}

func main(){
	fact[0] = 1
	ifact[0] = 1
	for i := 1; i <= N; i++{
		fact[i] = mul(fact[i-1], i);
		ifact[i] = mul(ifact[i-1], inv(i));
	}
	var T int = readInt()
	for ;T>0;T--{
		Solve()
	}
}

package main

import "fmt"

const mod = 998244353

func add(a int, b int) int{
	a+=b
	if a>=mod{ a-=mod}
	return a
}

func sub(a int, b int) int{
	a-=b
	if (a<0){a+=mod}
	return a
}

func mul(a int, b int) int{
	res := int64(a) * int64(b) % mod
	return int(res)
}

func pow(a int , b int) int{
	if b==0{
		return 1;
	}
	var t int = pow(a, b >> 1)
	t = mul(t,t)
	if (b&1) > 0{
		t = mul(t, a)
	}
	return t
}

func choosen (k int , n int) int{
	if (k > n) {
		return 0
	}
	return mul(fact[n], mul(ifact[k], ifact[n-k]))
}

func inv(a int) int{
	return pow(a, mod-2)
}

const N = 2e5
var fact[N+1] int
var ifact[N+1] int

func readInt() int{
	var n int
	fmt.Scanf("%d", &n)
	return n
}

func Solve(){
	var n int = readInt()
	var M1 int = 0
	var M2 int = 0
	var cnt1 int = 0
	var cnt2 int = 0
	var a = make([]int, n)
	for i := 0; i < n; i++{
		a[i] = readInt()
		if (a[i] >= M1) {
			if (a[i] > M1){
				cnt2 = cnt1
				M2 = M1
				cnt1 = 0
				M1 = a[i]
			}
			cnt1++
		} else if (a[i] >= M2){
			if (a[i] > M2){
				cnt2 = 0
				M2 = a[i]
			}
			cnt2++
		}
	}
	//fmt.Println(M1, M2, cnt1, cnt2)
	if (cnt1 > 1){
		fmt.Println(fact[n])
		return
	}
	if (M1 - M2 > 1){
		fmt.Println(0)
		return
	}
	var ans int = 0
	for right := 1; right <= n - 1; right++{
		var t = mul(mul(fact[right], fact[n - 1 - right]), sub(choosen(right, n-1), choosen(right, n-1-cnt2)))
		ans = add(ans, t)
	}
	fmt.Println(ans)
}

func main(){
	fact[0] = 1
	ifact[0] = 1
	for i := 1; i <= N; i++{
		fact[i] = mul(fact[i-1], i);
		ifact[i] = mul(ifact[i-1], inv(i));
	}
	var T int = readInt()
	for ;T>0;T--{
		Solve()
	}
}

package main

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

// var m []int
// err = json.Unmarshal([]byte(r.FormValue("example")), &m)

// for i := 0; i < len(m); i++ {
// 	fmt.Println(m[i])
// }

// sort.Slice(arr, func(i, j int) bool {
// 	return arr[i] > arr[j]
// })

func MIN(x, y int64) int64 {
	if x > y {
		return y
	} else {
		return x
	}
}

func MINN(x, y int) int {
	if x > y {
		return y
	} else {
		return x
	}
}

func MKO(x, y int64) int64 {
	if x < y {
		return 0
	} else {
		return x - y + 1
	}
}

func MAX(x, y int64) int64 {
	if x > y {
		return x
	} else {
		return y
	}
}

func MAXX(x, y int) int {
	if x > y {
		return x
	} else {
		return y
	}
}

func LCM(x, y int) int {
	if x >= y {
		initial := x
		for i := 1; i < y+2; i++ {
			if initial%y == 0 {
				break
			} else {
				initial = initial + x
			}

		}
		return initial
	} else {
		initial := y
		for i := 1; i < x+2; i++ {
			if initial%x == 0 {
				break
			} else {
				initial = initial + y
			}

		}
		return initial
	}
}

func GCD(x, y int64) int64 {
	if x*y == 0 {
		return MAX(x, y)
	} else {
		var l, q int64
		l = MAX(x, y)
		q = MIN(x, y)
		var m int64
		for l*q > 0 {
			a := l % q
			b := l - a
			cc := int64(b / q)
			remain := l - cc*q
			l = MAX(remain, q)
			q = MIN(remain, q)
			if l*q == 0 {
				m = l + q
				break
			}

		}
		return m
	}
}

func IsItPrime(x int) bool {
	if x < 10 {
		if x == 2 || x == 3 || x == 5 || x == 7 {
			return true
		} else {
			return false
		}
	} else {
		j := 0
		k := math.Sqrt(float64(x))
		for m := 3; m <= int(k); {
			if x%m == 0 {
				j = 1
				break
			}
			m = m + 2
		}
		if j == 1 {
			return false
		} else {
			return true
		}
	}
}

func PrimeFactorization(n int) (pfs map[int]int) {
	pfs = make(map[int]int)

	// Get the number of 2s that divide n
	for n%2 == 0 {
		if _, ok := pfs[2]; ok {
			pfs[2] += 1
		} else {
			pfs[2] = 1
		}
		n = n / 2
	}

	// n must be odd at this point. so we can skip one element
	// (note i = i + 2)
	for i := 3; i*i <= n; i = i + 2 {
		// while i divides n, append i and divide n
		for n%i == 0 {
			if _, ok := pfs[i]; ok {
				pfs[i] += 1
			} else {
				pfs[i] = 1
			}
			n = n / i
		}
	}

	// This condition is to handle the case when n is a prime number
	// greater than 2
	if n > 2 {
		pfs[n] = 1
	}

	return
}

func NumberOfDivisors(n int) int {
	pfs := PrimeFactorization(n)

	num := 1
	for _, exponents := range pfs {
		num *= (exponents + 1)
	}

	return num
}

func KthPrime(x int) (y int) {
	if x == 1 {
		y = 2
	}
	if x == 2 {
		y = 3
	}
	if x > 2 {
		var sum int = 2
		begin := 5
		for sum < x+1 {
			if IsItPrime(begin) == true {
				sum++
			}
			if sum == x {
				y = begin
				break
			} else {
				if begin%6 == 1 {
					begin = begin + 4
				} else {
					begin = begin + 2
				}
			}
		}
	}
	return
}

func MODULE(x, y int) int {
	if x > y {
		return x - y
	} else {
		return y - x
	}
}

func BITAND(x, y int64) int64 {
	var ans int64
	begin := x
	sec := y
	var count int64 = 1
	for 1 == 1 {
		if (begin%2)*(sec%2) == 1 {
			ans = ans + count
			count = count * 2
			begin = int64(begin / 2)
			sec = int64(sec / 2)
			if begin*sec == 0 {
				break
			}

		} else {
			count = count * 2
			begin = int64(begin / 2)
			sec = int64(sec / 2)
			if begin*sec == 0 {
				break
			}
		}
	}
	return ans
}

func Sum(n, m int) int {
	var ans int

	if n == 1 {
		return 0
	} else {
		for i := 2; i < n; i++ {
			ans = (ans + Sum(i, m)) % m
		}
		for j := 2; j < n+1; j++ {
			ans = (ans + Sum(n/j, m)) % m
		}
		return ans
	}
}

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)

		ARRAY := make([]int64, n)

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

		}

		sort.Slice(ARRAY, func(i, j int) bool {
			return ARRAY[i] < ARRAY[j]
		})

		q := ARRAY[n-1]

		if ARRAY[n-1]-ARRAY[n-2] > 1 {
			fmt.Println(0)
		} else {
			var m int64 = 1
			for i := 2; i < n+1; i++ {
				m = m * int64(i)
				m = m - m/998244353
			}
			if q-ARRAY[n-2] == 0 {
				fmt.Println(m)
			} else {
				ans := 0
				for i := n - 2; i > -1; i-- {
					if ARRAY[i] == q-1 {
						ans++
					} else {
						break
					}
				}

				m = m * int64(ans)
				m = m / int64(ans+1)
				m = m - m/998244353
				fmt.Println(m)

			}

		}

	}
}

package main

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

// var m []int
// err = json.Unmarshal([]byte(r.FormValue("example")), &m)

// for i := 0; i < len(m); i++ {
// 	fmt.Println(m[i])
// }

// sort.Slice(arr, func(i, j int) bool {
// 	return arr[i] > arr[j]
// })

func MIN(x, y int64) int64 {
	if x > y {
		return y
	} else {
		return x
	}
}

func MINN(x, y int) int {
	if x > y {
		return y
	} else {
		return x
	}
}

func MKO(x, y int64) int64 {
	if x < y {
		return 0
	} else {
		return x - y + 1
	}
}

func MAX(x, y int64) int64 {
	if x > y {
		return x
	} else {
		return y
	}
}

func MAXX(x, y int) int {
	if x > y {
		return x
	} else {
		return y
	}
}

func LCM(x, y int) int {
	if x >= y {
		initial := x
		for i := 1; i < y+2; i++ {
			if initial%y == 0 {
				break
			} else {
				initial = initial + x
			}

		}
		return initial
	} else {
		initial := y
		for i := 1; i < x+2; i++ {
			if initial%x == 0 {
				break
			} else {
				initial = initial + y
			}

		}
		return initial
	}
}

func GCD(x, y int64) int64 {
	if x*y == 0 {
		return MAX(x, y)
	} else {
		var l, q int64
		l = MAX(x, y)
		q = MIN(x, y)
		var m int64
		for l*q > 0 {
			a := l % q
			b := l - a
			cc := int64(b / q)
			remain := l - cc*q
			l = MAX(remain, q)
			q = MIN(remain, q)
			if l*q == 0 {
				m = l + q
				break
			}

		}
		return m
	}
}

func IsItPrime(x int) bool {
	if x < 10 {
		if x == 2 || x == 3 || x == 5 || x == 7 {
			return true
		} else {
			return false
		}
	} else {
		j := 0
		k := math.Sqrt(float64(x))
		for m := 3; m <= int(k); {
			if x%m == 0 {
				j = 1
				break
			}
			m = m + 2
		}
		if j == 1 {
			return false
		} else {
			return true
		}
	}
}

func PrimeFactorization(n int) (pfs map[int]int) {
	pfs = make(map[int]int)

	// Get the number of 2s that divide n
	for n%2 == 0 {
		if _, ok := pfs[2]; ok {
			pfs[2] += 1
		} else {
			pfs[2] = 1
		}
		n = n / 2
	}

	// n must be odd at this point. so we can skip one element
	// (note i = i + 2)
	for i := 3; i*i <= n; i = i + 2 {
		// while i divides n, append i and divide n
		for n%i == 0 {
			if _, ok := pfs[i]; ok {
				pfs[i] += 1
			} else {
				pfs[i] = 1
			}
			n = n / i
		}
	}

	// This condition is to handle the case when n is a prime number
	// greater than 2
	if n > 2 {
		pfs[n] = 1
	}

	return
}

func NumberOfDivisors(n int) int {
	pfs := PrimeFactorization(n)

	num := 1
	for _, exponents := range pfs {
		num *= (exponents + 1)
	}

	return num
}

func KthPrime(x int) (y int) {
	if x == 1 {
		y = 2
	}
	if x == 2 {
		y = 3
	}
	if x > 2 {
		var sum int = 2
		begin := 5
		for sum < x+1 {
			if IsItPrime(begin) == true {
				sum++
			}
			if sum == x {
				y = begin
				break
			} else {
				if begin%6 == 1 {
					begin = begin + 4
				} else {
					begin = begin + 2
				}
			}
		}
	}
	return
}

func MODULE(x, y int) int {
	if x > y {
		return x - y
	} else {
		return y - x
	}
}

func BITAND(x, y int64) int64 {
	var ans int64
	begin := x
	sec := y
	var count int64 = 1
	for 1 == 1 {
		if (begin%2)*(sec%2) == 1 {
			ans = ans + count
			count = count * 2
			begin = int64(begin / 2)
			sec = int64(sec / 2)
			if begin*sec == 0 {
				break
			}

		} else {
			count = count * 2
			begin = int64(begin / 2)
			sec = int64(sec / 2)
			if begin*sec == 0 {
				break
			}
		}
	}
	return ans
}

func Sum(n, m int) int {
	var ans int

	if n == 1 {
		return 0
	} else {
		for i := 2; i < n; i++ {
			ans = (ans + Sum(i, m)) % m
		}
		for j := 2; j < n+1; j++ {
			ans = (ans + Sum(n/j, m)) % m
		}
		return ans
	}
}

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)

		ARRAY := make([]int, n)

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

		}

		sort.Ints(ARRAY)

		q := ARRAY[n-1]

		if ARRAY[n-1]-ARRAY[n-2] > 1 {
			fmt.Println(0)
		} else {
			var m int64 = 1
			for i := 2; i < n+1; i++ {
				m = m * int64(i)
				m = m % 998244353
			}
			if q-ARRAY[n-2] == 0 {
				fmt.Println(m)
			} else {
				ans := 0
				for i := n - 2; i > -1; i-- {
					if ARRAY[i] == q-1 {
						ans++
					} else {
						break
					}
				}

				m = m * int64(ans)
				m = m / int64(ans+1)
				m = m % 998244353
				fmt.Println(m)

			}

		}

	}
}

package main

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

const mod = 998244353

var f [2 * 100001]int

func mul(arr ...int) int {
	res := 1
	for _, v := range arr {
		res = int((int64(res) * int64(v)) % mod)
	}
	return res
}

func fact() {
	f[0] = 1
	for i := 1; i < len(f); i++ {
		f[i] = mul(f[i-1], i)
	}
}

func pow(a, p int) int {
	if p == 0 {
		return 0
	}
	if p == 1 {
		return a
	}
	x := pow(a, p/2)
	x = mul(x, x)
	if p%2 == 1 {
		x = mul(x, a)
	}
	return x
}

func inv(n int) int {
	return pow(f[n], mod-2)
}

func main() {
	fact()

	r := bufio.NewReaderSize(os.Stdin, 1024*1024)
	var T int
	fmt.Fscanf(r, "%d\n", &T)

	for t := 0; t < T; t++ {
		var n int
		fmt.Fscanf(r, "%d\n", &n)
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscanf(r, "%d", &arr[i])
		}
		fmt.Fscanf(r, "\n")

		sort.Ints(arr)

		m, p := 0, 0
		for i := 0; i < n; i++ {
			if arr[i] == arr[n-1] {
				m++
			}
			if arr[i] == arr[n-2] {
				p++
			}
		}

		//1 2 3 3 3 4
		// 3 3 3 4
		// 3 3 3 X 4 X
		//

		if m > 1 {
			fmt.Println(f[n])
		} else if arr[n-1]-arr[n-2] > 1 {
			fmt.Println(0)
		} else {
			res := 0

			for i := p + 1; i <= n; i++ {
				v := n - m - p
				k := i - p - 1
				res += mul(f[n-i], f[i-1], f[v], inv(k), inv(v-k))
			}

			x := int64(f[n]) - int64(res)
			if x < 0 {
				x += mod
			}
			fmt.Println(x)
		}
	}
}

package main

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

const mod = 998244353

var f [2 * 100001]int

func mul(arr ...int) int {
	res := 1
	for _, v := range arr {
		res = int((int64(res) * int64(v)) % mod)
	}
	return res
}

func fact() {
	f[0] = 1
	for i := 1; i < len(f); i++ {
		f[i] = mul(f[i-1], i)
	}
}

func pow(a, p int) int {
	if p == 0 {
		return 0
	}
	if p == 1 {
		return a
	}
	x := pow(a, p/2)
	x = mul(x, x)
	if p%2 == 1 {
		x = mul(x, a)
	}
	return x
}

func inv(n int) int {
	return pow(f[n], mod-2)
}

func main() {
	fact()

	r := bufio.NewReaderSize(os.Stdin, 1024*1024)
	var T int
	fmt.Fscanf(r, "%d\n", &T)

	for t := 0; t < T; t++ {
		var n int
		fmt.Fscanf(r, "%d\n", &n)
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscanf(r, "%d", &arr[i])
		}
		fmt.Fscanf(r, "\n")

		sort.Ints(arr)

		m, p := 0, 0
		for i := 0; i < n; i++ {
			if arr[i] == arr[n-1] {
				m++
			}
			if arr[i] == arr[n-2] {
				p++
			}
		}

		//1 2 3 3 3 4
		// 3 3 3 4
		// 3 3 3 X 4 X
		//

		if m > 1 {
			fmt.Println(f[n])
		} else if arr[n-1]-arr[n-2] > 1 {
			fmt.Println(0)
		} else {
			res := 0

			for i := p + 1; i <= n; i++ {
				v := n - m - p
				k := i - p - 1
				res += mul(f[n-i], f[i-1], f[v], inv(k), inv(v-k))
			}

			res = f[n] - res
			if res < 0 {
				res += mod
			}
			fmt.Println(res)
		}
	}
}

package main

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

const MOD int64 = 998_244_353

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

		ar := make([]int, n)
		for i := range ar {
			fmt.Fscan(in, &ar[i])
		}

		max1, max2 := 0, 0
		max2count := 0
		for i := range ar {
			if ar[i] >= ar[max1] {
				max2 = max1
				max1 = i
				max2count = 1
			}
			if ar[i] == ar[max2] {
				max2count++
			}
		}

		if ar[max1]-ar[max2] > 1 {
			fmt.Fprintln(out, 0)
		} else {
			var fact, except int64 = 1, 1
			var cur int64 = 1
			for i := 0; i < n; i++ {
				fact *= cur
				fact %= MOD
				if cur != int64(max2count+1) {
					except *= cur
					except %= MOD
				}
				cur++
			}

			if ar[max1] != ar[max2] {
				fact = (fact - except + MOD) % MOD
			}

			fmt.Fprintln(out, fact)
		}

	}
}

package main

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

var in = bufio.NewReader(os.Stdin)

const kMod int64 = 998224353

func factorialWithSkip(rawX, rawSkip int) int64 {
	x, skip := int64(rawX), int64(rawSkip)
	result := int64(1)
	for i := int64(1); i <= x; i++ {
		if i == skip {
			result *= i - 1
		} else {
			result *= i
		}
		result %= kMod
	}
	return result
}

func solve() {
	var n int
	fmt.Fscan(in, &n)

	aCnt := make(map[int]int)
	maxV := 0
	for i := 0; i < n; i++ {
		var ai int
		fmt.Fscan(in, &ai)

		aCnt[ai]++
		if ai > maxV {
			maxV = ai
		}
	}

	if aCnt[maxV] > 1 {
		fmt.Println(factorialWithSkip(n, 0))
		return
	}
	if aCnt[maxV] == 1 {
		secondFreq, ok := aCnt[maxV-1]
		if !ok {
			fmt.Println(0)
			return
		}
		fmt.Println(factorialWithSkip(n, secondFreq+1))
	} else {
		panic("impossible")
	}
}

func main() {
	var t int
	fmt.Fscan(in, &t)

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


There is a polyline going through points (0, 0) – (x, x) – (2x, 0) – (3x, x) – (4x, 0) – ... - (2kx, 0) – (2kx + x, x) – .... We know that the polyline passes through the point (a, b). Find minimum positive value x such that it is true or determine that there is no such x.

Only one line containing two positive integers a and b (1 ≤ a, b ≤ 109).

Output the only line containing the answer. Your answer will be considered correct if its relative or absolute error doesn't exceed 10 - 9. If there is no such x then output  - 1 as the answer.

NoteYou can see following graphs for sample 1 and sample 3.

// test project main.go
package main

import (
	"fmt"
)

func main() {
	var a,b int64

	fmt.Scanf("%d %d", &a, &b)

	if(a < b){
		fmt.Println(-1)
		return
	}

	if(b == 0 || a==0){
		fmt.Println(1)
		return
	}

	if(a==b){
		fmt.Println(a)
		return
	}

	res1 := (float64)(a-b)/(float64)((a-b)/(2*b) * 2)
	res2 := (float64)(a+b)/(float64)((a+b)/(2*b) * 2)

	if(res1 > res2){
		res1 = res2
	}

	fmt.Println(res1)
}

// test project main.go
package main

import (
	"fmt"
)

func main() {
	var a,b int64

	fmt.Scanf("%d %d", &a, &b)

	if(a < b){
		fmt.Println(-1)
		return
	}

	if(b == 0){
		fmt.Println(1)
		return
	}
	res1 := (float64)(a-b)/(float64)((a-b)/2 * 2)
	res2 := (float64)(a+b)/(float64)((a+b)/2 * 2)
	if(res1 > res2){
		res1 = res2
	}
	fmt.Println(res1)
}

// test project main.go
package main

import (
	"fmt"
)

func main() {
	var a,b int64

	fmt.Scanf("%d %d", &a, &b)

	if(a < b){
		fmt.Println(-1)
		return
	}

	if(b == 0 || a==0){
		fmt.Println(1)
		return
	}

	if(a==b){
		fmt.Println(a)
		return
	}

	res1 := (float64)(a-b)/(float64)((a-b)/2 * 2)
	res2 := (float64)(a+b)/(float64)((a+b)/2 * 2)
	if(res1 > res2){
		res1 = res2
	}
	fmt.Println(res1)
}

// test project main.go
package main

import (
	"fmt"
)

func main() {
	var a,b int64

	fmt.Scanf("%d %d", &a, &b)

	if(a < b){
		fmt.Println(-1)
		return
	}

	if(b == 0){
		fmt.Println(1)
		return
	}

	if(a==b){
		fmt.Println(a)
		return
	}
	res1 := (float64)(a-b)/(float64)((a-b)/2 * 2)
	res2 := (float64)(a+b)/(float64)((a+b)/2 * 2)
	if(res1 > res2){
		res1 = res2
	}
	fmt.Println(res1)
}

// test project main.go
package main

import (
	"fmt"
)

func main() {
	var a,b int

	fmt.Scanf("%d %d", &a, &b)

	if(a < b){
		fmt.Println(-1)
		return
	}

	if(b == 0){
		fmt.Println(1)
		return
	}
	res1 := (float64)(a-b)/(float64)((a-b)/2 * 2)
	res2 := (float64)(a+b)/(float64)((a+b)/2 * 2)
	if(res1 > res2){
		res1 = res2
	}
	fmt.Println(res1)
}

// test project main.go
package main

import (
	"fmt"
)

func main() {
	var a,b int64

	fmt.Scanf("%d %d", &a, &b)

	if(a < b || a == 0){
		fmt.Println(-1)
		return
	}

	if(b == 0){
		fmt.Println(1)
		return
	}

	if(a==b){
		fmt.Println(a)
		return
	}

	res1 := (float64)(a-b)/(float64)((a-b)/2 * 2)
	res2 := (float64)(a+b)/(float64)((a+b)/2 * 2)
	if(res1 > res2){
		res1 = res2
	}
	fmt.Println(res1)
}

// test project main.go
package main

import (
	"fmt"
)

func main() {
	var a,b int64

	fmt.Scanf("%d %d", &a, &b)

	if(a < b){
		fmt.Println(-1)
		return
	}

	if(b == 0 || a==0){
		fmt.Println(1)
		return
	}

	if(a==b){
		fmt.Println(a)
		return
	}

	res1 := (float64)(a-b)/(float64)((a-b)/b)
	res2 := (float64)(a+b)/(float64)((a+b)/b)

	if(res1 > res2){
		res1 = res2
	}

	fmt.Println(res1)
}


Nauuo is a girl who loves drawing circles.One day she has drawn a circle and wanted to draw a tree on it.The tree is a connected undirected graph consisting of $$$n$$$ nodes and $$$n-1$$$ edges. The nodes are numbered from $$$1$$$ to $$$n$$$.Nauuo wants to draw a tree on the circle, the nodes of the tree should be in $$$n$$$ distinct points on the circle, and the edges should be straight without crossing each other."Without crossing each other" means that every two edges have no common point or the only common point is an endpoint of both edges.Nauuo wants to draw the tree using a permutation of $$$n$$$ elements. A permutation of $$$n$$$ elements is a sequence of integers $$$p_1,p_2,\ldots,p_n$$$ in which every integer from $$$1$$$ to $$$n$$$ appears exactly once.After a permutation is chosen Nauuo draws the $$$i$$$-th node in the $$$p_i$$$-th point on the circle, then draws the edges connecting the nodes.The tree is given, Nauuo wants to know how many permutations are there so that the tree drawn satisfies the rule (the edges are straight without crossing each other). She only wants to know the answer modulo $$$998244353$$$, can you help her?It is obvious that whether a permutation is valid or not does not depend on which $$$n$$$ points on the circle are chosen.

The first line contains a single integer $$$n$$$ ($$$2\le n\le 2\cdot 10^5$$$) — the number of nodes in the tree. Each of the next $$$n-1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1\le u,v\le n$$$), denoting there is an edge between $$$u$$$ and $$$v$$$. It is guaranteed that the given edges form a tree.

The output contains a single integer — the number of permutations suitable to draw the given tree on a circle satisfying the rule, modulo $$$998244353$$$.

NoteExample 1All valid permutations and their spanning trees are as follows.Here is an example of invalid permutation: the edges $$$(1,3)$$$ and $$$(2,4)$$$ are crossed.Example 2Every permutation leads to a valid tree, so the answer is $$$4! = 24$$$.

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1172B(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	const mod = 998244353
	const mx = 2e5
	F := [mx + 1]int64{1}
	for i := 1; i <= mx; i++ {
		F[i] = F[i-1] * int64(i) % mod
	}

	var n, v, w int
	Fscan(in, &n)
	g := make([][]int, n+1)
	for i := 1; i < n; i++ {
		Fscan(in, &v, &w)
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	ans := int64(n)
	for _, vs := range g {
		ans = ans * F[len(vs)] % mod
	}
	Fprint(out, ans)
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1172B(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	const mod = 998244353
	const mx = 2e5
	F := [mx + 1]int64{1}
	for i := 1; i <= mx; i++ {
		F[i] = F[i-1] * int64(i) % mod
	}
	F[1] = 2

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

	var f func(v, fa int) int64
	f = func(v, fa int) int64 {
		s := int64(1)
		for _, w := range g[v] {
			if w != fa {
				s = s * f(w, v) % mod
			}
		}
		return s * F[len(g[v])-1] % mod
	}
	Fprint(out, int64(n)*f(1, 0)%mod)
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1172B(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	const mod = 998244353
	const mx = 2e5
	F := [mx + 1]int64{1}
	for i := 1; i <= mx; i++ {
		F[i] = F[i-1] * int64(i) % mod
	}
	F[1] = 2

	var n, v, w int
	Fscan(in, &n)
	if n == 2 {
		Fprint(out, 2)
		return
	}
	g := make([][]int, n+1)
	g[1] = []int{0}
	for i := 1; i < n; i++ {
		Fscan(in, &v, &w)
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}

	var f func(v, fa int) int64
	f = func(v, fa int) int64 {
		s := int64(1)
		for _, w := range g[v] {
			if w != fa {
				s = s * f(w, v) % mod
			}
		}
		return s * F[len(g[v])-1] % mod
	}
	Fprint(out, int64(n)*f(1, 0)%mod)
}

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


Leo has developed a new programming language C+=. In C+=, integer variables can only be changed with a "+=" operation that adds the right-hand side value to the left-hand side variable. For example, performing "a += b" when a = $$$2$$$, b = $$$3$$$ changes the value of a to $$$5$$$ (the value of b does not change).In a prototype program Leo has two integer variables a and b, initialized with some positive values. He can perform any number of operations "a += b" or "b += a". Leo wants to test handling large integers, so he wants to make the value of either a or b strictly greater than a given value $$$n$$$. What is the smallest number of operations he has to perform?

The first line contains a single integer $$$T$$$ ($$$1 \leq T \leq 100$$$) — the number of test cases. Each of the following $$$T$$$ lines describes a single test case, and contains three integers $$$a, b, n$$$ ($$$1 \leq a, b \leq n \leq 10^9$$$) — initial values of a and b, and the value one of the variables has to exceed, respectively.

For each test case print a single integer — the smallest number of operations needed. Separate answers with line breaks.

NoteIn the first case we cannot make a variable exceed $$$3$$$ in one operation. One way of achieving this in two operations is to perform "b += a" twice.

package main

import (
  "fmt"
)

var T int
var fibs = [1000000]int{}
func main() {
  fmt.Scan(&T)
  for tc := 0; tc < T; tc++ {
    var a,b,n int
    fmt.Scan(&a, &b, &n)
    if a > b {
      a, b = b, a
    }
    if b > n {
      fmt.Println(0)
      continue
    }
    i := 1
    for {
      sum := a*(fib(i-1)) + b*fib(i)
      if sum > n {
        break
      }
      i++
    }
    fmt.Println(i)
  }

}

func fib(n int) int {
  if fibs[n] != 0 {
    return fibs[n]
  }
  if n == 0 || n == 1 {
    return 1
  }
  fibs[n] = fib(n-1) + fib(n-2)
  return fibs[n]
}

package main

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

func main() {
	var t, a, b, n int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &a, &b, &n)
		a, b := minmax(a, b)
		count := 0
		for b <= n {
			//fmt.Println(a, b)
			t := b
			b = a + b
			a = t
			count++

		}
		fmt.Println(count)
	}

}

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

package main

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

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

	tc := readNum(reader)

	for tc > 0 {
		tc--
		a, b, n := readThreeNums(reader)

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

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func solve(a, b, n int) int {
	if a > n || b > n {
		return 0
	}

	if a > b {
		a, b = b, a
	}
	var res int
	for b <= n {
		a += b
		a, b = b, a
		res++
	}
	return res
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := int64(1)
	fmt.Fscanf(in, "%d\n", &t)
	//fmt.Println("debug", t)
	for tt := int64(0); tt < t; tt++ {
		a, b, n := int64(0), int64(0), int64(0)
		fmt.Fscanf(in, "%d %d %d\n", &a, &b, &n)
		if a > b {
			a, b = b, a
		}
		ans := 0
		for b <= n {
			ans++
			a = a + b
			a, b = b, a
		}
		fmt.Fprintf(out, "%d\n", ans)
	}
	return
}

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

func same(s string, ss string) bool {
	a := make([]int, 26)
	for i := 0; i < len(s); i++ {
		b := int(s[i] - 'a')
		a[b]++
	}
	for i := 0; i < len(ss); i++ {
		b := int(ss[i] - 'a')
		a[b]--
		if a[b] == -1 {
			return false
		}
	}
	for i := 0; i < 26; i++ {
		if a[i] != 0 {
			return false
		}
	}
	return true
}

package main

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

var nTest, a, b, n int

func count(x, y, z int) int {
	cnt := 0
	for x <= z && y <= z {
		x += y
		cnt++
		if x > z {
			break
		}
		y += x
		cnt++
	}

	return cnt
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)

	fmt.Fscan(in, &nTest)
	for t := 0; t < nTest; t++ {
		fmt.Fscan(in, &a, &b, &n)

		if a > b {
			a, b = b, a
		}
		cnt2 := count(a, b, n)

		fmt.Fprintln(out, cnt2)
	}

	out.Flush()
}

package main

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

var nTest, a, b, n int

func count(x, y, z int) int {
	cnt := 0
	for x <= z && y <= z {
		x += y
		cnt++
		if x > z {
			break
		}
		y += x
		cnt++
	}

	return cnt
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)

	fmt.Fscan(in, &nTest)
	for t := 0; t < nTest; t++ {
		fmt.Fscan(in, &a, &b, &n)

		cnt1 := count(b, a, n)
		cnt2 := count(a, b, n)

		if cnt1 < cnt2 {
			fmt.Fprintln(out, cnt1)
		} else {
			fmt.Fprintln(out, cnt2)
		}
	}

	out.Flush()
}

package main

import (
	. "fmt"
)

// github.com/EndlessCheng/codeforces-go
func main() {
	var t, a, b, n int
	for Scan(&t); t > 0; t-- {
		Scan(&a, &b, &n)
		if a > b {
			a, b = b, a
		}
		i := 0
		for ; a <= n && b <= n; i++ {
			a, b = b, a+b
		}
		Println(i)
	}
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1368A(in io.Reader, out io.Writer) {
	var t, a, b, n int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &a, &b, &n)
		if a > b {
			a, b = b, a
		}
		i := 0
		for ; a <= n && b <= n; i++ {
			a, b = b, a+b
		}
		Fprintln(out, i)
	}
}

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

package main
import (
        "fmt"
        "os"
        "bufio"
)
func main(){
    var t, a, b, n, ans int
    in := bufio.NewReader(os.Stdin)
    out := bufio.NewWriter(os.Stdout)
    defer out.Flush()
    fmt.Fscan(in, &t)
    for ; t > 0; t-- {
        fmt.Fscan(in, &a, &b, &n)
        ans = 0
        for a <= n && b <= n {
            if a > b {
                a, b = b+a, a
            } else {
                a, b = a+b, b
            }
            ans++
        }
        fmt.Fprintln(out, ans)
    }
}

package main

import (
	"fmt"
)

func main() {
	var a, b, n, t int
	fmt.Scan(&t)

	for t != 0 {
		fmt.Scan(&a, &b, &n)
		count := 0
		for a <= n && b <= n {
			if a <= b {
				a += b
			} else {
				b += a
			}
			count++
		}
		fmt.Println(count)
		t--
	}
}

// a= 5 b =4

// a+=b  a = 9,4
// b += a a= 5,b=9  always increase the small one

package main

import (
	"fmt"
	"os"
)

func read(t ...interface{}) {
	fmt.Fscan(os.Stdin, t...)
}

func main() {
	var t int
	read(&t)
	for i:=0; i<t; i++ {
		var a, b, n, next, count int
		read(&a, &b, &n)
		next = max(a, b)
		for a <= n && b <= n {
			if next == b {
				a += b
				next = a
			} else  {
				b += a
				next = b
			}
			count++
		}
		fmt.Println(count)
	}
}

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

package main

import "fmt"

func main()  {
	var a,b,n,ans int64
	var c int
	_, _ = fmt.Scan(&c)
	for i:=0;i<c;i++{
		_, _ = fmt.Scan(&a, &b, &n)
		ans=0
		for {
			if a>b{
				b=a+b
			}else {
				a=a+b
			}
			ans++
			if a>n||b>n{
				fmt.Println(ans)
				break
			}
		}
	}
}

package main

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

func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}
func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()

	var t int
	fmt.Fscan(r, &t)
	for i := 0; i < t; i++ {
		var a, b, n int
		fmt.Fscan(r, &a, &b, &n)
		cnt := 1
		for a + b <= n {
			if a > b {
				b += a
			} else {
				a += b
			}
			cnt++
		}
		fmt.Fprintln(w, cnt)
	}
}

package main

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

func main() {
	var t, a, b, n int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &a, &b, &n)
		b, a := minmax(a, b)
		count := 0
		for b <= n {
			t := b
			b = a + b
			a = t
			count++
		}
		fmt.Println(count)
	}

}

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

package main

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

var nTest, a, b, n int

func count(x, y, z int) int {
	cnt := 0
	for x <= z && y <= z {
		x += y
		cnt++
		if x > z {
			break
		}
		y += x
		cnt++
	}

	return cnt
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)

	fmt.Fscan(in, &nTest)
	for t := 0; t < nTest; t++ {
		fmt.Fscan(in, &a, &b, &n)

		cnt2 := count(a, b, n)

		fmt.Fprintln(out, cnt2)
	}

	out.Flush()
}