Tired of boring office work, Denis decided to open a fast food restaurant.On the first day he made $$$a$$$ portions of dumplings, $$$b$$$ portions of cranberry juice and $$$c$$$ pancakes with condensed milk.The peculiarity of Denis's restaurant is the procedure of ordering food. For each visitor Denis himself chooses a set of dishes that this visitor will receive. When doing so, Denis is guided by the following rules:  every visitor should receive at least one dish (dumplings, cranberry juice, pancakes with condensed milk are all considered to be dishes);  each visitor should receive no more than one portion of dumplings, no more than one portion of cranberry juice and no more than one pancake with condensed milk;  all visitors should receive different sets of dishes. What is the maximum number of visitors Denis can feed?

The first line contains an integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases to solve. Each of the remaining $$$t$$$ lines contains integers $$$a$$$, $$$b$$$ and $$$c$$$ ($$$0 \leq a, b, c \leq 10$$$) — the number of portions of dumplings, the number of portions of cranberry juice and the number of condensed milk pancakes Denis made.

For each test case print a single integer — the maximum number of visitors Denis can feed.

NoteIn the first test case of the example, Denis can feed the first visitor with dumplings, give the second a portion of cranberry juice, and give the third visitor a portion of cranberry juice and a pancake with a condensed milk.In the second test case of the example, the restaurant Denis is not very promising: he can serve no customers.In the third test case of the example, Denise can serve four visitors. The first guest will receive a full lunch of dumplings, a portion of cranberry juice and a pancake with condensed milk. The second visitor will get only dumplings. The third guest will receive a pancake with condensed milk, and the fourth guest will receive a pancake and a portion of dumplings. Please note that Denis hasn't used all of the prepared products, but is unable to serve more visitors.

package main

import (
	"fmt"
	"sort"
)

func main() {
	var (
		t int
		a = make([]int, 3)
	)
	fmt.Scanf("%d\n", &t)
	for t != 0 {
		fmt.Scanf("%d %d %d\n", &a[0], &a[1], &a[2])
		sort.Sort(sort.Reverse(sort.IntSlice(a)))
		res := 0
		for i := 0; i < 3; i++ {
			if a[i] != 0 {
				res++
				a[i]--
			}
		}
		for i := 0; i < 3; i++ {
			for j := i + 1; j < 3; j++ {
				if a[i] != 0 && a[j] != 0 {
					res++
					a[i]--
					a[j]--
				}
			}
		}
		if a[0] != 0 && a[1] != 0 && a[2] != 0 {
			res++
		}
		//
		fmt.Println(res)
		t--
	}
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var t int
	fmt.Scan(&t)
	for ; t > 0; t-- {
		var a, b, c int
		fmt.Scan(&a, &b, &c)
		ans := 0
		arr := []int{a, b, c}
		sort.Sort(sort.IntSlice(arr))
		a = arr[2]
		b = arr[1]
		c = arr[0]
		if a > 0 {
			a--
			ans++
		}
		if b > 0 {
			b--
			ans++
		}
		if c > 0 {
			c--
			ans++
		}
		if a > 0 && b > 0 {
			a--
			b--
			ans++
		}
		if a > 0 && c > 0 {
			a--
			c--
			ans++
		}
		if c > 0 && b > 0 {
			c--
			b--
			ans++
		}
		if a > 0 && b > 0 && c > 0 {
			ans++
		}
		fmt.Println(ans)

	}
}

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)
	for tt := int64(0); tt < t; tt++ {
		a, b, c := 0, 0, 0
		fmt.Fscanf(in, "%d %d %d\n", &a, &b, &c)
		if a < b {
			a, b = b, a
		}
		if a < c {
			a, c = c, a
		}
		if b < c {
			b, c = c, b
		}
		ans := 0
		if a > 0 {
			a--
			ans++
		}
		if b > 0 {
			b--
			ans++
		}
		if c > 0 {
			c--
			ans++
		}
		if a > 0 && c > 0 {
			a--
			c--
			ans++
		}
		if a > 0 && b > 0 {
			a--
			b--
			ans++
		}
		if b > 0 && c > 0 {
			b--
			c--
			ans++
		}
		if a > 0 && b > 0 && c > 0 {
			a--
			b--
			c--
			ans++
		}
		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"
)

func main() {
	var n, a, b, c, cnt int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscanf(in, "%v\n", &n)
	exit := make([]int, n, n)
	for i:= 0; i<n; i++ {
		cnt = 0
		fmt.Fscanf(in, "%v %v %v\n", &a, &b, &c)
		if a < b {
			a, b = b, a
		}
		if a < c {
			b, c = c, b
		}
		if a < b {
			a, b = b, a
		}
		if a>0 {
			a--
			cnt++
		}
		if b>0 {
			b--
			cnt++
		}
		if c>0 {
			c--
			cnt++
		}
		if a>0 && b>0 {
			a--
			b--
			cnt++
		}
		if a>0 && c>0 {
			a--
			c--
			cnt++
		}
		if b>0 && c>0 {
			b--
			c--
			cnt++
		}
		if a>0 && b>0 && c>0 {
			cnt++
		}
		exit[i] = cnt

	}
	for i:=0; i<n; i++ {
		fmt.Println(exit[i])
	}

}

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

func main() {
	inf := os.Stdin
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	// inf, 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, 1010)

	T := NextInt()
	a := make([][3]int, 0)
	for i := 0; i <= 1; i++ {
		for j := 0; j <= 1; j++ {
			for k := 0; k <= 1; k++ {
				if i+j+k > 0 {
					a = append(a, [3]int{i, j, k})
				}
			}
		}
	}
	dp := make([][][][]int, 11)
	for i := 0; i <= 10; i++ {
		dp[i] = make([][][]int, 11)
		for j := 0; j <= 10; j++ {
			dp[i][j] = make([][]int, 11)
			for k := 0; k <= 10; k++ {
				dp[i][j][k] = make([]int, len(a))
				for l := 0; l < len(a); l++ {
					res := 0
					if l > 0 {
						res = dp[i][j][k][l-1]
					}

					if a[l][0] <= i && a[l][1] <= j && a[l][2] <= k {
						prev := 1
						if l > 0 {
							prev = dp[i-a[l][0]][j-a[l][1]][k-a[l][2]][l-1] + 1
						}
						if res == -1 || prev > res {
							res = prev
						}
					}
					dp[i][j][k][l] = res
				}
			}
		}
	}

	for t := 0; t < T; t++ {
		aa := NextInt()
		bb := NextInt()
		cc := NextInt()
		fmt.Fprintln(out, dp[aa][bb][cc][len(a)-1])
	}
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var cases int
	fmt.Scanf("%d\n", &cases)
	for i := 0; i < cases; i++ {
		var a, b, c int
		result := 0
		fmt.Scanf("%d %d %d\n", &a, &b, &c)
		arr := []int{a,b,c}
		sort.Ints(arr)
		if arr[2] > 0 {
			arr[2]--
			result++
		}
		if arr[1] > 0 {
			arr[1]--
			result++
		}
		if arr[0] > 0 {
			arr[0]--
			result++
		}
		if arr[2]>0&&arr[1]>0 {
			arr[2]--
			arr[1]--
			result++
		}
		if arr[2]>0 && arr[0]>0 {
			arr[2]--
			arr[0]--
			result++
		}
		if arr[1]>0&&arr[0]>0 {
			arr[1]--
			arr[0]--
			result++
		}
		if arr[2]>0&&arr[1]>0&&arr[0]>0 {
			arr[2]--
			arr[1]--
			arr[0]--
			result++
		}
		fmt.Println(result)
	}
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var testCases int
	fmt.Scanf("%d\n", &testCases)
	d, c, p := 0, 0, 0
	for i:=0; i<testCases; i++ {
		fmt.Scanf("%d %d %d\n", &d, &c, &p)
		res :=solution(d,c,p)
		fmt.Println(res)
	}
}

func solution(d int,c int, p int) int  {
	var res = 0
	dishes := []int{d,c,p}
	sort.Ints(dishes)

	d,c,p = dishes[2],dishes[1],dishes[0]

	if d ==0 && c==0 && p==0 {
		return res
	}
	if d > 0 {
		res +=1
		d -=1
	}
	if c > 0 {
		res +=1
		c -=1
	}
	if p > 0 {
		res +=1
		p -=1
	}

	if d > 0 && c > 0 {
		res +=1
		d -=1
		c -=1
	}
	if d > 0 && p > 0 {
		res +=1
		d -=1
		p -=1
	}

	if c > 0 && p > 0 {
		res +=1
		c -=1
		p -=1
	}

	if d > 0 && c > 0 && p > 0 {
		res +=1
		c -=1
		p -=1
	}

	return res

}

package main

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

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

	solve := func() (ans int) {
		a := make([]int, 3)
		for i := range a {
			Fscan(in, &a[i])
		}
		sort.Ints(a)
		if a[2] == 0 {
			return
		}
		if a[1] == 0 {
			return 1
		}
		if a[0] == 0 {
			if a[1] == 1 {
				return 2
			}
			return 3
		}
		if a[0] == 1 {
			if a[1] == 1 {
				return 3
			}
			return 4
		}
		if a[0] == 2 {
			if a[1] == 2 {
				if a[2] == 2 {
					return 4
				}
			}
			return 5
		}
		if a[0] == 3 {
			return 6
		}

		return 7
	}

	var t int
	Fscan(in, &t)
	for _case := 1; _case <= t; _case++ {
		Fprintln(out, solve())
	}
}

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

package main

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

const (
	MX_INT int64 = math.MaxInt64
	MN_INT int64 = math.MinInt64
)

func main() {
    for t := ScanInt(); t > 0; t-- {
        a, b, c := ScanInt(), ScanInt(), ScanInt()
		arr := []int{a, b, c}
		sort.Ints(arr)
		a = arr[2]
		b = arr[1]
		c = arr[0]

        count := 0

        if a > 0 {
			count++
			a--
		}
		if b > 0 {
			count++
			b--
		}
		if c > 0 {
			count++
			c--
		}

		if a > 0 && b > 0 {
			count++
			a--
			b--
		}

		if a > 0 && c > 0 {
			count++
			a--
			c--
		}
		if b>0 && c > 0 {
			count++
			b--
			c--
		}

		if a > 0 && b > 0 && c > 0 {
			count++
		}

        P(count)
    }

	close(O)
	oWg.Wait()
}

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

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

