package main

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

func main() {
  reader := bufio.NewReader(os.Stdin)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()
  line, _ := reader.ReadString('\n')
  s := strings.TrimSpace(line)
  n := len(s)

  counts := make([][]int, 3)
  for i := range(counts) {
    counts[i] = make([]int, n + 1)
  }
  for i, ch := range s {
    x := int(ch) - 120
    counts[x][i + 1] = 1
    for x = 0; x < 3; x++ {
      counts[x][i + 1] += counts[x][i]
    }
  }

  var m, a, b int
  line, _ = reader.ReadString('\n')
  fmt.Sscanf(line, "%d", &m)
  for i := 0; i < m; i++ {
    line, _ = reader.ReadString('\n')
    fmt.Sscanf(line, "%d %d", &a, &b)
    if b - a + 1 < 3 {
      writer.WriteString("YES\n")
      continue
    }
    spanCounts := make([]int, 3)
    for x := range spanCounts {
      spanCounts[x] = counts[x][b] - counts[x][a - 1]
    }
    sort.Ints(spanCounts)
    gap := spanCounts[2] - spanCounts[0]
    if gap <= 1 {
      writer.WriteString("YES\n")
    } else {
      writer.WriteString("NO\n")
    }
  }
}

package main
import (
	"fmt"
	"bufio"
	"os"
	"strconv"
	//"sort"
)
func abs(a int) int{
	if a<0 {
		a*=(-1)
	}
	return a
}

func main() {
	var a [100001][3]int
	var c string
	//reader := bufio.NewReader(os.Stdin)
	var i,j,m,n,p,k,l,r,x,y,z int
	input:= bufio.NewScanner(os.Stdin)
	input.Split(bufio.ScanWords)
	output := bufio.NewWriter(os.Stdout)
	//fmt.Fscan(reader,&c)
	fmt.Scan(&c)
 	lenc := len(c)
 	for i=0;i<lenc;i++ {
 		for j=0;j<3;j++ {
 			a[i+1][j]=a[i][j]
 		}
 		a[i+1][c[i]-'x']++
 	}
 	input.Scan()
 	aa,_:=strconv.Atoi(input.Text())
 	m=aa
 	for i=1;i<=m;i++ {
 		input.Scan()
 		bb,_:=strconv.Atoi(input.Text())
 		l=bb
 		input.Scan()
 		cc,_:=strconv.Atoi(input.Text())
 		r=cc
 		if r-l+1<3 {
 			output.WriteString("YES\n")
 		} else {
 			x=a[r][0]-a[l-1][0]
 			y=a[r][1]-a[l-1][1]
 			z=a[r][2]-a[l-1][2]
 			p=x-y
 			p=abs(p)
 			k=y-z
 			k=abs(k)
 			n=x-z
 			n=abs(n)
 			if p<=1 && k<=1 && n<=1 {
 				output.WriteString("YES\n")
 			} else {
 				output.WriteString("NO\n")
 			}
 		}
 	}
 	output.Flush()
}

package main

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

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

    ScanInts := func(a... *int) {
        for i := 0; i < len(a); i++ {
            scanner.Scan()
            *(a[i]), _ = strconv.Atoi(scanner.Text())
        }
    }

    var s string
    fmt.Scan(&s)
    x := make([][3]int,len(s)+1)
    for i := 1; i <= len(s); i++ {
        for j := 0; j < 3; j++ { x[i][j] += x[i-1][j]}
        if s[i-1] == 'x' { x[i][0]++ }
        if s[i-1] == 'y' { x[i][1]++ }
        if s[i-1] == 'z' { x[i][2]++ }
    }

    check := func(l,r int) bool {
        if r-l < 2 { return true }
        max, min := -1000000000, 1000000000
        for i := 0; i < 3; i++ {
            a := x[r][i]-x[l-1][i]
            if max < a { max = a }
            if min > a { min = a }
        }
        if max-min <= 1 { return true }
        return false
    }

    var m,l,r int
    ScanInts(&m)
    for i := 0; i < m; i++ {
        ScanInts(&l,&r)
        if check(l,r) { writer.WriteString("YES\n") } else { writer.WriteString("NO\n") }
    }
    writer.Flush()
}

package main
import (
	"fmt"
	"bufio"
	"os"
	"strconv"
	//"sort"
)
func abs(a int) int{
	if a<0 {
		a*=(-1)
	}
	return a
}

func main() {
	var a [100001][3]int
	var c string
	reader := bufio.NewReader(os.Stdin)
	var i,j,m,n,p,k,l,r,x,y,z int
	input:= bufio.NewScanner(os.Stdin)
	input.Split(bufio.ScanWords)
	output := bufio.NewWriter(os.Stdout)
	fmt.Fscan(reader,&c)
	lenc := len(c)
 	for i=0;i<lenc;i++ {
 		for j=0;j<3;j++ {
 			a[i+1][j]=a[i][j]
 		}
 		a[i+1][c[i]-'x']++
 	}
 	input.Scan()
 	aa,_:=strconv.Atoi(input.Text())
 	m=aa
 	for i=1;i<=m;i++ {
 		input.Scan()
 		bb,_:=strconv.Atoi(input.Text())
 		l=bb
 		input.Scan()
 		cc,_:=strconv.Atoi(input.Text())
 		r=cc
 		if r-l+1<3 {
 			output.WriteString("YES\n")
 		} else {
 			x=a[r][0]-a[l-1][0]
 			y=a[r][1]-a[l-1][1]
 			z=a[r][2]-a[l-1][2]
 			p=x-y
 			p=abs(p)
 			k=y-z
 			k=abs(k)
 			n=x-z
 			n=abs(n)
 			if p<=1 && k<=1 && n<=1 {
 				output.WriteString("YES\n")
 			} else {
 				output.WriteString("NO\n")
 			}
 		}
 	}
 	output.Flush()
}

package main

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

func main() {
    reader := bufio.NewReader(os.Stdin)
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Split(bufio.ScanWords)
    writer := bufio.NewWriter(os.Stdout)

    ScanInts := func(a... *int) {
        for i := 0; i < len(a); i++ {
            scanner.Scan()
            *(a[i]), _ = strconv.Atoi(scanner.Text())
        }
    }

    var s string
    for len(s) == 0 { fmt.Fscan(reader,&s) }
    x := make([][3]int,len(s)+1)
    for i := 1; i <= len(s); i++ {
        for j := 0; j < 3; j++ { x[i][j] += x[i-1][j]}
        if s[i-1] == 'x' { x[i][0]++ }
        if s[i-1] == 'y' { x[i][1]++ }
        if s[i-1] == 'z' { x[i][2]++ }
    }

    check := func(l,r int) bool {
        if r-l < 2 { return true }
        max, min := -1000000000, 1000000000
        for i := 0; i < 3; i++ {
            a := x[r][i]-x[l-1][i]
            if max < a { max = a }
            if min > a { min = a }
        }
        if max-min <= 1 { return true }
        return false
    }

    var m,l,r int
    ScanInts(&m)
    for i := 0; i < m; i++ {
        ScanInts(&l,&r)
        if check(l,r) { writer.WriteString("YES\n") } else { writer.WriteString("NO\n") }
    }
    writer.Flush()
}

package main

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

