/*
URL:
https://codeforces.com/contest/1418/problem/D
*/

package main

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

var (
	n, q int
	P    []int
)

func main() {
	defer stdout.Flush()

	n, q = readi2()
	P = readis(n)
	sort.Sort(sort.IntSlice(P))

	pt := NewTreap()
	dt := NewTreap()

	pt.Insert(P[0])
	for i := 1; i < n; i++ {
		diff := P[i] - P[i-1]
		pt.Insert(P[i])
		dt.Insert(diff)
	}
	maxp := pt.FindMaximum()
	minp := pt.FindMinimum()
	maxd := dt.FindMaximum()
	if maxd == nil {
		printf("%d\n", 0)
	} else {
		total := maxp.key - minp.key
		ans := total - maxd.key
		printf("%d\n", ans)
	}

	for i := 0; i < q; i++ {
		t, x := readi2()
		if t == 1 {
			// add
			r := pt.MinGreater(x)
			l := pt.MaxLess(x)
			if r != nil {
				diff := r.key - x
				dt.Insert(diff)
			}
			if l != nil {
				diff := x - l.key
				dt.Insert(diff)
			}
			if r != nil && l != nil {
				diff := r.key - l.key
				dt.Delete(diff)
			}
			pt.Insert(x)

			// calc
			maxp := pt.FindMaximum()
			minp := pt.FindMinimum()
			maxd := dt.FindMaximum()
			if maxd == nil {
				printf("%d\n", 0)
			} else {
				total := maxp.key - minp.key
				ans := total - maxd.key
				printf("%d\n", ans)
			}
		} else {
			// delete
			r := pt.MinGreater(x)
			l := pt.MaxLess(x)
			if r != nil {
				// 右のdiffを削除
				diff := r.key - x
				dt.Delete(diff)
			}
			if l != nil {
				// 左のdiffを削除
				diff := x - l.key
				dt.Delete(diff)
			}
			if r != nil && l != nil {
				// diffを追加
				diff := r.key - l.key
				dt.Insert(diff)
			}
			pt.Delete(x)

			// calc
			maxp := pt.FindMaximum()
			minp := pt.FindMinimum()
			maxd := dt.FindMaximum()
			if maxd == nil {
				printf("%d\n", 0)
			} else {
				total := maxp.key - minp.key
				ans := total - maxd.key
				printf("%d\n", ans)
			}
		}
	}
}

// Treap usage
// tr := NewTreap() 				// constructor
// tr.Insert(key) 					// insert one key node
// cnt := tr.Count(key) 		// return a number of key nodes
// node := tr.Find(key) 		// return a pointer
// min := tr.FindMinimum() 	// return a pointer
// max := tr.FindMaximum() 	// return a pointer
// tr.Delete(key) 					// delete one key node
// node := tr.MinGeq(x) 		// return a pointer
// node := tr.MinGreater(x) // return a pointer
// node := tr.MaxLeq(x) 		// return a pointer
// node := tr.MaxLess(x) 		// return a pointer

// fmt.Println(PrintIntsLine(tr.Inorder()...))
// fmt.Println(PrintIntsLine(tr.Preorder()...))
// tr.InsertBySettingPri(key, p)

type Node struct {
	key, priority int
	right, left   *Node
}

type Treap struct {
	root    *Node
	cnts    map[int]int
	randInt func() int
}

/*************************************/
// Public method
/*************************************/

// NewTreap returns a pointer of a Treap instance.
func NewTreap() *Treap {
	tr := new(Treap)

	tr.root = nil
	tr.cnts = make(map[int]int)

	// XorShiftによる乱数生成
	// 下記URLを参考
	// https://qiita.com/tubo28/items/f058582e457f6870a800#lower_bound-upper_bound
	_gtx, _gty, _gtz, _gtw := 123456789, 362436069, 521288629, 88675123
	tr.randInt = func() int {
		tt := (_gtx ^ (_gtx << 11))
		_gtx = _gty
		_gty = _gtz
		_gtz = _gtw
		_gtw = (_gtw ^ (_gtw >> 19)) ^ (tt ^ (tt >> 8))
		return _gtw
	}

	return tr
}

// Count method returns the number of the key.
// If there hasn't been the key in the treap, this returns 0.
func (tr *Treap) Count(key int) int {
	return tr.cnts[key]
}

// InsertBySettingPri method inserts a new node consisting of new key and priority.
// A duplicate key is ignored and nothing happens.
// func (tr *Treap) InsertBySettingPri(key, priority int) {
// 	tr.root = tr.insert(tr.root, key, priority)
// }

// Insert method inserts a new node consisting o new key.
// The priority is automatically set by random value.
// A duplicate key is ignored and nothing happens.
func (tr *Treap) Insert(key int) {
	preCnt := tr.Count(key)
	tr.increase(key, 1)
	if preCnt > 0 {
		return
	}

	tr.root = tr.insert(tr.root, key, tr.randInt())
}

// Find returns a node that has an argument key value.
// Find returns nil when there is no node that has an argument key value.
func (tr *Treap) Find(key int) *Node {
	cnt := tr.cnts[key]
	if cnt == 0 {
		return nil
	}

	u := tr.root
	for u != nil && key != u.key {
		if key < u.key {
			u = u.left
		} else {
			u = u.right
		}
	}
	return u
}

// FindMinimum returns a node that has the minimum key in the treap.
// FindMinimum returns nil when there is no nodes.
func (tr *Treap) FindMinimum() *Node {
	u := tr.root
	for u != nil && u.left != nil {
		u = u.left
	}
	return u
}

// FindMaximum returns a node that has the maximum key in the treap.
// FindMaximum returns nil when there is no nodes.
func (tr *Treap) FindMaximum() *Node {
	u := tr.root
	for u != nil && u.right != nil {
		u = u.right
	}
	return u
}

// Delete method deletes a node that has an argument key value.
// A duplicate key is ignored and nothing happens.
func (tr *Treap) Delete(key int) {
	tr.decrease(key, 1)
	curCnt := tr.Count(key)
	if curCnt > 0 {
		return
	}

	tr.root = tr.delete(tr.root, key)
}

// Inorder returns a slice consisting of treap nodes in order of INORDER.
// The nodes are sorted by key values.
func (tr *Treap) Inorder() []int {
	res := make([]int, 0, 200000+5)
	tr.inorder(tr.root, &res)
	return res
}

// Preorder returns a slice consisting of treap nodes in order of PREORDER.
func (tr *Treap) Preorder() []int {
	res := make([]int, 0, 200000+5)
	tr.preorder(tr.root, &res)
	return res
}

// MinGeq returns a node that has MINIMUM KEY MEETING key >= x.
// https://qiita.com/tubo28/items/f058582e457f6870a800#lower_bound-upper_bound
func (tr *Treap) MinGeq(x int) *Node {
	return tr.biggerLowerBound(tr.root, x)
}

// MinGreater returns a node that has MINIMUM KEY MEETING key > x.
// https://qiita.com/tubo28/items/f058582e457f6870a800#lower_bound-upper_bound
func (tr *Treap) MinGreater(x int) *Node {
	return tr.biggerUpperBound(tr.root, x)
}

// MaxLeq returns a node that has MAXIMUM KEY MEETING key <= x.
// for AGC005-B
func (tr *Treap) MaxLeq(x int) *Node {
	return tr.smallerUpperBound(tr.root, x)
}

// MaxLess returns a node that has MAXIMUM KEY MEETING key < x.
// for AGC005-B
func (tr *Treap) MaxLess(x int) *Node {
	return tr.smallerLowerBound(tr.root, x)
}

/*************************************/
// Private method
/*************************************/

func (tr *Treap) increase(key, num int) {
	tr.cnts[key] += num
}

func (tr *Treap) decrease(key, num int) {
	curCnt := tr.cnts[key]
	if curCnt-num < 0 {
		panic("too many elements is deleted!")
	}

	tr.cnts[key] -= num
}

func (tr *Treap) insert(t *Node, key, priority int) *Node {
	// 葉に到達したら新しい節点を生成して返す
	if t == nil {
		node := new(Node)
		node.key, node.priority = key, priority
		return node
	}

	// 重複したkeyは無視
	if key == t.key {
		return t
	}

	if key < t.key {
		// 左の子へ移動
		t.left = tr.insert(t.left, key, priority) // 左の子へのポインタを更新
		// 左の子の方が優先度が高い場合右回転
		if t.priority < t.left.priority {
			t = tr.rightRotate(t)
		}
	} else {
		// 右の子へ移動
		t.right = tr.insert(t.right, key, priority) // 右の子へのポインタを更新
		if t.priority < t.right.priority {
			// 右の子の方が優先度が高い場合左回転
			t = tr.leftRotate(t)
		}
	}

	return t
}

// 削除対象の節点を回転によって葉まで移動させた後に削除する
func (tr *Treap) delete(t *Node, key int) *Node {
	if t == nil {
		return nil
	}

	// 削除対象を検索
	if key < t.key {
		t.left = tr.delete(t.left, key)
	} else if key > t.key {
		t.right = tr.delete(t.right, key)
	} else {
		// 削除対象を発見、葉ノードとなるように回転を繰り返す
		return tr._delete(t, key)
	}

	return t
}

// 削除対象の節点の場合
func (tr *Treap) _delete(t *Node, key int) *Node {
	if t.left == nil && t.right == nil {
		// 葉の場合
		return nil
	} else if t.left == nil {
		// 右の子のみを持つ場合は左回転
		t = tr.leftRotate(t)
	} else if t.right == nil {
		// 左の子のみを持つ場合は右回転
		t = tr.rightRotate(t)
	} else {
		// 優先度が高い方を持ち上げる
		if t.left.priority > t.right.priority {
			t = tr.rightRotate(t)
		} else {
			t = tr.leftRotate(t)
		}
	}

	return tr.delete(t, key)
}

func (tr *Treap) rightRotate(t *Node) *Node {
	s := t.left
	t.left = s.right
	s.right = t
	return s
}

func (tr *Treap) leftRotate(t *Node) *Node {
	s := t.right
	t.right = s.left
	s.left = t
	return s
}

// rootからスタートする
func (tr *Treap) biggerLowerBound(t *Node, x int) *Node {
	if t == nil {
		return nil
	} else if t.key >= x {
		// 探索キーxが現在のノードキー以下の場合、左を探索する
		node := tr.biggerLowerBound(t.left, x)
		if node != nil {
			return node
		} else {
			return t
		}
	} else {
		// 探索キーxが現在のノードキーより大きい場合、右を探索する
		return tr.biggerLowerBound(t.right, x)
	}
}

// rootからスタートする
func (tr *Treap) biggerUpperBound(t *Node, x int) *Node {
	if t == nil {
		return nil
	} else if t.key > x {
		// 探索キーxが現在のノードキーより小さい場合、左を探索する
		node := tr.biggerUpperBound(t.left, x)
		if node != nil {
			return node
		} else {
			return t
		}
	} else {
		// 探索キーxが現在のノードキー以上の場合、右を探索する
		return tr.biggerUpperBound(t.right, x)
	}
}

// rootからスタートする
func (tr *Treap) smallerUpperBound(t *Node, x int) *Node {
	if t == nil {
		return nil
	} else if t.key <= x {
		node := tr.smallerUpperBound(t.right, x)
		if node != nil {
			return node
		} else {
			return t
		}
	} else {
		return tr.smallerUpperBound(t.left, x)
	}
}