func init() {
	P = fmt.Println

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

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

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

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

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

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

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

package main

import (
	"fmt"
	"sort"
)

func main() {
	var t int
	fmt.Scan(&t)
	for tc := 0; tc < t; tc++ {
		var a, b, c int
		fmt.Scan(&a, &b, &c)
		rls := solve(a, b, c)
		fmt.Println(rls)
	}
}

func solve(a, b, c int) int {
	arr := []int{a, b, c}
	sort.Sort(sort.Reverse(sort.IntSlice(arr)))
	rls := 0
	for i := 0; i < 3; i++ {
		if arr[i] > 0 {
			rls += 1
			arr[i] -= 1
		}
	}
	for i := 0; i < 3; i++ {
		for j := i + 1; j < 3; j++ {
			if arr[i] > 0 && arr[j] > 0 {
				rls += 1
				arr[i] -= 1
				arr[j] -= 1
			}
		}
	}
	if arr[0] > 0 && arr[1] > 0 && arr[2] > 0 {
		rls += 1
	}
	return rls
}

//многогранники
package main

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

func MySort (str []string) (out []int){
	out = make([]int, 0, 3)
	var new int
	for _, j := range str{
		new, _ = strconv.Atoi(j)
		if new > 4{
			new = 4
		}
		out = append(out, new)
	}
	sort.Ints(out)
	return
}
func MyCounter(in []int)(out [3]int){
	if in[0] == in[1] && in[1] == 0{
		if in[2] == 0{
			out = [3]int{0, 0, 0}
		}else{
			out = [3]int{0, 0, 1}
		}
	}else if in[0] == 0{
		if in[1] > 1{
			out = [3]int{1, 1, 1}
		}else {
			out = [3]int{0, 1, 1}
		}
	}else if in[0] == 1{
		if in[1] == 1{
			out = [3]int{1, 1, 1}
		}else {
			out = [3]int{1, 2, 2}
		}
	}else if in[0] == 2{
		if in[2] >= 3{
			out = [3]int{2, 2, 3}
		} else{
			out = [3]int{1, 2, 2}
		}
	}else if in[0] == 3{
		out = [3]int{3, 3, 3}
	} else{
		out = [3]int{4, 4, 4}
	}
	return
 }
func main() {
	var dict map [[3]int]int = map[[3]int]int{
		{0, 0, 0}: 0,
		{0, 0, 1}: 1,
		{0, 1, 1}: 2,
		{1, 1, 1}: 3,
		{1, 2, 2}: 4,
		{2, 2, 3}: 5,
		{3, 3, 3}: 6,
		{4, 4, 4}: 7,
	}
	var line string
	var q int
	fmt.Scanln(&q)
	answer := make([]int, q, q)
	reader := bufio.NewReader(os.Stdin)
	for i := 0; i < q; i++ {
		line, _ = reader.ReadString('\n')
		line = strings.TrimSpace(line)
		answer[i] = dict[MyCounter(MySort(strings.Split(line, " ")))]

	}
	for i:= 0; i < q; i++{
		fmt.Println(answer[i])
	}
}

/*
4
Icosahedron
Cube
Tetrahedron
Dodecahedron
42
*/

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

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 [3]int
		fmt.Fscan(r, &a[0], &a[1], &a[2])
		if a[1] > a[0] {
			a[0], a[1] = a[1], a[0]
		}
		if a[2] > a[1] {
			a[2], a[1] = a[1], a[2]
		}
		if a[1] > a[0] {
			a[0], a[1] = a[1], a[0]
		}
		cnt := 0
		for j := 1; j <= 7; j++ {
			flag := true
			for x := 0; x < 3; x++ {
				if j&(1<<x) > 0 {
					if a[x] == 0 {
						flag = false
						break
					}
				}
			}
			if flag {
				for x := 0; x < 3; x++ {
					if j&(1<<x) > 0 {
						a[x]--
					}
				}
				cnt++
			}

		}
		fmt.Fprintln(w, cnt)

	}
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var (
		t int
		a = make([]int, 3)
	)
	fmt.Scanf("%d\n", &t)
	for t != 0 {
		fmt.Scanf("%d %d %d\n", &a[0], &a[1], &a[2])
		sort.Sort(sort.Reverse(sort.IntSlice(a)))
		res := 0
		for i := 0; i < 3; i++ {
			if a[i] != 0 {
				res++
				a[i]--
			}
		}
		for i := 0; i < 3; i++ {
			for j := i + 1; j < 3; j++ {
				if a[i] != 0 && a[j] != 0 {
					res++
					a[i]--
					a[j]--
				}
			}
		}
		if a[0] != 0 && a[1] != 0 && a[2] != 0 {
			res++
		}
		//
		println(res)
		t--
	}
}

package main

import "fmt"

func main() {
	var t int
	fmt.Scan(&t)
	for ; t > 0; t-- {
		var a, b, c int
		fmt.Scan(&a, &b, &c)
		ans := 0
		if a > 0 {
			a--
			ans++
		}
		if b > 0 {
			b--
			ans++
		}
		if c > 0 {
			c--
			ans++
		}
		if a > 0 && b > 0 {
			a--
			b--
			ans++
		}
		if c > 0 && b > 0 {
			c--
			b--
			ans++
		}
		if a > 0 && c > 0 {
			a--
			c--
			ans++
		}
		if a > 0 && b > 0 && c > 0 {
			ans++
		}
		fmt.Println(ans)

	}
}

package main

import "fmt"

func main() {
	var testCases int
	fmt.Scanf("%d\n", &testCases)
	d, c, p := 0, 0, 0
	for i:=0; i<testCases; i++ {
		fmt.Scanf("%d %d %d\n", &d, &c, &p)
		res :=solution(d,c,p)
		fmt.Println(res)
	}
}

func solution(d int,c int, p int) int  {
	var res = 0
	if d ==0 && c==0 && p==0 {
		return res
	}
	if d > 0 {
		res +=1
		d -=1
	}
	if c > 0 {
		res +=1
		c -=1
	}
	if p > 0 {
		res +=1
		p -=1
	}

	if d > 0 && c > 0 {
		res +=1
		d -=1
		c -=1
	}
	if d > 0 && p > 0 {
		res +=1
		d -=1
		p -=1
	}

	if c > 0 && p > 0 {
		res +=1
		c -=1
		p -=1
	}

	if d > 0 && c > 0 && p > 0 {
		res +=1
		c -=1
		p -=1
	}

	return res

}

//многогранники
package main

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

func MySort (str []string) (out []int){
	out = make([]int, 0, 3)
	var new int
	for _, j := range str{
		new, _ = strconv.Atoi(j)
		if new > 4{
			new = 4
		}
		out = append(out, new)
	}
	sort.Ints(out)
	return
}
func MyCounter(in []int)(out [3]int){
	if in[0] == in[1] && in[1] == 0{
		if in[2] == 0{
			out = [3]int{0, 0, 0}
		}else{
			out = [3]int{0, 0, 1}
		}
	}else if in[0] == 0{
		if in[1] > 1{
			out = [3]int{1, 1, 1}
		}else {
			out = [3]int{0, 1, 1}
		}
	}else if in[0] == 1{
		if in[1] == 1{
			out = [3]int{1, 1, 1}
		}else {
			out = [3]int{1, 2, 2}
		}
	}else if in[0] == 2{
		if in[2] == 3{
			out = [3]int{2, 2, 3}
		} else{
			out = [3]int{1, 2, 2}
		}
	}else if in[2] == 3{
		out = [3]int{3, 3, 3}
	} else{
		out = [3]int{4, 4, 4}
	}
	return
 }
func main() {
	var dict map [[3]int]int = map[[3]int]int{
		{0, 0, 0}: 0,
		{0, 0, 1}: 1,
		{0, 1, 1}: 2,
		{1, 1, 1}: 3,
		{1, 2, 2}: 4,
		{2, 2, 3}: 5,
		{3, 3, 3}: 6,
		{4, 4, 4}: 7,
	}
	var line string
	var q int
	fmt.Scanln(&q)
	answer := make([]int, q, q)
	reader := bufio.NewReader(os.Stdin)
	for i := 0; i < q; i++ {
		line, _ = reader.ReadString('\n')
		line = strings.TrimSpace(line)
		answer[i] = dict[MyCounter(MySort(strings.Split(line, " ")))]

	}
	for i:= 0; i < q; i++{
		fmt.Println(answer[i])
	}
}

/*
4
Icosahedron
Cube
Tetrahedron
Dodecahedron
42
*/

//многогранники
package main

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

func MySort (str []string) (out []int){
	out = make([]int, 0, 3)
	var new int
	for _, j := range str{
		new, _ = strconv.Atoi(j)
		if new > 4{
			new = 4
		}
		out = append(out, new)
	}
	sort.Ints(out)
	return
}
func MyCounter(in []int)(out [3]int){
	if in[0] == in[1] && in[1] == 0{
		if in[2] == 0{
			out = [3]int{0, 0, 0}
		}else{
			out = [3]int{0, 0, 1}
		}
	}else if in[0] == 0{
		if in[1] > 1{
			out = [3]int{1, 1, 1}
		}else {
			out = [3]int{0, 1, 1}
		}
	}else if in[0] == 1{
		if in[1] == 1{
			out = [3]int{1, 1, 1}
		}else {
			out = [3]int{1, 2, 2}
		}
	}else if in[0] == 2{
		if in[2] >= 3{
			out = [3]int{2, 2, 3}
		} else{
			out = [3]int{1, 2, 2}
		}
	}else if in[2] == 3{
		out = [3]int{3, 3, 3}
	} else{
		out = [3]int{4, 4, 4}
	}
	return
 }
func main() {
	var dict map [[3]int]int = map[[3]int]int{
		{0, 0, 0}: 0,
		{0, 0, 1}: 1,
		{0, 1, 1}: 2,
		{1, 1, 1}: 3,
		{1, 2, 2}: 4,
		{2, 2, 3}: 5,
		{3, 3, 3}: 6,
		{4, 4, 4}: 7,
	}
	var line string
	var q int
	fmt.Scanln(&q)
	answer := make([]int, q, q)
	reader := bufio.NewReader(os.Stdin)
	for i := 0; i < q; i++ {
		line, _ = reader.ReadString('\n')
		line = strings.TrimSpace(line)
		answer[i] = dict[MyCounter(MySort(strings.Split(line, " ")))]

	}
	for i:= 0; i < q; i++{
		fmt.Println(answer[i])
	}
}

/*
4
Icosahedron
Cube
Tetrahedron
Dodecahedron
42
*/

//многогранники
package main

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

func MySort (str []string) (out []int){
	out = make([]int, 0, 3)
	var new int
	for _, j := range str{
		new, _ = strconv.Atoi(j)
		if new > 4{
			new = 4
		}
		out = append(out, new)
	}
	sort.Ints(out)
	return
}
func MyCounter(in []int)(out [3]int){
	if in[0] == in[1] && in[1] == 0{
		out = [3]int{0, 0, in[2]}
	}else if in[0] == 0{
		if in[1] > 1{
			out = [3]int{1, 1, 1}
		}else {
			out = [3]int{0, 1, 1}
		}
	}else if in[0] == 1{
		if in[1] == 1{
			out = [3]int{1, 1, 1}
		}else {
			out = [3]int{1, 2, 2}
		}
	}else if in[0] == 2{
		if in[2] == 3{
			out = [3]int{2, 2, 3}
		} else{
			out = [3]int{1, 2, 2}
		}
	}else if in[2] == 3{
		out = [3]int{3, 3, 3}
	} else{
		out = [3]int{4, 4, 4}
	}
	return
 }
func main() {
	var dict map [[3]int]int = map[[3]int]int{
		{0, 0, 0}: 0,
		{0, 0, 1}: 1,
		{0, 1, 1}: 2,
		{1, 1, 1}: 3,
		{1, 2, 2}: 4,
		{2, 2, 3}: 5,
		{3, 3, 3}: 6,
		{4, 4, 4}: 7,
	}
	var line string
	var q int
	fmt.Scanln(&q)
	answer := make([]int, q, q)
	reader := bufio.NewReader(os.Stdin)
	for i := 0; i < q; i++ {
		line, _ = reader.ReadString('\n')
		line = strings.TrimSpace(line)
		answer[i] = dict[MyCounter(MySort(strings.Split(line, " ")))]

	}
	for i:= 0; i < q; i++{
		fmt.Println(answer[i])
	}
}

/*
4
Icosahedron
Cube
Tetrahedron
Dodecahedron
42
*/


Alyona's mother wants to present an array of n non-negative integers to Alyona. The array should be special. Alyona is a capricious girl so after she gets the array, she inspects m of its subarrays. Subarray is a set of some subsequent elements of the array. The i-th subarray is described with two integers li and ri, and its elements are a[li], a[li + 1], ..., a[ri].Alyona is going to find mex for each of the chosen subarrays. Among these m mexes the girl is going to find the smallest. She wants this minimum mex to be as large as possible. You are to find an array a of n elements so that the minimum mex among those chosen by Alyona subarrays is as large as possible.The mex of a set S is a minimum possible non-negative integer that is not in S.

The first line contains two integers n and m (1 ≤ n, m ≤ 105). The next m lines contain information about the subarrays chosen by Alyona. The i-th of these lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n), that describe the subarray a[li], a[li + 1], ..., a[ri].

In the first line print single integer — the maximum possible minimum mex. In the second line print n integers — the array a. All the elements in a should be between 0 and 109. It is guaranteed that there is an optimal answer in which all the elements in a are between 0 and 109. If there are multiple solutions, print any of them.

NoteThe first example: the mex of the subarray (1, 3) is equal to 3, the mex of the subarray (2, 5) is equal to 3, the mex of the subarray (4, 5) is equal to 2 as well, thus the minumal mex among the subarrays chosen by Alyona is equal to 2.

package main

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

func main() {
	reader := bufio.NewReaderSize(os.Stdin, 1<<13)
	writer := bufio.NewWriterSize(os.Stdout, 1<<13)
	n, m := ReadInt(reader), ReadInt(reader)
	l, r := make([]int, m), make([]int, m)
	for i := 0; i < m; i++ {
		l[i], r[i] = ReadInt(reader)-1, ReadInt(reader)-1
	}
	ans := minMex(l, r, n)
	fmt.Fprintln(writer, ans)
	for i := 0; i < n; i++ {
		fmt.Fprint(writer, i%ans, " ")
	}
	writer.Flush()
}