func main() {
    reader := bufio.NewReader(os.Stdin)
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Split(bufio.ScanWords)
    writer := bufio.NewWriter(os.Stdout)

    ScanInts := func(a... *int) {
        for i := 0; i < len(a); i++ {
            scanner.Scan()
            *(a[i]), _ = strconv.Atoi(scanner.Text())
        }
    }

    var s string
    fmt.Fscan(reader,&s)
    x := make([][3]int,len(s)+1)
    for i := 1; i <= len(s); i++ {
        for j := 0; j < 3; j++ { x[i][j] += x[i-1][j]}
        if s[i-1] == 'x' { x[i][0]++ }
        if s[i-1] == 'y' { x[i][1]++ }
        if s[i-1] == 'z' { x[i][2]++ }
    }

    check := func(l,r int) bool {
        if r-l < 2 { return true }
        max, min := -1000000000, 1000000000
        for i := 0; i < 3; i++ {
            a := x[r][i]-x[l-1][i]
            if max < a { max = a }
            if min > a { min = a }
        }
        if max-min <= 1 { return true }
        return false
    }

    var m,l,r int
    ScanInts(&m)
    for i := 0; i < m; i++ {
        ScanInts(&l,&r)
        if check(l,r) { writer.WriteString("YES\n") } else { writer.WriteString("NO\n") }
    }
    writer.Flush()
}

package main

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

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

    ScanInts := func(a... *int) {
        for i := 0; i < len(a); i++ {
            scanner.Scan()
            *(a[i]), _ = strconv.Atoi(scanner.Text())
        }
    }

    scanner.Scan()
    s := scanner.Text()
    x := make([]int,len(s))
    for i := 2; i < len(s); i++ {
        x[i] += x[i-1]
        if (s[i] == s[i-1]) && (s[i] == s[i-2]) {
            x[i]++
        }
    }

    check := func(l,r int) bool {
        var c int
        if r > (l+1) {
            c = x[r]-x[l+1]
        }
        if c > 0 { return false }
        return true
    }

    var m,l,r int
    ScanInts(&m)
    for i := 0; i < m; i++ {
        ScanInts(&l,&r)
        if check(l-1,r-1) { writer.WriteString("YES\n") } else { writer.WriteString("NO\n") }
    }
    writer.Flush()
}

package main

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

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

    ScanInts := func(a... *int) {
        for i := 0; i < len(a); i++ {
            scanner.Scan()
            *(a[i]), _ = strconv.Atoi(scanner.Text())
        }
    }

    scanner.Scan()
    s := scanner.Text()
    x := make([][3]int,len(s)+1)
    for i := 1; i <= len(s); i++ {
        for j := 0; j < 3; j++ { x[i][j] += x[i-1][j]}
        if s[i-1] == 'x' { x[i][0]++ }
        if s[i-1] == 'y' { x[i][1]++ }
        if s[i-1] == 'z' { x[i][2]++ }
    }

    check := func(l,r int) bool {
        if r-l < 2 { return true }
        max, min := -1000000000, 1000000000
        for i := 0; i < 3; i++ {
            a := x[r][i]-x[l-1][i]
            if max < a { max = a }
            if min > a { min = a }
        }
        if max-min <= 1 { return true }
        return false
    }

    var m,l,r int
    ScanInts(&m)
    for i := 0; i < m; i++ {
        ScanInts(&l,&r)
        if check(l,r) { writer.WriteString("YES\n") } else { writer.WriteString("NO\n") }
    }
    writer.Flush()
}

package main

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

func main() {
  reader := bufio.NewReader(os.Stdin)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()
  line, _ := reader.ReadString('\n')
  s := strings.TrimSpace(line)
  n := len(s)

  counts := make([][]int, 3)
  for i := range(counts) {
    counts[i] = make([]int, n + 1)
  }
  for i, ch := range s {
    x := int(ch) - 120
    counts[x][i + 1] = 1
    for x = 0; x < 3; x++ {
      counts[x][i + 1] += counts[x][i]
    }
  }

  var m, a, b int
  line, _ = reader.ReadString('\n')
  fmt.Sscanf(line, "%d", &m)
  for i := 0; i < m; i++ {
    line, _ = reader.ReadString('\n')
    fmt.Sscanf(line, "%d %d", &a, &b)
    if b - a + 1 < 3 {
      writer.WriteString("YES\n")
      continue
    }
    spanCounts := make([]int, 3)
    for x := range spanCounts {
      spanCounts[x] = counts[x][a] - counts[x][b - 1]
    }
    sort.Ints(spanCounts)
    gap := spanCounts[2] - spanCounts[0]
    if gap <= 1 {
      writer.WriteString("YES\n")
    } else {
      writer.WriteString("NO\n")
    }
  }
}


package main
import (
	"fmt"
)
func main() {
	var ans int64
	ans=0
	var n,m,x,y,a int
	var w [1005]int
	fmt.Scan(&n,&m)
	for i:=1;i<=n;i++ {
		fmt.Scan(&w[i])
	}
	for i:=1;i<=m;i++ {
		fmt.Scan(&x,&y)
		if w[x]<w[y] {
			a=w[x]
		} else {
			a=w[y]
		}
		ans+=int64(a)
	}
	fmt.Println(ans)
}

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

package main

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

type Edge struct {
	from int
	to   int
}

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

func Solve(n, m int, v []int, edges []Edge) int64 {
	s := int64(0)
	for _, edge := range edges {
		s += int64(min(v[edge.from-1], v[edge.to-1]))
	}
	return s
}

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

	var n, m int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	v := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &v[i])
	}
	fmt.Fscanf(reader, "\n")
	edges := make([]Edge, m)
	for i := 0; i < m; i++ {
		fmt.Fscanf(reader, "%d %d\n", &edges[i].from, &edges[i].to)
	}

	fmt.Fprintf(writer, "%d\n", Solve(n, m, v, edges))
}

package main

import (
	"fmt"
	"sort"
)

func ReadInt64() int64 { /*{{{*/
	var ret int64
	_, err := fmt.Scan(&ret)
	if err != nil {
		return 0
	}
	return ret
} /*}}}*/

type pair struct {
	ind  int64
	cost int64
}

func addEdge(a, b int64, m map[int64][]int64) {
	if m[a] == nil {
		m[a] = []int64{b}
	} else {
		m[a] = append(m[a], b)
	}
}

func main() {
	n, m := ReadInt64(), ReadInt64()
	costList := []pair{}
	value := []int64{}
	edge := map[int64][]int64{}

	for i := int64(0); i < n; i++ { /*{{{*/
		a := ReadInt64()
		value = append(value, a)
		costList = append(costList, pair{
			ind:  i,
			cost: a,
		})
	} /*}}}*/
	for i := int64(0); i < m; i++ {
		a, b := ReadInt64(), ReadInt64()
		a--
		b--
		addEdge(a, b, edge)
		addEdge(b, a, edge)
	}
	ans := int64(0)
	sort.Slice(costList, func(i, j int) bool {
		return costList[i].cost > costList[j].cost
	})
	for _, p := range costList {
		for _, to := range edge[p.ind] {
			ans += value[to]
		}
		value[p.ind] = 0
	}
	fmt.Printf("%d\n", ans)
}

package main

import "fmt"

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

    var n,m,x,y int
    fmt.Scan(&n,&m)
    a := make([]int,n)
    for i := 0; i < n; i++ {
        fmt.Scan(&a[i])
    }
    ans := 0
    for i := 0; i < m; i++ {
        fmt.Scan(&x,&y)
        ans += min(a[x-1],a[y-1])
    }
    fmt.Println(ans)
}


