package main

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

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

func main() {

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

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

		var str string
		fmt.Fscan(rr, &str)

		cntN := int64(0)

		for j := 0; j < len(str); j++ {
			if str[j] == 'N' {
				cntN++
			}
		}

		if cntN == 1 {
			fmt.Fprintln(ww, "NO")

		} else {
			fmt.Fprintln(ww, "YES")
		}

		ww.Flush()

	}
	ww.Flush()
}

func Reverse(s string) string {
	runes := []rune(s)
	for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
		runes[i], runes[j] = runes[j], runes[i]
	}
	return string(runes)
}

package main

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

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, s := 0, ""
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &s)
		c := 0
		for j := 0; j < len(s); j++ {
			if string(s[j]) == "N" {
				c++
			}
		}
		if c == 1 {
			fmt.Fprintln(w, "NO")
		} else {
			fmt.Fprintln(w, "YES")
		}
	}
	w.Flush()
}

func main() {
	solve()
}

package main

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

/*
7
EEE
NNN
NNE
ENE
EEN
EE
NN
---
YES
YES
YES
NO
NO
YES
YES
====
1
NEE
---
NO
*/

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

	bld := strings.Builder{}

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

	for ; t > 0; t-- {
		var s string
		scanf("%s\n", &s)

		if len(s) == 2 {
			if s[0] == 'E' && s[1] == 'E' || s[0] == 'N' && s[1] == 'N' {
				fmt.Fprintln(&bld, "YES")
			} else {
				fmt.Fprintln(&bld, "NO")
			}
		} else {
			nCnt := 0
			for _, r := range s {
				if r == 'N' {
					nCnt++
				}
			}

			if nCnt == 1 {
				fmt.Fprintln(&bld, "NO")
			} else {
				fmt.Fprintln(&bld, "YES")
			}
		}
	}

	printf("%s", bld.String())
}

const maxUint = ^uint(0)

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

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

//goland:noinspection GoUnusedType
type pair struct {
	fst int
	snd int
}

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

//goland:noinspection GoUnusedFunction
func printf(f string, a ...interface{}) {
	if _, err := fmt.Fprintf(writer, f, a...); err != nil {
		panic(err)
	}
}

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

//goland:noinspection GoUnusedFunction
func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

//goland:noinspection GoUnusedFunction
func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

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

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

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

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

	return true
}

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

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

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

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

package main
// github.com/jamesgrimard/codeforces
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var word string
		fmt.Fscan(in,&word)
		var count int
		for j:=0;j<len(word);j++{
			if string(word[j]) == "N" {
				count++
			}
		}
		if count == 1 {
			fmt.Fprintln(out,"NO")
		} else {
			fmt.Fprintln(out,"YES")
		}
	}
}

package main

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

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

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

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

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

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

/**
Run solve the problem CF1620A
Date: 17/01/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1620/A
Problem: CF1620A
**/
func (in *CF1620A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		str := in.NextString()
		n := len(str)
		ans := true
		arr := make([]int, n)
		index := -1
		for i := 0; i < n; i++ {
			if str[0] != str[i] {
				index = i
				break
			}
		}

		if index != -1 {
			cont := 1
			for i, j := 0, index; i < n; i++ {
				if str[j] == 'E' {
				    arr[j] = cont
				    if arr[(j+1)%n] == 0{
					    arr[(j+1)%n] = cont
					}
				} else {
				   if arr[(j+1)%n] == 0{
						cont++
						arr[(j+1)%n] = cont
					}
				}
				j = (j + 1) % n
			}

			//fmt.Println("---> ", arr)

			for i := 0; i < n; i++ {
				if str[i] == 'E' && arr[i] != arr[(i+1)%n] {
					ans = false
					break
				} else if str[i] == 'N' && arr[i] == arr[(i+1)%n] {
					ans = false
					break
				}
			}
		}

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

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

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

package main

import "fmt"

func main() {
	var t int
	fmt.Scan(&t)
	for t > 0 {
		var s string
		count := 0
		fmt.Scan(&s)
		for _, item := range s {
			if string(item) == "N" {
				count++
			}
		}
		if count == 1 {
			fmt.Println("NO")
		} else {
			fmt.Println("YES")
		}
		t--
	}
}

package main

import (
	"fmt"
)

func main() {

	t := 0
	str := ""

	fmt.Scan(&t)

	for t > 0 {

		fmt.Scan(&str)
		fmt.Println(solve(str))

		t--
	}

}

func solve(str string) string {

	//size := len(str)
	count := 0

	for i := range str {
		if str[i] == 'N' {
			count++
		}
	}

	if count == 1 {
		return "NO"
	} else {
		return "YES"
	}

}

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

)
func main(){
	r := bufio.NewReader(os.Stdin)
    w := bufio.NewWriter(os.Stdout)
    defer w.Flush()
    var t int
    fmt.Fscan(r, &t)
    for j := 0; j < t; j++ {
		var s string
		fmt.Fscan(r, &s)
		i := 0
		for _, x := range s {
			if (x == 'N') {
				i++
			}
		}
		if i != 1 {
			fmt.Fprintln(w, "YES")
		} else {
			fmt.Fprintln(w, "NO")
		}
	}

}

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

		var c int
		for _, ch := range s {
			if ch == 'N' {
				c++
			}
		}

		if c == 1 {
			fmt.Fprintln(out, "NO")
		} else {
			fmt.Fprintln(out, "YES")
		}
	}
}

package main

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

var s string

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)

	for i := 0; i < t; i++ {
		fmt.Fscan(r, &s)

		ans := solve()

		fmt.Println(ans)
	}
}

type pair struct {
	v, id int
}

type ByV []*pair

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

type item struct {
	la, ra int
	lb, rb int
}

const MAX int = 1e6

func solve() string {

	var E, N int

	for i := 0; i < len(s); i++ {
		if s[i] == 'E' {
			E++
		} else {
			N++
		}
	}

	if N == 1 {
		return "NO"
	}

	return "YES"
}

func add(idx int, delta int, bit *[]int, n int) {
	for ; idx <= n; idx += idx & -idx {
		(*bit)[idx] += delta
	}
}

func get(idx int, bit *[]int) int {
	ret := 0
	for ; idx > 0; idx -= idx & -idx {
		ret += (*bit)[idx];
	}
	return ret
}

//A -  1 3 5 3 1

//B -  4 3 6 1

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

package main

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

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

	var T int
	var s string
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &s)
		if strings.Count(s, "N") == 1 {
			Fprintln(out, "NO")
		} else {
			Fprintln(out, "YES")
		}
	}
}

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

package main

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

func solve(r *bufio.Reader, w *bufio.Writer) {
	var s string
	fmt.Fscan(r, &s)
	if strings.Count(s, "N") == 1 {
		fmt.Fprintf(w, "NO\n")
	} else {
		fmt.Fprintf(w, "YES\n")
	}
}

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

	var tc int
	fmt.Fscan(r, &tc)
	for i := 0; i < tc; i++ {
		solve(r, w)
	}
}

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	t      int
	s      string
)

func solve(s string) string {
	countN := 0
	for _, x := range s {
		if x == 'N' {
			countN++
		}
	}
	if countN == 1 {
		return "NO"
	}
	return "YES"
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &s)
		fmt.Println(solve(s))
	}
}

package main

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

func main() {
	reader := bReader(os.Stdin)
	writer := bWriter(os.Stdout)

	t := readInt(reader)

	for t > 0 {
		s := readString(reader)
		countN := 0
		for i := 0; i < len(s); i++ {
			if s[i] == 'N' {
				countN++
			}
		}

		if countN == 1 {
			fmt.Fprintln(writer, "NO")
		} else {
			fmt.Fprintln(writer, "YES")
		}

		t--
	}

	writer.Flush()
}

func bReader(stdin *os.File) *bufio.Reader {
	return bufio.NewReaderSize(stdin, 16*1024*1024)
}

func bWriter(stdout *os.File) *bufio.Writer {
	return bufio.NewWriterSize(stdout, 16*1024*1024)
}

func readLine(reader *bufio.Reader) string {
	str, _, err := reader.ReadLine()
	if err == io.EOF {
		return ""
	}

	return strings.TrimRight(string(str), "\r\n")
}

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}

func readInt(reader *bufio.Reader) int {
	return int(readInt64(reader))
}

func readInt64(reader *bufio.Reader) int64 {
	value, err := strconv.ParseInt(readLine(reader), 10, 64)
	checkError(err)

	return value
}

func readString(reader *bufio.Reader) string {
	return readLine(reader)
}

func readArrayString(reader *bufio.Reader) []string {
	return strings.Split(strings.TrimSpace(readLine(reader)), " ")
}

func readArrayInt(reader *bufio.Reader) []int {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64)
		checkError(err)
		arItem := int(arItemTemp)
		ar = append(ar, arItem)
	}

	return ar
}

func readArrayInt64(reader *bufio.Reader) []int64 {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int64

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64)
		checkError(err)
		arItem := arItemTemp
		ar = append(ar, arItem)
	}

	return ar
}

