package main

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

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

	T:= 0
	for Fscan(in, &T); T > 0; T-- {
		var n,k int
		Fscan(in,&n,&k)
		a:=make([]int,n)
		for i:=0;i<n;i++{
			Fscan(in,&a[i])
		}

		for i:=0;i<n;i++{
			var j int
			Fscan(in,&j)
		}
		sort.Ints(a)
		if a[n-1]-a[0]<=2*k{
			Fprintln(out,n)
			continue
		}
		// Fprintln(out,k,a)
		dp1:=make([]int,n+1)
		dp2:=make([]int,n+1)

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

		i:=0
		for j,x:=range(a){
			for x-a[i]>k{
				i++
			}
			dp1[j+1]=max(dp1[j+1],j-i+1)
			dp2[j+1]=max(dp2[j+1],dp1[i]+j-i+1)

			dp1[j+1]=max(dp1[j+1],dp1[j])
			dp2[j+1]=max(dp2[j+1],dp2[j])
		}

		Fprintln(out,max(dp1[n],dp2[n]))
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1409E(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	type pair struct{ v, c int }
	max := func(a, b int) int {
		if a > b {
			return a
		}
		return b
	}

	var T, n, l, v int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &l)
		cnt := map[int]int{}
		for i := 0; i < n; i++ {
			Fscan(in, &v)
			cnt[v]++
		}
		for i := 0; i < n; i++ {
			Fscan(in, &v)
		}

		n = len(cnt)
		a := make([]int, 0, n)
		for v := range cnt {
			a = append(a, v)
		}
		sort.Ints(a)
		sum := make([]int, n+1)
		for i, v := range a {
			sum[i+1] = sum[i] + cnt[v]
		}
		cs := make([]int, n)
		for i, v := range a {
			cs[i] = sum[i+1] - sum[sort.SearchInts(a[:i], v-l)]
		}
		mxC := make([]int, n)
		mxC[0] = cs[0]
		for i := 1; i < n; i++ {
			mxC[i] = max(mxC[i-1], cs[i])
		}
		ans := 0
		for i, v := range a {
			j := sort.SearchInts(a[:i], v-l)
			c := sum[i+1] - sum[j]
			if j > 0 {
				c += mxC[j-1]
			}
			ans = max(ans, c)
		}
		Fprintln(out, ans)
	}
}

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

package main

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

func main() {
	io := NewFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	for t := io.NextInt(); t > 0; t-- {
		n, k := io.NextInt(), io.NextInt()
		a := io.NextInts(n)
		io.NextInts(n)

		sort.Ints(a)
		ans := 0
		best := make([]int, n+1)
		for i := n - 1; i >= 0; i-- {
			j := sort.Search(n, func(j int) bool { return a[j] > a[i]+k })
			best[i] = max(best[i+1], j-i)
			ans = max(ans, j-i+best[j])
		}

		io.Println(ans)
	}
}

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

type FastIO struct {
	*bufio.Writer
	r    io.Reader
	buf  [8192]byte
	i, n int
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{r: r, Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	if f.i == f.n {
		f.n, _ = f.r.Read(f.buf[:])
		if f.n == 0 {
			panic("EOF")
		}
		f.i = 0
	}
	f.i++
	return f.buf[f.i-1]
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) NextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt()
	}
	return a
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f, a...)
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1409E(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	type pair struct{ v, c int }
	max := func(a, b int) int {
		if a > b {
			return a
		}
		return b
	}

	var T, n, l, v int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &l)
		cnt := map[int]int{}
		for i := 0; i < n; i++ {
			Fscan(in, &v)
			cnt[v]++
		}
		for i := 0; i < n; i++ {
			Fscan(in, &v)
		}

		n = len(cnt)
		a := make([]int, 0, n)
		for v := range cnt {
			a = append(a, v)
		}
		sort.Ints(a)
		sum := make([]int, n+1)
		for i, v := range a {
			sum[i+1] = sum[i] + cnt[v]
		}
		cs := make([]int, n)
		for i, v := range a {
			cs[i] = sum[i+1] - sum[sort.SearchInts(a[:i], v-l)]
		}
		mxC := make([]int, n)
		mxC[0] = cs[0]
		for i := 1; i < n; i++ {
			mxC[i] = max(mxC[i-1], cs[i])
		}
		ans := 0
		for i, c := range cs {
			if j := sort.SearchInts(a[:i], a[i]-l); j > 0 {
				c += mxC[j-1]
			}
			ans = max(ans, c)
		}
		Fprintln(out, ans)
	}
}

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


package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