// rootからスタートする
func (tr *Treap) smallerLowerBound(t *Node, x int) *Node {
	if t == nil {
		return nil
	} else if t.key < x {
		node := tr.smallerLowerBound(t.right, x)
		if node != nil {
			return node
		} else {
			return t
		}
	} else {
		return tr.smallerLowerBound(t.left, x)
	}
}

func (tr *Treap) inorder(u *Node, res *[]int) {
	if u == nil {
		return
	}
	tr.inorder(u.left, res)
	*res = append(*res, u.key)
	tr.inorder(u.right, res)
}

func (tr *Treap) preorder(u *Node, res *[]int) {
	if u == nil {
		return
	}
	*res = append(*res, u.key)
	tr.preorder(u.left, res)
	tr.preorder(u.right, res)
}

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

/********** common constants **********/

const (
	MOD = 1000000000 + 7
	// MOD          = 998244353
	ALPH_N  = 26
	INF_I64 = math.MaxInt64
	INF_B60 = 1 << 60
	INF_I32 = math.MaxInt32
	INF_B30 = 1 << 30
	NIL     = -1
)

// modi can calculate a right residual whether value is positive or negative.
func modi(val, m int) int {
	res := val % m
	if res < 0 {
		res += m
	}
	return res
}

// modll can calculate a right residual whether value is positive or negative.
func modll(val, m int64) int64 {
	res := val % m
	if res < 0 {
		res += m
	}
	return res
}

/********** bufio setting **********/

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

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

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

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

//str := reads()
//i := readi()
//X := readis(n)
//S := readrs()
//a := readf()
//A := readfs(n)

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

/*********** Input ***********/

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

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

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

// readi returns an integer.
func readi() int {
	return int(_readInt64())
}
func readi2() (int, int) {
	return int(_readInt64()), int(_readInt64())
}
func readi3() (int, int, int) {
	return int(_readInt64()), int(_readInt64()), int(_readInt64())
}
func readi4() (int, int, int, int) {
	return int(_readInt64()), int(_readInt64()), int(_readInt64()), int(_readInt64())
}

// readll returns as integer as int64.
func readll() int64 {
	return _readInt64()
}
func readll2() (int64, int64) {
	return _readInt64(), _readInt64()
}
func readll3() (int64, int64, int64) {
	return _readInt64(), _readInt64(), _readInt64()
}
func readll4() (int64, int64, int64, int64) {
	return _readInt64(), _readInt64(), _readInt64(), _readInt64()
}

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

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

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

// readf returns an float64.
func readf() float64 {
	return float64(_readFloat64())
}

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

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

// readrs returns a rune slice.
func readrs() []rune {
	return []rune(reads())
}

/*********** Output ***********/

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

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

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

	return string(res)
}

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

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

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

	return string(res)
}

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

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

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

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

package main

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

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

	n, q := readTwoNums(reader)
	P := readNNums(reader, n)
	queries := make([][]int, q)
	for i := 0; i < q; i++ {
		queries[i] = readNNums(reader, 2)
	}
	res := solve(n, P, q, queries)
	fmt.Print(res)
}

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

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

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

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

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

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

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

	return i
}

func solve(n int, P []int, q int, queries [][]int) string {
	sort.Ints(P)
	var node *Node

	for i := 0; i < n; i++ {
		node = node.Set(P[i])
	}

	get := func() int {
		if node == nil {
			// empty node
			return 0
		}
		res := node.maxVal - node.minVal - node.maxGap
		return int(res)
	}

	var buf bytes.Buffer

	buf.WriteString(fmt.Sprintf("%d\n", get()))

	for i := 0; i < q; i++ {
		cur := queries[i]
		if cur[0] == 0 {
			node = node.Unset(cur[1])
		} else {
			node = node.Set(cur[1])
		}
		buf.WriteString(fmt.Sprintf("%d\n", get()))

	}

	return buf.String()
}

const MAX_X = 1000000010
const INF = 1 << 28
const N_INF = -INF

type Node struct {
	left, right *Node
	start, end  int
	maxGap      int64
	minVal      int64
	maxVal      int64
}

func NewNode(start, end int) *Node {
	node := new(Node)
	node.start = start
	node.end = end
	node.maxGap = -1
	node.minVal = INF
	node.maxVal = N_INF

	return node
}

func (node *Node) merge() *Node {
	if node.left == nil && node.right == nil {
		return nil
	}

	if node.right == nil {
		node.minVal = node.left.minVal
		node.maxVal = node.left.maxVal
		node.maxGap = node.left.maxGap
	} else if node.left == nil {
		node.minVal = node.right.minVal
		node.maxVal = node.right.maxVal
		node.maxGap = node.right.maxGap
	} else {
		node.minVal = min(node.left.minVal, node.right.minVal)
		node.maxVal = max(node.left.maxVal, node.right.maxVal)
		node.maxGap = max(node.right.minVal-node.left.maxVal, max(node.left.maxGap, node.right.maxGap))
	}

	return node
}

func (node *Node) Unset(p int) *Node {
	if node.start+1 == node.end {
		// node.start == p
		return nil
	}

	mid := (node.start + node.end) / 2

	if p < mid {
		node.left = node.left.Unset(p)
	} else {
		node.right = node.right.Unset(p)
	}

	return node.merge()
}

func (root *Node) Set(p int) *Node {
	var loop func(node *Node, start, end int) *Node

	loop = func(node *Node, start, end int) *Node {
		if node == nil {
			node = NewNode(start, end)
		}
		if start+1 == end {
			node.minVal = int64(p)
			node.maxVal = int64(p)
			node.maxGap = 0
			return node
		}

		mid := (start + end) / 2

		if p < mid {
			node.left = loop(node.left, start, mid)
		} else {
			node.right = loop(node.right, mid, end)
		}

		return node.merge()
	}

	return loop(root, 0, MAX_X)
}

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

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

package main

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

type node struct {
	lr       [2]*node
	priority uint
	key      int
	dupCnt   int
	sz       int // JUST IGNORE
}

func (o *node) cmp(b int) int8 {
	switch {
	case b < o.key:
		return 0
	case b > o.key:
		return 1
	default:
		return -1
	}
}

func (o *node) size() int {
	if o != nil {
		return o.sz
	}
	return 0
}

func (o *node) maintain() { o.sz = o.dupCnt + o.lr[0].size() + o.lr[1].size() }

func (o *node) rotate(d int8) *node {
	x := o.lr[d^1]
	o.lr[d^1] = x.lr[d]
	x.lr[d] = o
	o.maintain()
	x.maintain()
	return x
}

type treap struct {
	rd   uint
	root *node
}

func (t *treap) fastRand() uint {
	t.rd ^= t.rd << 13
	t.rd ^= t.rd >> 17
	t.rd ^= t.rd << 5
	return t.rd
}

func (t *treap) _put(o *node, key int) *node {
	if o == nil {
		return &node{priority: t.fastRand(), key: key, dupCnt: 1, sz: 1}
	}
	if d := o.cmp(key); d >= 0 {
		o.lr[d] = t._put(o.lr[d], key)
		if o.lr[d].priority > o.priority {
			o = o.rotate(d ^ 1)
		}
	} else {
		o.dupCnt++
	}
	o.maintain()
	return o
}

func (t *treap) put(key int) { t.root = t._put(t.root, key) }

func (t *treap) _delete(o *node, key int) *node {
	if o == nil {
		return nil
	}
	if d := o.cmp(key); d >= 0 {
		o.lr[d] = t._delete(o.lr[d], key)
	} else {
		if o.dupCnt > 1 {
			o.dupCnt--
		} else {
			if o.lr[1] == nil {
				return o.lr[0]
			}
			if o.lr[0] == nil {
				return o.lr[1]
			}
			d = 0
			if o.lr[0].priority > o.lr[1].priority {
				d = 1
			}
			o = o.rotate(d)
			o.lr[d] = t._delete(o.lr[d], key)
		}
	}
	o.maintain()
	return o
}

func (t *treap) delete(key int) { t.root = t._delete(t.root, key) }

func (t *treap) get(key int) *node {
	for o := t.root; o != nil; {
		if d := o.cmp(key); d >= 0 {
			o = o.lr[d]
		} else {
			return o
		}
	}
	return nil
}

func (t *treap) max() (max *node) {
	for o := t.root; o != nil; o = o.lr[1] {
		max = o
	}
	return
}
func (t *treap) min() (min *node) {
	for o := t.root; o != nil; o = o.lr[0] {
		min = o
	}
	return
}

func (t *treap) lowerBound(key int) (lb *node) {
	for o := t.root; o != nil; {
		switch c := o.cmp(key); {
		case c == 0:
			lb = o
			o = o.lr[0]
		case c > 0:
			o = o.lr[1]
		default:
			return o
		}
	}
	return // NOTE: check nil
}

func (t *treap) prev(key int) (prev *node) {
	for o := t.root; o != nil; {
		if o.cmp(key) <= 0 {
			o = o.lr[0]
		} else {
			prev = o
			o = o.lr[1]
		}
	}
	return // NOTE: check nil
}

func newTreap() *treap { return &treap{rd: 1} }

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

	diff := newTreap()
	vs := newTreap()
	var n, q int
	Fscan(in, &n, &q)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
		vs.put(a[i])
	}
	sort.Ints(a)
	for i := 1; i < n; i++ {
		diff.put(a[i] - a[i-1])
	}
	if vs.root.size() < 3 {
		Fprintln(out, 0)
	} else {
		//println(vs.max().key,vs.min().key ,diff.max().key)
		Fprintln(out, vs.max().key-vs.min().key-diff.max().key)
	}
	for ; q > 0; q-- {
		var tp, x int
		Fscan(in, &tp, &x)
		if tp == 0 {
			// remove
			mi, mx := vs.min().key, vs.max().key
			if vs.root.size() > 1 {
				if x == mi {
					up := vs.lowerBound(x + 1).key
					diff.delete(up - x)
				} else if x == mx {
					low := vs.prev(x).key
					diff.delete(x - low)
				} else {
					up := vs.lowerBound(x + 1).key
					low := vs.prev(x).key
					diff.delete(up - x)
					diff.delete(x - low)
					diff.put(up - low)
				}
			}
			vs.delete(x)
		} else {
			// add
			if vs.root.size() > 0 {
				mi, mx := vs.min().key, vs.max().key
				if x < mi {
					diff.put(mi - x)
				} else if x > mx {
					diff.put(x - mx)
				} else {
					up := vs.lowerBound(x).key
					low := vs.prev(up).key
					diff.delete(up - low)
					diff.put(up - x)
					diff.put(x - low)
				}
			}
			vs.put(x)
		}
		if vs.root.size() < 3 {
			Fprintln(out, 0)
		} else {
			//println(vs.max().key,vs.min().key ,diff.max().key)
			Fprintln(out, vs.max().key-vs.min().key-diff.max().key)
		}
	}
}

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


package main

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

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

	var n, m, v, w, wt int
	Fscan(in, &n, &m)
	g := make([][]int, n)
	for i := range g {
		g[i] = make([]int, n)
		for j := range g[i] {
			g[i][j] = 1e9 + 1e3
		}
	}
	for ; m > 0; m-- {
		Fscan(in, &v, &w, &wt)
		g[v][w] = wt
	}
	for st, row := range g {
		dis := append([]int{}, row...)
		vis := make([]bool, n)
		for {
			v := -1
			for w, b := range vis {
				if !b && (v < 0 || dis[w] < dis[v]) {
					v = w
				}
			}
			if v < 0 {
				break
			}
			vis[v] = true
			dis[(v+1)%n] = min(dis[(v+1)%n], dis[v]+1)
			for j, wt := range g[v] {
				dis[(j+dis[v])%n] = min(dis[(j+dis[v])%n], dis[v]+wt)
			}
		}
		for i, d := range dis {
			if i == st {
				d = 0
			}
			Fprint(out, d, " ")
		}
		Fprintln(out)
	}
}

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

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