func readTwoInt(reader *bufio.Reader) (int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1]
}

func readTwoInt64(reader *bufio.Reader) (int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1]
}

func readThreeInt(reader *bufio.Reader) (int, int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1], ar[2]
}

func readThreeInt64(reader *bufio.Reader) (int64, int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1], ar[2]
}

func gcd(x int64, y int64) int64 {
	for y > 0 {
		x, y = y, x % y
	}

	return x
}

func lcm(x int64, y int64) int64 {
	return x*y/gcd(x,y)
}

package main

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

func main() {
	reader := bReader(os.Stdin)
	writer := bWriter(os.Stdout)

	t := readInt(reader)

	for t > 0 {
		s := readString(reader)
		if len(s) == 2 && s[0] != s[1] {
			fmt.Fprintln(writer, "NO")
		} else if s == "ENE" {
			fmt.Fprintln(writer, "NO")
		} else {
			fmt.Fprintln(writer, "YES")
		}

		t--
	}

	writer.Flush()
}

func bReader(stdin *os.File) *bufio.Reader {
	return bufio.NewReaderSize(stdin, 16*1024*1024)
}

func bWriter(stdout *os.File) *bufio.Writer {
	return bufio.NewWriterSize(stdout, 16*1024*1024)
}

func readLine(reader *bufio.Reader) string {
	str, _, err := reader.ReadLine()
	if err == io.EOF {
		return ""
	}

	return strings.TrimRight(string(str), "\r\n")
}

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}

func readInt(reader *bufio.Reader) int {
	return int(readInt64(reader))
}

func readInt64(reader *bufio.Reader) int64 {
	value, err := strconv.ParseInt(readLine(reader), 10, 64)
	checkError(err)

	return value
}

func readString(reader *bufio.Reader) string {
	return readLine(reader)
}

func readArrayString(reader *bufio.Reader) []string {
	return strings.Split(strings.TrimSpace(readLine(reader)), " ")
}

func readArrayInt(reader *bufio.Reader) []int {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64)
		checkError(err)
		arItem := int(arItemTemp)
		ar = append(ar, arItem)
	}

	return ar
}

func readArrayInt64(reader *bufio.Reader) []int64 {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int64

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64)
		checkError(err)
		arItem := arItemTemp
		ar = append(ar, arItem)
	}

	return ar
}

func readTwoInt(reader *bufio.Reader) (int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1]
}

func readTwoInt64(reader *bufio.Reader) (int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1]
}

func readThreeInt(reader *bufio.Reader) (int, int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1], ar[2]
}

func readThreeInt64(reader *bufio.Reader) (int64, int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1], ar[2]
}

func gcd(x int64, y int64) int64 {
	for y > 0 {
		x, y = y, x % y
	}

	return x
}

func lcm(x int64, y int64) int64 {
	return x*y/gcd(x,y)
}

package main

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

func main() {
	reader := bReader(os.Stdin)
	writer := bWriter(os.Stdout)

	t := readInt(reader)

	for t > 0 {
		s := readString(reader)
		if s[0] == s[len(s)-1] {
			fmt.Fprintln(writer, "YES")
		} else {
			fmt.Fprintln(writer, "NO")
		}

		t--
	}

	writer.Flush()
}

func bReader(stdin *os.File) *bufio.Reader {
	return bufio.NewReaderSize(stdin, 16*1024*1024)
}

func bWriter(stdout *os.File) *bufio.Writer {
	return bufio.NewWriterSize(stdout, 16*1024*1024)
}

func readLine(reader *bufio.Reader) string {
	str, _, err := reader.ReadLine()
	if err == io.EOF {
		return ""
	}

	return strings.TrimRight(string(str), "\r\n")
}

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}

func readInt(reader *bufio.Reader) int {
	return int(readInt64(reader))
}

func readInt64(reader *bufio.Reader) int64 {
	value, err := strconv.ParseInt(readLine(reader), 10, 64)
	checkError(err)

	return value
}

func readString(reader *bufio.Reader) string {
	return readLine(reader)
}

func readArrayString(reader *bufio.Reader) []string {
	return strings.Split(strings.TrimSpace(readLine(reader)), " ")
}

func readArrayInt(reader *bufio.Reader) []int {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64)
		checkError(err)
		arItem := int(arItemTemp)
		ar = append(ar, arItem)
	}

	return ar
}

func readArrayInt64(reader *bufio.Reader) []int64 {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int64

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64)
		checkError(err)
		arItem := arItemTemp
		ar = append(ar, arItem)
	}

	return ar
}

func readTwoInt(reader *bufio.Reader) (int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1]
}

func readTwoInt64(reader *bufio.Reader) (int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1]
}

func readThreeInt(reader *bufio.Reader) (int, int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1], ar[2]
}

func readThreeInt64(reader *bufio.Reader) (int64, int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1], ar[2]
}

func gcd(x int64, y int64) int64 {
	for y > 0 {
		x, y = y, x % y
	}

	return x
}

func lcm(x int64, y int64) int64 {
	return x*y/gcd(x,y)
}

package main

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

// Brute forcing it. Cant seem to find a pattern
func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, s := 0, ""
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &s)
		k, e := 1, []int{}
		for j := 0; j < len(s); j++ {
			if j+1 == len(s) {
				if string(s[j]) == "E" && e[j] == e[0] {
					fmt.Fprintln(w, "YES")
				} else if string(s[j]) == "E" && e[j] != e[0] {
					fmt.Fprintln(w, "NO")
				} else if string(s[j]) == "N" && e[j] == e[0] {
					fmt.Fprintln(w, "NO")
				} else {
					fmt.Fprintln(w, "YES")
				}
				break
			}
			if string(s[j]) == "E" {
				e = append(e, k)
				if j == 0 {
					e = append(e, k)
				}
			} else {
				e = append(e, k)
				if j == 0 {
					e = append(e, k+1)
					k++
				}
			}
		}
	}
	w.Flush()
}

func main() {
	solve()
}

package main

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

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, s := 0, ""
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &s)
		if s != "EN" && s != "NE" {
			fmt.Fprintln(w, "YES")
		} else {
			fmt.Fprintln(w, "NO")
		}
	}
	w.Flush()

}

func main() {
	solve()
}

package main

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

/*
5
EEE
NNN
NNE
ENE
EEN
---
YES
YES
YES
NO
NO
====
1
ENE
*/

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

	bld := strings.Builder{}

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

	for ; t > 0; t-- {
		var s string
		scanf("%s\n", &s)

		if len(s) == 2 {
			if s[0] == 'E' && s[1] == 'E' {
				fmt.Fprintln(&bld, "YES")
			} else {
				fmt.Fprintln(&bld, "NO")
			}
		} else {
			nCnt := 0
			for _, r := range s {
				if r == 'N' {
					nCnt++
				}
			}

			if nCnt == 1 {
				fmt.Fprintln(&bld, "NO")
			} else {
				fmt.Fprintln(&bld, "YES")
			}

			//allExceptLastIsE := true
			//for i := 0; i < len(s)-1; i++ {
			//	if s[i] != 'E' {
			//		allExceptLastIsE = false
			//		break
			//	}
			//}
			//
			//if allExceptLastIsE && s[len(s)-1] == 'N' {
			//	fmt.Fprintln(&bld, "NO")
			//} else {
			//	fmt.Fprintln(&bld, "YES")
			//}
		}
	}

	printf("%s", bld.String())
}

const maxUint = ^uint(0)

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

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

//goland:noinspection GoUnusedType
type pair struct {
	fst int
	snd int
}

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

//goland:noinspection GoUnusedFunction
func printf(f string, a ...interface{}) {
	if _, err := fmt.Fprintf(writer, f, a...); err != nil {
		panic(err)
	}
}

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

//goland:noinspection GoUnusedFunction
func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

//goland:noinspection GoUnusedFunction
func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

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

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

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

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

	return true
}

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

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

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

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

package main

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

/*
5
EEE
NNN
NNE
ENE
EEN
---
YES
YES
YES
YES
NO
*/

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

	bld := strings.Builder{}

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

	for ; t > 0; t-- {
		var s string
		scanf("%s\n", &s)

		if len(s) == 2 {
			if s[0] == 'E' && s[1] == 'E' {
				fmt.Fprintln(&bld, "YES")
			} else {
				fmt.Fprintln(&bld, "NO")
			}
		} else {
			allExceptLastIsE := true
			for i := 0; i < len(s)-1; i++ {
				if s[i] != 'E' {
					allExceptLastIsE = false
					break
				}
			}

			if allExceptLastIsE && s[len(s)-1] == 'N' {
				fmt.Fprintln(&bld, "NO")
			} else {
				fmt.Fprintln(&bld, "YES")
			}

		}
	}

	printf("%s", bld.String())
}

const maxUint = ^uint(0)

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

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

//goland:noinspection GoUnusedType
type pair struct {
	fst int
	snd int
}

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

//goland:noinspection GoUnusedFunction
func printf(f string, a ...interface{}) {
	if _, err := fmt.Fprintf(writer, f, a...); err != nil {
		panic(err)
	}
}

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