func minMex(l, r []int, n int) int {
	ans := n
	for i := 0; i < len(l); i++ {
		ans = min(ans, r[i]-l[i]+1)
	}
	return ans
}

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

func ReadInt(reader *bufio.Reader) int {
	return int(ReadInt64(reader))
}

func ReadInt64(reader *bufio.Reader) int64 {
	b, err := reader.ReadByte()
	for !isValid(b, err) {
		b, err = reader.ReadByte()
	}
	var res int64
	for isValid(b, err) {
		res = res*10 + int64(b-'0')
		b, err = reader.ReadByte()
	}
	return res
}

func isValid(b byte, err error) bool {
	return err == nil && '0' <= b && b <= '9'
}

package main

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

type SubArr struct {
    l, r int
}

type ByL []SubArr
func (a ByL) Len() int           { return len(a) }
func (a ByL) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a ByL) Less(i, j int) bool { return a[i].l < a[j].l }

func solve() {
    n, m := readInt(), readInt()
    sl, mn, ans := make([]SubArr, m), n, make([]int, n)
    for i := range sl {
        sl[i] = SubArr{readInt()-1, readInt()-1}
        if dist := sl[i].r-sl[i].l+1; dist < mn {
            mn = dist
        }
    }
    for i := range sl {
        sl[i].r = sl[i].l+mn
    }
    sort.Sort(ByL(sl))

    l, r := 0, mn
    for i := l; i < r; i++ {
        ans[i] = i
    }

    for _, pr := range sl {

        toAdd := []int{}
        for l < pr.l {
            if l < r {
                toAdd = append(toAdd, ans[l])
            }
            l++
        }
        for i, v := range toAdd {
            ans[pr.r-i-1] = v
        }
        r = pr.r
    }

    fmt.Println(mn)
    for _, v := range ans {
        fmt.Print(v, " ")
    }
    fmt.Println()
}

// Helpers

var scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)

func main() {
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// Math

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

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

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

// IO

func readString() string {
	scanner.Scan()
	return scanner.Text()
}

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

// Sort

type Ints64 []int64

func (a Ints64) Len() int           { return len(a) }
func (a Ints64) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a Ints64) Less(i, j int) bool { return a[i] < a[j] }

/* Sort tempalte
func (a ) Len() int           { return len(a) }
func (a ) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a ) Less(i, j int) bool { return  }
*/


Oleg's favorite subjects are History and Math, and his favorite branch of mathematics is division.To improve his division skills, Oleg came up with $$$t$$$ pairs of integers $$$p_i$$$ and $$$q_i$$$ and for each pair decided to find the greatest integer $$$x_i$$$, such that:   $$$p_i$$$ is divisible by $$$x_i$$$;  $$$x_i$$$ is not divisible by $$$q_i$$$.  Oleg is really good at division and managed to find all the answers quickly, how about you?

The first line contains an integer $$$t$$$ ($$$1 \le t \le 50$$$) — the number of pairs. Each of the following $$$t$$$ lines contains two integers $$$p_i$$$ and $$$q_i$$$ ($$$1 \le p_i \le 10^{18}$$$; $$$2 \le q_i \le 10^{9}$$$) — the $$$i$$$-th pair of integers.

Print $$$t$$$ integers: the $$$i$$$-th integer is the largest $$$x_i$$$ such that $$$p_i$$$ is divisible by $$$x_i$$$, but $$$x_i$$$ is not divisible by $$$q_i$$$. One can show that there is always at least one value of $$$x_i$$$ satisfying the divisibility conditions for the given constraints.

NoteFor the first pair, where $$$p_1 = 10$$$ and $$$q_1 = 4$$$, the answer is $$$x_1 = 10$$$, since it is the greatest divisor of $$$10$$$ and $$$10$$$ is not divisible by $$$4$$$.For the second pair, where $$$p_2 = 12$$$ and $$$q_2 = 6$$$, note that   $$$12$$$ is not a valid $$$x_2$$$, since $$$12$$$ is divisible by $$$q_2 = 6$$$;  $$$6$$$ is not valid $$$x_2$$$ as well: $$$6$$$ is also divisible by $$$q_2 = 6$$$.  The next available divisor of $$$p_2 = 12$$$ is $$$4$$$, which is the answer, since $$$4$$$ is not divisible by $$$6$$$.

package main

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

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

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

func solve() {
	var p, q int64
	scanf("%d %d\n", &p, &q)
	prime := make([]int64, 0)
	t := q
	for i := int64(2); i * i <= q && t > 1; i++ {
		if t % i != 0 {
			continue
		}
		for ; t % i == 0; {
			t /= i
		}
		prime = append(prime, i)
	}
	if t != 1 {
		prime = append(prime, t)
	}
	//fmt.Println("prime = ", prime)
	if p % q != 0 {
		printf("%d\n", p)
		return
	}
	r := p
	for i := 0; i < len(prime); i++ {
		for ; r % prime[i] == 0; {
			r /= prime[i]
		}
	}
	//fmt.Println(prime)
	ans := int64(1)
	for i := 0; i < len(prime); i++ {
		part := p / r
		for ; part % q == 0 && part % prime[i] == 0; {
			part /= prime[i]
		}
		if part % q != 0 {
			if part > ans {
				ans = part
			}
		}
	}
	printf("%d\n", r * ans)
}

func main() {
	defer writer.Flush()
	var t int
	scanf("%d\n", &t)
	for ; t > 0; t-- {
		solve()
	}
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1444A(in io.Reader, out io.Writer) {
	var T, p, q int64
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &p, &q)
		if p%q > 0 {
			Fprintln(out, p)
			continue
		}
		ans := int64(0)
		for i := int64(2); i*i <= q; i++ {
			e := 0
			for ; q%i == 0; q /= i {
				e++
			}
			if e > 0 {
				c := 0
				for v := p; v%i == 0; v /= i {
					c++
				}
				v := p
				for ; c >= e; c-- {
					v /= i
				}
				ans = max(ans, v)
			}
		}
		if q > 1 {
			for ; p%q == 0; p /= q {
			}
			ans = max(ans, p)
		}
		Fprintln(out, ans)
	}
}

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

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

package main

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

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

	in := bufio.NewReader(os.Stdin)

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

	for i := 0; i < t; i++ {
		var p0, q uint64
		fmt.Fscan(in, &p0, &q)

		if p0%q != 0 {
			fmt.Fprintf(out, "%d\n", p0)
			continue
		}

		f := factorization(q)

		max := uint64(1)

		for k := range f {
			p := p0

			for ; p%k == 0; {
				x := p / k
				if x%q != 0 {
					if x > max {
						max = x
					}
					break
				}
				p /= k
			}
		}

		fmt.Fprintf(out, "%d\n", max)
	}
}

func factorization(a uint64) map[uint64]int {
	res := map[uint64]int{}
	b := uint64(0)
	for ; a > 1 && b != 1; {
		b := findD(a)
		if b > 1 {
			res[b]++
			a /= b
		}
	}

	return res
}

func findD(m uint64) uint64 {
	max := uint64(math.Sqrt(float64(m))) + 1

	for i := uint64(2); i < max; i++ {
		if m%i == 0 {
			return i
		}
	}

	return m
}

package main

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

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

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

func solve() {
	var p, q int64
	scanf("%d %d\n", &p, &q)
	prime := make([]int64, 0)
	t := q
	for i := int64(2); i * i <= q && t > 1; i++ {
		if t % i != 0 {
			continue
		}
		for ; t % i == 0; {
			t /= i
		}
		prime = append(prime, i)
	}
	if t != 1 {
		prime = append(prime, t)
	}
	//fmt.Println("prime = ", prime)
	if p % q != 0 {
		printf("%d\n", p)
		return
	}
	list := make([]int64, len(prime))
	for i := 0; i < len(prime); i++ {
		list[i] = prime[i]
	}
	//fmt.Println("list = ", list)
	for head := 0; head < len(list); head++ {
		/*sort.Slice(list, func(i, j int) bool {
			return list[i] < list[j]
		})*/
		//fmt.Println("el =", list[head])
		if p % list[head] == 0 && (p / list[head]) % q != 0 {
			printf("%d\n", p / list[head])
			return
		}
		for j := head; j < len(list); j++ {
			if p % (list[head] * list[j]) == 0 {
				list = append(list, list[head] * list[j])
			}
		}
	}
}

func main() {
	defer writer.Flush()
	var t int
	scanf("%d\n", &t)
	for ; t > 0; t-- {
		solve()
	}
}

package main

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

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

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

func solve() {
	var p, q int64
	scanf("%d %d\n", &p, &q)
	less := make([]int64, 0)
	more := make([]int64, 0)
	for i := int64(1); i * i <= q; i++ {
		if q % i != 0 {
			continue
		}
		less = append(less, i)
		if i * i != q {
			more = append(more, q / i)
		}
	}
	var ans int64
	ans = 1
	for _, v := range less {
		if (p / v) % q != 0 {
			ans = p / v
			printf("%d\n", ans)
			return
		}
	}
	for i := 0; i < len(more) / 2; i++ {
		more[i], more[len(more) - i - 1] = more[len(more) - i - 1], more[i]
	}
	//fmt.Println(more)
	for _, v := range(more) {
		if (p / v) % q != 0 {
			ans = p / v
			printf("%d\n", ans)
			return
		}
	}
	printf("%d\n", ans)
}

func main() {
	defer writer.Flush()
	var t int
	scanf("%d\n", &t)
	for ; t > 0; t-- {
		solve()
	}
}

package main

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

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

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

func solve() {
	var p, q int64
	scanf("%d %d\n", &p, &q)
	prime := make([]int64, 0)
	t := q
	for i := int64(2); i * i <= q && t > 1; i++ {
		if t % i != 0 {
			continue
		}
		for ; t % i == 0; {
			t /= i
		}
		prime = append(prime, i)
	}
	if t != 1 {
		prime = append(prime, t)
	}
	//fmt.Println("prime = ", prime)
	if p % q != 0 {
		printf("%d\n", p)
		return
	}
	r := p
	for i := 0; i < len(prime); i++ {
		for ; r % prime[i] == 0; {
			r /= prime[i]
		}
	}
	//fmt.Println(prime)
	ans := r
	for i := 0; i < len(prime); i++ {
		part := p / r
		for ; part % q == 0 && part % prime[i] == 0; {
			part /= prime[i]
		}
		if part % q != 0 {
			if part > ans {
				ans = part
			}
		}
	}
	printf("%d\n", r * ans)
}

func main() {
	defer writer.Flush()
	var t int
	scanf("%d\n", &t)
	for ; t > 0; t-- {
		solve()
	}
}

package main

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

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

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

func solve() {
	var p, q int64
	scanf("%d %d\n", &p, &q)
	prime := make([]int64, 0)
	t := q
	for i := int64(2); i * i <= q && t > 1; i++ {
		if t % i != 0 {
			continue
		}
		for ; t % i == 0; {
			t /= i
		}
		prime = append(prime, i)
	}
	if t != 1 {
		prime = append(prime, t)
	}
	//fmt.Println("prime = ", prime)
	if p % q != 0 {
		printf("%d\n", p)
		return
	}
	r := p
	deg := make([]int, len(prime))
	for i := 0; i < len(prime); i++ {
		for ; r % prime[i] == 0; {
			r /= prime[i]
			deg[i]++
		}
	}
	ans := r
	for i := 0; i < len(prime); i++ {
		part := p / r
		for ; part % q == 0 && part % prime[i] == 0; {
			part /= prime[i]
		}
		if part % q != 0 {
			if part > ans {
				ans = part
			}
		}
	}
	printf("%d\n", ans)
}