package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scanf("%d", &n)
	switch n % 3 {
	case 0:
		fmt.Printf("%d %d %d", n-2, 1, 1)
	case 1:
		fmt.Printf("%d %d %d", n-3, 2, 1)
	case 2:
		fmt.Printf("%d %d %d", n-4, 2, 2)
	}
}

package main

import (
    "fmt"
)

var N int

func main() {
    fmt.Scanf("%d\n", &N)

    if N % 3 == 0 || N % 3 == 1 {
        fmt.Printf("%d 1 1\n", N-2)
    } else if N % 3 == 2 {
        fmt.Printf("%d 2 1\n", N-3)
    }
}

package main

import "fmt"

func main() {
	var n, a, b, c int
	fmt.Scan(&n)
	a, b, c = 1, 1, n-2
	if c%3 == 0 {
		a++
		c--
	}
	fmt.Printf("%v %v %v\n", a, b, c)
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scanf("%d ", &n)

	for a := 1; a < n; a++ {
		for b := 1; a+b < n; b++ {
			c := n - a - b
			if a%3 != 0 && b%3 != 0 && c%3 != 0 {
				fmt.Println(a, b, c)
				return
			}
		}
	}
	return
}

package main

import "fmt"

func main() {
	var number int64
	fmt.Scanf("%d", &number)

	var val int64 = 1
	number = number - 2

	mod := (number) % 3
	if mod == 0 {
		val++
		number = number - 1
	}
	fmt.Printf("%d %d %d", 1, val, number)
}

package main

import "fmt"

func main() {
    var n int
    fmt.Scan(&n)
    fmt.Printf("%d ", 1);
    n--;
    for k := 0; k < 100; k++ {
		if ((n - k) % 3 != 0 && k % 3 != 0 ) {
		    fmt.Printf("%d %d",n - k,k);
		    break;
		}
	}
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scanf("%d", &n)

	if n%3 == 2 {
		fmt.Printf("1 2 %d", n-3)
		return
	}
	fmt.Printf("1 1 %d", n-2)
}

package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scanln(&n)
	if n%3 == 2 {
		fmt.Println(n-3, 2, 1)
	} else {
		fmt.Println(n-2, 1, 1)
	}
}

package main

import (
	"fmt"
	"os"
)

func main() {
	var n int
	fmt.Scanln(&n)
	for i := n - 2; i >= 1; i-- {
		if i % 3 == 0 {
			continue
		}
		for j := n - i - 1; j >= 1; j--{
			if j % 3 == 0 {
				continue
			}
			k := n - i - j
			if k % 3 == 0 {
				continue
			}
			fmt.Println(i, j, k)
			os.Exit(0)
		}
	}
}

package main

import (
	"fmt"
)

func main() {
	var i int64
	fmt.Scanf("%d", &i)
	a := 1
	b := 1
	c := i - 2
	if c % 3 == 0 {
		c -= 1
		b += 1
	}
	fmt.Println(a, b, c)
}

package main

import (
	"fmt"
)

func main() {
	var n, a, b, c int
	fmt.Scanf("%d", &n)
	if n%3 != 0 {
		a = n / 3
		b = n / 3
		c = n / 3
		for n-(a+b+c) > 0 {
			if a%3 == 0 {
				a++
			} else if b%3 == 0 {
				b++
			} else {
				c++
			}
		}
		fmt.Printf("%d %d %d\n", a, b, c)
	} else {
		third := n / 3
		if third%3 == 0 {
			fmt.Printf("%d %d %d\n", third-1, third-1, third+2)
		} else {
			fmt.Printf("%d %d %d\n", third, third, third)
		}
	}
}

package main

import "fmt"

func main() {
	var number int64
	fmt.Scanf("%d", &number)

	var val int64 = 1
	number = number - 1

	mod := (number-1) % 3
	if mod == 0 {
		val++
		number = number - 1
	}
	fmt.Printf("%d %d %d", 1, val, number)
}

package main

import "fmt"

func main() {
	var number int64
	fmt.Scanf("%d", &number)

	val := number / 3
	mod := number % 3

	fmt.Printf("%d %d %d", val, val, val + mod)
}


package main

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

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

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

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

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

	return res
}

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

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

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

// DigitSum returns digit sum of a decimal number.
// DigitSum only accept a positive integer.
func DigitSum(n int) int {
	if n < 0 {
		return -1
	}

	res := 0

	for n > 0 {
		res += n % 10
		n /= 10
	}

	return res
}

// DigitNumOfDecimal returns digits number of n.
// n is non negative number.
func DigitNumOfDecimal(n int) int {
	res := 0

	for n > 0 {
		n /= 10
		res++
	}

	return res
}

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

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

	return s
}

// Kiriage returns Ceil(a/b)
// a >= 0, b > 0
func Kiriage(a, b int) int {
	return (a + (b - 1)) / b
}

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

	if e == 0 {
		return 1
	}

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

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

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

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

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

	return a
}

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

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

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

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

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

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

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

	return string(res)
}

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

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

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

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

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

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

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

var q int

func main() {
	q = ReadInt()

	for i := 0; i < q; i++ {
		n := ReadInt()
		A := ReadIntSlice(n)
		maxIds, minIds := make(map[int]int), make(map[int]int)

		// A[j]の最後尾のidxを記録
		for j := 0; j < n; j++ {
			maxIds[A[j]] = j
		}
		for j := n - 1; j >= 0; j-- {
			minIds[A[j]] = j
		}

		// A[j]についてuniq | sortする
		uniqs := []int{}
		for k := range maxIds {
			uniqs = append(uniqs, k)
		}
		sort.Sort(sort.IntSlice(uniqs))

		ans := 1
		tmpAns := 1
		for j := 0; j < len(uniqs)-1; j++ {
			l, r := uniqs[j], uniqs[j+1]
			if maxIds[l] < minIds[r] {
				tmpAns++
			} else {
				ChMax(&ans, tmpAns)
				tmpAns = 1
			}
		}
		ChMax(&ans, tmpAns)
		fmt.Println(len(uniqs) - ans)
	}
}

// MODはとったか？
// 遷移だけじゃなくて最後の最後でちゃんと取れよ？

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

package main

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

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

	var T, n, v int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		pos := make([][2]int, n+1)
		for i := 1; i <= n; i++ {
			Fscan(in, &v)
			if pos[v][0] == 0 {
				pos[v] = [2]int{i, i}
			} else {
				pos[v][1] = i
			}
		}
		var tot, cnt, mxCnt, prev int
		for i := 1; i <= n; i++ {
			if pos[i][0] == 0 {
				continue
			}
			tot++
			if prev == 0 || pos[i][0] < pos[prev][1] {
				cnt = 1
			} else {
				cnt++
			}
			if cnt > mxCnt {
				mxCnt = cnt
			}
			prev = i
		}
		Fprintln(out, tot-mxCnt)
	}
}

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

package main

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

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