//goland:noinspection GoUnusedFunction
func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

//goland:noinspection GoUnusedFunction
func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

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

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

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

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

	return true
}

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

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

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

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

package main
// github.com/jamesgrimard/codeforces
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var word string
		fmt.Fscan(in,&word)
		if string(word[0]) == string(word[len(word)-1]) {
			fmt.Fprintln(out,"YES")
		} else {
			fmt.Fprintln(out,"NO")
		}
	}
}

package main

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

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

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

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

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

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

/**
Run solve the problem CF1620A
Date: 17/01/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1620/A
Problem: CF1620A
**/
func (in *CF1620A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		str := in.NextString()
		n := len(str)
		ans := true
		en := 0
		ne := 0
		for i := 0; i < n; i++ {
			if str[i] == 'E' && str[(i+1)%n] == 'N' {
				en++
			}
			if str[i] == 'N' && str[(i+1)%n] == 'E' {
				ne++
			}
		}
		if en == n || ne == n || (ne+en == n && en == 1) || (ne+en == n && ne == 1) {
			ans = false
		}

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

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

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

package main

import "fmt"

func main() {
	var t int
	fmt.Scan(&t)
	for t > 0 {
		var s string
		fmt.Scan(&s)
		if len(s) == 2 {
			if s[0] != s[1] {
				fmt.Println("NO")
			} else {
				fmt.Println("YES")
			}
		} else {
			fmt.Println("YES")
		}
		t--
	}
}

package main

import "fmt"

func main() {

	t := 0
	str := ""

	fmt.Scan(&t)

	for t > 0 {

		fmt.Scan(&str)

		if str[0] == str[len(str)-1] {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}

		t--
	}

}

package main

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

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

func main() {

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

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

		var str string
		fmt.Fscan(rr, &str)

		if len(str) > 2 {
			fmt.Fprintln(ww, "YES")
			ww.Flush()

			continue
		}

		if str[0] == str[1] {
			fmt.Fprintln(ww, "YES")
			ww.Flush()

			continue
		}

		fmt.Fprintln(ww, "NO")

		ww.Flush()

	}
	ww.Flush()
}

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

)
func main(){
	r := bufio.NewReader(os.Stdin)
    w := bufio.NewWriter(os.Stdout)
    defer w.Flush()
    var t int
    fmt.Fscan(r, &t)
    for j := 0; j < t; j++ {
		var s string
		fmt.Fscan(r, &s)
		l := len(s)
		la := s[l-2: l];
		flag := ("NE" == la && l > 2) || "EE" == la || "NN" == la

		if flag {
			fmt.Fprintln(w, "YES");
		} else {
			fmt.Fprintln(w, "NO");
		}
	}

}


package main

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

// github.com/EndlessCheng/codeforces-go
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	min := func(a, b int) int {
		if a < b {
			return a
		}
		return b
	}
	solve := func(Case int) {
		var n int
		Fscan(in, &n)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		pos := map[int][]int{}
		for i, v := range a {
			pos[v] = append(pos[v], i)
		}
		ans := int(1e9)

		for _, ps := range pos {
			if ps == nil {
				continue
			}
			c:=0
			if ps[0] > 0 {
				c++
			}
			if ps[len(ps)-1] < n-1 {
				c++
			}
			for i := 1; i < len(ps); i++ {
				if ps[i]-ps[i-1] > 1 {
					c++
				}
			}
			ans = min(ans, c)
		}
		Fprintln(out, ans)
	}

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

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

package main

import(

	"fmt"
	."fmt"
	"bufio"
	"os"
)

const hell int = 1000000007

func solve(a *[]int){
	arr := *a
	n := len(arr)

	m := make(map[int][]int)

	for i := range arr{
		m[arr[i]] = append(m[arr[i]],i)
	}

	ans := hell

	min := func (x,y int)int{
				if x>y{
					return y
				}
				return x
			}

	for _,varr := range m{
		cur,jumps := 0,0
		for _,val := range varr{
			if cur == val || (cur==val-1 && arr[cur]==arr[val]){
				cur = val
			}else{
				jumps+=1
				cur = val
			}
		}
		if cur+1 != n{
			jumps+=1
		}
		ans = min(ans,jumps)
	}

	fmt.Println(ans)

}
func main(){
	var TESTS int
	in := bufio.NewReader(os.Stdin)

	Fscan(in,&TESTS)

	for ;TESTS>0;TESTS--{
		var n int
		Fscan(in,&n)
		a := make([]int,n)

		for i := range a{
			Fscan(in,&a[i])
		}

		solve(&a)
	}
}

package main

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

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

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

type pp struct {
	val int
	ind int
}

func solve() {
	var n int
	scanf("%d\n", &n)
	v := make([]pp, n)
	for i := 0; i < n; i++ {
		if i + 1 == n {
			scanf("%d\n", &v[i].val)
		} else {
			scanf("%d ", &v[i].val)
		}
		v[i].ind = i
	}
	sort.Slice(v, func(i, j int) bool {
		return v[i].val < v[j].val || (v[i].val == v[j].val && v[i].ind < v[j].ind)
	})
	//fmt.Println(v)
	ans := n
	for i := 0; i < n; i++ {
		curr := 0
		if v[i].ind > 0 {
			curr++
		}
		j := i + 1
		for ; j < n && v[i].val == v[j].val; j++ {
			if v[j].ind - v[j - 1].ind != 1 {
				curr++
			}
		}
		j--
		if v[j].ind + 1 < n {
			curr++
		}
		if ans > curr {
			ans = curr
		}
		i = j
	}
	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"
)

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

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

	for ; t > 0; t-- {
		fmt.Fscan(in, &n)
		a := make(map[int]int)
		prev := -1
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &cur)
			a[cur]++
			if prev == cur {
				a[cur]--
			}
			if i == 0 || i == n-1 {
				a[cur]--
			}
			prev = cur
		}
		if len(a) == 1 {
			fmt.Println(0)
			continue
		}
		min := n
		for _, v := range a {
			if v < min {
				min = v
			}
		}
		fmt.Println(min + 1)
	}
}

package main

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

// github.com/EndlessCheng/codeforces-go
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	min := func(a, b int) int {
		if a < b {
			return a
		}
		return b
	}
	solve := func(Case int) {
		var n int
		Fscan(in, &n)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		pos := map[int][]int{}
		for i, v := range a {
			pos[v] = append(pos[v], i)
		}
		ans := int(1e9)

		for _, ps := range pos {
			if ps == nil {
				continue
			}
			c:=0
			if ps[0] > 0 {
				c++
			}
			if ps[len(ps)-1] < n {
				c++
			}
			for i := 1; i < len(ps); i++ {
				if ps[i]-ps[i-1] > 1 {
					c++
				}
			}
			ans = min(ans, c)
		}
		Fprintln(out, ans)
	}

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

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

package main

import(

	"fmt"
	."fmt"
	"bufio"
	"os"
)

const hell int = 1000000007

func solve(a *[]int){
	arr := *a
	n := len(arr)

	m := make(map[int][]int)

	for i := range arr{
		m[arr[i]] = append(m[arr[i]],i)
	}

	ans := hell

	min := func (x,y int)int{
				if x>y{
					return y
				}
				return x
			}

	for _,varr := range m{
		cur,jumps := 0,0
		for _,val := range varr{
			if cur == val || cur==val-1{
				cur = val
			}else{
				jumps+=1
				cur = val
			}
		}
		if cur+1 != n{
			jumps+=1
		}
		ans = min(ans,jumps)
	}

	fmt.Println(ans)

}
func main(){
	var TESTS int
	in := bufio.NewReader(os.Stdin)

	Fscan(in,&TESTS)

	for ;TESTS>0;TESTS--{
		var n int
		Fscan(in,&n)
		a := make([]int,n)

		for i := range a{
			Fscan(in,&a[i])
		}

		solve(&a)
	}
}

package main

import(

	"fmt"
	."fmt"
	"bufio"
	"os"
)

const hell int = 1000000007

func solve(a *[]int){
	arr := *a
	n := len(arr)

	m := make(map[int][]int)

	for i := range arr{
		m[arr[i]] = append(m[arr[i]],i)
	}

	ans := hell

	min := func (x,y int)int{
				if x>y{
					return y
				}
				return x
			}

	for _,varr := range m{
		cur,jumps := 0,0
		for _,val := range varr{
			if cur == val || (cur==val-1 && cur!=0){
				cur = val
			}else{
				jumps+=1
				cur = val
			}
		}
		if cur+1 != n{
			jumps+=1
		}
		ans = min(ans,jumps)
	}

	fmt.Println(ans)

}
func main(){
	var TESTS int
	in := bufio.NewReader(os.Stdin)

	Fscan(in,&TESTS)

	for ;TESTS>0;TESTS--{
		var n int
		Fscan(in,&n)
		a := make([]int,n)

		for i := range a{
			Fscan(in,&a[i])
		}

		solve(&a)
	}
}


package main

import (
	"fmt"
	"math"
)