package main

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

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

	var n, m, v, w, wt int
	Fscan(in, &n, &m)
	g := make([][]int, n)
	for i := range g {
		g[i] = make([]int, n)
		for j := range g[i] {
			g[i][j] = 1e9 + 1e3
		}
	}
	for ; m > 0; m-- {
		Fscan(in, &v, &w, &wt)
		g[v][w] = wt
	}
	for st, row := range g {
		dis := append([]int{}, row...)
		vis := make([]bool, n)
		for {
			v := -1
			for w, b := range vis {
				if !b && (v < 0 || dis[w] < dis[v]) {
					v = w
				}
			}
			if v < 0 {
				break
			}
			vis[v] = true
			d := dis[v]
			dis[(v+1)%n] = min(dis[(v+1)%n], d+1)
			for j, wt := range g[v] {
				dis[(j+d)%n] = min(dis[(j+d)%n], d+wt)
			}
		}
		for i, d := range dis {
			if i == st {
				d = 0
			}
			Fprint(out, d, " ")
		}
		Fprintln(out)
	}
}

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

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

package main

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

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

	var n, m, v, w, wt int
	Fscan(in, &n, &m)
	g := make([][]int, n)
	for i := range g {
		g[i] = make([]int, n)
		for j := range g[i] {
			g[i][j] = 1e9 + 1e3
		}
	}
	for ; m > 0; m-- {
		Fscan(in, &v, &w, &wt)
		g[v][w] = wt
	}
	for st, row := range g {
		dis := append([]int{}, row...)
		vis := make([]bool, n)
		for {
			v := -1
			for w, b := range vis {
				if !b && (v < 0 || dis[w] < dis[v]) {
					v = w
				}
			}
			if v < 0 {
				break
			}
			vis[v] = true
			dis[(v+1)%n] = min(dis[(v+1)%n], dis[v]+1)
			for j := 0; j < n; j++ {
				dis[(j+dis[v])%n] = min(dis[(j+dis[v])%n], dis[v]+g[v][j])
			}
		}
		for i, d := range dis {
			if i == st {
				d = 0
			}
			Fprint(out, d, " ")
		}
		Fprintln(out)
	}
}

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

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

package main

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

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

	var n, m, v, w, wt int
	Fscan(in, &n, &m)
	g := make([][]int, n)
	for i := range g {
		g[i] = make([]int, n)
		for j := range g[i] {
			g[i][j] = 1e9
		}
	}
	for ; m > 0; m-- {
		Fscan(in, &v, &w, &wt)
		g[v][w] = wt
	}
	for st, row := range g {
		dis := append([]int{}, row...)
		vis := make([]bool, n)
		for {
			v := -1
			for w, b := range vis {
				if !b && (v < 0 || dis[w] < dis[v]) {
					v = w
				}
			}
			if v < 0 {
				break
			}
			vis[v] = true
			dis[(v+1)%n] = min(dis[(v+1)%n], dis[v]+1)
			for j := 0; j < n; j++ {
				dis[(j+dis[v])%n] = min(dis[(j+dis[v])%n], dis[v]+g[v][j])
			}
		}
		for i, d := range dis {
			if i == st {
				d = 0
			}
			Fprint(out, d, " ")
		}
		Fprintln(out)
	}
}

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

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


package main

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

type FastIn struct {
	scanner *bufio.Scanner
}

func MakeFastIn() (obj FastIn)  {
	obj = FastIn{}
	obj.scanner = bufio.NewScanner(os.Stdin)
	obj.scanner.Split(bufio.ScanWords)
	return
}

func (fi *FastIn) ReadText() string {
	fi.scanner.Scan()
	return fi.scanner.Text()
}

func (fi *FastIn) ReadInt() int {
	ret, err := strconv.Atoi(fi.ReadText())
	if err != nil {
		panic(err)
	}
	return ret
}