type State struct {
  R, C, Steps int
}

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

  numR, numC := getI(), getI()
  startR, startC, exitR, exitC := 0, 0, 0, 0
  g := make([][]int, numR)
  for r := 0; r < numR; r++ {
    s := getS()
    g[r] = make([]int, numC)
    for c, char := range s {
      if char == 'T' {
        g[r][c] = -1
      } else if char == 'S' {
        startR, startC = r, c
      } else if char == 'E' {
        exitR, exitC = r, c
      } else {
        g[r][c] = int(char - '0')
      }
    }
  }
  dr, dc := []int{ -1, 0, 1, 0 }, []int{ 0, 1, 0, -1 }
  queue := []*State{ &State{ exitR, exitC, 0 } }
  tail, maxSteps := 0, 1000*1000
  result := 0
  for tail != len(queue) && queue[tail].Steps <= maxSteps {
    state := queue[tail]
    tail++
    r, c, steps := state.R, state.C, state.Steps
    if g[r][c] < 0 {
      continue
    }
    result += g[r][c]
    g[r][c] = -2
    if r == startR && c == startC {
      maxSteps = steps
    }
    if steps == maxSteps {
      continue
    }
    for i := 0; i < 4; i++ {
      R, C := r + dr[i], c + dc[i]
      if R == -1 || R == numR || C == -1 || C == numC || g[R][C] < 0 {
        continue
      }
      queue = append(queue, &State{ R, C, steps+1 } )
    }
  }
  writer.WriteString(fmt.Sprintf("%d\n", result))
}

package main

import (
    "fmt"
)

type Tuple struct{
    x int
    y int
    len int
}
func main(){
    var r, c int
    fmt.Scanf("%d%d", &r, &c)
    start := Tuple{}

    g:= make([][]int8, r)
    for i:= 0; i < r; i++{
        g[i] = make([]int8, c)
        var s string
        fmt.Scan(&s)
        for j:= 0; j < c; j++{
            switch s[j] {
                case byte('T') : g[i][j] = -1
                case byte('S') : g[i][j] =  -2
                case byte('E') : start, g[i][j] = Tuple{i, j, 0}, 10
                default : g[i][j] = int8(s[j] - '0')
            }
        }
    }
    fights:= 0
    var q []Tuple
    var startdist = 1000000000
    q = append(q, start)
    deltas := []Tuple{{-1, 0, 0}, {1, 0, 0}, {0, -1, 0}, {0, 1, 0}}
    for len(q) > 0{
        var v Tuple
        v, q = q[0], q[1:]
        if g[v.x][v.y] != -1{
            if v.len > startdist {
                break;
            }
            if g[v.x][v.y] == -2 {
                //We have reached start position
                startdist = v.len
            }else if g[v.x][v.y] < 10 {
                fights += int(g[v.x][v.y])
            }
            g[v.x][v.y] = -1
            for _, d := range deltas {
                dx, dy := v.x + d.x, v.y + d.y
                if (dx >= 0) && (dy >= 0) && (dx < r) && (dy < c) && (g[dx][dy] != -1){
                    q = append(q, Tuple{dx, dy, v.len + 1})
                }
            }

        }
    }
    fmt.Println(fights)

}

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

type Location struct {
	x , y ,cost int
}

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

	n , m := getI() , getI()

	cell := make([][]int , n)
	shortestPath := make([][]int , n)
	visited := make([][]bool , n)

	var startx , starty , endx, endy int
	for i := 0 ; i < n ; i ++{
		cell[i] = make([]int , m)
		shortestPath[i] = make([]int , m)
		visited[i] = make([]bool , m)
		var s string
		s = getS()

		for j := 0 ; j < m ; j ++ {

			if s[j] == 'S'{
				endx = i
				endy = j

			} else if s[j] == 'E' {
				startx = i
				starty = j
			}else if s[j] == 'T'{
				visited[i][j] = true
			}else {
				cell[i][j] = int(s[j] - '0')
			}
		}

	}

	dx := [4]int{1,0,-1,0}
	dy := [4]int{0,1,0,-1}

	var queue []Location;

	queue = append(queue, Location{startx,starty,0} )
	visited[startx][starty] = true
	en := 1
	for ;len(queue) > 0; {

		now := queue[0]
		X := now.x
		Y := now.y;
		queue = queue[1:]

		//fmt.Printf("!!%d %d \n",X,Y)

		shortestPath[X][Y] = now.cost

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

			nextX := now.x + dx[i]
			nextY := now.y + dy[i]

			if 0<= nextX && nextX < n && 0 <= nextY && nextY < m && !visited[nextX][nextY]  {
				visited[nextX][nextY] = true
				queue = append(queue, Location{nextX,nextY,now.cost+1})
				en ++
			}

		}

	}

	fight := 0
	bound := shortestPath[endx][endy]

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

		for j := 0 ; j < m ; j ++ {
		//	fmt.Printf("!!%d %d %d %d\n",i,j,cell[i][j],visited[i][j])
			if 1 <= cell[i][j] && cell[i][j] <= 9 && shortestPath[i][j] <= bound && visited[i][j] {

				fight += cell[i][j]
			}
		}
	}
	writer.WriteString(fmt.Sprintf("%d\n", fight))

}