func perm(n int) [][]int {
	if n == 1 {
		return [][]int{{0}}
	}
	var p [][]int
	for i := 0; i < n; i++ {
		for _, q := range perm(n - 1) {
			q := q
			q = append(q[:i], append([]int{n-1}, q[i:]...)...)
			p = append(p, q)
		}
	}
	return p
}

type board []string

func newBoard(n int, tl, tr, bl, br []string) board {
	var ss []string
	ss = make([]string, n+n)
	for i := range tl {
		ss[i] = tl[i] + tr[i]
		ss[i+n] = bl[i] + br[i]
	}
	return board(ss)
}

func (b board) recolor() int {
	var cnt int
	for i, row := range b {
		for j, sq := range row {
			sq := sq
			sq -= '0'
			if ((i + j) & 1) != int(sq) {
				cnt++
			}
		}
	}
	return cnt
}

func main() {
	var n int
	fmt.Scanf("%d\n", &n)
	a := make([][]string, 4)
	for k := range a {
		a[k] = make([]string, n)
		for i := range a[k] {
			fmt.Scanf("%s\n", &a[k][i])
		}
		fmt.Scanln()
	}
	var ans int
	ans = math.MaxInt32
	for _, p := range perm(4) {
		if v := newBoard(n, a[p[0]], a[p[1]], a[p[2]], a[p[3]]).recolor(); v < ans {
			ans = v
		}
	}
	fmt.Println(ans)
}


package main

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

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

	var T, n, x int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &x)
		a := make([]int, n)
		mi, mx := int(1e9), 0
		ans := int64(0)
		for i := range a {
			Fscan(in, &a[i])
			mi = min(mi, a[i])
			mx = max(mx, a[i])
			if i > 0 {
				ans += int64(abs(a[i] - a[i-1]))
			}
		}
		ans += int64(min(min(a[0], a[n-1])-1, mi*2-2))
		if mx < x {
			ans += int64(min(x-max(a[0], a[n-1]), (x-mx)*2))
		}
		Fprintln(out, ans)
	}
}

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

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

package main

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

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

func main() {
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		var n, x int
		fmt.Fscan(in, &n, &x)
		arr := make([]int, n)
		minn, maxn := math.MaxInt32, math.MinInt32
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
			minn = min(arr[i], minn)
			maxn = max(arr[i], maxn)
		}
		cur := int64(0)

		if n == 1 {
			if arr[0] >= 1 && arr[0] <= x {
				cur = int64(x - 1)
			} else {
				cur = int64(arr[0] - 1)
			}
		} else {
			for i := 0; i < n-1; i++ {
				cur += int64(abs(arr[i] - arr[i+1]))
			}
			if minn <= 1 && x <= maxn {

			}
			if 1 < minn {
				num1 := abs(min(arr[0], arr[n-1]) - 1)
				num2 := math.MaxInt32
				for i := 0; i < n-1; i++ {
					num2 = min(num2, (min(arr[i], arr[i+1])-1)*2)
				}
				cur += int64(min(num1, num2))
			}
			if x > maxn {
				num1 := abs(x - max(arr[0], arr[n-1]))
				num2 := math.MaxInt32
				for i := 0; i < n-1; i++ {
					num2 = min(num2, (x-max(arr[i], arr[i+1]))*2)
				}
				cur += int64(min(num1, num2))
			}
		}

		fmt.Fprintln(out, cur)
	}
}

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

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

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

package main

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

const MOD int64 = 1000000007

var n, x int

var a []int

// ByAge implements sort.Interface based on the Age field.
type ByV []*pair

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

func main() {

	read := bufio.NewReader(os.Stdin)

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

	for i := 0; i < t; i++ {

		fmt.Fscan(read, &n, &x)

		a = make([]int, n)

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

		solve()
	}
}

type pair struct {
	v int
	idx []int
}

//a b c d

//20 12 -- 4
//4  2  -- 4

func solve()  {

	var min int = 1e9
	var max int = 0

	var sum int64

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

	if x < min {

		var m int = 1e9

		if abs(1 - a[0]) < m {
			m = abs(1 - a[0])
		}
		if abs(x - a[0]) < m {
			m = abs(x - a[0])
		}
		if abs(1 - a[n-1]) < m {
			m = abs(1 - a[n-1])
		}
		if abs(x - a[n - 1]) < m {
			m = abs(x- a[n-1])
		}

		for i := 0; i < n - 1; i++ {
			l := a[i]
			r := a[i + 1]

			temp := abs(l - 1) + abs(x - r) - abs(r - l)

			if temp < m {
				m = temp
			}
		}

		sum += int64(m)
		sum += int64(x - 1)

		fmt.Println(sum)

	} else if x >= min && x <= max {

		if min == 1 {
			fmt.Println(sum)

		} else if min == 2 {
			aa := abs(1 - a[0])
			bb := abs(1 - a[n - 1])
			cc := 2

			if cc < bb && cc < aa {
				sum +=2
			} else {
				sum += int64(minf(aa, bb))
			}

			fmt.Println(sum)

		} else {
			//1 .... min - 1
			var m int = 1e9

			if abs(1 - a[0]) < m {
				m = abs(1 - a[0])
			}
			if abs(min - 1 - a[0]) < m {
				m = abs(min - 1 - a[0])
			}
			if abs(1 - a[n - 1]) < m {
				m = abs(1 - a[n - 1])
			}
			if abs(min - 1 - a[n - 1]) < m {
				m = abs(min - 1 - a[n - 1])
			}

			if abs(1 - min) + 1 < m {
				m = abs(1 - min) + 1
			}

			if abs(min) < m {
				m = abs(min)
			}

			//1  .... min - 1
			//min     min + 1

			sum += int64(m)
			sum += int64(min - 2)

			fmt.Println(sum)
		}
	} else {
		var m int = 1e9

		if abs(max + 1 - a[0]) < m {
			m = abs(max + 1 - a[0])
		}
		if abs(x - a[0]) < m {
			m = abs(x - a[0])
		}
		if abs(max + 1 - a[n - 1]) < m {
			m = abs(max + 1 - a[n - 1])
		}
		if abs(x - a[n - 1]) < m {
			m = abs(x - a[n - 1])
		}

		if abs(x - max) + 1 < m {
			m = abs(x - max) + 1
		}

		if abs(max - 1 - x) < m {
			m = abs(max - 1 - x)
		}

		//max - 1        max
		//max + 1   ..... x

		sum += int64(m)
		sum += int64(x - max - 1)

		if min == 1 {

			fmt.Println(sum)

		} else if min == 2 {

			aa := abs(1 - a[0])
			bb := abs(1 - a[n - 1])
			cc := 2

			if cc < bb && cc < aa {
				sum +=2
			} else {
				sum += int64(minf(aa, bb))
			}

			fmt.Println(sum)

		} else {

			var m int = 1e9

			if abs(1 - a[0]) < m {
				m = abs(1 - a[0])
			}
			if abs(min - 1 - a[0]) < m {
				m = abs(min - 1 - a[0])
			}
			if abs(1 - a[n - 1]) < m {
				m = abs(1 - a[n - 1])
			}
			if abs(min - 1 - a[n - 1]) < m {
				m = abs(min - 1 - a[n - 1])
			}

			if abs(1 - min) + 1 < m {
				m = abs(1 - min) + 1
			}

			if abs(min) < m {
				m = abs(min)
			}

			sum += int64(m)
			sum += int64(min - 2)

			fmt.Println(sum)

		}

	}
}

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

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

package main

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

var i int64

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

	return b
}

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

func reverse(s []int64) {
	for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
		s[i], s[j] = s[j], s[i]
	}
}

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

	defer w.Flush()

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

	for ; t > 0; t-- {
		var n int64
		var x int64

		fmt.Fscan(r, &n, &x)

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

		var ans int64 = 1e18
		var current int64 = 0

		for i = 0; i < n-1; i++ {
			current += abs(a[i] - a[i+1])
		}

		for j := 0; j < 2; j++ {
			mn := abs(a[0] - 1)
			ans = min(ans, current+abs(a[0]-x)+x-1)

			for i = 0; i < n-1; i++ {
				ans = min(ans, current+mn-abs(a[i]-a[i+1])+abs(a[i]-x)+abs(a[i+1]-x))
				ans = min(ans, current-abs(a[i]-a[i+1])+abs(a[i]-x)+abs(a[i+1]-1)+(x-1))
				mn = min(mn, 0-abs(a[i]-a[i+1])+abs(a[i]-1)+abs(a[i+1]-1))
			}

			ans = min(ans, current+mn+abs(a[n-1]-x))
			reverse(a)
		}

		fmt.Fprintln(w, ans)
	}
}

package main

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

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

	var T, n, x, v int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &x, &v)
		st, mi, mx, pre := v, v, v, v
		ans := int64(0)
		for ; n > 1; n-- {
			Fscan(in, &v)
			if v < mi {
				mi = v
			} else if v > mx {
				mx = v
			}
			ans += int64(abs(v - pre))
			pre = v
		}
		ans += int64(min(min(st, v)-1, mi*2-2))
		if mx < x {
			ans += int64(min(x-max(st, v), (x-mx)*2))
		}
		Fprintln(out, ans)
	}
}

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

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