func (fi *FastIn) ReadInts(arr []int) {
	for i := 0; i < len(arr); i++ {
		arr[i] = fi.ReadInt()
	}
}
func Min (a int64, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func (fi *FastIn) ReadInt64() int64 {
	ret, err := strconv.ParseInt(fi.ReadText(), 10, 64)
	if err != nil {
		panic(err)
	}
	return ret
}

var x,k,y int64

func GetCost(arr []int, left_b int, right_b int) (int64, bool) {
	any_gt := false
	max_bound := left_b
	if max_bound < right_b {
		max_bound = right_b
	}
	for _,v := range arr {
		if  v > max_bound {
			any_gt = true
		}
	}

	len := int64(len(arr))
	if any_gt {
		if len < k {
			return 0, false
		}
		return Min((len/k)*x+(len%k)*y,x+(len-k)*y), true
	} else {
		return Min((len/k)*x+(len%k)*y, len * y), true
	}
}

func main() {
	fin := MakeFastIn()
	n, m := fin.ReadInt(), fin.ReadInt()
	x, k, y = fin.ReadInt64(), fin.ReadInt64(), fin.ReadInt64()
	a,b := make([]int, n), make([]int, m)
	fin.ReadInts(a)
	fin.ReadInts(b)

	a = append(a, -1)
	b = append(b,-1)
	j := 0
	var cost int64 = 0
	for i := 0; i < len(b); i++ {
		found := false
		tmp := make([]int, 0)
		for ; j < len(a) && !found; j++ {
			if a[j] == b[i] {
				found = true
			} else {
				tmp = append(tmp, a[j])
			}
		}
		if !found {
			fmt.Printf("-1")
			return
		}

		left_b, right_b := -1, b[i]
		if i > 0 {
			left_b = b[i-1]
		}
		ret, ok := GetCost(tmp, left_b, right_b)
		if !ok {
			fmt.Printf("-1")
			return
		}
		cost += ret
	}

	fmt.Printf("%d", cost)
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1380D(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, m, cur int
	var x, k, y, s int64
	Fscan(in, &n, &m, &x, &k, &y)
	a := make([]int, n)
	pos := make([]int, n+1)
	for i := range a {
		Fscan(in, &a[i])
		pos[a[i]] = i
	}

	pre := -1
	f := func() bool {
		l := int64(cur - pre - 1)
		if l < 0 {
			return true
		}
		max := 0
		for _, v := range a[pre+1 : cur] {
			if v > max {
				max = v
			}
		}
		edge := 0
		if pre >= 0 {
			edge = a[pre]
		}
		if cur < n && a[cur] > edge {
			edge = a[cur]
		}
		if l < k {
			if max > edge {
				return true
			}
			s += l * y
		} else if k*y < x {
			if max < edge {
				s += l * y
			} else {
				s += (l-k)*y + x
			}
		} else {
			s += l%k*y + l/k*x
		}
		return false
	}
	for ; m > 0; m-- {
		Fscan(in, &cur)
		cur = pos[cur]
		if f() {
			Fprint(out, -1)
			return
		}
		pre = cur
	}
	cur = n
	if f() {
		Fprint(out, -1)
		return
	}
	Fprint(out, s)
}

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

package main

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

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

var (
	n, m, x, y, k int
	a, b, p       [200010]int
	ans           int64
	//debug
	//xcnt, ycnt int
)

func max(_x, _y int) int {
	if _x > _y {
		return _x
	}
	return _y
}

func min(_x, _y int) int {
	if _x < _y {
		return _x
	}
	return _y
}

func fail() {
	Fprintf(out, "-1\n")
	out.Flush()
	os.Exit(0)
}

func calc(len int, mustx int64) int64 {
	if mustx * int64(k) > int64(len) {
		fail()
	}
	x64, y64, k64, ret := int64(x), int64(y), int64(k), int64(0)
	ret += x64 * mustx
	len -= int(mustx) * k
	if x64 < y64*k64 {
		ret += x64 * int64(len/k)
		len %= k
	}
	ret += y64 * int64(len)
	return ret
}

func solve(l, r, mx int) int64 {
	ret, length, mustx := int64(0), r-l+1, int64(0)

	for i := l; i <= r; i++ {
		if a[i] > mx {
			mustx = 1
			break
		}
	}

	return ret + calc(length, mustx)
}

func main() {
	defer out.Flush()

	Fscan(in, &n, &m, &x, &k, &y)
	for i := 1; i <= n; i++ {
		Fscan(in, &a[i])
	}
	for i := 1; i <= m; i++ {
		Fscan(in, &b[i])
	}

	if m > n {
		fail()
	}

	for i, j := 1, 1; j <= m; i, j = i+1, j+1 {
		for i <= n && a[i] != b[j] {
			i++
		}
		if i > n {
			fail()
		}
		p[j] = i
	}

	if p[1] > 1 {
		ans += solve(1, p[1]-1, b[1])
	}
	if p[m] < n {
		ans += solve(p[m]+1, n, b[m])
	}

	for i := 1; i < m; i++ {
		if p[i]+1 == p[i+1] {
			continue
		}
		ans += solve(p[i]+1, p[i+1]-1, max(b[i], b[i+1]))
	}

	Fprintln(out, ans)
}

package main

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

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

var (
	n, m, x, y, k int
	a, b, p       [200010]int
	ans           int64
	qu            []int
	//debug
	//xcnt, ycnt int
)

func max(_x, _y int) int {
	if _x > _y {
		return _x
	}
	return _y
}

func min(_x, _y int) int {
	if _x < _y {
		return _x
	}
	return _y
}

func fail() {
	Fprintf(out, "-1\n")
	out.Flush()
	os.Exit(0)
}

func calc(len int) int64 {
	x64, y64, k64, ret := int64(x), int64(y), int64(k), int64(0)
	if x64 < y64*k64 {
		ret += x64 * int64(len/k)
		len %= k
	}
	ret += y64 * int64(len)
	return ret
}

func solve(l, r, mx int) int64 {
	ret, length := int64(0), r-l+1
	qu = []int{}

	for i := l; i <= r; i++ {
		if a[i] > mx {
			qu = append(qu, i)
		}
	}

	kcnt := (len(qu) + k - 1) / k
	if length < kcnt * k {
		fail()
	}
	length -= kcnt * k
	ret += int64(x) * int64(kcnt)

	return ret + calc(length)
}

func main() {
	defer out.Flush()

	Fscan(in, &n, &m, &x, &k, &y)
	for i := 1; i <= n; i++ {
		Fscan(in, &a[i])
	}
	for i := 1; i <= m; i++ {
		Fscan(in, &b[i])
	}

	if m > n {
		fail()
	}

	for i, j := 1, 1; j <= m; i, j = i+1, j+1 {
		for i <= n && a[i] != b[j] {
			i++
		}
		if i > n {
			fail()
		}
		p[j] = i
	}

	if p[1] > 1 {
		ans += solve(1, p[1]-1, b[1])
	}
	if p[m] < n {
		ans += solve(p[m]+1, n, b[m])
	}

	for i := 1; i < m; i++ {
		if p[i]+1 == p[i+1] {
			continue
		}
		ans += solve(p[i]+1, p[i+1]-1, max(b[i], b[i+1]))
	}

	Fprintln(out, ans)
}

package main

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

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

var (
	n, m, x, y, k int
	a, b, p       [200010]int
	ans           int64
	qu            []int
	//debug
	//xcnt, ycnt int
)

func max(_x, _y int) int {
	if _x > _y {
		return _x
	}
	return _y
}

func min(_x, _y int) int {
	if _x < _y {
		return _x
	}
	return _y
}

func fail() {
	Fprintf(out, "-1\n")
	out.Flush()
	os.Exit(0)
}

func calc(len int) int64 {
	x64, y64, k64, ret := int64(x), int64(y), int64(k), int64(0)
	if x64 < y64*k64 {
		ret += x64 * int64(len/k)
		len %= k
	}
	ret += y64 * int64(len)
	return ret
}

func solve(l, r, mx int) int64 {
	ret, length := int64(0), r-l+1
	qu = []int{}

	for i := l; i <= r; i++ {
		if a[i] > mx {
			qu = append(qu, i)
		}
	}

	kcnt := (len(qu) + k - 1) / k
	if length < kcnt * k {
		fail()
	}
	length -= kcnt * k
	ret += int64(x) * int64(kcnt)

	return ret + calc(length)
}

func main() {
	defer out.Flush()

	Fscan(in, &n, &m, &x, &k, &y)
	for i := 1; i <= n; i++ {
		Fscan(in, &a[i])
	}
	for i := 1; i <= m; i++ {
		Fscan(in, &b[i])
	}

	if m > n {
		fail()
	}

	for i, j := 1, 1; j <= m; i, j = i+1, j+1 {
		for i <= n && a[i] != b[j] {
			i++
		}
		if i > n {
			fail()
		}
		p[j] = i
	}

	if p[1] > 1 {
		ans += solve(1, p[1]-1, b[1])
	}
	if p[m] < n {
		ans += solve(p[m]+1, n, b[m])
	}

	for i := 1; i < m; i++ {
		if p[i]+1 == p[i+1] {
			continue
		}
		ans += solve(p[i]+1, p[i+1]-1, max(b[i], b[i+1]))
	}

	if n == 200000 && m == 2 {
		Fprintln(out, b[1], b[2], p[1], p[2])
	}
	Fprintln(out, ans)
}

package main

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

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

var (
	n, m, x, y, k int
	a, b, p       [200010]int
	ans           int64
	qu            []int
	//debug
	xcnt, ycnt int
)

func max(_x, _y int) int {
	if _x > _y {
		return _x
	}
	return _y
}

func min(_x, _y int) int {
	if _x < _y {
		return _x
	}
	return _y
}

func fail() {
	Fprintf(out, "-1\n")
	out.Flush()
	os.Exit(0)
}

func calc(len int) int64 {
	x64, y64, k64, ret := int64(x), int64(y), int64(k), int64(0)
	if x64 < y64*k64 {
		ret += x64 * int64(len/k)
		len %= k
		xcnt += len / k
	}
	ret += y64 * int64(len)
	ycnt += len
	return ret
}

func solve(l, r, mx int) int64 {
	ret, length := int64(0), r-l+1
	qu = []int{}

	for i := l; i <= r; i++ {
		if a[i] > mx {
			qu = append(qu, i)
		}
	}

	for len(qu) > 0 {
		if length < k {
			fail()
		}
		length -= k
		ret += int64(x)
		xcnt++
		R := min(r, qu[0]+k-1)
		i := 0
		for ; i < len(qu); i++ {
			if qu[i] > R {
				break
			}
		}
		if i > len(qu) {
			qu = []int{}
		} else {
			qu = qu[i:]
		}
	}

	return ret + calc(length)
}

func main() {
	defer out.Flush()

	Fscan(in, &n, &m, &x, &k, &y)
	for i := 1; i <= n; i++ {
		Fscan(in, &a[i])
	}
	for i := 1; i <= m; i++ {
		Fscan(in, &b[i])
	}

	if m > n {
		fail()
	}

	for i, j := 1, 1; j <= m; i, j = i+1, j+1 {
		for i <= n && a[i] != b[j] {
			i++
		}
		if i > n {
			fail()
		}
		p[j] = i
	}

	if p[1] > 1 {
		ans += solve(1, p[1]-1, b[1])
	}
	if p[m] < n {
		ans += solve(p[m]+1, n, b[m])
	}

	for i := 1; i < m; i++ {
		if p[i]+1 == p[i+1] {
			continue
		}
		ans += solve(p[i]+1, p[i+1]-1, max(b[i], b[i+1]))
	}

	if n == 200000 && m == 2 {
		Fprintln(out, ans, xcnt, ycnt)
	}
	Fprintln(out, ans)
}

package main

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

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

var (
	n, m, x, y, k int
	a, b, p       [200010]int
	ans           int64
	qu            []int
	//debug
	//xcnt, ycnt int
)

func max(_x, _y int) int {
	if _x > _y {
		return _x
	}
	return _y
}

func min(_x, _y int) int {
	if _x < _y {
		return _x
	}
	return _y
}

func fail() {
	Fprintf(out, "-1\n")
	out.Flush()
	os.Exit(0)
}

func calc(len int) int64 {
	x64, y64, k64, ret := int64(x), int64(y), int64(k), int64(0)
	if x64 < y64*k64 {
		ret += x64 * int64(len/k)
		len %= k
	}
	ret += y64 * int64(len)
	return ret
}

func solve(l, r, mx int) int64 {
	ret, length := int64(0), r-l+1
	qu = []int{}

	for i := l; i <= r; i++ {
		if a[i] > mx {
			qu = append(qu, i)
		}
	}

	kcnt := (len(qu) + k - 1) / k
	if length < kcnt * k {
		fail()
	}
	length -= kcnt * k
	ret += int64(x) * int64(kcnt)

	if n == 200000 && m == 2 {
		Fprintf(out, "l=%v, r=%v, maxcnt=%v, mincnt=%v\n", l, r, kcnt, length)
	}

	return ret + calc(length)
}

func main() {
	defer out.Flush()

	Fscan(in, &n, &m, &x, &k, &y)
	for i := 1; i <= n; i++ {
		Fscan(in, &a[i])
	}
	for i := 1; i <= m; i++ {
		Fscan(in, &b[i])
	}

	if m > n {
		fail()
	}

	for i, j := 1, 1; j <= m; i, j = i+1, j+1 {
		for i <= n && a[i] != b[j] {
			i++
		}
		if i > n {
			fail()
		}
		p[j] = i
	}

	if p[1] > 1 {
		ans += solve(1, p[1]-1, b[1])
	}
	if p[m] < n {
		ans += solve(p[m]+1, n, b[m])
	}

	for i := 1; i < m; i++ {
		if p[i]+1 == p[i+1] {
			continue
		}
		ans += solve(p[i]+1, p[i+1]-1, max(b[i], b[i+1]))
	}

	if n == 200000 && m == 2 {
		Fprintln(out, b[1], b[2], p[1], p[2])
	}
	Fprintln(out, ans)
}

package main

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

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

var (
	n, m, x, y, k int
	a, b, p       [200010]int
	ans           int64
	qu            []int
	//debug
	xcnt, ycnt int
)

func max(_x, _y int) int {
	if _x > _y {
		return _x
	}
	return _y
}

func min(_x, _y int) int {
	if _x < _y {
		return _x
	}
	return _y
}

func fail() {
	Fprintf(out, "-1\n")
	out.Flush()
	os.Exit(0)
}

func calc(len int) int64 {
	x64, y64, k64, ret := int64(x), int64(y), int64(k), int64(0)
	if x64 < y64*k64 {
		ret += x64 * int64(len/k)
		len %= k
		xcnt += len / k
	}
	ret += y64 * int64(len)
	ycnt += len
	return ret
}

func solve(l, r, mx int) int64 {
	ret, length := int64(0), r-l+1
	qu = []int{}

	for i := l; i <= r; i++ {
		if a[i] > mx {
			qu = append(qu, i)
		}
	}

	for len(qu) > 0 {
		if length < k {
			fail()
		}
		length -= k
		ret += int64(x)
		xcnt++
		R := min(r, qu[0]+k-1)
		i := 0
		for ; i < len(qu); i++ {
			if qu[i] > R {
				break
			}
		}
		if i > len(qu) {
			qu = []int{}
		} else {
			qu = qu[i:]
		}
	}

	return ret + calc(length)
}

func main() {
	defer out.Flush()

	Fscan(in, &n, &m, &x, &k, &y)
	for i := 1; i <= n; i++ {
		Fscan(in, &a[i])
	}
	for i := 1; i <= m; i++ {
		Fscan(in, &b[i])
	}

	if m > n {
		fail()
	}

	for i, j := 1, 1; j <= m; i, j = i+1, j+1 {
		for i <= n && a[i] != b[j] {
			i++
		}
		if i > n {
			fail()
		}
		p[j] = i
	}

	if p[1] > 1 {
		ans += solve(1, p[1]-1, b[1])
	}
	if p[m] < n {
		ans += solve(p[m]+1, n, b[m])
	}

	for i := 1; i < m; i++ {
		if p[i]+1 == p[i+1] {
			continue
		}
		ans += solve(p[i]+1, p[i+1]-1, max(b[i], b[i+1]))
	}

	if n == 200000 && m == 2 {
		Fprintln(out, ans, xcnt, ycnt, b[1], b[2])
	}
	Fprintln(out, ans)
}

package main

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

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

var (
	n, m, x, y, k int
	a, b, p       [200010]int
	ans           int64
	qu            []int
)

func max(_x, _y int) int {
	if _x > _y {
		return _x
	}
	return _y
}

func min(_x, _y int) int {
	if _x < _y {
		return _x
	}
	return _y
}

func fail() {
	Fprintf(out, "-1\n")
	out.Flush()
	os.Exit(0)
}

func calc(len int) int64 {
	x64, y64, k64, ret := int64(x), int64(y), int64(k), int64(0)
	if x64 < y64*k64 {
		ret += x64 * int64(len/k)
		len %= k
	}
	ret += y64 * int64(len)
	return ret
}

func solve(l, r, mx int) int64 {
	ret, length := int64(0), r-l+1
	qu = []int{}

	for i := l; i <= r; i++ {
		if a[i] > mx {
			qu = append(qu, i)
		}
	}

	for len(qu) > 0 {
		if length < k {
			fail()
		}
		length -= k
		ret += int64(x)
		R := min(r, qu[0]+k-1)
		i := 0
		for ; i < len(qu); i++ {
			if qu[i] > R {
				break
			}
		}
		if i > len(qu) {
			qu = []int{}
		} else {
			qu = qu[i:]
		}
	}

	return ret + calc(length)
}

func main() {
	defer out.Flush()

	Fscan(in, &n, &m, &x, &k, &y)
	for i := 1; i <= n; i++ {
		Fscan(in, &a[i])
	}
	for i := 1; i <= m; i++ {
		Fscan(in, &b[i])
	}

	if m > n {
		fail()
	}

	for i, j := 1, 1; j <= m; i, j = i+1, j+1 {
		for i <= n && a[i] != b[j] {
			i++
		}
		if i > n {
			fail()
		}
		p[j] = i
	}

	if p[1] > 1 {
		ans += solve(1, p[1]-1, b[1])
	}
	if p[m] < n {
		ans += solve(p[m]+1, n, b[m])
	}

	for i := 1; i < m; i++ {
		if p[i]+1 == p[i+1] {
			continue
		}
		ans += solve(p[i]+1, p[i+1]-1, max(b[i], b[i+1]))
	}

	Fprintln(out, ans)
}


package main

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

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

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

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

func sint() int {
	s := sstring()
	i := 0
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int(r - '0')
	}
	return i
}

func sints(n int) []int {
	a := make([]int, n)
	for i := range a {
		a[i] = sint()
	}
	return a
}

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

func main() {
	defer writer.Flush()
	t := sint()
	for ; t > 0; t-- {
		n := sint()
		q := sints(n)
		printsl(solveMin(n, q))
		printsl(solveMax(n, q))
	}
}

func printsl(in []int) {
	for _, i := range in {
		printf("%d ", i)
	}
	printf("\n")
}

func solveMin(n int, q []int) []int {
	curr := 1
	ret := make([]int, n)
	seen := map[int]bool{}
	for i := range ret {
		if i == 0 || q[i-1] != q[i] {
			ret[i] = q[i]
			seen[q[i]] = true
			continue
		}
		for seen[curr] {
			curr++
		}
		ret[i] = curr
		seen[curr] = true
		curr++
	}
	return ret
}

func solveMax(n int, q []int) []int {
	ds := make([]int, n+1)
	for i := range ds {
		ds[i] = int(i)
	}
	var find func(int) int
	find = func(t int) int {
		if ds[t] == t {
			return t
		}
		ret := find(ds[t])
		ds[t] = ret
		return ret
	}
	ret := make([]int, n)
	for i := range ret {
		if i == 0 || q[i-1] != q[i] {
			ret[i] = q[i]
			ds[q[i]] = q[i] - 1
			continue
		}
		ret[i] = find(q[i])
		ds[ret[i]] = ret[i] - 1
	}
	return ret
}

package main

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

type IntHeap []int

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

func main() {
	in := bufio.NewReader(os.Stdin)
	wr := bufio.NewWriter(os.Stdout)
	defer wr.Flush()
	var t int
	fmt.Fscan(in, &t)
	N := 200000
	v := make([]int, N)
	mn := make([]int, N)
	mx := make([]int, N)
	seen := make([]bool, N+1)
	for t > 0 {
		t--
		var n int
		fmt.Fscan(in, &n)
		h := &IntHeap{}
		heap.Init(h)
		val := 1
		valForMin := 1
		lastIdx := 0
		for i := 1; i <= n; i++ {
			seen[i] = false
		}
		for i := 0; i<=n; i++ {
			if i != n {
				fmt.Fscan(in, &v[i])
			}
			if i == 0 || i==n || v[i] != v[i-1] {
				for lastIdx != i {
					mx[lastIdx] = heap.Pop(h).(int)
					lastIdx++
				}
				if i == n {
					break
				}
				mn[i] = v[i]
				mx[i] = v[i]
				seen[v[i]] = true
				lastIdx = i + 1
			}else {
				for seen[valForMin] {
					valForMin++
				}
				mn[i] = valForMin
				valForMin++
			}
			for val <= v[i] {
				if val != v[i] {
					heap.Push(h, val)
				}
				val++
			}
		}
		for i := 0; i<n; i++ {
			fmt.Fprintf(wr, "%d ", mn[i])
		}
		fmt.Fprintln(wr, "")
		for i := 0; i<n; i++ {
			fmt.Fprintf(wr, "%d ", mx[i])
		}
		fmt.Fprintln(wr, "")
	}
}

package main

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

type IntHeap []int

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscan(in, &t)
	N := 200000
	v := make([]int, N)
	mn := make([]int, N)
	mx := make([]int, N)
	seen := make([]bool, N+1)
	for t > 0 {
		t--
		var n int
		fmt.Fscan(in, &n)
		h := &IntHeap{}
		heap.Init(h)
		val := 1
		valForMin := 1
		lastIdx := 0
		for i := 1; i <= n; i++ {
			seen[i] = false
		}
		for i := 0; i<=n; i++ {
			if i != n {
				fmt.Fscan(in, &v[i])
			}
			if i == 0 || i==n || v[i] != v[i-1] {
				for lastIdx != i {
					mx[lastIdx] = heap.Pop(h).(int)
					lastIdx++
				}
				if i == n {
					break
				}
				mn[i] = v[i]
				mx[i] = v[i]
				seen[v[i]] = true
				lastIdx = i + 1
			}else {
				for seen[valForMin] {
					valForMin++
				}
				mn[i] = valForMin
				valForMin++
			}
			for val <= v[i] {
				if val != v[i] {
					heap.Push(h, val)
				}
				val++
			}
		}
		for i := 0; i<n; i++ {
			fmt.Printf("%d ", mn[i])
		}
		fmt.Println("")
		for i := 0; i<n; i++ {
			fmt.Printf("%d ", mx[i])
		}
		fmt.Println("")
	}
}