func main() {
	defer out.Flush()
	inn.Buffer([]byte{}, 1000000)
	inn.Split(bufio.ScanWords)

	const INF = int(1e9)
	for tt := rd(); tt > 0; tt-- {
		n := rd()
		l, r, dp := make([]int, n), make([]int, n), make([]int, n)
		for i := 0; i < n; i++ {
			l[i] = INF
			r[i] = -INF
			dp[i] = 0
		}
		mp := map[int]int{}
		for i := 0; i < n; i++ {
			x := rd() - 1
			l[x] = min(l[x], i)
			r[x] = max(r[x], i)
			mp[x] = 1
		}
		v := []int{}
		for key := range mp {
			v = append(v, key)
		}
		sort.Ints(v)
		ans := n
		for i := len(v) - 1; i >= 0; i-- {
			if i == len(v)-1 || r[v[i]] >= l[v[i+1]] {
				dp[i] = 1
			} else {
				dp[i] = dp[i+1] + 1
			}
			ans = min(ans, len(v)-dp[i])
		}
		Fprintln(out, ans)
	}
}
func change(v []int) {
	v[0]++
}

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 rd() (x int) {
	inn.Scan()
	data := inn.Bytes()
	x, _ = strconv.Atoi(string(data))
	return x
}
func rdll() (x int64) {
	inn.Scan()
	data := inn.Bytes()
	x, _ = strconv.ParseInt(string(data), 10, 0)
	return x
}
func rds() string {
	inn.Scan()
	return string(inn.Bytes())
}

package main

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

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

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

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

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

	return res
}

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

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

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

// DigitSum returns digit sum of a decimal number.
// DigitSum only accept a positive integer.
func DigitSum(n int) int {
	if n < 0 {
		return -1
	}

	res := 0

	for n > 0 {
		res += n % 10
		n /= 10
	}

	return res
}

// DigitNumOfDecimal returns digits number of n.
// n is non negative number.
func DigitNumOfDecimal(n int) int {
	res := 0

	for n > 0 {
		n /= 10
		res++
	}

	return res
}

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

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

	return s
}

// Kiriage returns Ceil(a/b)
// a >= 0, b > 0
func Kiriage(a, b int) int {
	return (a + (b - 1)) / b
}

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

	if e == 0 {
		return 1
	}

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

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

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

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

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

	return a
}

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

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

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

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

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

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

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

	return string(res)
}

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

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

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

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

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

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

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

var q int

func main() {
	q = ReadInt()

	for i := 0; i < q; i++ {
		n := ReadInt()
		A := ReadIntSlice(n)
		memo := make([]int, n+1)

		for j := 0; j < n; j++ {
			memo[A[j]] = j
		}

		ans := 1
		tmpAns := 1
		for j := 1; j < n; j++ {
			if memo[j] < memo[j+1] {
				tmpAns++
			} else {
				ChMax(&ans, tmpAns)
				tmpAns = 1
			}
		}
		fmt.Println(n - ans)
	}
}

// MODはとったか？
// 遷移だけじゃなくて最後の最後でちゃんと取れよ？

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

package main

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

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

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

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

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

	return res
}

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

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

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

// DigitSum returns digit sum of a decimal number.
// DigitSum only accept a positive integer.
func DigitSum(n int) int {
	if n < 0 {
		return -1
	}

	res := 0

	for n > 0 {
		res += n % 10
		n /= 10
	}

	return res
}

// DigitNumOfDecimal returns digits number of n.
// n is non negative number.
func DigitNumOfDecimal(n int) int {
	res := 0

	for n > 0 {
		n /= 10
		res++
	}

	return res
}

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

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

	return s
}

// Kiriage returns Ceil(a/b)
// a >= 0, b > 0
func Kiriage(a, b int) int {
	return (a + (b - 1)) / b
}

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

	if e == 0 {
		return 1
	}

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

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

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

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

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

	return a
}

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

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

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

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

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

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

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

	return string(res)
}

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

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

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

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

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

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

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

var q int

func main() {
	q = ReadInt()

	for i := 0; i < q; i++ {
		n := ReadInt()
		A := ReadIntSlice(n)
		memo := make(map[int]int)

		// A[j]の最後尾のidxを記録
		for j := 0; j < n; j++ {
			memo[A[j]] = j
		}

		// A[j]についてuniq | sortする
		uniqs := []int{}
		for k := range memo {
			uniqs = append(uniqs, k)
		}
		sort.Sort(sort.IntSlice(uniqs))

		ans := 1
		tmpAns := 1
		for j := 0; j < len(uniqs)-1; j++ {
			l, r := uniqs[j], uniqs[j+1]
			if memo[l] < memo[r] {
				tmpAns++
			} else {
				ChMax(&ans, tmpAns)
				tmpAns = 1
			}
		}
		ChMax(&ans, tmpAns)
		fmt.Println(len(uniqs) - ans)
	}
}

// MODはとったか？
// 遷移だけじゃなくて最後の最後でちゃんと取れよ？

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

package main

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

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

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

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

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

	return res
}

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

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

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

// DigitSum returns digit sum of a decimal number.
// DigitSum only accept a positive integer.
func DigitSum(n int) int {
	if n < 0 {
		return -1
	}

	res := 0

	for n > 0 {
		res += n % 10
		n /= 10
	}

	return res
}

// DigitNumOfDecimal returns digits number of n.
// n is non negative number.
func DigitNumOfDecimal(n int) int {
	res := 0

	for n > 0 {
		n /= 10
		res++
	}

	return res
}

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

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

	return s
}

// Kiriage returns Ceil(a/b)
// a >= 0, b > 0
func Kiriage(a, b int) int {
	return (a + (b - 1)) / b
}

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

	if e == 0 {
		return 1
	}

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

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

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

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

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

	return a
}

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

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

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

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

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

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

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

	return string(res)
}

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

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

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

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

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

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

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

var q int

func main() {
	q = ReadInt()

	for i := 0; i < q; i++ {
		n := ReadInt()
		A := ReadIntSlice(n)

		memo := make([]int, n+1)
		for i := 0; i < n-1; i++ {
			if A[i] > A[i+1] {
				memo[A[i+1]]++
			}
		}

		ans := 0
		for a := 1; a <= n; a++ {
			if memo[a] > 0 {
				ans++
			}
		}

		fmt.Println(ans)
	}
}

// MODはとったか？
// 遷移だけじゃなくて最後の最後でちゃんと取れよ？

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


package main

import "fmt"

func main()  {
	var q int
	fmt.Scan(&q)
	relationMap := make(map[string]string)
	for i:=0; i<q; i++{
		var f,s string
		fmt.Scan(&f,&s)
		if r,ok := relationMap[f]; !ok{
			relationMap[s] = f
		}else{
			relationMap[s] = r
			delete(relationMap,f)
		}
	}
	fmt.Printf("%d\n",len(relationMap))
	for k,v := range relationMap{
		fmt.Printf("%s %s\n",v,k)
	}
}

package main

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

func main()  {
	var q int
	fmt.Scan(&q)
	relationMap := make(map[string]string)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for i:=0; i<q; i++{
		scanner.Scan()
		f := scanner.Text()
		scanner.Scan()
		s := scanner.Text()
		if r,ok := relationMap[f]; !ok{
			relationMap[s] = f
		}else{
			relationMap[s] = r
			delete(relationMap,f)
		}
	}
	fmt.Printf("%d\n",len(relationMap))
	for k,v := range relationMap{
		fmt.Printf("%s %s\n",v,k)
	}
}

package main

import (
	"fmt"
)

func main() {
	var q int
	fmt.Scanf("%d\n", &q)

	names := make(map[string]string)
	for i := 0; i < q; i++ {
		var oldName, newName string
		fmt.Scanf("%s %s\n", &oldName, &newName)

		priorName, exists := names[oldName]
		if exists {
			delete(names, oldName)
			oldName = priorName
		}

		names[newName] = oldName
	}

	fmt.Printf("%d\n", len(names))
	for newName, oldName := range names {
		fmt.Printf("%s %s\n", oldName, newName)
	}
}

package main

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