package main

import (
	"fmt"
//	"os"
//	"bufio"

)

type Location struct {
	x , y ,cost int
}

func main(){

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

	var n , m int
	//fmt.Fprintf(w,"kuy!@#!@#!@#\n")
	fmt.Scanf("%d%d",&n,&m)
	//fmt.Printf("!!%d %d\n",n,m)

	cell := make([][]int , n)
	shortestPath := make([][]int , n)
	visited := make([][]bool , n)

	var startx , starty , endx, endy int
	for i := 0 ; i < n ; i ++{
		cell[i] = make([]int , m)
		shortestPath[i] = make([]int , m)
		visited[i] = make([]bool , m)
		var s string
		fmt.Scan(&s)

		for j := 0 ; j < m ; j ++ {

			if s[j] == 'S'{
				endx = i
				endy = j

			} else if s[j] == 'E' {
				startx = i
				starty = j
			}else if s[j] == 'T'{
				visited[i][j] = true
			}else {
				cell[i][j] = int(s[j] - '0')
			}
		}

	}

	dx := [4]int{1,0,-1,0}
	dy := [4]int{0,1,0,-1}

	var queue []Location;

	queue = append(queue, Location{startx,starty,0} )
	en := 1
	for i := 0 ; i < en ; i ++ {
		now := queue[i]
		X := now.x
		Y := now.y;

		visited[X][Y] = true

		shortestPath[X][Y] = now.cost

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

			nextX := now.x + dx[i]
			nextY := now.y + dy[i]

			if 0<= nextX && nextX < n && 0 <= nextY && nextY < m && !visited[nextX][nextY]  {

				queue = append(queue, Location{nextX,nextY,now.cost+1})
				en ++
			}

		}

	}

	fight := 0
	bound := shortestPath[endx][endy]

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

		for j := 0 ; j < m ; j ++ {
			if 1 <= cell[i][j] && cell[i][j] <= 9 && shortestPath[i][j] <= bound {
				fight += cell[i][j]
			}
		}
	}

	fmt.Printf("%d\n",fight)

}

package main

import (
	"fmt"
//	"os"
//	"bufio"

)

type Location struct {
	x , y ,cost int
}

func main(){

	var n , m int

	fmt.Scanf("%d%d",&n,&m)

	cell := make([][]int , n)
	shortestPath := make([][]int , n)
	visited := make([][]bool , n)

	var startx , starty , endx, endy int
	for i := 0 ; i < n ; i ++{
		cell[i] = make([]int , m)
		shortestPath[i] = make([]int , m)
		visited[i] = make([]bool , m)
		var s string
		fmt.Scan(&s)

		for j := 0 ; j < m ; j ++ {

			if s[j] == 'S'{
				endx = i
				endy = j

			} else if s[j] == 'E' {
				startx = i
				starty = j
			}else if s[j] == 'T'{
				visited[i][j] = true
			}else {
				cell[i][j] = int(s[j] - '0')
			}
		}

	}

	dx := [4]int{1,0,-1,0}
	dy := [4]int{0,1,0,-1}

	var queue []Location;

	queue = append(queue, Location{startx,starty,0} )
	visited[startx][starty] = true
	en := 1
	for ;len(queue) > 0; {
		now := queue[0	]
		X := now.x
		Y := now.y;
		queue = queue[1:]

		shortestPath[X][Y] = now.cost

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

			nextX := now.x + dx[i]
			nextY := now.y + dy[i]

			if 0<= nextX && nextX < n && 0 <= nextY && nextY < m && !visited[nextX][nextY]  {
				visited[X][Y] = true
				queue = append(queue, Location{nextX,nextY,now.cost+1})
				en ++
			}

		}

	}

	fight := 0
	bound := shortestPath[endx][endy]

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

		for j := 0 ; j < m ; j ++ {
			if 1 <= cell[i][j] && cell[i][j] <= 9 && shortestPath[i][j] <= bound && visited[i][j] {
				fight += cell[i][j]
			}
		}
	}

	fmt.Printf("%d\n",fight)

}


package main

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

var _, _, _ = main, rw, do

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

func rw(r io.Reader, w io.Writer) {
	var t int
	fmt.Fscan(r, &t)
	for ti := 0; ti < t; ti++ {
		var n int
		fmt.Fscan(r, &n)
		a := make([][]int, n)
		for i := range a {
			a[i] = make([]int, n)
			for j := range a[i] {
				fmt.Fscan(r, &a[i][j])
			}
		}
		w1 := do(n, a)
		for _, v := range w1 {
			for i, v := range v {
				if i > 0 {
					fmt.Fprint(w, " ")
				}
				fmt.Fprint(w, v)
			}
			fmt.Fprintln(w)
		}
	}
}