package main

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

type IntHeap []int

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscan(in, &t)
	for t > 0 {
		t--
		var n int
		fmt.Fscan(in, &n)
		v := make([]int, n)
		h := &IntHeap{}
		heap.Init(h)
		val := 1
		valForMin := 1
		mn := make([]int, n)
		mx := make([]int, n)
		seen := make([]bool, n+1)
		lastIdx := 0
		for i := 0; i<=n; i++ {
			if i != n {
				fmt.Fscan(in, &v[i])
			}
			if i == 0 || i==n || v[i] != v[i-1] {
				for lastIdx != i {
					mx[lastIdx] = heap.Pop(h).(int)
					lastIdx++
				}
				if i == n {
					break
				}
				mn[i] = v[i]
				mx[i] = v[i]
				seen[v[i]] = true
				lastIdx = i + 1
			}else {
				for seen[valForMin] {
					valForMin++
				}
				mn[i] = valForMin
				valForMin++
			}
			for val <= v[i] {
				if val != v[i] {
					heap.Push(h, val)
				}
				val++
			}
		}
		for _, x := range mn {
			fmt.Printf("%d ", x)
		}
		fmt.Println("")
		for _, x := range mx {
			fmt.Printf("%d ", x)
		}
		fmt.Println("")
	}
}

package main

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

type IntHeap []int

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscan(in, &t)
	for t > 0 {
		t--
		var n int
		fmt.Fscan(in, &n)
		v := make([]int, n)
		h := &IntHeap{}
		heap.Init(h)
		val := 1
		valForMin := 1
		mn := make([]int, n)
		mx := make([]int, n)
		seen := make(map[int]bool)
		lastIdx := 0
		for i := 0; i<=n; i++ {
			if i != n {
				fmt.Fscan(in, &v[i])
			}
			if i == 0 || i==n || v[i] != v[i-1] {
				for lastIdx != i {
					mx[lastIdx] = heap.Pop(h).(int)
					lastIdx++
				}
				if i == n {
					break
				}
				mn[i] = v[i]
				mx[i] = v[i]
				seen[v[i]] = true
				lastIdx = i + 1
			}else {
				for seen[valForMin] {
					valForMin++
				}
				mn[i] = valForMin
				valForMin++
			}
			for val <= v[i] {
				if val != v[i] {
					heap.Push(h, val)
				}
				val++
			}
		}
		for _, x := range mn {
			fmt.Printf("%d ", x)
		}
		fmt.Println("")
		for _, x := range mx {
			fmt.Printf("%d ", x)
		}
		fmt.Println("")
	}
}

package main

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

type Scanner struct {
	bScanner *bufio.Scanner
}

func NewScanner() *Scanner {
	bScanner := bufio.NewScanner(os.Stdin)
	bScanner.Split(bufio.ScanWords)
	bScanner.Buffer([]byte{}, math.MaxInt32)
	return &Scanner{
		bScanner,
	}
}
func (s *Scanner) next() string {
	s.bScanner.Scan()
	return s.bScanner.Text()
}
func (s *Scanner) nextInt() int {
	s.bScanner.Scan()
	res, err := strconv.Atoi(s.bScanner.Text())
	if err != nil {
		panic(err)
	}
	return res
}
func (s *Scanner) nextInt64() int64 {
	s.bScanner.Scan()
	res, err := strconv.ParseInt(s.bScanner.Text(), 10, 64)
	if err != nil {
		panic(err)
	}
	return res
}
func (s *Scanner) nextFloat() float64 {
	s.bScanner.Scan()
	res, err := strconv.ParseFloat(s.bScanner.Text(), 64)
	if err != nil {
		panic(err)
	}
	return res
}
func (s *Scanner) nextInts(N int) []int {
	res := make([]int, N)
	for i := 0; i < N; i++ {
		res[i] = s.nextInt()
	}
	return res
}
func (s *Scanner) nextInt64s(N int) []int64 {
	res := make([]int64, N)
	for i := 0; i < N; i++ {
		res[i] = s.nextInt64()
	}
	return res
}
func min(nums ...int) int {
	ans := math.MaxInt32
	for _, num := range nums {
		if num < ans {
			ans = num
		}
	}
	return ans
}
func max(nums ...int) int {
	ans := math.MinInt32
	for _, num := range nums {
		if num > ans {
			ans = num
		}
	}
	return ans
}

type SegmentTreeNode struct {
	val int
}

func (n *SegmentTreeNode) merge(n2 *SegmentTreeNode) *SegmentTreeNode {
	return &SegmentTreeNode{val: n.val + n2.val}
}

type SegmentTree struct {
	size int
	tree []*SegmentTreeNode
}

func NewSegmentTree(arr []int) *SegmentTree {
	size := len(arr)
	tree := make([]*SegmentTreeNode, 4*size)
	result := &SegmentTree{
		size: size,
		tree: tree,
	}
	result.initTree(1, 0, size-1, arr)
	return result
}
func (s *SegmentTree) initTree(pos int, l int, r int, arr []int) {
	if l == r {
		s.tree[pos] = &SegmentTreeNode{val: arr[l]}
		return
	}
	m := (l + r) / 2
	s.initTree(pos*2, l, m, arr)
	s.initTree(pos*2+1, m+1, r, arr)
	s.tree[pos] = s.tree[pos*2].merge(s.tree[pos*2+1])
}
func (s *SegmentTree) query(pos int, l int, r int, ql int, qr int) *SegmentTreeNode {
	if ql <= l && r <= qr {
		return s.tree[pos]
	} else if ql <= r && l <= qr {
		m := (l + r) / 2
		return s.query(pos*2, l, m, ql, qr).merge(s.query(pos*2+1, m+1, r, ql, qr))
	} else {
		return &SegmentTreeNode{val: 0}
	}
}
func (s *SegmentTree) Query(ql int, qr int) int {
	queryResult := s.query(1, 0, s.size-1, ql, qr)
	return queryResult.val
}
func (s *SegmentTree) update(pos int, l int, r int, uidx int, uval int) {
	if l == r {
		s.tree[pos] = &SegmentTreeNode{val: uval}
		return
	}
	m := (l + r) / 2
	if uidx <= m {
		s.update(pos*2, l, m, uidx, uval)
	} else {
		s.update(pos*2+1, m+1, r, uidx, uval)
	}
	s.tree[pos] = s.tree[pos*2].merge(s.tree[pos*2+1])
}
func (s *SegmentTree) Update(uidx int, uval int) {
	s.update(1, 0, s.size-1, uidx, uval)
}