var scanner = bufio.NewScanner(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

//Wrong
func main() {
	defer writer.Flush()
	// scanner.Split(bufio.ScanWords)

	q := nextInt()
	nameMap := make(map[string]string)

	for ; q > 0; q-- {
		names := strings.Fields(next())
		if _, ok := nameMap[names[0]]; !ok {
			nameMap[names[1]] = names[0]
		} else {
			oldName := nameMap[names[0]]
			delete(nameMap, names[0])
			nameMap[names[1]] = oldName
		}
	}

	println(len(nameMap))
	for k, v := range nameMap {
		println(v, k)
	}
}

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

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

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{}) {
	fmt.Fprintln(writer, a...)
}

package main

import "fmt"

func main() {
	var q int
	fmt.Scan(&q)
	nameMap := make(map[string]string)
	for ; q > 0; q-- {
		var oldName, newName string
		fmt.Scan(&oldName, &newName)
		oldest, exists := nameMap[oldName]
		if exists {
			delete(nameMap, oldName)
			nameMap[newName] = oldest
		} else {
			nameMap[newName] = oldName
		}
	}
	fmt.Println(len(nameMap))
	for newName, oldName := range nameMap {
		fmt.Println(oldName + " " + newName)
	}
}

package main

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

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

func main() {
  scanner := bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()
  q := scanInt(scanner)
  currentToOriginal := map[string]string{}
  for i := 0; i < q; i++ {
    scanner.Scan()
    previous := scanner.Text()
    scanner.Scan()
    current := scanner.Text()
    original, found := currentToOriginal[previous]
    if found {
      delete(currentToOriginal, previous)
      currentToOriginal[current] = original
    } else {
      currentToOriginal[current] = previous
    }
  }
  writer.WriteString(fmt.Sprintf("%d\n", len(currentToOriginal)))
  for current, original := range currentToOriginal {
    writer.WriteString(fmt.Sprintf("%s %s\n", original, current))
  }
}

package main

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

func reader(a []string) func() string {
	var ptr int = -1
	return func () string {
		ptr += 1
		return a[ptr]
	}
}

func main() {
	in := bufio.NewReader(os.Stdin)
	s, _ := in.ReadString('\n')
	next := reader(strings.Fields(s))
	q, _ := strconv.Atoi(next())

	map1, map2 := make(map[string]string), make(map[string]string)

	for i := 0; i < q; i++ {
		s, _ := in.ReadString('\n')
		next = reader(strings.Fields(s))
		a, b := next(), next()
		orig, ok := map2[a]
		if !ok {
			map1[a] = b
			map2[b] = a
		}else {
			map1[orig] = b
			delete(map2, a)
			map2[b] = orig
		}
	}

	fmt.Println(len(map1))
	for key, value := range map1 {
		fmt.Println(key, value)
	}
}

package main

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

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

func main() {
	defer out.Flush()
	in.Split(bufio.ScanWords)

	q := nextInt()
	map1, map2 := make(map[string]string), make(map[string]string)
	for i := 0; i < q; i++ {
		a, b := next(), next()
		orig, exists := map2[a]
		if !exists {
			map1[a] = b
			map2[b] = a
		}else {
			map1[orig] = b
			delete(map2, a)
			map2[b] = orig
		}
	}

	fmt.Fprintln(out, len(map1))
	for key, value := range map1 {
		fmt.Fprintln(out, key, value)
	}
}

func next() string {
	if !in.Scan() {
		panic("Scan error")
	}
	return in.Text()
}

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

func nextFloat() float64 {
	ret, _ := strconv.ParseFloat(next(), 64)
	return ret
}

package main

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

func main() {
	var n int
	if _, err := fmt.Scanf("%d\n", &n); err != nil {
		return
	}

	used := make(map[string]bool)
	new_old := make(map[string]string)

	reader := bufio.NewReader(os.Stdin)
	re := regexp.MustCompile(`\r?\n`)
	for i := 0; i < n; i++ {
		if s, err := reader.ReadString('\n'); err != nil {
			if err != io.EOF {
				return
			}
		} else {
			ss := strings.Split(re.ReplaceAllString(s, ""), " ")
			if len(ss) != 2 {
				return
			}
			oh := ss[0]
			nh := ss[1]

			used[oh] = true
			if !used[nh] {
				if ooh, ok := new_old[oh]; ok {
					new_old[nh] = ooh
					delete(new_old, oh)
				} else {
					new_old[nh] = oh
				}
				used[nh] = true
			}
		}
	}

	fmt.Println(len(new_old))
	for nh, oh := range new_old {
		fmt.Println(oh, nh)
	}
}

package main

import (
	"fmt"
)

func main() {
	var n, q int
	d := make(map[string]string)

	fmt.Scan(&q)

	for i := 0; i < q; i++ {
		var oldH, newH string

		fmt.Scan(&oldH, &newH)

		origH, exists := d[oldH]

		if exists {
			d[newH] = origH
			delete(d, oldH)
		} else {
			d[newH] = oldH
			n++
		}
	}

	fmt.Println(n)

	for newH, oldH := range d {
		fmt.Println(oldH, newH)
	}
}

package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)
	newUser := make(map[string]bool)
	pastUser := make(map[string]bool)
	history := make(map[string]string)
	for i := 0; i < n; i++ {
		var from, to string
		fmt.Scan(&from, &to)
		if !pastUser[from] {
			newUser[from] = true
		}
		history[from] = to
		pastUser[to] = true
	}

	currentName := func(s string) string {
		for history[s] != "" {
			s = history[s]
		}
		return s
	}

	fmt.Println(len(newUser))
	for k := range newUser {
		fmt.Println(k, currentName(k))
	}
}

package main

import "fmt"

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

	m := make(map[string]string)
	not_begin := make(map[string]int)
	var s, t string
	for i := 0; i < n; i++ {
		fmt.Scan(&s, &t)
		m[s] = t
		not_begin[t] = 1
	}

	ans := make(map[string]string)
	cnt := 0
	for k, _ := range m {
		if _, ok := not_begin[k]; ok {
			continue
		}

		cur := k
		next := ""
		ok := true
		for ok {
			next, ok = m[cur]
			if ok {
				cur = next
			}
		}

		ans[k] = cur
		cnt++
	}

	fmt.Println(cnt)
	for k, v := range ans {
		fmt.Println(k, v)
	}
}

package main

import "fmt"

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

	names := make(map[string]string)

	for i := 0; i < n; i++ {
		var oldName, newName string
		fmt.Scanf("%s%s\n", &oldName, &newName)

		storedName, ok := names[oldName]

		if ok {
			delete(names, oldName)
			names[newName] = storedName
		} else {
			names[newName] = oldName
		}
	}

	fmt.Println(len(names))

	for key, value := range names {
		fmt.Println(value, key)
	}
}

package main

import "fmt"

func main()  {
	var q int
	fmt.Scan(&q)
	relationMap := make(map[string]string)
	for i:=0; i<q; i++{
		var f,s string
		fmt.Scanf("%s %s",&f,&s)
		if r,ok := relationMap[f]; !ok{
			relationMap[s] = f
		}else{
			relationMap[s] = r
			delete(relationMap,f)
		}
	}
	fmt.Printf("%d\n",len(relationMap))
	for k,v := range relationMap{
		fmt.Printf("%v %v\n",v,k)
	}
}

package main

import "fmt"