package main

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

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

	var T, n, x int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &x)
		a := make([]int, n)
		mi, mx := int(1e9), 0
		ans := int64(0)
		for i := range a {
			Fscan(in, &a[i])
			mi = min(mi, a[i])
			mx = max(mx, a[i])
			if i > 0 {
				ans += int64(abs(a[i] - a[i-1]))
			}
		}
		ans += int64(min(min(a[0], a[n-1])-1, mi*2-2))
		if mx < x {
			ans += int64(min(x-max(a[0], a[n-1]), (x-mx)*2))
		}
		Fprintln(out, ans)
	}
}

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

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

package main

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

const mod = 998244353

func solve(in *bufio.Reader, out *bufio.Writer) {
	defer out.Flush()
	var n int
	var x int64
	fmt.Fscan(in,&n,&x)
	arr := make([]int64,n)
	for i := 0;i < n;i++{
		fmt.Fscan(in,&arr[i])
	}
	cur := int64(0)
	for i := 1;i < n;i++{
		cur += abs(arr[i] - arr[i - 1])
	}
	ans := int64(1e18)
	for z := 0; z < 2; z++ {
		mn := arr[0] - 1
		ans = min(ans,cur + abs(arr[0] - x) + x - 1)
		for i := 0;i < n - 1;i++{
			ans = min(ans,cur - abs(arr[i + 1] - arr[i]) + abs(arr[i + 1] - x) + abs(arr[i] - 1) + (x - 1))
			ans = min(ans,cur - abs(arr[i + 1] - arr[i]) + mn + abs(arr[i] - x) + abs(arr[i + 1] - x))
			mn = min(mn,0 - abs(arr[i + 1] - arr[i]) + abs(arr[i] - 1) + abs(arr[i + 1] - 1))
		}
		ans = min(ans,cur + mn + abs(arr[n - 1] - x))
		reverse(arr)
	}
	fmt.Fprintln(out,ans)
}
func reverse(arr []int64){
	n := len(arr)
	for i := 0;i < len(arr) / 2;i++{
		arr[i],arr[n - i - 1] = arr[n - i - 1],arr[i]
	}
}

func abs(a int64)int64{
	if a > 0{
		return a
	}
	return -a
}
func sum(a,b int64)int64{
	return (b + a) * (b - a + 1) / 2
}

func max(a,b int64)int64{
	if a > b{
		return a
	}
	return b
}
func min(a,b int64)int64{
	if a > b{
		return b
	}
	return a
}
func gcd(a,b int)int{
	if a % b == 0{
		return b
	}
	return gcd(b,a%b)
}

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

package main

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

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

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

	t, n, x := 0, 0, int64(0)
	Fscan(in, &t)
	for ; t > 0; t-- {
		Fscan(in, &n, &x)
		a := make([]int64, n)
		mx, mi := int64(-1e10), int64(1e10)
		ans := int64(0)
		for i := range a {
			Fscan(in, &a[i])
			mx = max(mx, a[i])
			mi = min(mi, a[i])
			if i > 0 {
				ans += abs(a[i] - a[i-1])
			}
		}
		ans += min((mi-1)*2, min(a[0]-1, a[n-1]-1))
		mx = min(mx, x)
		ans += min((x-mx)*2, min(max(0, x-a[0]), max(0, x-a[n-1])))
		Fprintln(out, ans)
	}
}

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

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

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

package main

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

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

func slove() {
	defer w.Flush()
	var n int
	var x int64
	fmt.Fscan(r, &n, &x)
	nums := make([]int64, n)
	var ans int64
	var max_v, min_v int64
	min_v = math.MaxInt64
	for i := range nums {
		fmt.Fscan(r,&nums[i])
		if i > 0 {
			ans += abs(nums[i] - nums[i-1])
		}
		if nums[i] > max_v {
			max_v = nums[i]
		}
		if nums[i] < min_v {
			min_v = nums[i]
		}
	}
	if x > max_v {
		ans += min((x-max_v) * 2, min(x-nums[0], x-nums[n-1]))
	}
	if min_v > 1 {
		ans += min((min_v - 1) * 2, min(nums[0]-1, nums[n-1]-1))
	}
	fmt.Fprintln(w, ans)
}

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

func main() {
	var n int
	fmt.Fscan(r, &n)
	for i:=0;i<n;i++ {
		slove()
	}
}

package main

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

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

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

	var t int
	Fscan(in, &t)

	for tt := 0; tt < t; tt++ {
		var n, x int64
		Fscan(in, &n, &x)
		a := make([]int64, n+x)
		ans := L0
		ma := L0
		mi := int64(math.MaxInt64)

		for i := L0; i < n; i++ {
			Fscan(in, &a[i])
			if a[i] > ma {
				ma = a[i]
			}
			if a[i] < mi {
				mi = a[i]
			}
			if i > 0 {
				ans += abs(int64(a[i] - a[i-1]))
			}
		}

		ans += int64(Min(Min(a[0]-L1, a[n-1]-L1), (mi-1)*2))

		if x > ma {
			ans += int64(Min(Min(x-a[0], x-a[n-1]), (x-ma)*2))
		}

		Fprintln(out, ans)
	}
}

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

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

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

package main

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

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

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

	var t int
	Fscan(in, &t)

	for tt := 0; tt < t; tt++ {
		var n, x int64
		Fscan(in, &n, &x)
		a := make([]int64, n+x)
		ans := L0
		ma := L0
		mi := int64(math.MaxInt64)

		for i := L0; i < n; i++ {
			Fscan(in, &a[i])
			if a[i] > ma {
				ma = a[i]
			}
			if a[i] < mi {
				mi = a[i]
			}
			if i > 0 {
				ans += abs(int64(a[i] - a[i-1]))
			}
		}

		ans += int64(Min(Min(a[0]-L1, a[n-1]-L1), (mi-1)*2))

		if x > ma {
			ans += int64(Min(x-a[0], x-a[n-1]))
		}

		Fprintln(out, ans)
	}
}

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

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

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

package main

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

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

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

	var t int
	Fscan(in, &t)

	for tt := 0; tt < t; tt++ {
		var n, x int64
		Fscan(in, &n, &x)
		a := make([]int64, n+x)
		ans := L0
		ma := L0
		mi := int64(math.MaxInt)

		for i := L0; i < n; i++ {
			Fscan(in, &a[i])
			if a[i] > ma {
				ma = a[i]
			}
			if a[i] < mi {
				mi = a[i]
			}
			if i > 0 {
				ans += abs(int64(a[i] - a[i-1]))
			}
		}

		ans += int64(Min(Min(a[0]-L1, a[n-1]-L1), (mi-1)*2))

		if x > ma {
			ans += int64(Min(x-a[0], x-a[n-1]))
		}

		Fprintln(out, ans)
	}
}

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

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

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

package main

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

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

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

	var t int
	Fscan(in, &t)

	for tt := 0; tt < t; tt++ {
		var n, x int
		Fscan(in, &n, &x)
		a := make([]int, n+x)
		ans := L0
		ma := 0
		mi := math.MaxInt

		for i := 0; i < n; i++ {
			Fscan(in, &a[i])
			if a[i] > ma {
				ma = a[i]
			}
			if a[i] < mi {
				mi = a[i]
			}
			if i > 0 {
				ans += abs(int64(a[i] - a[i-1]))
			}
		}

		ans += int64(Min(Min(a[0]-1, a[n-1]-1), int(mi-1)*2))

		if x > ma {
			ans += int64(Min(x-a[0], x-a[n-1]))
		}

		Fprintln(out, ans)
	}
}

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

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

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

package main

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

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

func main() {
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		var n, x int
		fmt.Fscan(in, &n, &x)
		arr := make([]int, n)
		minn, maxn := math.MaxInt32, math.MinInt32
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
			minn = min(arr[i], minn)
			maxn = max(arr[i], maxn)
		}
		cur := 0

		if n == 1 {
			if arr[0] >= 1 && arr[0] <= x {
				cur = x - 1
			} else {
				cur = arr[0] - 1
			}
		} else {
			for i := 0; i < n-1; i++ {
				cur += abs(arr[i] - arr[i+1])
			}
			if minn <= 1 && x <= maxn {

			}
			if 1 < minn {
				num1 := abs(min(arr[0], arr[n-1]) - 1)
				num2 := math.MaxInt32
				for i := 0; i < n-1; i++ {
					num2 = min(num2, (min(arr[i], arr[i+1])-1)*2)
				}
				cur += min(num1, num2)
			}
			if x > maxn {
				num1 := abs(x - max(arr[0], arr[n-1]))
				num2 := math.MaxInt32
				for i := 0; i < n-1; i++ {
					num2 = min(num2, (x-max(arr[i], arr[i+1]))*2)
				}
				cur += min(num1, num2)
			}
		}

		fmt.Fprintln(out, cur)
	}
}

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

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

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

package main

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

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