func main() {
	defer writer.Flush()
	var t int
	scanf("%d\n", &t)
	for ; t > 0; t-- {
		solve()
	}
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1444A(in io.Reader, out io.Writer) {
	var T, p, q int64
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &p, &q)
		if p%q > 0 {
			Fprintln(out, p)
			continue
		}
		ans := int64(0)
		for i := int64(2); i*i <= q; i++ {
			e := 0
			for ; q%i == 0; q /= i {
				e++
			}
			if e > 0 {
				c := 0
				for v := p; v%i == 0; v /= i {
					c++
				}
				v := p
				for ; c >= e; c-- {
					v /= i
				}
				if v > ans {
					ans = v
				}
			}
		}
		if q > 1 && p/q > ans {
			ans = p / q
		}
		Fprintln(out, ans)
	}
}

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

package main

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

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

	in := bufio.NewReader(os.Stdin)

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

	for i := 0; i < t; i++ {
		var p0, q uint64
		fmt.Fscan(in, &p0, &q)

		if p0%q != 0 {
			fmt.Fprintf(out, "%d\n", p0)
			continue
		}

		f := factorization(q)
		fi := make([]int, len(f))
		fv := make([]uint64, 0, len(f))

		for k := range f {
			fv = append(fv, k)
		}

		max := uint64(0)

	L:
		for {
			fi[0]++

			for i := 0; i < len(fv); i++ {
				if fi[i] > f[fv[i]] {
					if i == len(fv)-1 {
						break L
					}
					fi[i+1]++
					fi[i] = 0
				} else {
					break
				}
			}

			p := p0
			r := []uint64{}
			for j := 0; j < len(fi); j++ {
				if fi[j] > 0 {
					for w := 0; w < fi[j]; w++ {
						if p%fv[j] != 0 {
							continue L
						}
						p /= fv[j]
						r = append(r, fv[j])
					}
				}
			}

			if p > max && p%q != 0 {
				max = p
			}
		}
		fmt.Fprintf(out, "%d\n", max)
	}
}

func factorization(a uint64) map[uint64]int {
	res := map[uint64]int{}
	b := uint64(0)
	for ; a > 1 && b != 1; {
		b := findD(a)
		if b > 1 {
			res[b]++
			a /= b
		}
	}

	return res
}

func findD(m uint64) uint64 {
	max := uint64(math.Sqrt(float64(m))) + 1

	for i := uint64(2); i < max; i++ {
		if m%i == 0 {
			return i
		}
	}

	return m
}

package main

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

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

	in := bufio.NewReader(os.Stdin)

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

LOOP:
	for i := 0; i < t; i++ {
		var p, q uint64
		fmt.Fscan(in, &p, &q)

		for j := uint64(1); j <= p; {
			x := p / j

			if p%j == 0 && j%q != 0 {
				fmt.Fprintf(out, "%d\n", j)
				continue LOOP
			}

			j = uint64(math.Ceil(float64(p) / float64(x-1)))
		}
	}
}

package main

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

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

	in := bufio.NewReader(os.Stdin)

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

LOOP:
	for i := 0; i < t; i++ {
		var p, q uint64
		fmt.Fscan(in, &p, &q)

		for j := uint64(1); j <= p; {
			x := p / j

			if p%j == 0 && j%q != 0 {
				fmt.Fprintf(out, "%d\n", x)
				continue LOOP
			}

			j = uint64(math.Ceil(float64(p) / float64(x-1)))
		}
	}
}

package main

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

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

	in := bufio.NewReader(os.Stdin)

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

	for i := 0; i < t; i++ {
		var p0, q uint64
		fmt.Fscan(in, &p0, &q)

		if p0%q != 0 {
			fmt.Fprintf(out, "%d\n", p0)
			continue
		}

		f := factorization(q)
		fi := make([]int, len(f))
		fv := make([]uint64, 0, len(f))

		for k := range f {
			fv = append(fv, k)
		}

		max := uint64(1)

	L:
		for {
			fi[0]++

			for i := 0; i < len(fv); i++ {
				if fi[i] > f[fv[i]] {
					if i == len(fv)-1 {
						break L
					}
					fi[i+1]++
					fi[i] = 0
				} else {
					break
				}
			}

			p := p0
			r := []uint64{}
			for j := 0; j < len(fi); j++ {
				if fi[j] > 0 {
					for w := 0; w < fi[j]; w++ {
						if p%fv[j] != 0 {
							continue L
						}
						p /= fv[j]
						r = append(r, fv[j])
					}
				}
			}

			if p > max && p%q != 0 {
				max = p
			}
		}
		fmt.Fprintf(out, "%d\n", max)
	}
}

func factorization(a uint64) map[uint64]int {
	res := map[uint64]int{}
	b := uint64(0)
	for ; a > 1 && b != 1; {
		b := findD(a)
		if b > 1 {
			res[b]++
			a /= b
		}
	}

	return res
}

func findD(m uint64) uint64 {
	max := uint64(math.Sqrt(float64(m))) + 1

	for i := uint64(2); i < max; i++ {
		if m%i == 0 {
			return i
		}
	}

	return m
}


You are given a garland consisting of $$$n$$$ lamps. States of the lamps are represented by the string $$$s$$$ of length $$$n$$$. The $$$i$$$-th character of the string $$$s_i$$$ equals '0' if the $$$i$$$-th lamp is turned off or '1' if the $$$i$$$-th lamp is turned on. You are also given a positive integer $$$k$$$.In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa).The garland is called $$$k$$$-periodic if the distance between each pair of adjacent turned on lamps is exactly $$$k$$$. Consider the case $$$k=3$$$. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa.Your task is to find the minimum number of moves you need to make to obtain $$$k$$$-periodic garland from the given one.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 25~ 000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10^6; 1 \le k \le n$$$) — the length of $$$s$$$ and the required period. The second line of the test case contains the string $$$s$$$ consisting of $$$n$$$ characters '0' and '1'. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$ ($$$\sum n \le 10^6$$$).

For each test case, print the answer — the minimum number of moves you need to make to obtain $$$k$$$-periodic garland from the given one.

package main

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

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

func CF1353E(_r io.Reader, _w io.Writer) {
    in := bufio.NewReader(_r)
    out := bufio.NewWriter(_w)
    defer out.Flush()
    var t, n, k int
    var s []byte
    for Fscan(in, &t); t > 0; t-- {
        Fscan(in, &n, &k, &s)
        dp := make([]int, 0, n+5)
        dp = dp[:cap(dp)]
        pre := make([]int, 0, n+5)
        pre = pre[:cap(pre)]
        for i:=1; i<=n; i++ {
            pre[i] = pre[i-1]
            if s[i-1] == '1' {
                pre[i]++
            }
        }
        ans := pre[n]
        for i:=n; i>=1; i-- {
            ct := 0
            if pre[i] == pre[i-1] {
                ct++
            }
            mn := 2000000000
            mn = min(mn, pre[n]-pre[i])
            if i+k<=n {
                mn = min(mn, dp[i+k]+pre[i+k-1]-pre[i])
            }
            ct += mn
            ans = min(ans, ct+pre[i-1])
            dp[i] = ct
        }
        Fprintln(out, ans)
    }
}
func main() {
    CF1353E(os.Stdin, os.Stdout)
}

package main

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

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

	var t, n, k int
	var s []byte
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &k, &s)
		ones := 0
		for i := range s {
			s[i] -= '0'
			ones += int(s[i])
		}
		if ones <= 1 {
			Fprintln(out, 0)
			continue
		}
		ans := n
		for st := 0; st < k; st++ {
			for i := st; i < n; i += k {
				if s[i] > 0 {
					cnt := [2]int{}
					for ; i < n; i += k {
						cnt[s[i]]++
						if cnt[0] > cnt[1] {
							break
						}
						if cost := cnt[0] + ones - cnt[1]; cost < ans {
							ans = cost
						}
					}
				}
			}
		}
		Fprintln(out, ans)
	}
}

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

package main

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

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

	var t, n, k int
	var s []byte
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &k, &s)
		sum1 := make([]int, n+1)
		for i := range s {
			s[i] -= '0'
			sum1[i+1] = sum1[i] + int(s[i])
		}
		all1 := sum1[n]
		if all1 <= 1 {
			Fprintln(out, 0)
			continue
		}
		ans := n
		for st := 0; st < k; st++ {
			r := n - 1 - st
			r = st + r - r%k
			for ; r >= st; r -= k {
				if s[r] > 0 {
					break
				}
			}
			if r < st {
				continue
			}
			for l := st; l <= r; l += k {
				if s[l] > 0 {
					cnt := [2]int{}
					for ; l <= r; l += k {
						cnt[s[l]]++
						if cnt[0] > cnt[1] {
							break
						}
						if cost := cnt[0] + all1 - cnt[1]; cost < ans {
							ans = cost
						}
					}
				}
			}
		}
		Fprintln(out, ans)
	}
}

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

package main

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

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

	var t, n, k int
	var s []byte
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &k, &s)
		sum1 := make([]int, n+1)
		for i := range s {
			s[i] -= '0'
			sum1[i+1] = sum1[i] + int(s[i])
		}
		all1 := sum1[n]
		if all1 <= 1 {
			Fprintln(out, 0)
			continue
		}
		ans := n
		for st := 0; st < k; st++ {
			for i := st; i < n; i += k {
				if s[i] > 0 {
					cnt := [2]int{}
					for ; i < n; i += k {
						cnt[s[i]]++
						if cnt[0] > cnt[1] {
							break
						}
						if cost := cnt[0] + all1 - cnt[1]; cost < ans {
							ans = cost
						}
					}
				}
			}
		}
		Fprintln(out, ans)
	}
}

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


Polycarp wrote on a whiteboard an array $$$p$$$ of length $$$n$$$, which is a permutation of numbers from $$$1$$$ to $$$n$$$. In other words, in $$$p$$$ each number from $$$1$$$ to $$$n$$$ occurs exactly once.He also prepared a resulting array $$$a$$$, which is initially empty (that is, it has a length of $$$0$$$).After that, he did exactly $$$n$$$ steps. Each step looked like this:  Look at the leftmost and rightmost elements of $$$p$$$, and pick the smaller of the two. If you picked the leftmost element of $$$p$$$, append it to the left of $$$a$$$; otherwise, if you picked the rightmost element of $$$p$$$, append it to the right of $$$a$$$. The picked element is erased from $$$p$$$. Note that on the last step, $$$p$$$ has a length of $$$1$$$ and its minimum element is both leftmost and rightmost. In this case, Polycarp can choose what role the minimum element plays. In other words, this element can be added to $$$a$$$ both on the left and on the right (at the discretion of Polycarp).Let's look at an example. Let $$$n=4$$$, $$$p=[3, 1, 4, 2]$$$. Initially $$$a=[]$$$. Then: During the first step, the minimum is on the right (with a value of $$$2$$$), so after this step, $$$p=[3,1,4]$$$ and $$$a=[2]$$$ (he added the value $$$2$$$ to the right).  During the second step, the minimum is on the left (with a value of $$$3$$$), so after this step, $$$p=[1,4]$$$ and $$$a=[3,2]$$$ (he added the value $$$3$$$ to the left).  During the third step, the minimum is on the left (with a value of $$$1$$$), so after this step, $$$p=[4]$$$ and $$$a=[1,3,2]$$$ (he added the value $$$1$$$ to the left).  During the fourth step, the minimum is both left and right (this value is $$$4$$$). Let's say Polycarp chose the right option. After this step, $$$p=[]$$$ and $$$a=[1,3,2,4]$$$ (he added the value $$$4$$$ to the right).Thus, a possible value of $$$a$$$ after $$$n$$$ steps could be $$$a=[1,3,2,4]$$$.You are given the final value of the resulting array $$$a$$$. Find any possible initial value for $$$p$$$ that can result the given $$$a$$$, or determine that there is no solution.

The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case consists of two lines. The first of them contains an integer $$$n$$$ ($$$1 \le n \le 2\cdot10^5$$$) — the length of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the elements of the array $$$a$$$. All elements of the $$$a$$$ array are distinct numbers. It is guaranteed that the sum of the values $$$n$$$ over all test cases in the test does not exceed $$$2\cdot10^5$$$.

Print $$$t$$$ lines, each of the lines must contain the answer to the corresponding set of input data: numbers $$$p_1, p_2, \dots, p_n$$$  — any of the possible initial values of the array $$$p$$$, which will lead to the given array $$$a$$$. All elements of $$$p$$$ are distinct integers from $$$1$$$ to $$$n$$$. Thus, if there are several solutions, print any. If there is no solution, then print -1 on the line.