func main()  {
	var q int
	fmt.Scan(&q)
	relationMap := make(map[string]string)
	for i:=0; i<q; i++{
		var f,s string
		fmt.Scanf("%s %s",&f,&s)
		if r,ok := relationMap[f]; !ok{
			relationMap[s] = f
		}else{
			relationMap[s] = r
			delete(relationMap,f)
		}
	}
	fmt.Printf("%d\n",len(relationMap))
	for k,v := range relationMap{
		fmt.Printf("%s %s\n",v,k)
	}
}

package main

import (
    "fmt"
)

func main() {
	var q int
    fmt.Scanf("%d", &q)

	names := make(map[string]string)
	for i := 0; i < q; i++ {
		var oldName, newName string
		fmt.Scanf("%s %s", &oldName, &newName)

		priorName, exists := names[oldName]
		if exists {
			delete(names, oldName)
			oldName = priorName
		}

		names[newName] = oldName
	}

	fmt.Printf("%d\n", len(names))
	for newName, oldName := range names {
		fmt.Printf("%s %s\n", oldName, newName)
	}
}


package main

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

var n int

var a []int
var b []int

func main() {

	r := bufio.NewReader(os.Stdin)

	fmt.Fscan(r, &n)

	a = make([]int, n + 1)
	b = make([]int, n + 1)

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

	for j := 1; j <= n; j++ {
		fmt.Fscan(r, &b[j])
	}

	solve()

	fmt.Println()

}

var dp []int

var noSlip []int
var slip []int

func solve() {
	dp = make([]int, n + 1)
	noSlip = make([]int, n + 1)
	slip = make([]int, n + 1)

	for i := 0; i < n + 1; i++ {
		dp[i] = 1e9
	}

	if n - a[n] <= 0 {
		fmt.Println(1)
		fmt.Println(0)
		return
	}

	var arr []int

	for p := n; p >= n - a[n]; p-- {
		noSlip[p + b[p]] = p

		dp[p + b[p]] = 1

		arr = append(arr, p + b[p])
	}

	lo := n - a[n]

	for ;len(arr) > 0; {
		p := arr[0]
		arr = arr[1 :]

		x := a[p]

		if p - x <= 0 {

			fmt.Println(dp[p] + 1)

			k := dp[p]

			var stack []int

			stack = append(stack, 0)

			for ;k > 0; k--{
				pos := noSlip[p]
				s := slip[pos]

				stack = append(stack, pos)

				p = s
			}

			for j := len(stack) - 1; j >= 0; j-- {
				fmt.Print(stack[j])
				fmt.Print(" ")
			}

			return
		}

		if p - x < lo {

			for k := lo - 1; k >= p - x; k-- {
				to := k + b[k]

				if dp[to] == 1e9 {
					dp[to] = dp[p] + 1

					noSlip[to] = k
					slip[k] = p

					arr = append(arr, to)
				}
			}

			lo = p - x
		}
	}

	fmt.Println(-1)
}

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

package main

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

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

	var n int
	Fscan(in, &n)
	dec := make([]int, n)
	for i := range dec {
		Fscan(in, &dec[i])
	}
	inc := make([]int, n)
	for i := range inc {
		Fscan(in, &inc[i])
	}

	vis := make([]bool, n)
	vis[n-1] = true
	type pair struct{ h, fa int }
	from := make([]pair, n)
	q := []int{n - 1}
	for step, min := 1, n-1; q != nil; step++ {
		tmp := q
		q = nil
		for _, v := range tmp {
			st := v - dec[v]
			if st < 0 {
				Fprintln(out, step)
				path := make([]int, 1, step)
				for ; v != n-1; v = from[v].fa {
					path = append(path, from[v].h+1)
				}
				for i := len(path) - 1; i >= 0; i-- {
					Fprint(out, path[i], " ")
				}
				return
			}
			if st >= min {
				continue
			}
			for h := st; h < min; h++ {
				if w := h + inc[h]; !vis[w] {
					vis[w] = true
					from[w] = pair{h, v}
					q = append(q, w)
				}
			}
			min = st
		}
	}
	Fprint(out, -1)
}

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

package main

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

const N = 300010

var (
	n     int
	a     []int
	b     []int
	fa    [N]int
	que   [N]int
	lst   [N]int
	from  [N]int
	reach [N][]int
	head  int
	tail  int
)

func find(x int) int {
	//fmt.Printf("find %v\n", x)
	if x != fa[x] {
		fa[x] = find(fa[x])
	}
	return fa[x]
}

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs)+1)
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i+1] = val
	}
	arr[0] = 0
	return arr
}

func main() {
	in := bufio.NewReader(os.Stdin)
	n = readInt(in)
	a = readArrInt(in)
	b = readArrInt(in)
	for i := 1; i <= n; i++ {
		reach[i+b[i]] = append(reach[i+b[i]], i)
		fa[i] = i
	}
	que[tail] = n
	tail++
	for _, value := range reach[n] {
		fa[value] = find(value - 1)
	}
	for head < tail {

		v := que[head]
		//fmt.Printf("bfs %d\n", v)
		if v == 0 {
			break
		}
		head++
		for i := find(v); i >= v-a[v]; i = find(i - 1) {
			//fmt.Printf("i = %d\n", i)
			lst[i+b[i]] = v
			from[i+b[i]] = i
			que[tail] = i + b[i]
			tail++
			for _, value := range reach[i+b[i]] {
				fa[find(value)] = find(value - 1)
			}
			if i <= 0 {
				tail = 0
				head = 0
				for head != n {
					que[tail] = from[head]
					tail++
					head = lst[head]
				}
				fmt.Println(tail)
				for tail > 0 {
					tail--
					fmt.Printf("%d ", que[tail])
				}
				return
			}
		}
	}
	fmt.Print(-1)
}

package main

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

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

	var n int
	Fscan(in, &n)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	b := make([]int, n)
	for i := range b {
		Fscan(in, &b[i])
	}

	vis := make([]bool, n)
	vis[n-1] = true
	type pair struct{ h, v int }
	from := make([]pair, n)
	for i := range from {
		from[i].v = -1
	}
	min := n - 1
	q := []int{n - 1}
	for step := 1; q != nil; step++ {
		tmp := q
		q = nil
		for _, v := range tmp {
			if v < a[v] {
				Fprintln(out, step)
				path := []int{}
				for x := v; x != n-1; x = from[x].v {
					path = append(path, from[x].h)
				}
				for i := len(path) - 1; i >= 0; i-- {
					Fprint(out, path[i]+1, " ")
				}
				Fprint(out, 0)
				return
			}
			if v-a[v] >= min {
				continue
			}
			for i := v - a[v]; i < min; i++ {
				if w := i + b[i]; !vis[w] {
					vis[w] = true
					from[w] = pair{i, v}
					q = append(q, w)
				}
			}
			min = v - a[v]
		}
	}
	Fprint(out, -1)
}

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

package main

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

const N = 300010

var (
	n     int
	a     []int
	b     []int
	fa    [N]int
	que   [N]int
	lst   [N]int
	from  [N]int
	reach [N][]int
	head  int
	tail  int
)

func find(x int) int {
	//fmt.Printf("find %v\n", x)
	if x != fa[x] {
		fa[x] = find(fa[x])
	}
	return fa[x]
}

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs)+1)
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i+1] = val
	}
	arr[0] = 0
	return arr
}