func do(n int, a [][]int) [][]int {
	f := make([]int, n+n+2)
	for i := range f {
		f[i] = i
	}
	g := ((n + 1) >> 1) << 1
	for i := 0; i < n-1; i++ {
		for j := i + 1; j < n; j++ {
			if a[i][j] == a[j][i] {
				continue
			}
			fi := getf(f, i)
			fj := getf(f, j)
			if a[i][j] > a[j][i] {
				if fi < n && fj < n {
					f[fi], f[fj] = g, g+1
					g += 2
				} else if fi < n {
					f[fi] = fj ^ 1
				} else if fj < n {
					f[fj] = fi ^ 1
				} else if fi != fj && fi != fj^1 {
					f[fj] = fi ^ 1
					f[fj^1] = fi
				}
			} else {
				if fi < n && fj < n {
					f[fi], f[fj] = g, g
					g += 2
				} else if fi < n {
					f[fi] = fj
				} else if fj < n {
					f[fj] = fi
				} else if fi != fj && fi != fj^1 {
					f[fj] = fi
					f[fj^1] = fi ^ 1
				}
			}
		}
	}
	m := make(map[int]bool)
	for i := 0; i < n; i++ {
		fi := getf(f, i)
		if fi < n {
			continue
		}
		if m[fi] {
			continue
		} else if m[fi^1] {
			for j := 0; j < n; j++ {
				a[i][j], a[j][i] = a[j][i], a[i][j]
			}
			continue
		}
		m[fi] = true
	}
	return a
}

func getf(f []int, x int) int {
	if f[x] != x {
		f[x] = getf(f, f[x])
	}
	return f[x]
}

package main

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

func readLine() string {
	s, err := in.ReadString('\n')
	if err != nil {
		panic(err)
	}
	s = strings.ReplaceAll(s, "\n", "")
	s = strings.ReplaceAll(s, "\r", "")
	return s
}

func readLines(n int) []string {
	res := make([]string, n)
	for i := 0; i < n; i++ {
		res[i] = readLine()
	}
	return res
}

func readInt() int {
	n, err := strconv.ParseInt(readLine(), 10, 32)
	if err != nil {
		panic(err)
	}
	return int(n)
}

func readInt2() (int, int) {
	as := readInts(2)
	return as[0], as[1]
}

func readInt3() (int, int, int) {
	as := readInts(3)
	return as[0], as[1], as[2]
}

func readUint() uint64 {
	n, err := strconv.ParseUint(readLine(), 10, 64)
	if err != nil {
		panic(err)
	}
	return n
}

func readInts(n int) []int {
	s := readLine()
	as := strings.SplitN(s, " ", n)
	if len(as) != n {
		panic("invalid len")
	}
	arr := make([]int, n)
	for k, a := range as {
		x, _ := strconv.ParseInt(a, 10, 64)
		arr[k] = int(x)
	}
	return arr
}

func readStrings(n int) []string {
	s := readLine()
	as := strings.SplitN(s, " ", n)
	if len(as) != n {
		panic("invalid len")
	}
	return as
}

func arr(n, m int) [][]int {
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, m)
	}
	return a
}

const MAXN int = 1007

var in = bufio.NewReader(os.Stdin)
var out = bufio.NewWriter(os.Stdout)
var a [MAXN][MAXN]int
var pre [MAXN << 1]int

func findPre(pos int) int {
	var temp, rcd int
	temp = pre[pos]
	for temp != pre[temp] {
		temp = pre[temp]
	}
	for pre[pos] != pos {
		rcd = pre[pos]
		pre[pos] = temp
		pos = rcd
	}
	return temp
}

func main() {
	defer out.Flush()
	var t, n int
	var ch byte
	fmt.Fscan(in, &t)
	for ; t != 0; t-- {
		fmt.Fscan(in, &n)
		for i := 1; i <= 2*n; i++ {
			pre[i] = i
		}
		for i := 1; i <= n; i++ {
			for j := 1; j <= n; j++ {
				fmt.Fscan(in, &a[i][j])
			}
		}
		for i := 1; i <= n; i++ {
			for j := i + 1; j <= n; j++ {
				if a[i][j] == a[j][i] {
					continue
				}
				ii, in, jj, jn := findPre(i), findPre(i+n), findPre(j), findPre(j+n)
				if ii == jj || in == jn {
					continue
				} else if ii == jn || jj == in {
					a[i][j], a[j][i] = a[j][i], a[i][j]
					continue
				}
				if a[i][j] < a[j][i] {
					pre[ii], pre[in] = jj, jn
				} else {
					pre[ii], pre[in] = jn, jj
					a[i][j], a[j][i] = a[j][i], a[i][j]
				}
			}
		}
		for i := 1; i <= n; i++ {
			for j := 1; j <= n; j++ {
				if j == n {
					ch = '\n'
				} else {
					ch = ' '
				}
				fmt.Fprintf(out, "%d%c", a[i][j], ch)
			}
		}
	}
}