func main() {
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		var n, x int
		fmt.Fscan(in, &n, &x)
		arr := make([]int, n)
		minn, maxn := math.MaxInt32, math.MinInt32
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
			minn = min(arr[i], minn)
			maxn = max(arr[i], maxn)
		}
		cur := 0

		if n == 1 {
			if arr[0] >= 1 && arr[0] <= x {
				cur = x - 1
			} else {
				cur = arr[0] - 1
			}
		} else {
			for i := 0; i < n-1; i++ {
				cur += abs(arr[i] - arr[i+1])
			}
			if minn <= 1 && x <= maxn {

			}
			if 1 < minn {
				num1 := abs(min(arr[0], arr[n-1]) - 1)
				num2 := math.MaxInt32
				for i := 0; i < n-1; i++ {
					num2 = min(num2, (min(arr[i], arr[i+1])-1)*2)
				}
				cur += min(num1, num2)
			}
			if x > maxn {
				num1 := abs(x - max(arr[0], arr[n-1]))
				num2 := math.MaxInt32
				for i := 0; i < n-1; i++ {
					num2 = min(num2, (x-min(arr[i], arr[i+1]))*2)
				}
				cur += min(num1, num2)
			}
		}

		fmt.Fprintln(out, cur)
	}
}

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

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

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

package main

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

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

func main() {
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		var n, x int
		fmt.Fscan(in, &n, &x)
		arr := make([]int, n)
		minn, maxn := math.MaxInt32, math.MinInt32
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
			minn = min(arr[i], minn)
			maxn = max(arr[i], maxn)
		}
		cur := 0
		for i := 0; i < n-1; i++ {
			cur += abs(arr[i] - arr[i+1])
		}
		if minn <= 1 && x <= maxn {

		}
		if 1 < minn {
			num1 := abs(min(arr[0], arr[n-1]) - 1)
			num2 := math.MaxInt32
			for i := 0; i < n-1; i++ {
				num2 = min(num2, (min(arr[i], arr[i+1])-1)*2)
			}
			cur += min(num1, num2)
		}
		if x > maxn {
			num1 := abs(x - max(arr[0], arr[n-1]))
			num2 := math.MaxInt32
			for i := 0; i < n-1; i++ {
				num2 = min(num2, (x-min(arr[i], arr[i+1]))*2)
			}
			cur += min(num1, num2)
		}
		fmt.Fprintln(out, cur)
	}
}

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

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

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

package main

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

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

func main() {
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		var n, x int
		fmt.Fscan(in, &n, &x)
		arr := make([]int, n)
		minn, maxn := math.MaxInt32, math.MinInt32
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
			minn = min(arr[i], minn)
			maxn = max(arr[i], maxn)
		}
		cur := 0
		for i := 0; i < n-1; i++ {
			cur += abs(arr[i] - arr[i+1])
		}
		if minn <= 1 && x <= maxn {

		}
		if 1 < minn {
			num1 := min(arr[0], arr[n-1]) - 1
			num2 := math.MaxInt32
			for i := 0; i < n-1; i++ {
				num2 = min(num2, (min(arr[i], arr[i+1])-1)*2)
			}
			cur += min(num1, num2)
		}
		if x > maxn {
			num1 := x - max(arr[0], arr[n-1])
			num2 := math.MaxInt32
			for i := 0; i < n-1; i++ {
				num2 = min(num2, (x-min(arr[i], arr[i+1]))*2)
			}
			cur += min(num1, num2)
		}
		fmt.Fprintln(out, cur)
	}
}

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

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

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

package main

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

const MOD int64 = 1000000007

var n, x int

var a []int

// ByAge implements sort.Interface based on the Age field.
type ByV []*pair

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

func main() {

	read := bufio.NewReader(os.Stdin)

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

	for i := 0; i < t; i++ {

		fmt.Fscan(read, &n, &x)

		a = make([]int, n)

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

		solve()
	}
}

type pair struct {
	v int
	idx []int
}

//a b c d

//20 12 -- 4
//4  2  -- 4

func solve()  {

	var min int = 1e9
	var max int = 0

	var sum int64

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

	if x < min {

		s := do(1, x)

		sum += s

		fmt.Println(sum)

	} else if x >= min && x <= max {

		if min == 1 {
			fmt.Println(sum)

		} else if min == 2 {
			aa := abs(1 - a[0])
			bb := abs(1 - a[n - 1])
			cc := 2

			if cc < bb && cc < aa {
				sum +=2
			} else {
				sum += int64(minf(aa, bb))
			}

			fmt.Println(sum)

		} else {
			//1 .... min - 1
			var m int = 1e9

			if abs(1 - a[0]) < m {
				m = abs(1 - a[0])
			}
			if abs(min - 1 - a[0]) < m {
				m = abs(min - 1 - a[0])
			}
			if abs(1 - a[n - 1]) < m {
				m = abs(1 - a[n - 1])
			}
			if abs(min - 1 - a[n - 1]) < m {
				m = abs(min - 1 - a[n - 1])
			}

			if abs(1 - min) + 1 < m {
				m = abs(1 - min) + 1
			}

			if abs(min) < m {
				m = abs(min)
			}

			//1  .... min - 1
			//min     min + 1

			sum += int64(m)
			sum += int64(min - 2)

			fmt.Println(sum)
		}
	} else {
		var m int = 1e9

		if abs(max + 1 - a[0]) < m {
			m = abs(max + 1 - a[0])
		}
		if abs(x - a[0]) < m {
			m = abs(x - a[0])
		}
		if abs(max + 1 - a[n - 1]) < m {
			m = abs(max + 1 - a[n - 1])
		}
		if abs(x - a[n - 1]) < m {
			m = abs(x - a[n - 1])
		}

		if abs(x - max) + 1 < m {
			m = abs(x - max) + 1
		}

		if abs(max - 1 - x) < m {
			m = abs(max - 1 - x)
		}

		//max - 1        max
		//max + 1   ..... x

		sum += int64(m)
		sum += int64(x - max - 1)

		if min == 1 {

			fmt.Println(sum)

		} else if min == 2 {

			aa := abs(1 - a[0])
			bb := abs(1 - a[n - 1])
			cc := 2

			if cc < bb && cc < aa {
				sum +=2
			} else {
				sum += int64(minf(aa, bb))
			}

			fmt.Println(sum)

		} else {

			var m int = 1e9

			if abs(1 - a[0]) < m {
				m = abs(1 - a[0])
			}
			if abs(min - 1 - a[0]) < m {
				m = abs(min - 1 - a[0])
			}
			if abs(1 - a[n - 1]) < m {
				m = abs(1 - a[n - 1])
			}
			if abs(min - 1 - a[n - 1]) < m {
				m = abs(min - 1 - a[n - 1])
			}

			if abs(1 - min) + 1 < m {
				m = abs(1 - min) + 1
			}

			if abs(min) < m {
				m = abs(min)
			}

			sum += int64(m)
			sum += int64(min - 2)

			fmt.Println(sum)

		}

	}
}

func do(l, r int) int64  {
	var m int = 1e9
	var k int

	var s int64

	for i := 0; i < n - 1; i++ {
		l := a[i]
		r := a[i + 1]

		temp := abs(l - 1) + abs(x - r)

		if temp < m {
			m = temp
			k = abs(r - l)
		}
	}

	if abs(l - a[0]) < m {
		m = abs(l - a[0])
		k = 0
	}
	if abs(r - a[0]) < m {
		m = abs(r - a[0])
		k = 0
	}
	if abs(l - a[n-1]) < m {
		m = abs(l - a[n-1])
		k = 0
	}
	if abs(r - a[n - 1]) < m {
		m = abs(r- a[n-1])
		k = 0
	}

	s -= int64(k)
	s += int64(m)
	s += int64(r - l)

	return s
}

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

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

package main

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

const MOD int64 = 1000000007

var n, x int

var a []int

// ByAge implements sort.Interface based on the Age field.
type ByV []*pair

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

func main() {

	read := bufio.NewReader(os.Stdin)

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

	for i := 0; i < t; i++ {

		fmt.Fscan(read, &n, &x)

		a = make([]int, n)

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

		solve()
	}
}

type pair struct {
	v int
	idx []int
}

//a b c d

//20 12 -- 4
//4  2  -- 4