func main() {
	in := bufio.NewReader(os.Stdin)
	n = readInt(in)
	a = readArrInt(in)
	b = readArrInt(in)
	for i := 1; i <= n; i++ {
		reach[i+b[i]] = append(reach[i+b[i]], i)
		fa[i] = i
	}
	que[tail] = n
	tail++
	for _, value := range reach[n] {
		fa[value] = find(value - 1)
	}
	for head < tail {

		v := que[head]
		//fmt.Printf("bfs %d\n", v)
		if v == 0 {
			break
		}
		head++
		for i := find(v); i >= v-a[v]; i = find(i - 1) {
			//fmt.Printf("i = %d\n", i)
			lst[i+b[i]] = v
			from[i+b[i]] = i
			que[tail] = i + b[i]
			tail++
			for _, value := range reach[i+b[i]] {
				fa[find(value)] = find(value - 1)
			}
			if i <= 0 {
				break
			}
		}
	}
	if lst[0] == 0 {
		fmt.Print("-1")
	} else {
		tail = 0
		head = 0
		for head != n {
			que[tail] = from[head]
			tail++
			head = lst[head]
		}
		fmt.Println(tail)
		for tail > 0 {
			tail--
			fmt.Printf("%d ", que[tail])
		}
	}
}


package main

import "fmt"

func main() {
	var n, m, k int
	fmt.Scan(&n, &m, &k)
	p := make([]int, k)
	for i := range p {
		fmt.Scan(&p[i])
	}
	res := 0
	for i := 0; i < n*m; i++ {
		var a int
		fmt.Scan(&a)
		i := 0
		for i = range p {
			if p[i] == a {
				break
			}
		}
		res += i + 1
		for j := i; j > 0; j-- {
			p[j] = p[j-1]
		}
		p[0] = a
	}
	fmt.Println(res)
}

package main

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

var (
	writer  = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
)

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n, m, k := nextInt(), nextInt(), nextInt()
	items := make([]int, k)
	totalTime := 0

	for i := range items {
		items[i] = nextInt()
	}

	for i := 0; i < n*m; i++ {
		totalTime += findPos(nextInt(), items)
	}

	println(totalTime)
}

func findPos(order int, items []int) int {
	i := 0
	pos := -1

	for ; i < len(items); i++ {
		if items[i] == order {
			pos = i + 1
			break
		}
	}

	temp := items[i]

	for ; i > 0; i-- {
		items[i] = items[i-1]
	}

	items[0] = temp

	return pos
}

func next() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func nextInt64() int64 {
	n, _ := strconv.ParseInt(next(), 0, 64)
	return n
}

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

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{})               { fmt.Fprintln(writer, a...) }
func print(a ...interface{})                 { fmt.Fprint(writer, a...) }
func printf(format string, a ...interface{}) { fmt.Fprintf(writer, format, a...) }


package main

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

var n, q int

var a []int

var qu[][2]int

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)

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

		a = make([]int, n)

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

		fmt.Fscan(r, &q)

		qu = make([][2]int, q)

		for j := 0; j < q; j++ {
			fmt.Fscan(r, &qu[j][0], &qu[j][1])
		}

		solve()

		fmt.Println()
	}
}

const MAX = 2101

var mat [MAX][MAX]int

func solve()  {
	//2 1 1 4 3 1 1 1 2
	//2 5 5 1 1 5 5 5 2
	//2 5 5 2 2 5 5 5 2

	arr := make([]int, n + 1)

	for i :=0 ;i < n; i++ {
		arr[a[i]]++
	}

	for i := 0; i < n; i++ {
		mat[0][i] = a[i]
	}

	for k := 1; k < MAX; k++ {

		for i := 0; i < n; i++ {
			mat[k][i] = arr[mat[k - 1][i]]
		}

		arr = make([]int, n + 1)

		for i := 0; i < n; i++ {
			arr[mat[k][i]]++
		}
	}

	for i := 0; i < q; i++ {
		x, k := qu[i][0], qu[i][1]
		x--

		if k > MAX {
			k = MAX - 1
		}

		fmt.Println(mat[k][x])
	}

}

package main

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

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

func Solve(in [][]int) {
	values := make(map[int]int, len(in[0]))

	for iterCount := 1; iterCount < len(in); iterCount++ {
		for key := range values {
			values[key] = 0
		}

		for _, v := range in[iterCount-1] {
			values[v]++
		}

		in[iterCount] = make([]int, len(in[0]))

		//equal := true

		for i, v := range in[iterCount-1] {
			in[iterCount][i] = values[v]

			//if values[v] != v {
			//	equal = false
			//}
		}

		//if equal {
		//	break
		//}
	}
}

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

	defer out.Flush()

	n := readInt(in)

	for i := 0; i < n; i++ {
		_ = readInt(in)

		initialArr := make([][]int, 1000)

		initialArr[0] = readArrInt(in)

		nQueries := readInt(in)

		Solve(initialArr)

		for j := 0; j < nQueries; j++ {
			query := readArrInt(in)

			k := query[1]

			if query[1] >= len(initialArr) {
				k = len(initialArr) - 1
			}

			//fmt.Fprintf(out, "%+v\n", initialArr[k])
			fmt.Fprintf(out, "%d\n", initialArr[k][query[0]-1])
		}
	}
}

package main

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

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

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

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

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

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

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

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

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

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

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

		}
		return initial
	}
}

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

		}
		return m
	}
}

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

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

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

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

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

	return
}

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

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

	return num
}

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

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

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

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

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

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

func ChangeStringToDate(x string) (t time.Time, err error) {
	t, err = time.Parse("2006-01-02", x)
	return
}

func DiffrenceBetweenTwoDates(first, second time.Time) int64 {
	date := first
	date2 := second
	k := date.Unix() - date2.Unix()
	return k
}

func Union(a, b []int) []int {
	m := make(map[int]bool)

	for _, item := range a {
		m[item] = true
	}

	for _, item := range b {
		if _, ok := m[item]; !ok {
			a = append(a, item)
		}
	}
	return a
}

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

// 	A := a % 2
// 	B := b % 2
// 	C := c % 2
// 	if A+B+C == 0 {
// 		fmt.Println(0)
// 	}
// 	if A+B+C == 1 {
// 		if A == 1 {
// 			fmt.Println(1)
// 		}
// 		if B == 1 {
// 			if a >= 2 {
// 				fmt.Println(0)
// 			} else {
// 				fmt.Println(2)
// 			}
// 		}
// 		if C == 1 {
// 			if a > 0
// 			fmt.Println(1)
// 		}
// 	}
// 	if A+B+C == 2 {

// 	}
// 	if A+B+C == 3 {
// 		fmt.Println(0)
// 	}

// }

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