package main

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

var (
	read  = bufio.NewReader(os.Stdin)
	write = bufio.NewWriter(os.Stdout)
)

const N = 200005

func main() {
	defer write.Flush()

	var (
		T   int
		n   int
		k   int
		a   = [N]int{}
		fa  = [N]int{}
		pre = [N]int{}
	)

	fmt.Fscan(read, &T)
	for i := 0; i < T; i++ {
		k = 0
		fmt.Fscan(read, &n)
		for i := 1; i <= n; i++ {
			fmt.Fscan(read, &a[i])
			fa[a[i]] = i
		}
		for i := 1; i <= n; i++ {
			if fa[i] != 0 {
				k += 1
				pre[fa[i]] = i
			}
		}
		fmt.Fprintln(write, k)
		j := 1
		for i := 1; i <= n; i++ {
			if pre[i] == 0 {
				for fa[j] != 0 {
					j += 1
				}
				fa[j] = i
				pre[i] = j
			}
		}
		for i := 1; i <= n; i++ {
			if pre[i] == i {
				pre[i] = a[i]
				pre[fa[a[i]]] = i
				fa[a[i]] = i
			}
		}
		for i := 1; i <= n; i++ {
			fmt.Fprintf(write, "%d ", pre[i])
			pre[i] = 0
			fa[i] = 0
		}
		fmt.Fprint(write, "\n")
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := os.Stdout
	var t int
	fmt.Fscan(in,&t)
	for u:=1;u<=t;u++{
		var n int
		fmt.Fscan(in,&n)
		a := make([]int,n+5)
		for i:=1;i<=n;i++{
			fmt.Fscan(in,&a[i])
		}
		wish := map[int]int{}
		ans := make([]int,n+5)
		cnt:=0
		p := map[int]int{}
		for i:=1;i<=n;i++{
			if wish[a[i]]==0{
				wish[a[i]]=1
			}
		}
		l := []int{}
		for i:=1;i<=n;i++{
			if wish[i]==0{
				l = append(l,i)
			}
		}
		cnt = len(l)-1
		wish = map[int]int{}
		for i:=1;i<=n;i++{
			if wish[a[i]]==0{
				p[a[i]]=i
				wish[a[i]]=1
				ans[i]=a[i]
			}else{
				if l[cnt]!=i{
					ans[i]=l[cnt]
				}else{
					index := p[a[i]]
					ans[index]=i
					ans[i]=a[i]
					p[a[i]]=i
				}
				cnt--
			}
		}
		ret := 0
		for i:=1;i<=n;i++{
			if ans[i]==a[i]{
				ret++
			}
		}
		fmt.Fprintln(out,ret)
		for i:=1;i<=n;i++{
			fmt.Fprintf(out,"%d ", ans[i])
		}
		fmt.Fprintln(out)
	}
}


// Author: sighduck
// URL: https://codeforces.com/contest/1196/problem/E

package main

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

type Cell struct {
	X int
	Y int
}

type Solution struct {
	Exists    bool
	Component []Cell
}

func getComponent(b int, w int, firstCell Cell) []Cell {
	component := []Cell{}
	for i := 0; i < 2*w; i++ {
		component = append(component, Cell{X: firstCell.X, Y: firstCell.Y + i})
	}
	b -= w
	if b > 0 {
		b -= 1
		component = append(component, Cell{X: firstCell.X, Y: firstCell.Y + 2*w})
	}
	secondCell := Cell{X: firstCell.X, Y: firstCell.Y + 1}
	for {
		nextCells := []Cell{
			{X: secondCell.X - 1, Y: secondCell.Y},
			{X: secondCell.X + 1, Y: secondCell.Y},
		}
		for _, cell := range nextCells {
			if b > 0 {
				b -= 1
				component = append(component, cell)
			}
		}
		secondCell.Y += 2
		if b == 0 {
			break
		}
	}
	return component
}

func _Solve(b int, w int, firstCell Cell) Solution {
	if (b > w) && (3*w+1 < b) {
		return Solution{Exists: false}
	}
	return Solution{Exists: true, Component: getComponent(b, w, firstCell)}
}

func Solve(b int, w int) Solution {
	if b >= w {
		return _Solve(b, w, Cell{2, 3})
	}
	return _Solve(w, b, Cell{2, 2})
}

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

	var q int
	fmt.Fscanf(reader, "%d\n", &q)
	for i := 0; i < q; i++ {
		var b, w int
		fmt.Fscanf(reader, "%d %d\n", &b, &w)
		solution := Solve(b, w)
		if solution.Exists {
			fmt.Fprintf(writer, "YES\n")
			for _, cell := range solution.Component {
				fmt.Fprintf(writer, "%d %d\n", cell.X, cell.Y)
			}
		} else {
			fmt.Fprintf(writer, "NO\n")
		}
	}
}