func (s *SegmentTree) traverse(pos int, l int, r int, rem int) int {
	if l == r {
		return l
	}
	m := (l + r) / 2
	if s.tree[pos*2].val < rem {
		return s.traverse(pos*2+1, m+1, r, rem-s.tree[pos*2].val)
	} else {
		return s.traverse(pos*2, l, m, rem)
	}
}
func (s *SegmentTree) Traverse(rem int) int {
	return s.traverse(1, 0, s.size-1, rem)
}

func main() {
	sc := NewScanner()
	wr := bufio.NewWriter(os.Stdout)
	defer wr.Flush()

	TC := sc.nextInt()
	for tc := 0; tc < TC; tc++ {
		N := sc.nextInt()
		q := sc.nextInts(N)

		minm := make([]int, N)
		maxm := make([]int, N)
		minm[0] = q[0]
		maxm[0] = q[0]
		for i := 1; i < N; i++ {
			if q[i] != q[i-1] {
				minm[i] = q[i]
				maxm[i] = q[i]
			}
		}

		used := make([]bool, N+1)
		for i := 0; i < N; i++ {
			if minm[i] > 0 {
				used[minm[i]] = true
			}
		}
		cur := 1
		for i := 0; i < N; i++ {
			if minm[i] == 0 {
				for used[cur] {
					cur++
				}
				minm[i] = cur
				used[cur] = true
			}
		}

		available := make([]int, N+1)
		for i := 1; i <= N; i++ {
			available[i] = 1
		}
		for i := 0; i < N; i++ {
			if maxm[i] != 0 {
				available[maxm[i]] = 0
			}
		}
		segTree := NewSegmentTree(available)
		for i := 0; i < N; i++ {
			if maxm[i] == 0 {
				cntOn := segTree.Query(0, q[i])
				maxm[i] = segTree.Traverse(cntOn)
				segTree.Update(maxm[i], 0)
			}
		}

		for i := 0; i < N; i++ {
			fmt.Fprint(wr, minm[i], " ")
		}
		fmt.Fprintln(wr)
		for i := 0; i < N; i++ {
			fmt.Fprint(wr, maxm[i], " ")
		}
		fmt.Fprintln(wr)

	}

}

package main

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

type uf struct {
	fa []int
}

func newUnionFind(n int) uf {
	fa := make([]int, n)
	for i := range fa {
		fa[i] = i
	}
	return uf{fa}
}

func (u uf) find(x int) int {
	if u.fa[x] != x {
		u.fa[x] = u.find(u.fa[x])
	}
	return u.fa[x]
}

func (u uf) merge(from, to int) (isNewMerge bool) {
	x, y := u.find(from), u.find(to)
	if x == y {
		return false
	}
	u.fa[x] = y
	return true
}

func (u uf) same(x, y int) bool { return u.find(x) == u.find(y) }

func (u uf) countRoots(l, r int) (cnt int) {
	for i := l; i <= r; i++ {
		if u.find(i) == i {
			cnt++
		}
	}
	return
}

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

	// single test case

	var tt int
	Fscan(in, &tt)

	for tt > 0 {
		tt--

		var n int
		Fscan(in, &n)

		a := make([]int, n)
		pmin := make([]int, n)
		pmax := make([]int, n)

		check := make(map[int]bool, n)

		u := newUnionFind(n + 1)

		for i := 0; i < n; i++ {
			Fscan(in, &a[i])
			if i == 0 || a[i] != a[i-1] {
				check[a[i]] = true
				pmin[i] = a[i]
				pmax[i] = a[i]
				u.merge(a[i], a[i]-1)
			}
		}

		cur := 1
		for k, v := range pmin {
			if v == 0 {
				for ; check[cur]; cur++ {
				}
				pmin[k] = cur
				cur++
			}
		}

		pre := 0
		for k, v := range pmax {
			if v > 0 {
				pre = v
			} else {
				cur := u.find(pre)
				pmax[k] = cur
				u.merge(pre, cur-1)
			}
		}

		for k := range pmin {
			Fprint(out, pmin[k])
			Fprint(out, " ")
		}
		Fprint(out, "\n")
		for k := range pmax {
			Fprint(out, pmax[k])
			Fprint(out, " ")
		}
		Fprint(out, "\n")
	}

	// 입력 에러 방지
	_leftData, _ := ioutil.ReadAll(in)
	if _s := strings.TrimSpace(string(_leftData)); _s != "" {
		panic("읽지않은 데이터 발견：\n" + _s)
	}
}

// LETYLETYLETY
func main() { CF1506E(os.Stdin, os.Stdout) }

// YTELYTELYTEL

package main

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

// github.com/EndlessCheng/codeforces-go
type uf struct {
	fa []int
}

func newUnionFind(n int) uf {
	fa := make([]int, n)
	for i := range fa {
		fa[i] = i
	}
	return uf{fa}
}

func (u uf) find(x int) int {
	if u.fa[x] != x {
		u.fa[x] = u.find(u.fa[x])
	}
	return u.fa[x]
}

func (u uf) merge(from, to int) (isNewMerge bool) {
	x, y := u.find(from), u.find(to)
	if x == y {
		return false
	}
	u.fa[x] = y
	return true
}

func (u uf) same(x, y int) bool { return u.find(x) == u.find(y) }

func (u uf) countRoots(l, r int) (cnt int) {
	for i := l; i <= r; i++ {
		if u.find(i) == i {
			cnt++
		}
	}
	return
}

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

	solve := func(Case int) {
		var n int
		Fscan(in, &n)
		q := make([]int, n)
		vis := make([]bool, n+1)
		mi := make([]int, n)
		mx := make([]int, n)
		u := newUnionFind(n + 1)
		for i := range q {
			Fscan(in, &q[i])
			if i == 0 || q[i] != q[i-1] {
				vis[q[i]] = true
				mi[i] = q[i]
				mx[i] = q[i]
				u.merge(q[i], q[i]-1)
			}
		}

		cur := 1
		for i, v := range mi {
			if v == 0 {
				for ; vis[cur]; cur++ {
				}
				mi[i] = cur
				cur++
			}
		}

		pre := 0
		for i, v := range mx {
			if v > 0 {
				pre = v
			} else {
				cur := u.find(pre)
				mx[i] = cur
				u.merge(pre, cur-1)
			}
		}

		for _, v := range mi {
			Fprint(out, v, " ")
		}
		Fprintln(out)
		for _, v := range mx {
			Fprint(out, v, " ")
		}
		Fprintln(out)
	}

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

	_leftData, _ := ioutil.ReadAll(in)
	if _s := strings.TrimSpace(string(_leftData)); _s != "" {
		panic("有未读入的数据：\n" + _s)
	}
}

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

package main

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

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

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

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

func sint64() int64 {
	s := sstring()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

func sint64s(n int64) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = sint64()
	}
	return a
}

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

func main() {
	defer writer.Flush()
	t := sint64()
	for ; t > 0; t-- {
		n := sint64()
		q := sint64s(n)
		printsl(solveMin(n, q))
		printsl(solveMax(n, q))
	}
}

func printsl(in []int64) {
	for _, i := range in {
		printf("%d ", i)
	}
	printf("\n")
}

func solveMin(n int64, q []int64) []int64 {
	curr := int64(1)
	ret := make([]int64, n)
	seen := map[int64]bool{}
	for i := range ret {
		if i == 0 || q[i-1] != q[i] {
			ret[i] = q[i]
			seen[q[i]] = true
			continue
		}
		for seen[curr] {
			curr++
		}
		ret[i] = curr
		seen[curr] = true
		curr++
	}
	return ret
}

func solveMax(n int64, q []int64) []int64 {
	ds := make([]int64, n+1)
	for i := range ds {
		ds[i] = int64(i)
	}
	var find func(int64) int64
	find = func(t int64) int64 {
		if ds[t] == t {
			return t
		}
		ret := find(ds[t])
		ds[t] = ret
		return ret
	}
	ret := make([]int64, n)
	for i := range ret {
		if i == 0 || q[i-1] != q[i] {
			ret[i] = q[i]
			ds[q[i]] = q[i] - 1
			continue
		}
		ret[i] = find(q[i])
		ds[ret[i]] = ret[i] - 1
	}
	return ret
}


package main

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

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

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

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

func _int() int {
	s := _string()
	i := 0
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int(r - '0')
	}
	return i
}

func _int64() int64 {
	s := _string()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

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

func main() {
	defer writer.Flush()
	for t := _int(); t > 0; t-- {
		h := _int()
		w := _int()
		ret := make([][]byte, h)
		for i := range ret {
			ret[i] = make([]byte, w)
			for j := range ret[i] {
				ret[i][j] = '0'
			}
		}
		var fill func(int, int)
		fill = func(r, c int) {
			if r >= 0 && r < h && c >= 0 && c < w {
				for _, d := range [][2]int{
					{0, 1},
					{0, -1},
					{1, 1},
					{1, 0},
					{1, -1},
					{-1, 0},
					{-1, -1},
					{-1, 1},
				} {
					rn := r + d[0]
					cn := c + d[1]
					if rn >= 0 && rn < h && cn >= 0 && cn < w && ret[rn][cn] == '1' {
						return
					}
				}
				ret[r][c] = '1'
			}
		}
		fill(0, 0)
		for i := 1; i < w; i++ { // first row
			fill(0, i)
		}
		for i := 1; i < h; i++ { // first col
			fill(i, 0)
		}
		for i := 1; i < w; i++ { // last row
			fill(h-1, i)
		}
		for i := 1; i < h; i++ { // last col
			fill(i, w-1)
		}
		for _, row := range ret {
			printf("%s\n", string(row))
		}
	}
}

package main

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

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

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

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

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

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

/**
Run solve the problem CF1530B
Date: 9/5/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1530/B
Problem: CF1530B
**/
func (in *CF1530B) Run() {
  for t := in.NextInt(); t > 0; t-- {
    h, w := in.NextInt(), in.NextInt()

    m := make([][]int, h)
    for i := 0; i < h; i++ {
      m[i] = make([]int, w)
    }
    var str strings.Builder
    for i := 0; i < h; i++ {
      for j := 0; j < w; j++ {
        if valid(&m, i, j, h, w) {
          m[i][j] = 1
          str.WriteString("1")
        } else {
          str.WriteString("0")
        }

      }
      str.WriteString("\n")
    }
    fmt.Print(str.String())
  }
}

func valid(m *[][]int, a, b, h, w int) bool {
  dx := []int{1, -1, 0, 0, 1, -1, 1, -1}
  dy := []int{0, 0, 1, -1, 1, -1, -1, 1}

  if a >= 1 && b >= 1 && a < h-1 && b < w-1 {
    return false
  }
  count := 0
  for i := 0; i < len(dx); i++ {
    x, y := a+dx[i], b+dy[i]
    if x >= 0 && x < h && y >= 0 && y < w && (*m)[x][y] == 1 {
      count++
    }
  }
  return count == 0
}

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

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

package main

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

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

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

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

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

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

/**
Run solve the problem CF1530B
Date: 9/5/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1530/B
Problem: CF1530B
**/
func (in *CF1530B) Run() {
	for t := in.NextInt(); t > 0; t-- {
		h, w := in.NextInt(), in.NextInt()

		m := make([][]int, h)
		for i := 0; i < h; i++ {
			m[i] = make([]int, w)
		}
		for i := 0; i < h; i++ {
			for j := 0; j < w; j++ {
				if valid(&m, i, j, h, w) {
					m[i][j] = 1
				}
			}
		}

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

	}
}

func valid(m *[][]int, a, b, h, w int) bool {
	dx := []int{1, -1, 0, 0, 1, -1, 1, -1}
	dy := []int{0, 0, 1, -1, 1, -1, -1, 1}

	if a >= 1 && b >= 1 && a < h-1 && b < w-1 {
		return false
	}
	count := 0
	for i := 0; i < len(dx); i++ {
		x, y := a+dx[i], b+dy[i]
		if x >= 0 && x < h && y >= 0 && y < w && (*m)[x][y] == 1 {
			count++
		}
	}
	return count == 0
}

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

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

package main

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

func solve() {
    f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, r, c := 0, 0, 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &r, &c)
		o := make([][]int, r)
		for j := 0; j < r; j++ {
			o[j] = make([]int, c)
			if j&1 == 1 {
				continue
			}
			for k := 0; k < c; k++ {
				if k&1 == 0 && (j == 0 || j == r-1) {
					o[j][k] = 1
				} else if 0 < j && j < r-1 && (k == 0 || k == c-1) {
					o[j][k] = 1
				}
			}
		}
		if r > 2 && r&1 == 0 {
			o[r-1], o[r-2] = o[r-2], o[r-1]
			copy(o[r-1], o[0])
		}
		for j := 0; j < r; j++ {
			for k := 0; k < c; k++ {
				fmt.Fprint(w, o[j][k])
			}
			fmt.Fprintln(w, "")
		}
		fmt.Fprintln(w, " ")
	}
	w.Flush()
}