func Ans(ARR []int) {
	if len(ARR) < 2 {
		return
	}
	if len(ARR) == 2 {
		fmt.Println(ARR[1])
		fmt.Println(-ARR[0])
	} else {
		if len(ARR) == 3 {
			first := ARR[0]
			Midd := ARR[1]
			Last := ARR[2]
			var F, M, L int
			Pos := 0
			Neg := 0
			if first > 0 {
				Pos++
			} else {
				Neg++
			}
			if Midd > 0 {
				Pos++
			} else {
				Neg++
			}
			if Last > 0 {
				Pos++
			} else {
				Neg++
			}
			if Pos*Neg != 0 {
				if ABS(first) == ABS(Midd) && ABS(Midd) == ABS(Last) {
					if first == Midd {
						if Midd == Last {
							F = -1
							M = -1
							L = 2
						} else {
							F = 1
							M = 1
							L = 2
						}
					} else {
						if first == Last {
							F = 1
							L = 1
							M = 2
						} else {
							M = 1
							L = 1
							F = 2
						}
					}
				} else {
					One := first + Midd
					Two := Midd + Last
					Thr := first + Last
					l := 0
					if One != 0 && ABS(One) <= 10000 {
						F = Last
						M = Last
						L = -One
						l = 1
					}

					if Two != 0 && ABS(Two) <= 10000 && l == 0 {
						F = -Two
						M = first
						L = first
						l = 1
					}
					if Thr != 0 && ABS(Thr) <= 10000 && l == 0 {
						F = Midd
						M = -Thr
						L = Midd
					}

				}
			} else {
				if ABS(first) == ABS(Midd) && ABS(Midd) == ABS(Last) {
					F = -1
					M = -1
					L = 2
				} else {
					One := first - Midd
					Two := Midd - Last
					Thr := first - Last
					l := 0
					if One != 0 && ABS(One) <= 10000 {
						F = -Last
						M = Last
						L = One
						l = 1
					}

					if Two != 0 && ABS(Two) <= 10000 && l == 0 {
						F = Two
						M = -first
						L = first
						l = 1
					}
					if Thr != 0 && ABS(Thr) <= 10000 && l == 0 {
						F = -Midd
						M = Thr
						L = Midd
					}

				}
			}

			fmt.Println(F)
			fmt.Println(M)
			fmt.Println(L)
		} else {
			Q := make([]int, 0)
			P := make([]int, 0)
			P = append(P, ARR[0], ARR[1])
			p := len(ARR)
			for i := 2; i < p; i++ {
				Q = append(Q, ARR[i])
			}
			Ans(P)
			Ans(Q)
		}
	}
}

var t int

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &t)

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

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

		B := make([][]int, 0)
		B = append(B, ARR)
		for j := 1; j < 100; j++ {
			C := make([]int, 0)
			NewD := make([]int, 2001)
			for i := 0; i < n; i++ {
				q := DDD[ARR[i]]
				C = append(C, q)
				NewD[q]++

			}
			DDD = NewD
			B = append(B, C)
			ARR = C

		}

		var q int
		fmt.Fscan(input, &q)
		for i := 0; i < q; i++ {
			var a, b int64
			fmt.Fscan(input, &a, &b)
			if b >= 100 {
				fmt.Println(B[99][a-1])
			} else {
				fmt.Println(B[b][a-1])

			}
		}

	}

}

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)

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

		z := [][]int{}
		z = append(z, arr)

		// build up transformations.
		for {
			t := make([]int, n)
			var modified bool
			for i := 0; i < n; i++ {
				var c int
				for k := 0; k < n; k++ {
					if z[len(z)-1][k] == z[len(z)-1][i] {
						c++
					}
				}

				t[i] = c
				if t[i] != z[len(z)-1][i] {
					modified = true
				}
			}

			if !modified {
				break
			}
			z = append(z, t)
		}

		var q int
		fmt.Fscan(in, &q)
		for i := 0; i < q; i++ {
			var x, k int
			fmt.Fscan(in, &x, &k)

			if k > len(z)-1 {
				k = len(z) - 1
			}
			fmt.Fprintln(out, z[k][x-1])
		}
	}
}

package main

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

/*
2
7
2 1 1 4 3 1 2
4
3 0
1 1
2 2
6 1
2
1 1
2
1 0
2 1000000000
---
1
2
3
3
1
2
======
1
7
2 1 1 4 3 1 2
4
3 0
1 1
2 2
6 1
---
1
2
3
3
========
1
4
5 5 1 2
8
1 2
2 2
3 2
4 2
1 20
2 20
3 20
4 20

*/

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

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

	bld := strings.Builder{}

	for l := 0; l < t; l++ {
		var n int
		scanf("%d\n", &n)

		cnts := make(map[int]int)

		a := make([][]int, 1000)
		a[0] = make([]int, n)

		for i := 0; i < n; i++ {
			scanf("%d", &a[0][i])
			cnts[a[0][i]]++
		}
		scanf("\n")

		for i := 1; i < 1000; i++ {
			a[i] = make([]int, n)

			isChanged := false

			newCnts := make(map[int]int)
			for j := 0; j < n; j++ {
				a[i][j] = cnts[a[i-1][j]]
				newCnts[a[i][j]]++

				if a[i][j] != a[i-1][j] {
					isChanged = true
				}
			}

			if !isChanged {
				a = a[:i]
				break
			}

			cnts = newCnts
		}

		var q int
		scanf("%d\n", &q)

		for i := 0; i < q; i++ {
			var x, k int
			scanf("%d %d\n", &x, &k)

			layer := k
			if k >= len(a) {
				layer = len(a) - 1
			}

			ans := a[layer][x-1]

			fmt.Fprintf(&bld, "%d\n", ans)
		}
	}

	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 rune
	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
}

package main

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

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

func Solve(in []int, q []int) int {
	var iterCount int

	input := make([]int, len(in))
	copy(input, in)

	if q[1] == 0 {
		return input[q[0]-1]
	}

	values := make(map[int]int, len(input))

	for iterCount < q[1] {
		for key := range values {
			values[key] = 0
		}

		for _, v := range input {
			values[v]++
		}

		equal := true

		for i, v := range input {
			input[i] = values[v]

			if values[v] != v {
				equal = false
			}
		}

		if equal {
			return input[q[0]-1]
		}

		iterCount++
		fmt.Printf("iter: %d values: %+v", iterCount, input)
	}

	return input[q[0]-1]
}

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

	for i := 0; i < n; i++ {
		_ = readInt(in)

		initialArr := readArrInt(in)

		nQueries := readInt(in)

		for j := 0; j < nQueries; j++ {
			query := readArrInt(in)
			res := Solve(initialArr, query)

			fmt.Printf("%d\n", res)
		}
	}
}

package main

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

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

func Solve(in []int, q []int) int {
	var iterCount int

	if q[1] == 0 {
		return in[q[0]-1]
	}

	values := make(map[int]int, len(in))

	for iterCount < q[1] {
		for key := range values {
			values[key] = 0
		}

		for _, v := range in {
			values[v]++
		}

		equal := true

		for i, v := range in {
			in[i] = values[v]

			if values[v] != v {
				equal = false
			}
		}

		if equal {
			return in[q[0]-1]
		}

		iterCount++
	}

	return in[q[0]-1]
}

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

	for i := 0; i < n; i++ {
		_ = readInt(in)

		initialArr := readArrInt(in)

		nQueries := readInt(in)

		for j := 0; j < nQueries; j++ {
			query := readArrInt(in)
			res := Solve(initialArr, query)

			fmt.Printf("%d\n", res)
		}
	}
}

package main

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

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

func Solve(in []int, q []int) int {
	var iterCount int

	if q[1] == 0 {
		return in[q[0]-1]
	}

	for iterCount < q[1] {
		values := make(map[int]int, len(in))

		for _, v := range in {
			values[v]++
		}

		equal := true

		for i, v := range in {
			in[i] = values[v]

			if values[v] != v {
				equal = false
			}
		}

		if equal {
			return in[q[0]-1]
		}

		iterCount++
	}

	return in[q[0]-1]
}

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

	for i := 0; i < n; i++ {
		_ = readInt(in)

		initialArr := readArrInt(in)

		nQueries := readInt(in)

		for j := 0; j < nQueries; j++ {
			query := readArrInt(in)
			res := Solve(initialArr, query)

			fmt.Printf("%d\n", res)
		}
	}
}