func solve()  {

	var min int = 1e9
	var max int = 0

	var sum int64

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

	if x < min {

		s := do(1, x)

		sum += s

		fmt.Println(sum)

	} else if x >= min && x <= max {

		if min == 1 {
			fmt.Println(sum)

		} else if min == 2 {
			aa := abs(1 - a[0])
			bb := abs(1 - a[n - 1])
			cc := 2

			if cc < bb && cc < aa {
				sum +=2
			} else {
				sum += int64(minf(aa, bb))
			}

			fmt.Println(sum)

		} else {
			//1 .... min - 1
			var m int = 1e9

			if abs(1 - a[0]) < m {
				m = abs(1 - a[0])
			}
			if abs(min - 1 - a[0]) < m {
				m = abs(min - 1 - a[0])
			}
			if abs(1 - a[n - 1]) < m {
				m = abs(1 - a[n - 1])
			}
			if abs(min - 1 - a[n - 1]) < m {
				m = abs(min - 1 - a[n - 1])
			}

			if abs(1 - min) + 1 < m {
				m = abs(1 - min) + 1
			}

			sum += int64(m)
			sum += int64(min - 2)

			fmt.Println(sum)
		}
	} else {
		var m int = 1e9

		if abs(max + 1 - a[0]) < m {
			m = abs(max + 1 - a[0])
		}
		if abs(x - a[0]) < m {
			m = abs(x - a[0])
		}
		if abs(max + 1 - a[n - 1]) < m {
			m = abs(max + 1 - a[n - 1])
		}
		if abs(x - a[n - 1]) < m {
			m = abs(x - a[n - 1])
		}

		if abs(x - max) + 1 < m {
			m = abs(x - max) + 1
		}

		sum += int64(m)
		sum += int64(x - max - 1)

		if min == 1 {

			fmt.Println(sum)

		} else if min == 2 {

			aa := abs(1 - a[0])
			bb := abs(1 - a[n - 1])
			cc := 2

			if cc < bb && cc < aa {
				sum +=2
			} else {
				sum += int64(minf(aa, bb))
			}

			fmt.Println(sum)

		} else {

			var m int = 1e9

			if abs(1 - a[0]) < m {
				m = abs(1 - a[0])
			}
			if abs(min - 1 - a[0]) < m {
				m = abs(min - 1 - a[0])
			}
			if abs(1 - a[n - 1]) < m {
				m = abs(1 - a[n - 1])
			}
			if abs(min - 1 - a[n - 1]) < m {
				m = abs(min - 1 - a[n - 1])
			}

			if abs(1 - min) + 1 < m {
				m = abs(1 - min) + 1
			}

			sum += int64(m)
			sum += int64(min - 2)

			fmt.Println(sum)

		}

	}
}

func do(l, r int) int64  {
	var m int = 1e9
	var k int

	var s int64

	for i := 0; i < n - 1; i++ {
		l := a[i]
		r := a[i + 1]

		temp := abs(l - 1) + abs(x - r)

		if temp < m {
			m = temp
			k = abs(r - l)
		}
	}

	if abs(l - a[0]) < m {
		m = abs(l - a[0])
		k = 0
	}
	if abs(r - a[0]) < m {
		m = abs(r - a[0])
		k = 0
	}
	if abs(l - a[n-1]) < m {
		m = abs(l - a[n-1])
		k = 0
	}
	if abs(r - a[n - 1]) < m {
		m = abs(r- a[n-1])
		k = 0
	}

	s -= int64(k)
	s += int64(m)
	s += int64(r - l)

	return s
}

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

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

package main

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

const MOD int64 = 1000000007

var n, x int

var a []int

// ByAge implements sort.Interface based on the Age field.
type ByV []*pair

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

func main() {

	read := bufio.NewReader(os.Stdin)

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

	for i := 0; i < t; i++ {

		fmt.Fscan(read, &n, &x)

		a = make([]int, n)

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

		solve()
	}
}

type pair struct {
	v int
	idx []int
}

//a b c d

//20 12 -- 4
//4  2  -- 4

func solve()  {

	var min int = 1e9
	var max int = 0

	var sum int64

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

	if x < min {

		s := do(1, x)

		sum += s

		fmt.Println(sum)

	} else if x >= min && x <= max {

		if min == 1 {
			fmt.Println(sum)

		} else if min == 2 {
			aa := abs(1 - a[0])
			bb := abs(1 - a[n - 1])
			cc := 1

			if cc < bb && cc < aa {
				sum +=1
			} else {
				sum += int64(minf(aa, bb))
			}

			fmt.Println(sum)

		} else {
			//1 .... min - 1
			var m int = 1e9

			if abs(1 - a[0]) < m {
				m = abs(1 - a[0])
			}
			if abs(min - 1 - a[0]) < m {
				m = abs(min - 1 - a[0])
			}
			if abs(1 - a[n - 1]) < m {
				m = abs(1 - a[n - 1])
			}
			if abs(min - 1 - a[n - 1]) < m {
				m = abs(min - 1 - a[n - 1])
			}

			if abs(1 - min) + 1 < m {
				m = abs(1 - min) + 1
			}

			sum += int64(m)
			sum += int64(min - 2)

			fmt.Println(sum)
		}
	} else {
		var m int = 1e9

		if abs(max + 1 - a[0]) < m {
			m = abs(max + 1 - a[0])
		}
		if abs(x - a[0]) < m {
			m = abs(x - a[0])
		}
		if abs(max + 1 - a[n - 1]) < m {
			m = abs(max + 1 - a[n - 1])
		}
		if abs(x - a[n - 1]) < m {
			m = abs(x - a[n - 1])
		}

		if abs(x - max) + 1 < m {
			m = abs(x - max) + 1
		}

		sum += int64(m)
		sum += int64(x - max - 1)

		if min == 1 {

			fmt.Println(sum)

		} else if min == 2 {

			aa := abs(1 - a[0])
			bb := abs(1 - a[n - 1])
			cc := 1

			if cc < bb && cc < aa {
				sum +=1
			} else {
				sum += int64(minf(aa, bb))
			}

			fmt.Println(sum)

		} else {

			var m int = 1e9

			if abs(1 - a[0]) < m {
				m = abs(1 - a[0])
			}
			if abs(min - 1 - a[0]) < m {
				m = abs(min - 1 - a[0])
			}
			if abs(1 - a[n - 1]) < m {
				m = abs(1 - a[n - 1])
			}
			if abs(min - 1 - a[n - 1]) < m {
				m = abs(min - 1 - a[n - 1])
			}

			if abs(1 - min) + 1 < m {
				m = abs(1 - min) + 1
			}

			sum += int64(m)
			sum += int64(min - 2)

			fmt.Println(sum)

		}

	}
}

func do(l, r int) int64  {
	var m int = 1e9
	var k int

	var s int64

	for i := 0; i < n - 1; i++ {
		l := a[i]
		r := a[i + 1]

		temp := abs(l - 1) + abs(x - r)

		if temp < m {
			m = temp
			k = abs(r - l)
		}
	}

	if abs(l - a[0]) < m {
		m = abs(l - a[0])
		k = 0
	}
	if abs(r - a[0]) < m {
		m = abs(r - a[0])
		k = 0
	}
	if abs(l - a[n-1]) < m {
		m = abs(l - a[n-1])
		k = 0
	}
	if abs(r - a[n - 1]) < m {
		m = abs(r- a[n-1])
		k = 0
	}

	s -= int64(k)
	s += int64(m)
	s += int64(r - l)

	return s
}

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

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

package main

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

const MOD int64 = 1000000007

var n, x int

var a []int

// ByAge implements sort.Interface based on the Age field.
type ByV []*pair

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

func main() {

	read := bufio.NewReader(os.Stdin)

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

	for i := 0; i < t; i++ {

		fmt.Fscan(read, &n, &x)

		a = make([]int, n)

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

		solve()
	}
}

type pair struct {
	v int
	idx []int
}

//a b c d

//20 12 -- 4
//4  2  -- 4

func solve()  {

	var min int = 1e9
	var max int = 0

	var sum int64

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

	if x < min {

		s := do(1, x)

		sum += s

		fmt.Println(sum)

	} else if x >= min && x <= max {

		if min == 1 {
			fmt.Println(sum)

		} else if min == 2 {
			v := minf(abs(1 - a[0]), minf(abs(1 - a[n - 1]), 2))

			sum += int64(v)

			fmt.Println(sum)

		} else {
			//1 .... min - 1
			var m int = 1e9

			if abs(1 - a[0]) < m {
				m = abs(1 - a[0])
			}
			if abs(min - 1 - a[0]) < m {
				m = abs(min - 1 - a[0])
			}
			if abs(1 - a[n - 1]) < m {
				m = abs(1 - a[n - 1])
			}
			if abs(min - 1 - a[n - 1]) < m {
				m = abs(min - 1 - a[n - 1])
			}

			if abs(1 - min) + 2 < m {
				m = abs(1 - min) + 2
			}

			sum += int64(m)
			sum += int64(min - 2)

			fmt.Println(sum)
		}
	} else {
		var m int = 1e9

		if abs(max + 1 - a[0]) < m {
			m = abs(max + 1 - a[0])
		}
		if abs(x - a[0]) < m {
			m = abs(x - a[0])
		}
		if abs(max + 1 - a[n - 1]) < m {
			m = abs(max + 1 - a[n - 1])
		}
		if abs(x - a[n - 1]) < m {
			m = abs(x - a[n - 1])
		}

		if abs(x - max) + 2 < m {
			m = abs(x - max) + 2
		}

		sum += int64(m)
		sum += int64(x - max - 1)

		if min == 1 {

			fmt.Println(sum)

		} else if min == 2 {

			v := minf(abs(1 - a[0]), minf(abs(1 - a[n - 1]), 2))

			sum += int64(v)

			fmt.Println(sum)

		} else {

			var m int = 1e9

			if abs(1 - a[0]) < m {
				m = abs(1 - a[0])
			}
			if abs(min - 1 - a[0]) < m {
				m = abs(min - 1 - a[0])
			}
			if abs(1 - a[n - 1]) < m {
				m = abs(1 - a[n - 1])
			}
			if abs(min - 1 - a[n - 1]) < m {
				m = abs(min - 1 - a[n - 1])
			}

			if abs(1 - min) + 2 < m {
				m = abs(1 - min) + 2
			}

			sum += int64(m)
			sum += int64(min - 2)

			fmt.Println(sum)

		}

	}
}