NoteThe first test case in the example is clarified in the main section of the problem statement. There may be other correct answers for this test set.In the second test case, $$$n=1$$$. Thus, there is only one permutation that can be the answer: $$$p=[1]$$$. Indeed, this is the answer to this test case.In the third test case of the example, no matter what permutation you take as $$$p$$$, after applying the $$$n$$$ steps, the result will differ from $$$a=[1, 3, 5, 4, 2]$$$.

// 00
package main

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

func write(f *bufio.Writer, a ...interface{}) {
	f.Write([]byte(fmt.Sprint(a...)))
}

type Number interface {
	int64 | float64 | int
}

func min[K Number](a K, b K) K {
	if a < b {
		return a
	}
	return b
}
func max[K Number](a K, b K) K {
	if a > b {
		return a
	}
	return b
}
func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	f := bufio.NewWriter(os.Stdout)
	defer f.Flush()
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n int
		fmt.Fscan(reader, &n)
		a := make([]int, n)
		mx := 0
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a[i])
			mx = max(mx, a[i])
		}
		start := 0
		end := n - 1
		if mx == a[start] {
			start++
			for k := end; k > 0; k-- {
				write(f, a[k], " ")
			}
			write(f, a[0], "\n")

		} else if mx == a[end] {
			write(f, a[end])
			end--
			for k := end; k >= 0; k-- {
				write(f, " ", a[k])
			}
			write(f, "\n")
		} else {
			write(f, "-1\n")
		}

	}
}

package main

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

func solve(in *bufio.Reader, out *bufio.Writer){
	defer out.Flush()
	n := 0
	Fscan(in,&n)
	var arr []int
	for i := 0;i < n;i++{
		var next int
		Fscan(in,&next)
		arr = append(arr,next)
	}
	if arr[n - 1] == n{
		Fprintf(out,"%d ",n)
		for i := n - 2;i >= 0;i--{
			Fprintf(out,"%d ",arr[i])
		}
	}else if arr[0] == n{
		Fprintf(out,"%d ",n)
		for i := n - 1;i >= 1;i--{
			Fprintf(out,"%d ",arr[i])
		}
	}else{
		Fprintf(out,"%d",-1)
	}
	Fprintln(out)
}

func main(){
	stdin := os.Stdin
	stdout := os.Stdout
	in := bufio.NewReader(stdin)
	out := bufio.NewWriter(stdout)
	n := 0
	Fscan(in,&n)
	for i := 0;i < n;i++{
		solve(in,out)
	}
}

package main

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

func solve(in *bufio.Reader, out *bufio.Writer){
	defer out.Flush()
	n := 0
	Fscan(in,&n)
	var arr []int
	for i := 0;i < n;i++{
		var next int
		Fscan(in,&next)
		arr = append(arr,next)
	}
	if arr[n - 1] == n{
		Printf("%d ",n)
		for i := n - 2;i >= 0;i--{
			Printf("%d ",arr[i])
		}
	}else if arr[0] == n{
		Printf("%d ",n)
		for i := n - 1;i >= 1;i--{
			Printf("%d ",arr[i])
		}
	}else{
		Printf("%d",-1)
	}
	Println()
}

func main(){
	stdin := os.Stdin
	stdout := os.Stdout
	in := bufio.NewReader(stdin)
	out := bufio.NewWriter(stdout)
	n := 0
	Fscan(in,&n)
	for i := 0;i < n;i++{
		solve(in,out)
	}
}

package main

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

var sc = bufio.NewScanner(os.Stdin)

func scan() string {
	for sc.Scan() {
		break
	}
	return sc.Text()
}

func scanInt() int {
	i, err := strconv.Atoi(scan())
	if err != nil {
		panic(err)
	}
	return i
}

func scanInt64() int64 {
	i, err := strconv.ParseInt(scan(), 10, 64)
	if err != nil {
		panic(err)
	}
	return i
}

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

func isEven(c byte) bool {
	return c == '2' || c == '4' || c == '6' || c == '8'
}

func main() {
	sc.Split(bufio.ScanWords)
	t := scanInt()
	for i := 0; i < t; i++ {
		l := scanInt()
		var tmp = make([]int, l)
		for j := range tmp {
			tmp[j] = scanInt()
		}
		if tmp[0] == l {
			fmt.Print(l)
			for k := len(tmp)-1; k > 0; k-- {
				fmt.Print(" ", tmp[k])
			}
			fmt.Println()
		} else if tmp[len(tmp)-1] == l {
			for k := len(tmp)-2; k >= 0; k-- {
				fmt.Print(tmp[k], " ")
			}
			fmt.Println(l)
		} else {
			fmt.Println(-1)
		}
	}
}

package main

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

type DequeItem = int

type Deque struct {
	first    int
	size     int
	capacity int
	values   *[]DequeItem
}

func NewDeque(capacity int) Deque {
	var values = make([]DequeItem, capacity)
	return Deque{
		first:    0,
		size:     0,
		capacity: capacity,
		values:   &values,
	}
}

func (d *Deque) Grow() {
	d.SetCapacity((d.capacity + 1) * 2)
}

func (d *Deque) SetCapacity(newCapacity int) {
	var newValues = make([]DequeItem, newCapacity)
	for i := 0; i < d.size; i += 1 {
		newValues[i] = d.Get(i)
	}
	d.first = 0
	d.capacity = newCapacity
	d.values = &newValues
}

func (d *Deque) Get(i int) DequeItem {
	return (*d.values)[(d.first+i)%d.capacity]
}

func (d *Deque) Left() DequeItem {
	return (*d.values)[d.first]
}

func (d *Deque) Right() DequeItem {
	return d.Get(d.size - 1)
}

func (d *Deque) PushRight(x DequeItem) {
	if d.size == d.capacity {
		d.Grow()
	}
	(*d.values)[(d.first+d.size)%d.capacity] = x
	d.size++
}

func (d *Deque) PushLeft(x DequeItem) {
	if d.size == d.capacity {
		d.Grow()
	}
	d.first = (d.first + d.capacity - 1) % d.capacity
	(*d.values)[d.first] = x
	d.size++
}

func (d *Deque) PopRight() {
	d.size--
}

func (d *Deque) PopLeft() {
	d.first = (d.first + 1) % d.capacity
	d.size--
}

func (d *Deque) ToString() string {
	var s string = ""
	for i := 0; i < d.size; i += 1 {
		s += strconv.Itoa(d.Get(i)) + " "
	}
	return fmt.Sprintf("{ values: [%s] first=%d, size=%d, capacity=%d }", s, d.first, d.size, d.capacity)
}

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

func Solve(d *Deque, sol *Deque) {
	for d.size > 0 {
		var left = d.Left() < sol.Right()
		var right = d.Right() < sol.Left()
		if !left && !right {
			return
		}
		if left && right {
			left = sol.Left() < sol.Right()
		}
		if left {
			sol.PushLeft(d.Left())
			d.PopLeft()
		} else {
			sol.PushRight(d.Right())
			d.PopRight()
		}
	}
}

func main() {
	var ntc, n int
	var arr = make([]int, 2e5+100)
	fmt.Fscan(in, &ntc)
	for t := 0; t < ntc; t += 1 {
		fmt.Fscan(in, &n)
		for i := 0; i < n; i += 1 {
			fmt.Fscan(in, &arr[i])
		}

		var lastAddedIndex = 0
		if arr[n-1] > arr[0] {
			lastAddedIndex = n - 1
		}
		var d = NewDeque(n)
		var sol = NewDeque(n)

		sol.PushRight(arr[lastAddedIndex])
		for i := 0; i < n; i += 1 {
			if i != lastAddedIndex {
				d.PushRight(arr[i])
			}
		}

		Solve(&d, &sol)
		if sol.size < n {
			fmt.Fprintf(out, "-1\n")
		} else {
			for i := 0; i < n; i++ {
				fmt.Fprintf(out, "%d ", sol.Get(i))
			}
			fmt.Fprintln(out)
		}
	}
	out.Flush()
}

package main

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

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

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

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

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

		max := nums[0]
		if nums[len(nums)-1] > max {
			max = nums[len(nums)-1]
		}

		valid := true
		for _, v := range nums {
			if v > max {
				valid = false
				break
			}
		}

		if !valid {
			fmt.Fprintln(out, -1)
		} else {
			if max == nums[0] {
				for i := len(nums) - 1; i >= 1; i-- {
					fmt.Fprintf(out, "%d ", nums[i])
				}
				fmt.Fprintf(out, "%d", max)
			} else {
				fmt.Fprintf(out, "%d ", max)
				for i := len(nums) - 2; i >= 0; i-- {
					fmt.Fprintf(out, "%d ", nums[i])
				}
			}
			fmt.Fprintln(out)
		}
	}
}

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	t, n   int
)

func gcd(x, y int) int {
	if y > x {
		return gcd(y, x)
	}
	if y == 0 {
		return x
	}
	return gcd(y, x%y)
}

func solve(n int, a []int) string {
	if a[0] != n && a[n-1] != n {
		return "-1"
	}
	var x []string
	for i := n - 1; i >= 0; i-- {
		x = append(x, fmt.Sprint(a[i]))
	}
	return strings.Join(x, " ")
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a[i])
		}
		fmt.Println(solve(n, a))
	}
}

package main

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

var (
	rr = bufio.NewReader(os.Stdin)
	ww = bufio.NewWriter(os.Stdout)
)

const (
	MaxN int = 2e5
)

var (
	T int
	A [MaxN]int
)

func solve(a []int) []int {
	if a[0] != len(a) && a[len(a)-1] != len(a) {
		return []int{-1}
	}
	for l, r := 0, len(a)-1; l < r; l, r = l+1, r-1 {
		a[l], a[r] = a[r], a[l]
	}
	return a
}

func main() {
	_, _ = fmt.Fscan(rr, &T)
	for iCase := 0; iCase < T; iCase++ {
		var (
			n int
			a = A[:0]
		)

		_, _ = fmt.Fscan(rr, &n)
		for i := 0; i < n; i++ {
			var t int
			_, _ = fmt.Fscan(rr, &t)
			a = append(a, t)
		}

		for _, i := range solve(a) {
			_, _ = fmt.Fprintf(ww, "%d ", i)
		}
		_, _ = fmt.Fprintln(ww)
	}
	_ = ww.Flush()
}

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }

func min(a,b int) int{
    if a < b {
        return a
    }
    return b
}
func solve() {
    var n int
    scanf("%d\n", &n)
    ar := make([]int, 0)
    var x int
    for i:=0; i<n; i++ {
        scanf("%d", &x)
        ar = append(ar,x)
    }
    scanf("\n")

    if ar[0] == n {
        for i:=1; i+i<n;i++ {
            x := ar[i]
            ar[i] = ar[n-i]
            ar[n-i] = x
        }

        for i:=0; i<n;i++ {
            printf("%d ", ar[i])
        }
        printf("\n")
    }else if ar[n-1] == n {
        for i:=0; i+i<n-2;i++ {
            x := ar[i]
            ar[i] = ar[n-i-2]
            ar[n-i-2] = x
        }
        for i:=0; i<n;i++ {
            printf("%d ", ar[i])
        }
        printf("\n")
    }else {
        printf("-1\n")
    }

}

func main() {

    defer writer.Flush()

    var tc int
    scanf("%d\n", &tc)
    for ;tc>0;tc-- {
        solve()
    }
}

package main

import (
	"bufio"
	//	"container/heap" for heap.Init()
	"fmt"
	"os"
	"runtime"
)

func check(b bool) {
	if !b {
		_, _, line, _ := runtime.Caller(1)
		fmt.Printf("check failed line %d\n", line)
		os.Exit(-1)
	}
}

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

type IntHeap []int