package main

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

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)
	var q int
	fmt.Fscanf(reader, "%d\n", &q)
	for i := 0;i < q;i ++ {
		var b, w int
		fmt.Fscanf(reader, "%d %d\n", &b, &w)
		var big, small, col int
		if b > w {
			big, small, col = b, w, 2
		} else {
			big, small, col = w, b, 3
		}
		if big  - 1 > small * 3 {
			fmt.Printf("NO\n")
			continue
		} else {
			fmt.Printf("YES\n")
		}
		for i := 0;i < small;i ++ {
			fmt.Printf("%d %d\n", i * 2 + 1, col)
			fmt.Printf("%d %d\n", i * 2 + 2, col)
		}
		big = big - small
		if big > 0 {
			big --
			fmt.Printf("%d %d\n", 2 * small + 1, col)
		}
		for i := 0;i < big;i ++ {
			if i % 2 == 0 {
				fmt.Printf("%d %d\n", i + 2, col - 1)
			} else {
				fmt.Printf("%d %d\n", i + 1, col + 1)
			}
		}

	}
}

package main

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

func Sol1196E(reader io.Reader, writer io.Writer) {
	min := func(a, b int) int {
		if a <= b {
			return a
		}
		return b
	}

	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n int
	Fscan(in, &n)
	for ; n > 0; n-- {
		var b, w int
		Fscan(in, &b, &w)

		start := 2
		if b > w {
			start = 1
			b, w = w, b
		}
		if w > 3*b+1 {
			Fprintln(out, "NO")
			continue
		}

		Fprintln(out, "YES")
		for i := 0; i < b; i++ {
			Fprintln(out, 2, start+2*i)
			Fprintln(out, 2, start+2*i+1)
		}
		w -= b
		if w > 0 {
			Fprintln(out, 2, start+2*b)
			w--
		}
		minC := min(w, b)
		for i := 0; i < minC; i++ {
			Fprintln(out, 1, start+2*i+1)
		}
		w -= minC
		for i := 0; i < w; i++ {
			Fprintln(out, 3, start+2*i+1)
		}
	}
}

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

package main

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

/* solution */

func solve() {
	q := readInt()
	for i := 0; i < q; i++ {
		solveOne()
	}
}

func solveOne() {

	b, w := readInt(), readInt()

	var less, more int
	dx := 3
	if b > w {
		less, more = w, b
		dx = 2
	} else {
		less, more = b, w
	}

	if more > less*3+1 {
		println("NO")
		return
	}

	println("YES")

	for i := 0; i < less*2; i++ {
		printInt(1 + dx + i)
		printInt(3)
		println("")
	}

	remains := more - less
	if remains > 0 {
		printInt(0 + dx)
		printInt(3)
		println("")
		remains--
	}
	for i := 0; remains > 0 && i < less; i++ {
		printInt(1 + dx + i*2)
		printInt(2)
		println("")
		remains--
	}
	for i := 0; remains > 0 && i < less; i++ {
		printInt(1 + dx + i*2)
		printInt(4)
		println("")
		remains--
	}

}

/* stubs */

func main() {

	var f *os.File
	if runtime.GOROOT() == "C:\\Soft\\Go" {
		f, _ = os.Open("input.txt")
		defer f.Close()
	} else {
		f = os.Stdin
	}

	writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	reader = bufio.NewScanner(f)

	const maxCapacity = 1024 * 1024
	buf := make([]byte, maxCapacity)
	reader.Buffer(buf, maxCapacity)

	reader.Split(bufio.ScanWords)

	solve()

}

var reader *bufio.Scanner
var writer *bufio.Writer

func read() string {
	reader.Scan()
	return reader.Text()
}

func readInt() int {
	reader.Scan()
	ret, _ := strconv.Atoi(reader.Text())
	return ret
}

func print(s string) {
	writer.WriteString(s)
	writer.WriteString(" ")
}

func println(s string) {
	if len(s) > 0 {
		writer.WriteString(s)
	}
	writer.WriteString("\n")
}

func printInt(i int) {
	print(strconv.Itoa(i))
}

func readInt64() int64 {
	reader.Scan()
	ret, _ := strconv.ParseInt(reader.Text(), 10, 64)
	return ret
}

package main

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

/* solution */

func solve() {
	q := readInt()
	for i := 0; i < q; i++ {
		solveOne()
	}
}