func do(l, r int) int64  {
	var m int = 1e9
	var k int

	var s int64

	for i := 0; i < n - 1; i++ {
		l := a[i]
		r := a[i + 1]

		temp := abs(l - 1) + abs(x - r)

		if temp < m {
			m = temp
			k = abs(r - l)
		}
	}

	if abs(l - a[0]) < m {
		m = abs(l - a[0])
		k = 0
	}
	if abs(r - a[0]) < m {
		m = abs(r - a[0])
		k = 0
	}
	if abs(l - a[n-1]) < m {
		m = abs(l - a[n-1])
		k = 0
	}
	if abs(r - a[n - 1]) < m {
		m = abs(r- a[n-1])
		k = 0
	}

	s -= int64(k)
	s += int64(m)
	s += int64(r - l)

	return s
}

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

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

package main

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

const mod = 998244353

func solve(in *bufio.Reader, out *bufio.Writer) {
	defer out.Flush()
	var n int
	var x int64
	fmt.Fscan(in,&n,&x)
	arr := make([]int64,n)
	for i := 0;i < n;i++{
		fmt.Fscan(in,&arr[i])
	}
	cur := int64(0)
	for i := 1;i < n;i++{
		cur += abs(arr[i] - arr[i - 1])
	}
	ans := int64(1e18)
	for z := 0; z < 2; z++ {
		mn := arr[0] - 1
		ans = min(ans,cur + abs(arr[0] - x) + x - 1)
		for i := 0;i < n - 1;i++{
			ans = min(ans,cur - abs(arr[i + 1] - arr[i]) + abs(arr[i + 1] - x) + abs(arr[i] - 1) + (x - 1))
			ans = min(ans,cur - abs(arr[i + 1] - arr[i]) + mn + abs(arr[i] - x) + abs(arr[i + 1] - x))
			mn = min(mn,cur - abs(arr[i + 1] - arr[i]) + abs(arr[i] - 1) + abs(arr[i + 1] - 1))
		}
		ans = min(ans,cur + mn + abs(arr[n - 1] - x))
		reverse(arr)
	}
	fmt.Fprintln(out,ans)
}
func reverse(arr []int64){
	n := len(arr)
	for i := 0;i < len(arr) / 2;i++{
		arr[i],arr[n - i - 1] = arr[n - i - 1],arr[i]
	}
}

func abs(a int64)int64{
	if a > 0{
		return a
	}
	return -a
}
func sum(a,b int64)int64{
	return (b + a) * (b - a + 1) / 2
}

func max(a,b int64)int64{
	if a > b{
		return a
	}
	return b
}
func min(a,b int64)int64{
	if a > b{
		return b
	}
	return a
}
func gcd(a,b int)int{
	if a % b == 0{
		return b
	}
	return gcd(b,a%b)
}

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


package main

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

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

	var T, n, m, v, w int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &m)
		es := []int{}
		vis := make([]bool, 3*n+1)
		for i := 1; i <= m; i++ {
			Fscan(in, &v, &w)
			if len(es) < n && !vis[v] && !vis[w] {
				vis[v] = true
				vis[w] = true
				es = append(es, i)
			}
		}
		if len(es) == n {
			Fprintln(out, "Matching")
			for _, i := range es {
				Fprint(out, i, " ")
			}
		} else {
			Fprintln(out, "IndSet")
			for i, c := 1, 0; i <= 3*n; i++ {
				if !vis[i] {
					Fprint(out, i, " ")
					if c++; c == n {
						break
					}
				}
			}
		}
		Fprintln(out)
	}
}

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

package main

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

var edges [100000]int
var vertices [300000]bool

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

	writer := bytes.NewBuffer(make([]byte, 0, 10000000))

	var T int
	fmt.Fscanf(reader, "%d\n", &T)
	for i := 0;i < T;i ++ {
		var n, m int
		fmt.Fscanf(reader, "%d %d\n", &n, &m)
		for j := 0;j < 3 * n;j ++ {
			vertices[j] = false
		}
		count := 0
		for j := 0;j < m;j ++ {
			var u, v int
			fmt.Fscanf(reader, "%d %d\n", &u, &v)
			u, v = u - 1, v - 1
			if !vertices[u] && !vertices[v] {
				if count < n {
					edges[count] = j + 1
				}
				count ++
				vertices[u], vertices[v] = true, true
			}
		}
		if count >= n {
			fmt.Fprintf(writer, "Matching\n")
			for i := 0;i < n;i ++ {
				fmt.Fprintf(writer, "%d ", edges[i])
			}
		} else {
			fmt.Fprintf(writer, "IndSet\n")
			for i, c := 0, 0; c < n;i ++ {
				if !vertices[i] {
					fmt.Fprintf(writer, "%d ", i + 1)
					c ++
				}
			}
		}
		fmt.Fprintf(writer, "\n")
	}
	writer.WriteTo(os.Stdout)
}

package main

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

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

	var T, n, m, v, w int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &m)
		var e []interface{}
		vis := make([]bool, 3*n+1)
		for i := 1; i <= m; i++ {
			if Fscan(in, &v, &w); len(e) < n && !vis[v] && !vis[w] {
				vis[v] = true
				vis[w] = true
				e = append(e, i)
			}
		}
		if len(e) == n {
			Fprintln(out, "Matching")
			Fprintln(out, e...)
		} else {
			Fprintln(out, "IndSet")
			for i, c := 1, 0; i <= 3*n; i++ {
				if !vis[i] {
					Fprint(out, i, " ")
					if c++; c == n {
						break
					}
				}
			}
		}
		Fprintln(out)
	}
}

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

package main

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

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

	var T int
	fmt.Fscanf(reader, "%d\n", &T)
	for i := 0;i < T;i ++ {
		var n, m int
		fmt.Fscanf(reader, "%d %d\n", &n, &m)
		vertices := make([]bool, 3 * n)
		count := 0
		edges := make([]int, 0, n)
		for j := 0;j < m;j ++ {
			var u, v int
			fmt.Fscanf(reader, "%d %d\n", &u, &v)
			u, v = u - 1, v - 1
			if !vertices[u] && !vertices[v] && count < n {
				count ++
				vertices[u], vertices[v] = true, true
				edges = append(edges, j + 1)
			}
		}
		if count == n {
			fmt.Printf("Matching\n")
			for i := 0;i < n;i ++ {
				fmt.Printf("%d ", edges[i])
			}
		} else {
			fmt.Printf("IndSet\n")
			for i, c := 0, 0; c < n;i ++ {
				if !vertices[i] {
					fmt.Printf("%d ", i)
					c ++
				}
			}
		}
		fmt.Printf("\n")
	}
}

package main

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

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

	var T, n, m, v, w int
o:
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &m)
		es := []int{}
		vis := make([]bool, 3*n+1)
		for i := 1; i <= m; i++ {
			Fscan(in, &v, &w)
			if !vis[v] && !vis[w] {
				vis[v] = true
				vis[w] = true
				if es = append(es, i); len(es) == n {
					Fprintln(out, "Matching")
					for _, i := range es {
						Fprint(out, i, " ")
					}
					Fprintln(out)
					continue o
				}
			}
		}
		Fprintln(out, "IndSet")
		for i, c := 1, 0; i <= 3*n; i++ {
			if !vis[i] {
				Fprint(out, i, " ")
				if c++; c == n {
					Fprintln(out)
					break
				}
			}
		}
	}
}

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

package main

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

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

	var T int
	fmt.Fscanf(reader, "%d\n", &T)
	for i := 0;i < T;i ++ {
		var n, m int
		fmt.Fscanf(reader, "%d %d\n", &n, &m)
		vertices := make([]bool, 3 * n)
		count := 0
		edges := make([]int, 0, 2 * n)
		for j := 0;j < m;j ++ {
			var u, v int
			fmt.Fscanf(reader, "%d %d\n", &u, &v)
			u, v = u - 1, v - 1
			if !vertices[u] && !vertices[v] {
				count ++
				vertices[u], vertices[v] = true, true
				edges = append(edges, j + 1)
			}
		}
		if count >= n {
			fmt.Printf("Matching\n")
			for i := 0;i < n;i ++ {
				fmt.Printf("%d ", edges[i])
			}
		} else {
			fmt.Printf("IndSet\n")
			for i, c := 0, 0; c < n;i ++ {
				if !vertices[i] {
					fmt.Printf("%d ", i)
					c ++
				}
			}
		}
		fmt.Printf("\n")
	}
}