func (h IntHeap) Len() int            { return len(h) }
func (h IntHeap) Less(i, j int) bool  { return h[i] < h[j] }
func (h IntHeap) Swap(i, j int)       { h[i], h[j] = h[j], h[i] }
func (h *IntHeap) Push(x interface{}) { *h = append(*h, x.(int)) }
func (h *IntHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

const (
	INIT = -1
)

var (
	n int
	a []int
)

func solve(start, l, r int) bool {
	pl := []int{a[start]}
	pr := []int{a[start]}

	for l <= r {
		if a[l] < pr[len(pr)-1] {
			pl = append(pl, a[l])
			l++
		} else if a[r] < pl[len(pl)-1] {
			pr = append(pr, a[r])
			r--
		} else {
			return false
		}
	}

	for i := len(pl) - 1; i >= 0; i-- {
		fmt.Printf("%d ", pl[i])
	}
	for i := 1; i < len(pr); i++ {
		fmt.Printf("%d ", pr[i])
	}
	fmt.Println()

	return true
}

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

	var tc int
	fmt.Fscan(r, &tc)

	for ; tc > 0; tc-- {

		fmt.Fscan(r, &n)

		a = make([]int, n)

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

		if solve(0, 1, n-1) {
			continue
		}

		if solve(n-1, 0, n-2) {
			continue
		}

		fmt.Println(INIT)

	}
}

// 00
package main

import (
	"bufio"
	"container/list"
	"fmt"
	"os"
)

func write(f *bufio.Writer, a ...interface{}) {
	f.Write([]byte(fmt.Sprint(a...)))
}

type Number interface {
	int64 | float64 | int
}

func min[K Number](a K, b K) K {
	if a < b {
		return a
	}
	return b
}
func max[K Number](a K, b K) K {
	if a > b {
		return a
	}
	return b
}
func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	f := bufio.NewWriter(os.Stdout)
	defer f.Flush()
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n int
		fmt.Fscan(reader, &n)
		a := make([]int, n)
		mx := 0
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a[i])
			mx = max(mx, a[i])
		}
		l := list.New()
		start := 0
		end := n - 1
		ok := true
		for start <= end {
			possible := false
			if a[start] > a[end] {
				if l.Len() == 0 {
					l.PushBack(a[start])
					start++
					possible = true
				} else {
					back := l.Back().Value.(int)
					if a[start] < back {
						l.PushFront(a[start])
						start++
						possible = true
					}

				}
			}
			if !possible {
				if l.Len() == 0 {
					l.PushBack(a[end])
					end--
					possible = true
				} else {
					front := l.Front().Value.(int)
					if a[end] < front {
						l.PushBack(a[end])
						end--
						possible = true
					}
				}
			}
			if !possible {
				if l.Len() == 0 {
					l.PushBack(a[start])
					start++
					possible = true
				} else {
					back := l.Back().Value.(int)
					if a[start] < back {
						l.PushFront(a[start])
						start++
						possible = true
					}

				}
			}
			if !possible {
				if start == end {

					if a[start] != mx {
						l.PushBack(a[start])
						break
					}
				}
				ok = false
				break
			}
		}
		if ok {
			for e := l.Front(); e != nil; e = e.Next() {
				write(f, e.Value.(int))
				write(f, " ")
			}
			write(f, "\n")
		} else {
			write(f, "-1\n")
		}

	}
}

// 00
package main

import (
	"bufio"
	"container/list"
	"fmt"
	"os"
)

func write(f *bufio.Writer, a ...interface{}) {
	f.Write([]byte(fmt.Sprint(a...)))
}

type Number interface {
	int64 | float64 | int
}

func min[K Number](a K, b K) K {
	if a < b {
		return a
	}
	return b
}
func max[K Number](a K, b K) K {
	if a > b {
		return a
	}
	return b
}
func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	f := bufio.NewWriter(os.Stdout)
	defer f.Flush()
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n int
		fmt.Fscan(reader, &n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a[i])
		}
		l := list.New()
		start := 0
		end := n - 1
		ok := true
		for start <= end {
			possible := false
			if a[start] > a[end] {
				if l.Len() == 0 {
					l.PushBack(a[start])
					start++
					possible = true
				} else {
					back := l.Back().Value.(int)
					if a[start] < back {
						l.PushFront(a[start])
						start++
						possible = true
					}

				}
			}
			if !possible {
				if l.Len() == 0 {
					l.PushBack(a[end])
					end--
					possible = true
				} else {
					front := l.Front().Value.(int)
					if a[end] < front {
						l.PushBack(a[end])
						end--
						possible = true
					}
				}
			}
			if !possible {
				if l.Len() == 0 {
					l.PushBack(a[start])
					start++
					possible = true
				} else {
					back := l.Back().Value.(int)
					if a[start] < back {
						l.PushFront(a[start])
						start++
						possible = true
					}

				}
			}
			if !possible {
				// if start == end {
				// 	l.PushBack(a[start])
				// 	break
				// }
				ok = false
				break
			}
		}
		if ok {
			for e := l.Front(); e != nil; e = e.Next() {
				write(f, e.Value.(int))
				write(f, " ")
			}
			write(f, "\n")
		} else {
			write(f, "-1\n")
		}

	}
}

// 00
package main

import (
	"bufio"
	"container/list"
	"fmt"
	"os"
)

func write(f *bufio.Writer, a ...interface{}) {
	f.Write([]byte(fmt.Sprint(a...)))
}

type Number interface {
	int64 | float64 | int
}

func min[K Number](a K, b K) K {
	if a < b {
		return a
	}
	return b
}
func max[K Number](a K, b K) K {
	if a > b {
		return a
	}
	return b
}
func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	f := bufio.NewWriter(os.Stdout)
	defer f.Flush()
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n int
		fmt.Fscan(reader, &n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a[i])
		}
		l := list.New()
		start := 0
		end := n - 1
		ok := true
		for start <= end {
			possible := false
			if a[start] > a[end] {
				if l.Len() == 0 {
					l.PushBack(a[start])
					start++
					possible = true
				} else {
					back := l.Back().Value.(int)
					if a[start] < back {
						l.PushFront(a[start])
						start++
						possible = true
					}

				}
			}
			if !possible {
				if l.Len() == 0 {
					l.PushBack(a[end])
					end--
					possible = true
				} else {
					front := l.Front().Value.(int)
					if a[end] < front {
						l.PushBack(a[end])
						end--
						possible = true
					}
				}
			}
			if !possible {
				if l.Len() == 0 {
					l.PushBack(a[start])
					start++
					possible = true
				} else {
					back := l.Back().Value.(int)
					if a[start] < back {
						l.PushFront(a[start])
						start++
						possible = true
					}

				}
			}
			if !possible {
				if start == end {
					l.PushBack(a[start])
					break
				}
				ok = false
				break
			}
		}
		if ok {
			for e := l.Front(); e != nil; e = e.Next() {
				write(f, e.Value.(int))
				write(f, " ")
			}
			write(f, "\n")
		} else {
			write(f, "-1\n")
		}

	}
}

// 00
package main

import (
	"bufio"
	"container/list"
	"fmt"
	"os"
)

func write(f *bufio.Writer, a ...interface{}) {
	f.Write([]byte(fmt.Sprint(a...)))
}

type Number interface {
	int64 | float64 | int
}

func min[K Number](a K, b K) K {
	if a < b {
		return a
	}
	return b
}
func max[K Number](a K, b K) K {
	if a > b {
		return a
	}
	return b
}
func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	f := bufio.NewWriter(os.Stdout)
	defer f.Flush()
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n int
		fmt.Fscan(reader, &n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a[i])
		}
		l := list.New()
		start := 0
		end := n - 1
		ok := true
		for start <= end {
			possible := false
			if a[start] > a[end] {
				if l.Len() == 0 {
					l.PushBack(a[start])
					start++
					possible = true
				} else {
					back := l.Back().Value.(int)
					if a[start] < back {
						l.PushFront(a[start])
						start++
						possible = true
					}

				}
			}
			if !possible {
				if l.Len() == 0 {
					l.PushBack(a[end])
					end--
					possible = true
				} else {
					front := l.Front().Value.(int)
					if a[end] < front {
						l.PushBack(a[end])
						end--
						possible = true
					}
				}
			}
			if !possible {
				ok = false
				break
			}
		}
		if ok {
			for e := l.Front(); e != nil; e = e.Next() {
				write(f, e.Value.(int))
				write(f, " ")
			}
			write(f, "\n")
		} else {
			write(f, "-1\n")
		}

	}
}

package main

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

var sc = bufio.NewScanner(os.Stdin)

func scan() string {
	for sc.Scan() {
		break
	}
	return sc.Text()
}

func scanInt() int {
	i, err := strconv.Atoi(scan())
	if err != nil {
		panic(err)
	}
	return i
}

func scanInt64() int64 {
	i, err := strconv.ParseInt(scan(), 10, 64)
	if err != nil {
		panic(err)
	}
	return i
}

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

func isEven(c byte) bool {
	return c == '2' || c == '4' || c == '6' || c == '8'
}

func main() {
	sc.Split(bufio.ScanWords)
	t := scanInt()
	for i := 0; i < t; i++ {
		l := scanInt()
		var tmp = make([]int, l)
		for j := range tmp {
			tmp[j] = scanInt()
		}
		if tmp[0] == l {
			fmt.Print(l)
			for k := 1; k < len(tmp); k++ {
				fmt.Print(" ", tmp[k])
			}
			fmt.Println()
		} else if tmp[len(tmp)-1] == l {
			for k := len(tmp)-2; k >= 0; k-- {
				fmt.Print(tmp[k], " ")
			}
			fmt.Println(l)
		} else {
			fmt.Println(-1)
		}
	}
}

package main

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

var sc = bufio.NewScanner(os.Stdin)

func scan() string {
	for sc.Scan() {
		break
	}
	return sc.Text()
}

func scanInt() int {
	i, err := strconv.Atoi(scan())
	if err != nil {
		panic(err)
	}
	return i
}

func scanInt64() int64 {
	i, err := strconv.ParseInt(scan(), 10, 64)
	if err != nil {
		panic(err)
	}
	return i
}

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

func isEven(c byte) bool {
	return c == '2' || c == '4' || c == '6' || c == '8'
}

func main() {
	sc.Split(bufio.ScanWords)
	t := scanInt()
	for i := 0; i < t; i++ {
		l := scanInt()
		var tmp = make([]int, l)
		for j := range tmp {
			tmp[j] = scanInt()
		}
		if tmp[0] == t {
			fmt.Print(t)
			for k := 1; k < len(tmp); k++ {
				fmt.Print(" ", tmp[k])
			}
			fmt.Println()
		} else if tmp[len(tmp)-1] == t {
			for k := len(tmp)-2; k >= 0; k-- {
				fmt.Print(tmp[k], " ")
			}
			fmt.Println(t)
		} else {
			fmt.Println(-1)
		}
	}
}

package main

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

type DequeItem = int

type Deque struct {
	first    int
	size     int
	capacity int
	values   *[]DequeItem
}

func NewDeque(capacity int) Deque {
	var values = make([]DequeItem, capacity)
	return Deque{
		first:    0,
		size:     0,
		capacity: capacity,
		values:   &values,
	}
}

func (d *Deque) Grow() {
	d.SetCapacity((d.capacity + 1) * 2)
}

func (d *Deque) SetCapacity(newCapacity int) {
	var newValues = make([]DequeItem, newCapacity)
	for i := 0; i < d.size; i += 1 {
		newValues[i] = d.Get(i)
	}
	d.first = 0
	d.capacity = newCapacity
	d.values = &newValues
}

func (d *Deque) Get(i int) DequeItem {
	return (*d.values)[(d.first+i)%d.capacity]
}

func (d *Deque) Left() DequeItem {
	return (*d.values)[d.first]
}

func (d *Deque) Right() DequeItem {
	return d.Get(d.size - 1)
}

func (d *Deque) PushRight(x DequeItem) {
	if d.size == d.capacity {
		d.Grow()
	}
	(*d.values)[(d.first+d.size)%d.capacity] = x
	d.size++
}

func (d *Deque) PushLeft(x DequeItem) {
	if d.size == d.capacity {
		d.Grow()
	}
	d.first = (d.first + d.capacity - 1) % d.capacity
	(*d.values)[d.first] = x
	d.size++
}

func (d *Deque) PopRight() {
	d.size--
}

func (d *Deque) PopLeft() {
	d.first = (d.first + 1) % d.capacity
	d.size--
}

func (d *Deque) ToString() string {
	var s string = ""
	for i := 0; i < d.size; i += 1 {
		s += strconv.Itoa(d.Get(i)) + " "
	}
	return fmt.Sprintf("{ values: [%s] first=%d, size=%d, capacity=%d }", s, d.first, d.size, d.capacity)
}

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