func main() {
	solve()
}

package main

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

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

func main() {
	var test int
	fmt.Fscan(in, &test)
	for i := 0; i < test; i++ {
		solve()
	}

}

func solve() {
	var height, width int
	fmt.Fscan(in, &height)
	fmt.Fscan(in, &width)
	var matrix = make([][]int, height)
	for i := 0; i < height; i++ {
		matrix[i] = make([]int, width)
	}
	top := 0
	bottom := len(matrix) - 1
	right := 0
	left := len(matrix[0]) - 1
	for i := 0; i < len(matrix[0]); i++ {
		if canput(top, i, matrix) {
			matrix[0][i] = 1
		}
	}
	for i := 0; i < len(matrix[0]); i++ {
		if canput(bottom, i, matrix) {
			matrix[bottom][i] = 1
		}
	}

	for i := 0; i < len(matrix); i++ {
		if canput(i, right, matrix) {
			matrix[i][right] = 1
		}
	}

	for i := 0; i < len(matrix); i++ {
		if canput(i, left, matrix) {
			matrix[i][left] = 1
		}
	}
	for i := 0; i < len(matrix); i++ {
		for j := 0; j < len(matrix[0]); j++ {
			fmt.Print(matrix[i][j])
		}
		fmt.Println()
	}
	fmt.Println()
}

func canput(currentX int, currentY int, matrix [][]int) bool {
	if currentX-1 >= 0 {
		tempX := currentX - 1
		if matrix[tempX][currentY] == 1 {
			return false
		}
		if currentY-1 >= 0 && matrix[tempX][currentY-1] == 1 {
			return false
		}
		if currentY+1 < len(matrix[0]) && matrix[tempX][currentY+1] == 1 {
			return false
		}

	}
	if currentX+1 < len(matrix) {
		tempX := currentX + 1
		if matrix[tempX][currentY] == 1 {
			return false
		}
		if currentY-1 >= 0 && matrix[tempX][currentY-1] == 1 {
			return false
		}
		if currentY+1 < len(matrix[0]) && matrix[tempX][currentY+1] == 1 {
			return false
		}
	}
	if currentY-1 >= 0 {
		if matrix[currentX][currentY-1] == 1 {
			return false
		}
	}
	if currentY+1 < len(matrix[0]) && matrix[currentX][currentY+1] == 1 {
		return false
	}
	return true
}

package main

import "fmt"

func main() {
   var t int
   fmt.Scanln(&t)
   for T := 0; T < t; T++ {
       var h, w int
       fmt.Scanln(&h, &w)
       for i := 0; i < h; i++ {
           if (i == 0 || i == h-1) {
               if (w % 2 == 0) {
                   for j := 0; j < w; j++ {
                       if ((j % 2 == 0 && j != w-2) || j == w-1) {
                           fmt.Print("1")
                       } else {
                           fmt.Print("0")
                       }
                   }
               } else {
                   for j := 0; j < w; j++ {
                       if (j % 2 == 0) {
                           fmt.Print("1")
                       } else {
                           fmt.Print("0")
                       }
                   }
               }
               fmt.Println()
           } else {
               if (h % 2 == 1 && i % 2 == 0) {
                   fmt.Print("1")
                   for j := 1; j < w-1; j++ {
                       fmt.Print("0")
                   }
                   fmt.Println("1")
               } else if (h % 2 == 0 && i % 2 == 0 && i != h-2) {
                   fmt.Print("1")
                   for j := 1; j < w-1; j++ {
                       fmt.Print("0")
                   }
                   fmt.Println("1")
               } else {
                   for j := 0; j < w; j++ {
                       fmt.Print("0")
                   }
                   fmt.Println()
               }
           }
       }
       fmt.Println()
   }
}

package main

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

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

func isSorted(a []int) bool {
	for i := range a {
		if a[i] != i+1 {
			return false
		}
	}
	return true
}

func solve(n, m int) string {
	x := make([]string, n)
	for i := range x {
		if i == 0 {
			x[i] = strings.Repeat("10", (m+1)/2)[:m]
			continue
		}
		if i == n-1 {
			y := strings.Repeat("10", (m+1)/2)[:m]
			if i%2 == 1 {
				y = "00" + y[2:]
				y = y[:len(y)-2] + "00"
			}
			x[i] = y
			continue
		}
		if i%2 == 1 {
			x[i] = strings.Repeat("0", m)
			continue
		}
		if i%2 == 0 {
			x[i] = "1" + strings.Repeat("0", m-2) + "1"
			continue
		}
	}

	return strings.Join(x, "\n")
}

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

package main

import "fmt"

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

	for i := 0; int16(i) < t; i++ {
		var h uint8
		var w uint8

		_, err := fmt.Scanf("%d %d\n", &h, &w)
		if err != nil {
			return
		}

		result := make([][]int, h + 2)
		for key := range result {
			result[key] = make([]int, w + 2)
		}

		var i uint8
		for i = 1; i <= h; i++ {
			var j uint8
			for j = 1; j <= w; j++ {
				if i == 1 || i == h || j == 1 || j == w {
					if result[i - 1][j] == 0 && result[i][j - 1] == 0 && result[i + 1][j] == 0 && result[i][j + 1] == 0 && result[i - 1][j - 1] == 0 && result[i - 1][j + 1] == 0 && result[i + 1][j - 1] == 0 && result[i + 1][j + 1] == 0 {
						result[i][j] = 1
					}
				}
			}
		}

		for i = 1; i <= h; i++ {
			var j uint8
			for j = 1; j <= w; j++ {
				fmt.Print(result[i][j])
			}
			fmt.Println()
		}
		fmt.Println()
	}

}

package main

import (
	"fmt"
)

func main() {
	var t int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		var h, w int
		fmt.Scan(&h, &w)
		// first line
		for j := 0; j < w; j++ {
			if j%2 == 0 {
				fmt.Print(1)
			} else {
				fmt.Print(0)
			}
		}
		fmt.Println()

		if h%2 == 0 {
			for j := 0; j < w; j++ {
				fmt.Print(0)
			}
			fmt.Println()
			h--
		}
		for i := 1; i < h-1; i++ {
			x := 0
			if i%2 == 0 {
				x = 1
			}
			fmt.Print(x)
			for j := 1; j < w-1; j++ {
				fmt.Print(0)
			}
			fmt.Println(x)
		}

		// last line
		for j := 0; j < w; j++ {
			if j%2 == 0 {
				fmt.Print(1)
			} else {
				fmt.Print(0)
			}
		}
		fmt.Println()
	}
}

package main

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

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

	var tcs int
	_, _ = fmt.Fscan(in, &tcs)

	for tc := 0; tc < tcs; tc++ {
		var h, w int
		_, _ = fmt.Fscan(in, &h, &w)

		mat := make([][]int, h)
		for i := 0; i < h; i++ {
			mat[i] = make([]int, w)
		}

		for i := 0; i < w; i += 2 {
			mat[0][i] = 1
			mat[h-1][i] = 1
		}

		for i := 2; i+2 < h; i += 2 {
			mat[i][0] = 1
			mat[i][w-1] = 1
		}

		for i := 0; i < h; i++ {
			for j := 0; j < w; j++ {
				_, _ = fmt.Fprintf(out, "%d", mat[i][j])
			}
			_, _ = fmt.Fprintln(out)
		}

		_, _ = fmt.Fprintln(out)
	}

	_ = out.Flush()
}

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

	return n
}

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

	return b
}

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

	return b
}

package main

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

func main() {
    reader := bufio.NewReader(os.Stdin)
    input, _ := reader.ReadString('\n')

	// Get t
	t, _ := GetInt(input)

	for i:=0; i<t; i++ {
		// Get n
		input, _ = reader.ReadString('\n')
		var h,w int
		h, input = GetInt(input)
		w, _ = GetInt(input)

		// Solve
		for i:=1; i<=h; i++ {
			if i & 0x1 == 1 {
				if i == 1 || i == h {
					for j:=1; j<w-1; j+=2 {
						fmt.Printf("10")
					}
					if w & 0x1 == 0 {
						fmt.Printf("01")

					} else {
						fmt.Printf("1")
					}

				} else {
					fmt.Printf("1")
					for j:=2; j<w; j++ {
						fmt.Printf("0")
					}
					fmt.Printf("1")
				}

			} else {
				if i == h && w > 4 {
					fmt.Printf("00")
					for j:=1; j<=w-4; j++ {
						if j & 0x1 == 1 {
							fmt.Printf("1")

						} else {
							fmt.Printf("0")
						}
					}
					fmt.Printf("00")

				} else {
					for j:=0; j<w; j++ {
						fmt.Printf("0")
					}
				}
			}
			fmt.Println()
		}
		fmt.Println()
	}
}

// func abs(val float64) float64 {
//     if val >= 0.0 {
//         return val
//     }
//     return -val
// }

func GetInt(s string) (result int, output string) {
	iter, n := 0, len(s)
	for iter < n && s[iter] >= '0' && s[iter] <= '9' {
		iter++
	}
	result, _ = strconv.Atoi(s[:iter])
	// Skip the redundant characters
	for iter < n && (s[iter] < '0' || s[iter] > '9') {
		iter++
	}
	if iter == n {
		output = ""

	} else {
		output = s[iter:]
	}
	return
}

func GetInt64(s string) (result int64, output string) {
	iter, n := 0, len(s)
	for iter < n && s[iter] >= '0' && s[iter] <= '9' {
		iter++
	}
	result, _ = strconv.ParseInt(s[:iter], 10, 64)
	// Skip the redundant characters
	for iter < n && (s[iter] < '0' || s[iter] > '9') {
		iter++
	}
	if iter == n {
		output = ""

	} else {
		output = s[iter:]
	}
	return
}

package main

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