func solveOne() {

	b, w := readInt(), readInt()

	var less, more int
	dx := 2
	if b > w {
		less, more = w, b
		dx = 3
	} else {
		less, more = b, w
	}

	if more > less*3+1 {
		println("NO")
		return
	}

	println("YES")

	for i := 0; i < less*2; i++ {
		printInt(1 + dx + i)
		printInt(3)
		println("")
	}

	remains := more - less
	if remains > 0 {
		printInt(0 + dx)
		printInt(3)
		println("")
		remains--
	}
	for i := 0; remains > 0 && i < less; i++ {
		printInt(1 + dx + i*2)
		printInt(2)
		println("")
		remains--
	}
	for i := 0; remains > 0 && i < less; i++ {
		printInt(1 + dx + i*2)
		printInt(4)
		println("")
		remains--
	}

}

/* stubs */

func main() {

	var f *os.File
	if runtime.GOROOT() == "C:\\Soft\\Go" {
		f, _ = os.Open("input.txt")
		defer f.Close()
	} else {
		f = os.Stdin
	}

	writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	reader = bufio.NewScanner(f)

	const maxCapacity = 1024 * 1024
	buf := make([]byte, maxCapacity)
	reader.Buffer(buf, maxCapacity)

	reader.Split(bufio.ScanWords)

	solve()

}

var reader *bufio.Scanner
var writer *bufio.Writer

func read() string {
	reader.Scan()
	return reader.Text()
}

func readInt() int {
	reader.Scan()
	ret, _ := strconv.Atoi(reader.Text())
	return ret
}

func print(s string) {
	writer.WriteString(s)
	writer.WriteString(" ")
}

func println(s string) {
	if len(s) > 0 {
		writer.WriteString(s)
	}
	writer.WriteString("\n")
}

func printInt(i int) {
	print(strconv.Itoa(i))
}

func readInt64() int64 {
	reader.Scan()
	ret, _ := strconv.ParseInt(reader.Text(), 10, 64)
	return ret
}

package main

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

/* solution */

func solve() {
	q := readInt()
	for i := 0; i < q; i++ {
		solveOne()
	}
}

func solveOne() {

	b, w := readInt(), readInt()

	var less, more int
	dx := 2
	if b > w {
		less, more = w, b
		dx = 3
	} else {
		less, more = b, w
	}

	if more > less*3+1 {
		println("NO")
		return
	}

	println("YES")

	for i := 0; i < less*2; i++ {
		printInt(1 + dx + i)
		printInt(1)
		println("")
	}

	remains := more - less
	if remains > 0 {
		printInt(0 + dx)
		printInt(1)
		println("")
		remains--
	}
	for i := 0; remains > 0 && i < less; i++ {
		printInt(1 + dx + i*2)
		printInt(0)
		println("")
		remains--
	}
	for i := 0; remains > 0 && i < less; i++ {
		printInt(1 + dx + i*2)
		printInt(2)
		println("")
		remains--
	}

}

/* stubs */

func main() {

	var f *os.File
	if runtime.GOROOT() == "C:\\Soft\\Go" {
		f, _ = os.Open("input.txt")
		defer f.Close()
	} else {
		f = os.Stdin
	}

	writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	reader = bufio.NewScanner(f)

	const maxCapacity = 1024 * 1024
	buf := make([]byte, maxCapacity)
	reader.Buffer(buf, maxCapacity)

	reader.Split(bufio.ScanWords)

	solve()

}

var reader *bufio.Scanner
var writer *bufio.Writer

func read() string {
	reader.Scan()
	return reader.Text()
}

func readInt() int {
	reader.Scan()
	ret, _ := strconv.Atoi(reader.Text())
	return ret
}

func print(s string) {
	writer.WriteString(s)
	writer.WriteString(" ")
}

func println(s string) {
	if len(s) > 0 {
		writer.WriteString(s)
	}
	writer.WriteString("\n")
}

func printInt(i int) {
	print(strconv.Itoa(i))
}

func readInt64() int64 {
	reader.Scan()
	ret, _ := strconv.ParseInt(reader.Text(), 10, 64)
	return ret
}

package main

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

/* solution */

func solve() {
	q := readInt()
	for i := 0; i < q; i++ {
		solveOne()
	}
}

func solveOne() {

	b, w := readInt(), readInt()

	var less, more int
	dx := 0
	if b > w {
		less, more = w, b
		dx = 1
	} else {
		less, more = b, w
	}

	if more > less*3+1 {
		println("NO")
		return
	}

	println("YES")

	for i := 0; i < less*2; i++ {
		printInt(1 + dx + i)
		printInt(1)
		println("")
	}

	remains := more - less
	if remains > 0 {
		printInt(0 + dx)
		printInt(1)
		println("")
		remains--
	}
	for i := 0; remains > 0 && i < less; i++ {
		printInt(1 + dx + i*2)
		printInt(0)
		println("")
		remains--
	}
	for i := 0; remains > 0 && i < less; i++ {
		printInt(1 + dx + i*2)
		printInt(2)
		println("")
		remains--
	}

}

/* stubs */