func Solve(d *Deque, sol *Deque) {
	for d.size > 0 {
		var left = d.Left() < sol.Right()
		var right = d.Right() < sol.Left()
		if !left && !right {
			return
		}
		if left && right {
			left = d.Left() > d.Right()
		}
		if left {
			sol.PushLeft(d.Left())
			d.PopLeft()
		} else {
			sol.PushRight(d.Right())
			d.PopRight()
		}
	}
}

func main() {
	var ntc, n int
	var arr = make([]int, 2e5+100)
	fmt.Fscan(in, &ntc)
	for t := 0; t < ntc; t += 1 {
		fmt.Fscan(in, &n)
		for i := 0; i < n; i += 1 {
			fmt.Fscan(in, &arr[i])
		}

		var lastAddedIndex = 0
		if arr[n-1] > arr[0] {
			lastAddedIndex = n - 1
		}
		var d = NewDeque(n)
		var sol = NewDeque(n)

		sol.PushRight(arr[lastAddedIndex])
		for i := 0; i < n; i += 1 {
			if i != lastAddedIndex {
				d.PushRight(arr[i])
			}
		}

		Solve(&d, &sol)
		if sol.size < n {
			fmt.Fprintf(out, "-1\n")
		} else {
			for i := 0; i < n; i++ {
				fmt.Fprintf(out, "%d ", sol.Get(i))
			}
			fmt.Fprintln(out)
		}
	}
	out.Flush()
}


You are given an array consisting of $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$. Initially $$$a_x = 1$$$, all other elements are equal to $$$0$$$.You have to perform $$$m$$$ operations. During the $$$i$$$-th operation, you choose two indices $$$c$$$ and $$$d$$$ such that $$$l_i \le c, d \le r_i$$$, and swap $$$a_c$$$ and $$$a_d$$$.Calculate the number of indices $$$k$$$ such that it is possible to choose the operations so that $$$a_k = 1$$$ in the end.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then the description of $$$t$$$ testcases follow. The first line of each test case contains three integers $$$n$$$, $$$x$$$ and $$$m$$$ ($$$1 \le n \le 10^9$$$; $$$1 \le m \le 100$$$; $$$1 \le x \le n$$$). Each of next $$$m$$$ lines contains the descriptions of the operations; the $$$i$$$-th line contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 \le l_i \le r_i \le n$$$).

For each test case print one integer — the number of indices $$$k$$$ such that it is possible to choose the operations so that $$$a_k = 1$$$ in the end.

NoteIn the first test case, it is possible to achieve $$$a_k = 1$$$ for every $$$k$$$. To do so, you may use the following operations:  swap $$$a_k$$$ and $$$a_4$$$;  swap $$$a_2$$$ and $$$a_2$$$;  swap $$$a_5$$$ and $$$a_5$$$. In the second test case, only $$$k = 1$$$ and $$$k = 2$$$ are possible answers. To achieve $$$a_1 = 1$$$, you have to swap $$$a_1$$$ and $$$a_1$$$ during the second operation. To achieve $$$a_2 = 1$$$, you have to swap $$$a_1$$$ and $$$a_2$$$ during the second operation.

package main

import "fmt"

func main() {
	var test int
	fmt.Scan(&test)
	for test > 0 {
		solve()
		test--
	}
}

func solve() {
	var n, x, m, l, r int
	fmt.Scan(&n, &x, &m)
	lo, hi := x, x
	for m > 0 {
		m--
		fmt.Scan(&l, &r)
		if r >= lo && l <= hi {
			if l < lo {
				lo = l
			}
			if r > hi {
				hi = r
			}
		}
	}
	fmt.Println(hi - lo + 1)
}

package main

import "fmt"

func main() {
	var test int
	fmt.Scanf("%d\n", &test)
	for test > 0 {
		solve()
		test--
	}
}

func solve() {
	var n, x, m, l, r int
	fmt.Scanf("%d %d %d\n", &n, &x, &m)
	lo, hi := x, x
	for m > 0 {
		m--
		fmt.Scanf("%d %d\n", &l, &r)
		if r >= lo && l <= hi {
			lo = min(lo, l)
			hi = max(hi, r)
		}
	}
	fmt.Println(hi - lo + 1)
}

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

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

package main

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

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

func main() {
	var test int
	fmt.Scan(&test)
	for test > 0 {
		solve()
		test--
	}
	out.Flush()
}

func solve() {
	var n, x, m, l, r int
	fmt.Scan(&n, &x, &m)
	lo, hi := x, x
	for m > 0 {
		m--
		fmt.Scan(&l, &r)
		if r >= lo && l <= hi {
			if l < lo {
				lo = l
			}
			if r > hi {
				hi = r
			}
		}
	}
	fmt.Fprintln(out, hi-lo+1)
}

package main

import "fmt"

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

	for test_case := 0; test_case < t; test_case++ {
		var n, x, m int
		fmt.Scan(&n, &x, &m)

		tempL := x
		tempR := x

		for i:=0; i<m; i++ {
			var l, r int
			fmt.Scan(&l, &r)
			if (r>=tempL && tempR >= l) {
				if l < tempL {
					tempL = l
				}
				if r > tempR {
					tempR = r
				}
			}
		}
		fmt.Println(tempR-tempL+1)
	}

}

package main

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

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

	tc := readNum(reader)

	for tc > 0 {
		tc--
		line := readNNums(reader, 3)
		n, x, m := line[0], line[1], line[2]
		ops := make([][]int, m)
		for i := 0; i < m; i++ {
			ops[i] = readNNums(reader, 2)
		}
		fmt.Println(solve(n, x, m, ops))
	}
}

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(n int, x int, m int, ops [][]int) int {
	l, r := x, x

	for i := 0; i < m; i++ {
		a, b := ops[i][0], ops[i][1]
		if b < l || a > r {
			continue
		}
		l = min(l, a)
		r = max(r, b)
	}

	return r - l + 1
}

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

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

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := 0
	fmt.Fscanf(in, "%d\n", &t)
	for tt := 0; tt < t; tt++ {
		n, x, m := 0, 0, 0
		fmt.Fscanf(in, "%d %d %d\n", &n, &x, &m)
		leftMost := x
		rightMost := x
		for i := 0; i < m; i++ {
			l, r := 0, 0
			fmt.Fscanf(in, "%d %d\n", &l, &r)
			if !(l > rightMost || r < leftMost) {
				if l < leftMost {
					leftMost = l
				}
				if r > rightMost {
					rightMost = r
				}
			}
		}
		fmt.Fprintf(out, "%d\n", rightMost-leftMost+1)
	}
	return
}

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

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

package main

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

// Range ... Just range
type Range struct {
	l, h uint64
}

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

	in := func() uint64 {
		sc.Scan()
		i, _ := strconv.ParseUint(sc.Text(), 10, 64)
		return i
	}

	for t := in(); t > 0; t-- {
		_, x, m := in(), in(), in()

		rng := Range{l: x, h: x}

		for i := m; i > 0; i-- {
			l, h := in(), in()

			if l <= rng.h && h >= rng.l {
				if rng.h < h {
					rng.h = h
				}

				if rng.l > l {
					rng.l = l
				}
			}
		}

		fmt.Println(rng.h - rng.l + 1)
	}
}

package main

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

func main() {
	t := nextInt()
	for i := 0; i < t; i++ {
		n, x, m := nextInt64(), nextInt64(), nextInt()
		ops := make([][]int64, m)
		for i := 0; i < m; i++ {
			ops[i] = nextInt64s(2)
		}
		fmt.Println(solve(n, x, ops))
	}
}

func solve(n, x int64, ops [][]int64) int64 {
	l, r := x, x
	for _, op := range ops {
		opl, opr := op[0], op[1]
		if (opl <= l && l <= opr) || (opl <= r && r <= opr) {
			l = Min(l, opl)
			r = Max(r, opr)
		}
	}
	return r - l + 1
}

func Max(xs ...int64) int64 {
	max := xs[0]
	for _, x := range xs[1:] {
		if max < x {
			max = x
		}
	}
	return max
}

func Min(xs ...int64) int64 {
	min := xs[0]
	for _, x := range xs[1:] {
		if min > x {
			min = x
		}
	}
	return min
}

var sc = bufio.NewScanner(os.Stdin)

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

func next() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	a, err := strconv.Atoi(next())
	if err != nil {
		panic(err)
	}
	return a
}

func nextInts(n int) []int {
	ret := make([]int, n)
	for i := 0; i < n; i++ {
		ret[i] = nextInt()
	}
	return ret
}

func nextInt64() int64 {
	a, err := strconv.ParseInt(next(), 10, 64)
	if err != nil {
		panic(err)
	}
	return a
}

func nextInt64s(n int) []int64 {
	ret := make([]int64, n)
	for i := 0; i < n; i++ {
		ret[i] = nextInt64()
	}
	return ret
}

/*
URL:
https://codeforces.com/problemset/problem/1366/B
*/

package main

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

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

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

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

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

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

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

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

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

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

var (
	t int

	n, x, m int
	L, R    []int
)

func main() {
	t = ReadInt()
	for i := 0; i < t; i++ {
		n, x, m = ReadInt3()
		L, R = []int{}, []int{}
		for j := 0; j < m; j++ {
			l, r := ReadInt2()
			L = append(L, l)
			R = append(R, r)
		}

		solve()
	}
}

func solve() {
	cl, cr := x, x
	for i := 0; i < m; i++ {
		l, r := L[i], R[i]

		if l <= cl && cl <= r || l <= cr && cr <= r {
			cl, cr = Min(cl, l), Max(cr, r)
		}
	}

	fmt.Println(cr - cl + 1)
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

	return string(res)
}

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

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

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

	return string(res)
}

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

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

package main

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

var nTest, n, x, m int

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	fmt.Fscan(in, &nTest)

	for i := 0; i < nTest; i++ {
		fmt.Fscan(in, &n, &x, &m)

		L, R := x, x
		var l, r int
		for j := 0; j < m; j++ {
			fmt.Fscan(in, &l, &r)

			if (l <= R && R <= r) || (l <= L && L <= r) {
				if l < L {
					L = l
				}

				if r > R {
					R = r
				}
			}
		}

		fmt.Fprintln(out, R-L+1)
	}
	out.Flush()
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1366B(_r io.Reader, out io.Writer) {
	type pair struct{ l, r int }
	in := bufio.NewReader(_r)
	var t, n, x, m, l, r int
	for Fscan(in, &t); t > 0; t-- {
		var a, es []pair
		for Fscan(in, &n, &x, &m); m > 0; m-- {
			Fscan(in, &l, &r)
			if l <= x && x <= r {
				a = append(a, pair{l, r})
				es = append(es, pair{l, 1}, pair{r, -1})
				continue
			}
			for _, p := range a {
				if p.l <= r && p.r >= l {
					a = append(a, pair{l, r})
					es = append(es, pair{l, 1}, pair{r, -1})
					break
				}
			}
		}
		if len(es) == 0 {
			Fprintln(out, 1)
			continue
		}
		sort.Slice(es, func(i, j int) bool { return es[i].l < es[j].l })
		ans := es[len(es)-1].l - es[0].l + 1
		c, st := 0, es[0].l
		for _, e := range es {
			if c == 0 {
				if d := e.l - st - 1; d > 0 {
					ans -= d
				}
			}
			c += e.r
			if c == 0 {
				st = e.l
			}
		}
		Fprintln(out, ans)
	}
}

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

package main

import . "fmt"

// github.com/EndlessCheng/codeforces-go
func main() {
	var t, n, x, m, l, r int
	for Scan(&t); t > 0; t-- {
		Scan(&n, &x, &m)
		ll, rr := x, x
		for ; m > 0; m-- {
			if Scan(&l, &r); l <= rr && r >= ll {
				if l < ll {
					ll = l
				}
				if r > rr {
					rr = r
				}
			}
		}
		Println(rr - ll + 1)
	}
}

// 範囲は飛び飛びにならず、ひとつながりで広がっていくので、左右の端で判断する

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

var sc = bufio.NewScanner(os.Stdin)
var out = bufio.NewWriter(os.Stdout)