func main() {
    reader := bufio.NewReader(os.Stdin)
    input, _ := reader.ReadString('\n')

	// Get t
	t, _ := GetInt(input)

	for i:=0; i<t; i++ {
		// Get n
		input, _ = reader.ReadString('\n')
		var h,w int
		h, input = GetInt(input)
		w, _ = GetInt(input)

		// Solve
		for i:=1; i<=h; i++ {
			if i & 0x1 == 1 {
				if i == 1 || i == h {
					for j:=1; j<w-1; j+=2 {
						fmt.Printf("10")
					}
					if w & 0x1 == 0 {
						fmt.Printf("01")

					} else {
						fmt.Printf("1")
					}

				} else {
					fmt.Printf("1")
					for j:=2; j<w; j++ {
						fmt.Printf("0")
					}
					fmt.Printf("1")
				}

			} else {
				if i == h && i & 0x1 == 0 && h > 4 {
					fmt.Printf("00")
					for j:=1; j<=w-4; j++ {
						if j & 0x1 == 1 {
							fmt.Printf("1")

						} else {
							fmt.Printf("0")
						}
					}
					fmt.Printf("00")

				} else {
					for j:=0; j<w; j++ {
						fmt.Printf("0")
					}
				}
			}
			fmt.Println()
		}
		fmt.Println()
	}
}

// func abs(val float64) float64 {
//     if val >= 0.0 {
//         return val
//     }
//     return -val
// }

func GetInt(s string) (result int, output string) {
	iter, n := 0, len(s)
	for iter < n && s[iter] >= '0' && s[iter] <= '9' {
		iter++
	}
	result, _ = strconv.Atoi(s[:iter])
	// Skip the redundant characters
	for iter < n && (s[iter] < '0' || s[iter] > '9') {
		iter++
	}
	if iter == n {
		output = ""

	} else {
		output = s[iter:]
	}
	return
}

func GetInt64(s string) (result int64, output string) {
	iter, n := 0, len(s)
	for iter < n && s[iter] >= '0' && s[iter] <= '9' {
		iter++
	}
	result, _ = strconv.ParseInt(s[:iter], 10, 64)
	// Skip the redundant characters
	for iter < n && (s[iter] < '0' || s[iter] > '9') {
		iter++
	}
	if iter == n {
		output = ""

	} else {
		output = s[iter:]
	}
	return
}

package main

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

func main() {
    reader := bufio.NewReader(os.Stdin)
    input, _ := reader.ReadString('\n')

	// Get t
	t, _ := GetInt(input)

	for i:=0; i<t; i++ {
		// Get n
		input, _ = reader.ReadString('\n')
		var h,w int
		h, input = GetInt(input)
		w, _ = GetInt(input)

		// Solve
		for i:=1; i<=h; i++ {
			if i & 0x1 == 1 {
				if i == 1 || i == h {
					for j:=1; j<w-1; j+=2 {
						fmt.Printf("10")
					}
					if w & 0x1 == 0 {
						fmt.Printf("01")

					} else {
						fmt.Printf("1")
					}

				} else {
					fmt.Printf("1")
					for j:=2; j<w; j++ {
						fmt.Printf("0")
					}
					fmt.Printf("1")
				}

			} else {
				if i == h && i & 0x1 == 0 && h > 4 {
					fmt.Printf("00")
					for j:=1; j<=w-4; j++ {
						if j & 0x1 == 1 {
							fmt.Printf("1")

						} else {
							fmt.Printf("0")
						}
					}
					fmt.Printf("00")
					continue
				}
				for j:=0; j<w; j++ {
					fmt.Printf("0")
				}
			}
			fmt.Println()
		}
		fmt.Println()
	}
}

// func abs(val float64) float64 {
//     if val >= 0.0 {
//         return val
//     }
//     return -val
// }

func GetInt(s string) (result int, output string) {
	iter, n := 0, len(s)
	for iter < n && s[iter] >= '0' && s[iter] <= '9' {
		iter++
	}
	result, _ = strconv.Atoi(s[:iter])
	// Skip the redundant characters
	for iter < n && (s[iter] < '0' || s[iter] > '9') {
		iter++
	}
	if iter == n {
		output = ""

	} else {
		output = s[iter:]
	}
	return
}

func GetInt64(s string) (result int64, output string) {
	iter, n := 0, len(s)
	for iter < n && s[iter] >= '0' && s[iter] <= '9' {
		iter++
	}
	result, _ = strconv.ParseInt(s[:iter], 10, 64)
	// Skip the redundant characters
	for iter < n && (s[iter] < '0' || s[iter] > '9') {
		iter++
	}
	if iter == n {
		output = ""

	} else {
		output = s[iter:]
	}
	return
}

package main

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

func main() {
    reader := bufio.NewReader(os.Stdin)
    input, _ := reader.ReadString('\n')

	// Get t
	t, _ := GetInt(input)

	for i:=0; i<t; i++ {
		// Get n
		input, _ = reader.ReadString('\n')
		var h,w int
		h, input = GetInt(input)
		w, _ = GetInt(input)

		// Solve
		for i:=1; i<=h; i++ {
			if i & 0x1 == 1 {
				if i == 1 || i == h {
					for j:=1; j<w-1; j+=2 {
						fmt.Printf("10")
					}
					if w & 0x1 == 0 {
						fmt.Printf("01")

					} else {
						fmt.Printf("1")
					}

				} else {
					fmt.Printf("1")
					for j:=2; j<w; j++ {
						fmt.Printf("0")
					}
					fmt.Printf("1")
				}

			} else {
				for j:=0; j<w; j++ {
					fmt.Printf("0")
				}
			}
			fmt.Println()
		}
		fmt.Println()
	}
}

// func abs(val float64) float64 {
//     if val >= 0.0 {
//         return val
//     }
//     return -val
// }

func GetInt(s string) (result int, output string) {
	iter, n := 0, len(s)
	for iter < n && s[iter] >= '0' && s[iter] <= '9' {
		iter++
	}
	result, _ = strconv.Atoi(s[:iter])
	// Skip the redundant characters
	for iter < n && (s[iter] < '0' || s[iter] > '9') {
		iter++
	}
	if iter == n {
		output = ""

	} else {
		output = s[iter:]
	}
	return
}

func GetInt64(s string) (result int64, output string) {
	iter, n := 0, len(s)
	for iter < n && s[iter] >= '0' && s[iter] <= '9' {
		iter++
	}
	result, _ = strconv.ParseInt(s[:iter], 10, 64)
	// Skip the redundant characters
	for iter < n && (s[iter] < '0' || s[iter] > '9') {
		iter++
	}
	if iter == n {
		output = ""

	} else {
		output = s[iter:]
	}
	return
}

package main

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

func main() {
    reader := bufio.NewReader(os.Stdin)
    input, _ := reader.ReadString('\n')

	// Get t
	t, _ := GetInt(input)

	for i:=0; i<t; i++ {
		// Get n
		input, _ = reader.ReadString('\n')
		var h,w int
		h, input = GetInt(input)
		w, _ = GetInt(input)

		// Solve
		for i:=1; i<=h; i++ {
			if i & 0x1 == 1 {
				if i == 1 || i == h {
					for j:=1; j<w-1; j+=2 {
						fmt.Printf("1 0 ")
					}
					if w & 0x1 == 0 {
						fmt.Printf("0 1")

					} else {
						fmt.Printf("1")
					}

				} else {
					fmt.Printf("1 ")
					for j:=2; j<w; j++ {
						fmt.Printf("0 ")
					}
					fmt.Printf("1")
				}

			} else {
				for j:=0; j<w; j++ {
					fmt.Printf("0 ")
				}
			}
			fmt.Println()
		}
		fmt.Println()
	}
}

// func abs(val float64) float64 {
//     if val >= 0.0 {
//         return val
//     }
//     return -val
// }

func GetInt(s string) (result int, output string) {
	iter, n := 0, len(s)
	for iter < n && s[iter] >= '0' && s[iter] <= '9' {
		iter++
	}
	result, _ = strconv.Atoi(s[:iter])
	// Skip the redundant characters
	for iter < n && (s[iter] < '0' || s[iter] > '9') {
		iter++
	}
	if iter == n {
		output = ""

	} else {
		output = s[iter:]
	}
	return
}

func GetInt64(s string) (result int64, output string) {
	iter, n := 0, len(s)
	for iter < n && s[iter] >= '0' && s[iter] <= '9' {
		iter++
	}
	result, _ = strconv.ParseInt(s[:iter], 10, 64)
	// Skip the redundant characters
	for iter < n && (s[iter] < '0' || s[iter] > '9') {
		iter++
	}
	if iter == n {
		output = ""

	} else {
		output = s[iter:]
	}
	return
}

package main

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

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, r, c := 0, 0, 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &r, &c)
		o := make([][]int, r)
		for j := 0; j < r; j++ {
			o[j] = make([]int, c)
			if j&1 == 1 {
				continue
			}
			for k := 0; k < c; k++ {
				if k&1 == 0 && (j == 0 || j == r-1) {
					o[j][k] = 1
				} else if 0 < j && j < r-1 && (k == 0 || k == c-1) {
					o[j][k] = 1
				}
			}
		}
		for j := 0; j < r; j++ {
			for k := 0; k < c; k++ {
				fmt.Fprint(w, o[j][k])
			}
			fmt.Fprintln(w, "")
		}
		fmt.Fprintln(w, " ")
	}
	w.Flush()
}

func main() {
	solve()
}

package main

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

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

func main() {
	var test int
	fmt.Fscan(in, &test)
	for i := 0; i < test; i++ {
		solve()
	}

}

func solve() {
	var height, width int
	fmt.Fscan(in, &height)
	fmt.Fscan(in, &width)
	var matrix = make([][]int, height)
	for i := 0; i < height; i++ {
		matrix[i] = make([]int, width)
	}
	top := 0
	bottom := len(matrix) - 1
	right := 0
	left := len(matrix[0]) - 1
	for i := 0; i < len(matrix[0]); i++ {
		if canput(top, i, matrix) {
			matrix[0][i] = 1
		}
	}
	for i := 0; i < len(matrix[0]); i++ {
		if canput(bottom, i, matrix) {
			matrix[bottom][i] = 1
		}
	}

	for i := 0; i < len(matrix); i++ {
		if canput(i, right, matrix) {
			matrix[i][right] = 1
		}
	}

	for i := 0; i < len(matrix); i++ {
		if canput(i, left, matrix) {
			matrix[i][left] = 1
		}
	}
	for i := 0; i < len(matrix); i++ {
		for j := 0; j < len(matrix[0]); j++ {
			fmt.Print(matrix[i][j], " ")
		}
		fmt.Println()
	}
	fmt.Println()
}

func canput(currentX int, currentY int, matrix [][]int) bool {
	if currentX-1 >= 0 {
		tempX := currentX - 1
		if matrix[tempX][currentY] == 1 {
			return false
		}
		if currentY-1 >= 0 && matrix[tempX][currentY-1] == 1 {
			return false
		}
		if currentY+1 < len(matrix[0]) && matrix[tempX][currentY+1] == 1 {
			return false
		}

	}
	if currentX+1 < len(matrix) {
		tempX := currentX + 1
		if matrix[tempX][currentY] == 1 {
			return false
		}
		if currentY-1 >= 0 && matrix[tempX][currentY-1] == 1 {
			return false
		}
		if currentY+1 < len(matrix[0]) && matrix[tempX][currentY+1] == 1 {
			return false
		}
	}
	if currentY-1 >= 0 {
		if matrix[currentX][currentY-1] == 1 {
			return false
		}
	}
	if currentY+1 < len(matrix[0]) && matrix[currentX][currentY+1] == 1 {
		return false
	}
	return true
}