func main() {

	var f *os.File
	if runtime.GOROOT() == "C:\\Soft\\Go" {
		f, _ = os.Open("input.txt")
		defer f.Close()
	} else {
		f = os.Stdin
	}

	writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	reader = bufio.NewScanner(f)

	const maxCapacity = 1024 * 1024
	buf := make([]byte, maxCapacity)
	reader.Buffer(buf, maxCapacity)

	reader.Split(bufio.ScanWords)

	solve()

}

var reader *bufio.Scanner
var writer *bufio.Writer

func read() string {
	reader.Scan()
	return reader.Text()
}

func readInt() int {
	reader.Scan()
	ret, _ := strconv.Atoi(reader.Text())
	return ret
}

func print(s string) {
	writer.WriteString(s)
	writer.WriteString(" ")
}

func println(s string) {
	if len(s) > 0 {
		writer.WriteString(s)
	}
	writer.WriteString("\n")
}

func printInt(i int) {
	print(strconv.Itoa(i))
}

func readInt64() int64 {
	reader.Scan()
	ret, _ := strconv.ParseInt(reader.Text(), 10, 64)
	return ret
}

package main

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

func Sol1196E(reader io.Reader, writer io.Writer) {
	min := func(a, b int) int {
		if a <= b {
			return a
		}
		return b
	}

	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n int
	Fscan(in, &n)
	for ; n > 0; n-- {
		var b, w int
		Fscan(in, &b, &w)

		start := 2
		if b > w {
			start = 1
			b, w = w, b
		}
		if w > 3*b+1 {
			Fprintln(out, "NO")
			continue
		}

		Fprintln(out, "YES")
		for i := 0; i < b; i++ {
			Fprintln(out, 2, start+2*i)
			Fprintln(out, 2, start+2*i+1)
		}
		w -= b
		minC := min(w, b)
		for i := 0; i < minC; i++ {
			Fprintln(out, 1, start+2*i+1)
		}
		w -= b
		for i := 0; i < w; i++ {
			Fprintln(out, 3, start+2*i+1)
		}
	}
}

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

// Author: sighduck
// URL: https://codeforces.com/contest/1196/problem/E

package main

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

type Cell struct {
	X int
	Y int
}

type Solution struct {
	Exists    bool
	Component []Cell
}

func shiftCell(cell Cell, shift int) Cell {
	return Cell{X: cell.X, Y: cell.Y + shift}
}

func getComponent(w int, b int, firstMove bool, component []Cell, lastWhiteCell Cell, shift int) []Cell {
	if w == 0 && b == 0 {
		return component
	}
	if firstMove {
		whiteCell := Cell{X: 2, Y: 2}
		blackCells := []Cell{
			{X: whiteCell.X, Y: whiteCell.Y - 1},
			{X: whiteCell.X - 1, Y: whiteCell.Y},
			{X: whiteCell.X, Y: whiteCell.Y + 1},
			{X: whiteCell.X + 1, Y: whiteCell.Y},
		}
		component = append(component, shiftCell(whiteCell, shift))
		for _, cell := range blackCells {
			if b > 0 {
				b -= 1
				component = append(component, shiftCell(cell, shift))
			}
		}
		return getComponent(w-1, b, false, component, whiteCell, shift)
	}
	whiteCell := Cell{X: lastWhiteCell.X, Y: lastWhiteCell.Y + 2}
	blackCells := []Cell{
		{X: whiteCell.X - 1, Y: whiteCell.Y},
		{X: whiteCell.X, Y: whiteCell.Y + 1},
		{X: whiteCell.X + 1, Y: whiteCell.Y},
	}
	component = append(component, shiftCell(whiteCell, shift))
	for _, cell := range blackCells {
		if b > 0 {
			b -= 1
			component = append(component, shiftCell(cell, shift))
		}
	}
	return getComponent(w-1, b, false, component, whiteCell, shift)
}

func _Solve(b int, w int, shift int) Solution {
	if (b > w) && (3*w+1 < b) {
		return Solution{Exists: false}
	}
	return Solution{Exists: true, Component: getComponent(w, b, true, []Cell{}, Cell{}, shift)}
}

func Solve(b int, w int) Solution {
	if b >= w {
		return _Solve(b, w, 0)
	}
	return _Solve(w, b, 1)
}

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

	var q int
	fmt.Fscanf(reader, "%d\n", &q)
	for i := 0; i < q; i++ {
		var b, w int
		fmt.Fscanf(reader, "%d %d\n", &b, &w)
		solution := Solve(b, w)
		if solution.Exists {
			fmt.Fprintf(writer, "YES\n")
			for _, cell := range solution.Component {
				fmt.Fprintf(writer, "%d %d\n", cell.X, cell.Y)
			}
		} else {
			fmt.Fprintf(writer, "NO\n")
		}
	}
}