func main(){
	buf := make([]byte, 1024*1024)
	sc.Buffer(buf, bufio.MaxScanTokenSize)
	sc.Split(bufio.ScanWords)
	defer out.Flush()
	T := nextInt()
	for i:=0; i<T; i++{
		_, X, M := nextInt(), nextInt(), nextInt()
		left, right := X, X
		for i:=0; i<M; i++{
			L, R := nextInt(),nextInt()
			if L<=left && left<=R{
				left = L
			}
			if L<=right && right <= R{
				right = R
			}
		}
		fmt.Fprintln(out, right - left + 1)
	}
}

func next() string {
	sc.Scan()
	return sc.Text()
}

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

func nextInts(n int) []int{
	ret := make([]int, n)
	for i := 0; i < n; i++ {
		ret[i] = nextInt()
	}
	return ret
}

func nextStrings(n int) []string {
	ret := make([]string, n)
	for i := 0; i < n; i++ {
		ret[i] = next()
	}
	return ret
}

func chars(s string) []string {
	ret := make([]string, len([]rune(s)))
	for i, v := range []rune(s) {
		ret[i] = string(v)
	}
	return ret
}

package main

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

func min(a, b int) int {
	return int(math.Min(float64(a), float64(b)))
}

func max(a, b int) int {
	return int(math.Max(float64(a), float64(b)))
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t, now int64
	fmt.Fscanf(in, "%d\n", &t)
	for now < t {
		now++
		var n, m, x int
		fmt.Fscanf(in, "%d %d %d\n", &n, &x, &m)
		x--
		mas := make([]int, 2*m)
		for i := 0; i < m; i++ {
			fmt.Fscanf(in, "%d %d\n", &mas[2*i], &mas[2*i+1])
			mas[2*i]--
			mas[2*i+1]--
		}
		var left, right int
		var flag bool
		for i := 0; i < m; i++ {
			if flag {
				if (left <= mas[2*i] && right >= mas[2*i]) || (left <= mas[2*i+1] && right >= mas[2*i+1]) || (mas[2*i] <= left && mas[2*i+1] >= right){
					left = min(left, mas[2*i])
					right = max(right, mas[2*i+1])
				}
				continue
			}
			if x >= mas[2*i] && x <= mas[2*i+1] {
				flag = true
				left = mas[2*i]
				right = mas[2*i+1]
			}
		}
		if !flag{
			fmt.Fprintln(out, 1)
		} else{
			fmt.Fprintln(out, right+1-left)
		}
	}
	return
}

package main

import (
	"fmt"
	"math"
)

const PI = math.Pi
const mod = 1e9 + 7

func main() {
	//rand.Seed(time.Now().UnixNano())
	var t int
	fmt.Scanln(&t)
	for t > 0 {
		t--
		var n,x,m int
		fmt.Scanln(&n,&x,&m)
		l, r := x, x
		for i := 0; i < m ; i++ {
			var q,e int
			fmt.Scanln(&q,&e)
			if (q <= l && e >= l) || (q <= r && e >= r) {
				l = int(math.Min(float64(l),float64(q)))
				r = int(math.Max(float64(r),float64(e)))
			}
			//fmt.Printf("l=%d r=%d\n",l,r)
		}
		fmt.Println(r-l+1)
	}
}

package main

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

func configure(scanner *bufio.Scanner) {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
}
func getNextString(scanner *bufio.Scanner) string {
	scanned := scanner.Scan()
	if !scanned {
		panic("scan failed")
	}
	return scanner.Text()
}
func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}
func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}
func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	extra := 0
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		extra = 100
	}
	scanner := bufio.NewScanner(fp)
	configure(scanner)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	solve(scanner, writer)
	for i := 0; i < extra; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	t := getNextInt(scanner)
	for i := 0; i < t; i++ {
		testcase(scanner, writer)
	}
}
func testcase(scanner *bufio.Scanner, writer *bufio.Writer) {
	n := getNextInt(scanner)
	x := getNextInt(scanner)
	m := getNextInt(scanner)
	min := x
	max := x
	for i := 0; i < m; i++ {
		l := getNextInt(scanner)
		r := getNextInt(scanner)
		if r < min {
			continue
		}
		if max < l {
			continue
		}
		min = minint(l, min)
		max = minint(maxint(r, max), n)
	}
	fmt.Fprintln(writer, max-min+1)
}

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

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

package main

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

const (
	SCANNER_BUF_SIZE = 2 << 20

	INF = int64(math.MaxInt64)
	MOD = 1e9 + 7
)

var (
	writer  *bufio.Writer
	scanner *bufio.Scanner
)

func init() {
	writer = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, SCANNER_BUF_SIZE), SCANNER_BUF_SIZE)
}

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

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

func suint64() uint64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func sstring() string {
	scanner.Scan()
	return scanner.Text()
}

func gcd(a, b int64) int64 {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

func lcm(a, b int64) int64 {
	return a * b / gcd(a, b)
}

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

func min(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r < a {
			a = r
		}
	}
	return a
}

func max(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r > a {
			a = r
		}
	}
	return a
}

func main() {
	defer writer.Flush()
	t := sint64()
	for ; t > 0; t-- {
		n := sint64()
		x := sint64()
		m := sint64()
		lr := make([][2]int64, m)
		for i := range lr {
			lr[i] = [2]int64{sint64(), sint64()}
		}
		printf("%d\n", sol(n, x, lr))
	}
}

func sol(_, x int64, lr [][2]int64) int64 {
	l, r := x, x
	for i := range lr {
		if lr[i][1] < l || lr[i][0] > r {
			continue
		}
		l = min(l, lr[i][0])
		r = max(r, lr[i][1])
	}
	return r-l+1
}

package main

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

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

func main() {
	var test int
	fmt.Fscan(in, &test)
	for test > 0 {
		solve()
		test--
	}
	out.Flush()
}

func solve() {
	var n, x, m, l, r int
	fmt.Fscan(in, &n, &x, &m)
	lo, hi := x, x
	for m > 0 {
		m--
		fmt.Fscan(in, &l, &r)
		if r >= lo && l <= hi {
			if l < lo {
				lo = l
			}
			if r > hi {
				hi = r
			}
		}
	}
	fmt.Fprintln(out, hi-lo+1)
}

package main

import "fmt"

func main() {
	var test int
	fmt.Scan(&test)
	for test > 0 {
		solve()
		test--
	}
}

func solve() {
	var n, x, m, l, r int
	fmt.Scan(&n, &x, &m)
	lo, hi := x, x
	for m > 0 {
		m--
		fmt.Scan(&l, &r)
		if r >= lo && l <= hi {
			lo = min(lo, l)
			hi = max(hi, r)
		}
	}
	fmt.Println(hi - lo + 1)
}

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

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

package main

import "fmt"

func main() {
	var test int
	fmt.Scanf("%d", &test)
	for test > 0 {
		solve()
		test--
	}
}

func solve() {
	var n, x, m, l, r int
	fmt.Scanf("%d %d %d", &n, &x, &m)
	lo, hi := x, x
	for m > 0 {
		m--
		fmt.Scanf("%d %d", &l, &r)
		if r >= lo && l <= hi {
			lo = min(lo, l)
			hi = max(hi, r)
		}
	}
	fmt.Println(hi - lo + 1)
}

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

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

package main

import "fmt"

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

	for test_case := 0; test_case < t; test_case++ {
		var n, x, m int
		fmt.Scan(&n, &x, &m)

		tempL := x
		tempR := x

		for i:=0; i<m; i++ {
			var l, r int
			fmt.Scan(&l, &r)
			if (!(r < tempR || tempL < l)) {
				if l < tempL {
					tempL = l
				}
				if r > tempR {
					tempR = r
				}
			}
		}
		fmt.Println(tempR-tempL+1)
	}

}

package main

import . "fmt"

// github.com/EndlessCheng/codeforces-go
func main() {
	var t, n, x, m, l, r int
	for Scan(&t); t > 0; t-- {
		Scan(&n, &x, &m)
		ll, rr := x, x
		for ; m > 0; m-- {
			if Scan(&l, &r); l <= rr && r >= ll {
				if l < ll {
					ll = l
				}
				if r > rr {
					rr = r
				}
			}
			Println(rr - ll + 1)
		}
	}
}

package main

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

func min(a, b int) int {
	return int(math.Min(float64(a), float64(b)))
}

func max(a, b int) int {
	return int(math.Max(float64(a), float64(b)))
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t, now int64
	fmt.Fscanf(in, "%d\n", &t)
	for now < t {
		now++
		var n, m, x int
		fmt.Fscanf(in, "%d %d %d\n", &n, &x, &m)
		x--
		mas := make([]int, 2*m)
		for i := 0; i < m; i++ {
			fmt.Fscanf(in, "%d %d\n", &mas[2*i], &mas[2*i+1])
			mas[2*i]--
			mas[2*i+1]--
		}
		var left, right int
		var flag bool
		for i := 0; i < m; i++ {
			if flag {
				if (left <= mas[2*i] && right >= mas[2*i]) || (left <= mas[2*i+1] && right >= mas[2*i+1]) {
					left = min(left, mas[2*i])
					right = max(right, mas[2*i+1])
				}
				continue
			}
			if x >= mas[2*i] && x <= mas[2*i+1] {
				flag = true
				left = mas[2*i]
				right = mas[2*i+1]
			}
		}
		fmt.Fprintln(out, right+1-left)
	}
	return
}

package main

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

func min(a, b int) int {
	return int(math.Min(float64(a), float64(b)))
}

func max(a, b int) int {
	return int(math.Max(float64(a), float64(b)))
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t, now int64
	fmt.Fscanf(in, "%d\n", &t)
	for now < t {
		now++
		var n, m, x int
		fmt.Fscanf(in, "%d %d %d\n", &n, &x, &m)
		x--
		mas := make([]int, 2*m)
		for i := 0; i < m; i++ {
			fmt.Fscanf(in, "%d %d\n", &mas[2*i], &mas[2*i+1])
			mas[2*i]--
			mas[2*i+1]--
		}
		var left, right int
		var flag bool
		for i := 0; i < m; i++ {
			if flag {
				if (left <= mas[2*i] && right >= mas[2*i]) || (left <= mas[2*i+1] && right >= mas[2*i+1]) || (mas[2*i] <= left && mas[2*i+1] >= right){
					left = min(left, mas[2*i])
					right = max(right, mas[2*i+1])
				}
				continue
			}
			if x >= mas[2*i] && x <= mas[2*i+1] {
				flag = true
				left = mas[2*i]
				right = mas[2*i+1]
			}
		}
		if !flag{
			fmt.Fprintln(out, 0)
		} else{
			fmt.Fprintln(out, right+1-left)
		}
	}
	return
}

package main

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

func min(a, b int) int {
	return int(math.Min(float64(a), float64(b)))
}

func max(a, b int) int {
	return int(math.Max(float64(a), float64(b)))
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t, now int64
	fmt.Fscanf(in, "%d\n", &t)
	for now < t {
		now++
		var n, m, x int
		fmt.Fscanf(in, "%d %d %d\n", &n, &x, &m)
		x--
		mas := make([]int, 2*m)
		for i := 0; i < m; i++ {
			fmt.Fscanf(in, "%d %d\n", &mas[2*i], &mas[2*i+1])
			mas[2*i]--
			mas[2*i+1]--
		}
		var left, right int
		var flag bool
		for i := 0; i < m; i++ {
			if flag {
				if (left <= mas[2*i] && right >= mas[2*i]) || (left <= mas[2*i+1] && right >= mas[2*i+1]) {
					left = min(left, mas[2*i])
					right = max(right, mas[2*i+1])
				}
				continue
			}
			if x >= mas[2*i] && x <= mas[2*i+1] {
				flag = true
				left = mas[2*i]
				right = mas[2*i+1]
			}
		}
		if !flag{
			fmt.Fprintln(out, 0)
		} else{
			fmt.Fprintln(out, right+1-left)
		}
	}
	return
}

package main

import "fmt"

func main() {
	var test int
	fmt.Scan(&test)
	for test > 0 {
		solve()
		test--
	}
}

func solve() {
	var n, x, m, l, r int
	fmt.Scan(&n, &x, &m)
	lo, hi := x, x
	for m > 0 {
		m--
		fmt.Scan(&l, &r)
		if r >= lo && l <= hi {
			if l < lo {
				lo = lo
			}
			if r > hi {
				hi = r
			}
		}
	}
	fmt.Println(hi - lo + 1)
}