Given a Circular Linked List of size N. The task is to delete the given node (excluding the first and last node) in the circular linked list and then print the reverse of the circular linked list.
 
Example 1:
Input:
5
2 5 7 8 10
8
Output:
10 7 5 2
Explanation: 
After deleting 8 from the given circular linked 
list, it has elements as 2, 5, 7, 10. Now, 
reversing this list will result in 10, 7, 5, 2.
 
Example 2:
Input:
4
1 7 8 10
8
Output:
10 7 1
Explanation:
After deleting 8 from the given circular linked 
list, it has elements as 1, 7,10. Now, reversing 
this list will result in 10, 7, 1.
 
Your Task:
You don't need to read, input, or print anything. Your task is to complete the function deleteNode( ), which takes Node head, and integer key as input parameters, and delete the node with data key. Also Complete function reverse() which reverses the linked list.
 
Constraints:
1 <= T <= 100
1 <= n <= 100
1 <= data <= 100

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3



'''

class Node:

    def __init__(self, data):

        self.data = data

        self.next = None



'''



# Function to delete a given node from the list 

def deleteNode(head, key):

    #your code goes here





#Function to reverse the list

def reverse(head):

    #your code goes here

Solutions:
def deleteNode(head, key):
	temp = head
	while True:
		if temp.next.data == key:
			temp.next = temp.next.next
			break
		else:
			temp = temp.next

def reverse(head):
	last = head
	curr = head
	while last.next != head:
		last = last.next
	prev = last
	while curr != last:
		tmp = curr.next
		curr.next = prev
		prev = curr
		curr = tmp
	curr.next = prev
	cur = head
	while cur.next != head:
		(cur.data, cur.next.data) = (cur.next.data, cur.data)
		cur = cur.next


def deleteNode(head, key):
	curr = head
	while curr.next != head:
		if curr.next.data == key:
			curr.next = curr.next.next
		else:
			curr = curr.next
	return head

def reverse(head):
	last = head
	curr = head
	while last.next != head:
		last = last.next
	prev = last
	while curr != last:
		tmp = curr.next
		curr.next = prev
		prev = curr
		curr = tmp
	curr.next = prev
	cur = head
	while cur.next != head:
		(cur.data, cur.next.data) = (cur.next.data, cur.data)
		cur = cur.next


def deleteNode(head, key):
	itr = head
	while itr.next.data != key:
		itr = itr.next
	itr.next = itr.next.next
	itr = itr.next
	return head

def reverse(head):
	itr = head
	s = []
	while itr.next != head:
		s.append(itr.data)
		itr = itr.next
	s.append(itr.data)
	dummy = head
	dummy.data = s.pop()
	while s:
		dummy = dummy.next
		dummy.data = s.pop()
	return dummy


def deleteNode(head, key):
	tmp = head
	prev = tmp
	while tmp.data != key:
		prev = tmp
		tmp = tmp.next
	prev.next = tmp.next
	return head

def reverse(head):
	last = head
	while last.next != head:
		last = last.next
	prev = last
	curr = head
	while curr != last:
		nxt = curr.next
		curr.next = prev
		prev = curr
		curr = nxt
	curr.next = prev
	curr = head
	while curr.next != head:
		(curr.data, curr.next.data) = (curr.next.data, curr.data)
		curr = curr.next


def deleteNode(head, key):
	if head is None:
		return
	temp = head
	while temp.next != head:
		if temp.next.data == key:
			temp.next = temp.next.next
		else:
			temp = temp.next
	return head

def reverse(head):
	last = head
	curr = head
	while last.next != head:
		last = last.next
	prev = last
	while curr != last:
		next = curr.next
		curr.next = prev
		prev = curr
		curr = next
	curr.next = prev
	temp1 = head
	while temp1.next != head:
		(temp1.data, temp1.next.data) = (temp1.next.data, temp1.data)
		temp1 = temp1.next


def deleteNode(head, key):
	h = head
	while h.next != head:
		if h.next.data == key:
			h.next = h.next.next
		else:
			h = h.next

def reverse(head):
	k = []
	l = []
	h = head
	while True:
		if h in l:
			break
		l.append(h)
		k.append(h.data)
		h = h.next
	i = len(k) - 1
	h = head
	while i >= 0:
		h.data = k[i]
		i -= 1
		h = h.next


def deleteNode(head, key):
	if head:
		temp = head
		prev = None
		while temp:
			if temp.data == key:
				temp = temp.next
				break
			prev = temp
			temp = temp.next
		prev.next = temp

def reverse(head):
	if head:
		t11 = head
		while t11.next != head:
			t11 = t11.next
		t1 = t11
		temp = head
		while temp.next != head:
			t2 = temp
			temp = temp.next
			t2.next = t1
			t1 = t2
		temp.next = t1
		temp1 = head
		while temp1.next != head:
			t = temp1.data
			temp1.data = temp1.next.data
			temp1.next.data = t
			temp1 = temp1.next


def deleteNode(head, key):
	tmp = head
	temp = Node(key)
	if head.data == key:
		(head.next.data, head.data) = (head.data, head.next.data)
		head.next = head.next.next
	while head.next.data != key:
		head = head.next
	head.next = head.next.next
	return tmp

def reverse(headd):
	global head
	tmp = headd
	pre = None
	while True:
		nxt = headd.next
		headd.next = pre
		pre = headd
		headd = nxt
		if headd == tmp:
			break
	tmp.next = pre
	head = pre


class Node:

	def __init__(self, data):
		self.data = data
		self.next = None

def deleteNode(head, key):
	tmp = head
	while tmp.next.data != key:
		tmp = tmp.next
	tmp.next = tmp.next.next
	return head

def reverse(head):
	curr = head
	last = head
	while last.next != head:
		last = last.next
	prev = last
	while curr != last:
		temp = curr.next
		curr.next = prev
		prev = curr
		curr = temp
	curr.next = prev
	curr = head
	while curr.next != head:
		(curr.data, curr.next.data) = (curr.next.data, curr.data)
		curr = curr.next


def deleteNode(head, key):
	temp1 = head
	temp2 = head.next
	while temp2:
		if temp2.data == key:
			temp1.next = temp2.next
			break
		temp1 = temp1.next
		temp2 = temp2.next

def reverse(head):
	last = head
	curr = head
	while last.next != head:
		last = last.next
	prev = last
	while curr != last:
		tmp = curr.next
		curr.next = prev
		prev = curr
		curr = tmp
	curr.next = prev
	cur = head
	while cur.next != head:
		(cur.data, cur.next.data) = (cur.next.data, cur.data)
		cur = cur.next


def deleteNode(head, key):
	cur = head
	prev = None
	while cur.data != key:
		prev = cur
		cur = cur.next
		if cur == head:
			return
	prev.next = cur.next
	cur.next = None

def reverse(head):
	cur = head
	prev = None
	while cur != None:
		next = cur.next
		cur.next = prev
		prev = cur
		cur = next
	cur = head
	while cur.next != head:
		(cur.data, cur.next.data) = (cur.next.data, cur.data)
		cur = cur.next


def deleteNode(head, key):
	temp = head
	while temp.next.data != key:
		temp = temp.next
	temp.next = temp.next.next

def reverse(head):
	if head:
		t11 = head
		while t11.next != head:
			t11 = t11.next
		t1 = t11
		temp = head
		while temp.next != head:
			t2 = temp
			temp = temp.next
			t2.next = t1
			t1 = t2
		temp.next = t1
		temp1 = head
		while temp1.next != head:
			t = temp1.data
			temp1.data = temp1.next.data
			temp1.next.data = t
			temp1 = temp1.next


def deleteNode(head, key):
	nd = head
	pd = None
	while nd and nd.data != key:
		pd = nd
		nd = nd.next
	if nd.data == key:
		pd.next = nd.next
	return head

def reverse(head):
	if head:
		t11 = head
		while t11.next != head:
			t11 = t11.next
		t1 = t11
		temp = head
		while temp.next != head:
			t2 = temp
			temp = temp.next
			t2.next = t1
			t1 = t2
		temp.next = t1
		temp1 = head
		while temp1.next != head:
			t = temp1.data
			temp1.data = temp1.next.data
			temp1.next.data = t
			temp1 = temp1.next
		return head


def deleteNode(head, key):
	curr = head
	while curr.next.data != key:
		curr = curr.next
	curr.next = curr.next.next

def reverse(head):
	curr = head.next
	prev = head
	while curr != head:
		next = curr.next
		curr.next = prev
		prev = curr
		curr = next
	curr.next = prev
	while curr.next != head:
		(curr.data, curr.next.data) = (curr.next.data, curr.data)
		curr = curr.next


def deleteNode(head, key):
	if head == None:
		return
	if head == head.next and head.data == key:
		head = None
		return
	else:
		prev = None
		curr = head
		prev = curr
		curr = curr.next
		while curr != head:
			prev = curr
			curr = curr.next
		curr = head
		if curr.data == key:
			prev.next = curr.next
			curr = prev.next
		else:
			curr = curr.next
			prev = prev.next
		while curr != head:
			if curr.data == key:
				prev.next = curr.next
				curr = prev.next
			else:
				curr = curr.next
				prev = prev.next

def reverse(head):
	if head == None:
		return
	prev = None
	curr = head
	t1 = curr.data
	t2 = curr.next.data
	curr.next.data = t1
	t1 = t2
	curr = curr.next
	while curr != head:
		t2 = curr.next.data
		curr.next.data = t1
		t1 = t2
		curr = curr.next
	prev = None
	curr = head
	a = curr.next
	curr.next = prev
	prev = curr
	curr = a
	while curr != head:
		a = curr.next
		curr.next = prev
		prev = curr
		curr = a
	curr.next = prev


def deleteNode(head, key):
	tmp = head
	while tmp.next.data != key:
		tmp = tmp.next
	tmp.next = tmp.next.next
	return head

def reverse(head):
	tmp = head
	b = []
	b.append(tmp.data)
	tmp = tmp.next
	while tmp != head:
		b.append(tmp.data)
		tmp = tmp.next
	tmp = head
	for i in reversed(b):
		tmp.data = i
		if tmp.next == head:
			return head
		tmp = tmp.next
	return head


def deleteNode(head, key):
	(prev, curr) = (None, head)
	while curr.next != head and curr.data != key:
		prev = curr
		curr = curr.next
	prev.next = curr.next

def reverseUtil(head, curr):
	if curr.next == head:
		return curr
	rest = reverseUtil(head, curr.next)
	curr.next.next = curr
	curr.next = rest
	return rest

def reverse(head):
	head = reverseUtil(head, head)
	temp = head.data
	curr = head
	while curr.next != head:
		curr.data = curr.next.data
		curr = curr.next
	curr.data = temp


def deleteNode(head, key):
	(p, n, c) = (head, head.next, head)
	while n != head:
		c = n
		n = n.next
		if c.data == key:
			p.next = n
			c.next = None
			break
		p = c

def reverse(head):
	if head:
		t11 = head
	while t11.next != head:
		t11 = t11.next
	t1 = t11
	temp = head
	while temp.next != head:
		t2 = temp
		temp = temp.next
		t2.next = t1
		t1 = t2
	temp.next = t1
	temp1 = head
	while temp1.next != head:
		t = temp1.data
		temp1.data = temp1.next.data
		temp1.next.data = t
		temp1 = temp1.next


def deleteNode(head, key):
	prev = None
	temp = head
	while temp.data != key:
		prev = temp
		temp = temp.next
	prev.next = temp.next

def reverse(head):
	l = []
	temp = head
	while temp.next != head:
		l.append(temp.data)
		temp = temp.next
	l.append(temp.data)
	temp = head
	j = -1
	while temp.next != head:
		temp.data = l[j]
		j -= 1
		temp = temp.next
	temp.data = l[0]
	return head
	return None


def deleteNode(head, key):
	curr = head
	if head == None:
		return
	if head.data == key and head.next == head:
		return
	p = None
	if head.data == key:
		while curr.next != head:
			curr = curr.next
		curr.next = head.next
		head = curr.next
	while curr.next != head and curr.next.data != key:
		curr = curr.next
	if curr.next.data == key:
		p = curr.next
		curr.next = p.next

def reverse(head):
	if head == None:
		return
	prev = None
	current = head
	next = current.next
	current.next = prev
	prev = current
	current = next
	while current != head:
		next = current.next
		current.next = prev
		prev = current
		current = next
	head.next = prev
	head = prev
	temp1 = head
	while temp1.next != head:
		t = temp1.data
		temp1.data = temp1.next.data
		temp1.next.data = t
		temp1 = temp1.next


import gc

def deleteNode(head, key):
	if head == None:
		return
	if head.data == key and head.next == head:
		head = None
		return
	last = head
	if head.data == key:
		while last.next != head:
			last = last.next
		last.next = head.next
		head = last.next
	while last.next != head and last.next.data != key:
		last = last.next
	if last.next.data == key:
		last.next = last.next.next

def reverse(head):
	if head == None:
		return
	prev = None
	current = head
	next = current.next
	current.next = prev
	prev = current
	current = next
	while current != head:
		next = current.next
		current.next = prev
		prev = current
		current = next
	head.next = prev
	temp1 = head
	while temp1.next != head:
		t = temp1.data
		temp1.data = temp1.next.data
		temp1.next.data = t
		temp1 = temp1.next


import gc

def deleteNode(head, key):
	if head == None:
		return
	if head.data == key and head.next == head:
		head = None
		return
	last = head
	d = None
	if head.data == key:
		while last.next != head:
			last = last.next
		last.next = head.next
		head = last.next
	while last.next != head and last.next.data != key:
		last = last.next
	if last.next.data == key:
		d = last.next
		last.next = d.next

def reverse(head_ref):
	if head:
		t11 = head
	while t11.next != head:
		t11 = t11.next
	t1 = t11
	temp = head
	while temp.next != head:
		t2 = temp
		temp = temp.next
		t2.next = t1
		t1 = t2
	temp.next = t1
	temp1 = head
	while temp1.next != head:
		t = temp1.data
		temp1.data = temp1.next.data
		temp1.next.data = t
		temp1 = temp1.next


def deleteNode(head, key):
	start = head
	while True:
		if head.next.data == key:
			head.next = head.next.next
			return
		head = head.next
		if head == start:
			print('not found')
			return

def reverse(head):
	start = head
	prev_node = start
	head = head.next
	while True:
		next_node = head.next
		head.next = prev_node
		prev_node = head
		head = next_node
		if head == start:
			start.next = prev_node
			break
	key = head.data
	while True:
		head.data = head.next.data
		head = head.next
		if head.next == start:
			head.data = key
			return


def deleteNode(head, key):
	n = head
	while n.next.data != key:
		n = n.next
	n.next = n.next.next
	return head

def reverse(head):
	n = head
	list1 = []
	while n.next != head:
		list1.append(n.data)
		n = n.next
	list1.append(n.data)
	list1.reverse()
	n = head
	i = 0
	while n.next != head:
		n.data = list1[i]
		n = n.next
		i += 1
	n.data = list1[i]
	return head


def deleteNode(head, key):
	p = c = head
	while c.data != key:
		p = c
		c = c.next
	p.next = c.next

def reverse(head):
	p = None
	c = head
	while c:
		cnext = c.next
		c.next = p
		p = c
		c = cnext
	c = head
	while c.next != head:
		(c.data, c.next.data) = (c.next.data, c.data)
		c = c.next


def deleteNode(head, key):
	if head:
		temp = head
		prev = None
		while temp:
			if temp.data == key:
				temp = temp.next
				break
			prev = temp
			temp = temp.next
		prev.next = temp

def reverse(head):
	if head:
		cur = head
		while cur.next != head:
			cur = cur.next
		curr = cur
		temp = head
		while temp.next != head:
			temp1 = temp
			temp = temp.next
			temp1.next = curr
			curr = temp1
		temp.next = curr
		temp2 = head
		while temp2.next != head:
			t = temp2.data
			temp2.data = temp2.next.data
			temp2.next.data = t
			temp2 = temp2.next


def deleteNode(head, key):
	curr = head
	prev = None
	while curr.data != key:
		prev = curr
		curr = curr.next
		if curr == head:
			return
	prev.next = curr.next
	curr.next = None

def reverse(head):
	prev = None
	curr = head
	while curr:
		temp = curr.next
		curr.next = prev
		prev = curr
		curr = temp
	curr = head
	while curr.next != head:
		(curr.data, curr.next.data) = (curr.next.data, curr.data)
		curr = curr.next


def deleteNode(head, key):
	ptr = head
	while ptr.next is not head:
		if ptr.next.data == key:
			a = ptr.next.next
			ptr.next = a
		ptr = ptr.next
	return head

def reverse(head):
	l = []
	curr = head
	while curr.next != head:
		l.append(curr.data)
		curr = curr.next
	l.append(curr.data)
	c = head
	for i in l[::-1]:
		c.data = i
		c = c.next
	return head


def deleteNode(head, key):
	if head is None:
		return
	if head.data == key:
		if head.next is not None:
			head = head.next
		else:
			head = None
	prev = None
	cur = head
	while cur.next != head:
		if cur.data == key:
			prev.next = cur.next
			break
		prev = cur
		cur = cur.next

def reverse(head):
	if head is None or head.next == head:
		return head
	cur = head
	l = []
	while True:
		if cur.data in l:
			break
		l.append(cur.data)
		cur = cur.next
	l = l[::-1]
	for x in l:
		head.data = x
		head = head.next


def deleteNode(head, key):
	curr = head
	my_prev = None
	while curr.data != key:
		my_prev = curr
		curr = curr.next
	my_prev.next = curr.next
	curr.next = None
	return head

def reverse(head):
	t = head
	a = []
	while t.next != head:
		a.append(t.data)
		t = t.next
	a.append(t.data)
	c = head
	for i in a[::-1]:
		c.data = i
		c = c.next


def deleteNode(head, key):
	t = head
	while t.next != head:
		if t.next.data == key:
			t.next = t.next.next
			return head
		t = t.next

def reverse(head):
	t = head
	a = []
	while t.next != head:
		a.append(t.data)
		t = t.next
	a.append(t.data)
	c = head
	for i in a[::-1]:
		c.data = i
		c = c.next


def deleteNode(head, data):
	curr = head
	while curr.next != head:
		if curr.next.data == data:
			curr.next = curr.next.next
			return head
		curr = curr.next

def reverse(head):
	curr = head
	l = []
	while curr.next != head:
		l.append(curr.data)
		curr = curr.next
	l.append(curr.data)
	c = head
	for i in l[::-1]:
		c.data = i
		c = c.next


def deleteNode(head, key):
	temp = head
	before = head
	while temp is not None:
		if temp.data == key:
			before.next = temp.next
			break
		else:
			before = temp
			temp = temp.next
	return head

def reverse(head):
	curr = head
	l = []
	while curr.next != head:
		l.append(curr.data)
		curr = curr.next
	l.append(curr.data)
	c = head
	for i in l[::-1]:
		c.data = i
		c = c.next


def deleteNode(head, key):
	p = head
	while p:
		if p.next.data == key:
			break
		else:
			p = p.next
	p.next = p.next.next

def reverse(head):
	l = []
	p = head.next
	l.append(head.data)
	while p:
		if p == head:
			break
		l.append(p.data)
		p = p.next
	q = head
	l = l[::-1]
	for i in l:
		q.data = i
		q = q.next


def getTail(head):
	temp = head
	while temp.next != head:
		temp = temp.next
	return temp

def deleteNode(head, key):
	if head is not None:
		if head.data == key:
			tail = getTail(head)
			tail.next = tail.next.next
			head = head.next
			return head
		temp = head
		while temp.next != head and temp.next.data != key:
			temp = temp.next
		temp.next = temp.next.next
		return head

def reverse(head):
	prev = None
	tail = None
	curr = head
	next = None
	while curr.next != head:
		next = curr.next
		curr.next = prev
		prev = curr
		if tail is None:
			tail = prev
		tail.next = prev
		curr = next
	curr.next = prev
	prev = curr
	tail.next = prev
	head = prev
	return head


def deleteNode(head, key):
	curr = head
	prev = None
	while curr != None:
		if curr.data == key:
			prev.next = curr.next
			break
		prev = curr
		curr = curr.next

def reverse(head):
	curr = head.next
	prev = head
	while curr != None:
		temp = curr.next
		curr.next = prev
		if curr == head:
			break
		prev = curr
		curr = temp
	pl = head.next
	while pl != head:
		print(str(pl.data) + ' ', end='')
		pl.data = ''
		pl = pl.next


def deleteNode(head, key):
	prev = None
	cur = head
	while cur:
		if cur.data == key:
			break
		prev = cur
		cur = cur.next
	prev.next = cur.next
	del cur

def reverse(head):
	stk = []
	cur = head
	while cur:
		stk.append(cur.data)
		cur = cur.next
		if cur is head:
			break
	cur = head
	while cur:
		cur.data = stk.pop()
		cur = cur.next
		if cur is head:
			break


def deleteNode(head, key):
	prev = None
	cur = head
	while cur:
		if cur.data == key:
			break
		prev = cur
		cur = cur.next
	prev.next = cur.next
	del cur

def reverse(head):
	stk = []
	cur = head
	while cur:
		stk.append(cur.data)
		cur = cur.next
		if cur is head:
			break
	cur = head
	while cur:
		cur.data = stk.pop()
		cur = cur.next
		if cur is head:
			break

class Node:

	def __init__(self, data):
		self.data = data
		self.next = None

def push(data, prev):
	if prev == None:
		prev = Node(data)
		return prev
	tmp = Node(data)
	prev.next = tmp
	return tmp

def printList(head):
	flg = False
	tmp = head
	while flg == False or tmp != head:
		flg = True
		print(tmp.data, end=' ')
		tmp = tmp.next
	print()


def deleteNode(head, key):
	prev = None
	cur = head
	while cur:
		if cur.data == key:
			break
		prev = cur
		cur = cur.next
	prev.next = cur.next
	del cur

def reverse(head):
	stk = []
	cur = head
	while cur:
		stk.append(cur.data)
		cur = cur.next
		if cur is head:
			break
	cur = head
	while cur:
		cur.data = stk.pop()
		cur = cur.next
		if cur is head:
			break

class Node:

	def __init__(self, data):
		self.data = data
		self.next = None

def push(data, prev):
	if prev == None:
		prev = Node(data)
		return prev
	tmp = Node(data)
	prev.next = tmp
	return tmp

def printList(head):
	flg = False
	tmp = head
	while flg == False or tmp != head:
		flg = True
		print(tmp.data, end=' ')
		tmp = tmp.next
	print()
if __name__ == '__main__':
	for _ in range(int(input())):
		n = int(input())
		arr = [int(i) for i in input().split()]
		delNode = int(input())
		head = Node(None)
		prev = head
		for i in arr:
			prev = push(i, prev)
		head = head.next
		prev.next = head
		deleteNode(head, delNode)
		reverse(head)
		printList(head)


def deleteNode(bead, key):
	rod = bead
	while True:
		if rod.next.data == key:
			rod.next = rod.next.next
			break
		rod = rod.next

def reverse(bead):
	global head
	mad = bead
	past = bead
	head = bead.next
	while True:
		if bead.next == mad:
			mad.next = bead
			bead.next = past
			head = bead
			return 0
		y = bead.next
		bead.next = past
		past = bead
		bead = y


def deleteNode(head, key):
	prev = None
	curr = head
	nxt = curr.next
	while curr:
		if curr.data == key and curr != head and (nxt != head):
			prev.next = nxt
			break
		prev = curr
		curr = nxt
		nxt = nxt.next

def reverse(head):
	arr = []
	curr = head
	while curr:
		arr.append(curr.data)
		if curr.next == head:
			break
		curr = curr.next
	curr = head
	arr.reverse()
	for i in range(len(arr)):
		curr.data = arr[i]
		curr = curr.next


def deleteNode(head, key):
	if head == key and head.next == head:
		return None
	curr = head.next
	prev = head
	while curr != head:
		if curr.data == key:
			prev.next = curr.next
		prev = curr
		curr = curr.next

def reverse(head):
	prev = head
	curr = head.next
	while curr != head:
		nxt = curr.next
		curr.next = prev
		prev = curr
		curr = nxt
	head.next = prev
	curr = head.next
	saved = head.next.data
	while curr != head:
		curr.data = curr.next.data
		curr = curr.next
	curr.data = saved


def deleteNode(head, key):
	curr = head
	prev = None
	while curr.data != key:
		prev = curr
		curr = curr.next
	prev.next = curr.next

def reverse(head):
	curr = head.next
	prev = head
	nex = None
	while curr != head:
		nex = curr.next
		curr.next = prev
		prev = curr
		curr = nex
	head.next = prev
	last_node_data = head.data
	head.data = head.next.data
	curr = head.next
	while curr.next != head:
		curr.data = curr.next.data
		curr = curr.next
	curr.data = last_node_data


def deleteNode(head, key):
	fast = head
	while fast.next:
		if fast.next.data == key:
			break
		fast = fast.next
	fast.next = fast.next.next

def reverse(head):
	prev = None
	current = head
	next = current.next
	current.next = prev
	prev = current
	current = next
	while current != head:
		next = current.next
		current.next = prev
		prev = current
		current = next
	head.next = prev
	head = prev
	start = head.next
	startData = start.data
	while start.data != head.data:
		temp = start.next.data
		start.data = temp
		start = start.next
	start.data = startData


def deleteNode(head, key):
	s = {}
	l = []
	t = head
	while True:
		if t in s:
			break
		s[t] = 1
		if t.data != key:
			l.append(t)
		t = t.next
	n = len(l)
	l = l[::-1]
	for i in range(n - 1):
		l[i].next = l[i + 1]
	l[-1].next = l[0]

def reverse(head1):
	global head
	head = head.next
	pass


def deleteNode(head, key):
	temp = head
	head = head.next
	if head.data == key:
		temp.next = head.next
		head = head.next
		return
	while head != temp:
		if head.next.data == key:
			head.next = head.next.next
			return
		else:
			head = head.next
	return

def reverse(head):
	prev = None
	current = head
	next = current.next
	current.next = prev
	prev = current
	current = next
	while current != head:
		next = current.next
		current.next = prev
		prev = current
		current = next
	head.next = prev
	head = prev
	start = head.next
	startData = start.data
	while start.data != head.data:
		temp = start.next.data
		start.data = temp
		start = start.next
	start.data = startData


def deleteNode(head, key):
	curr = head
	prev = None
	while curr.next != head:
		if curr.data == key:
			prev.next = curr.next
			break
		prev = curr
		curr = curr.next

def reverse(head):
	prev = head
	curr = head.next
	while curr != head:
		nxt = curr.next
		curr.next = prev
		prev = curr
		curr = nxt
	head.next = prev
	curr = head.next
	store = head.next.data
	while curr != head:
		curr.data = curr.next.data
		curr = curr.next
	head.data = store


def deleteNode(head, key):
	curr = head
	prev = None
	while curr.next != head:
		if curr.data == key:
			prev.next = curr.next
			break
		prev = curr
		curr = curr.next

def reverse(head):
	prev = head
	curr = head.next
	while curr != head:
		nxt = curr.next
		curr.next = prev
		prev = curr
		curr = nxt
	head.next = prev
	curr = head.next
	store1 = head.data
	store2 = head.next.data
	while curr:
		curr.data = curr.next.data
		curr = curr.next
		if curr.next == head:
			curr.data = store1
			break
	head.data = store2


def deleteNode(head, key):
	curr = head
	prev = None
	while curr.next != head:
		if curr.data == key:
			prev.next = curr.next
			break
		prev = curr
		curr = curr.next

def reverse(head):
	curr = head
	prev = None
	while curr.next != head:
		curr = curr.next
	curr.next = None
	curr = head
	prev = None
	while curr:
		n_node = curr.next
		curr.next = prev
		prev = curr
		curr = n_node
	head.next = prev
	curr = head.next
	store1 = head.data
	store2 = head.next.data
	temp = None
	while curr:
		temp = curr.data
		curr.data = curr.next.data
		curr = curr.next
		if curr.next == head:
			curr.data = store1
			break
	head.data = store2


def deleteNode(head, key):
	prev = head
	curr = head
	while True:
		curr = curr.next
		if curr.data == key:
			prev.next = curr.next
			break
		prev = curr

def reverse(head):
	(prev, curr) = (head, head.next)
	while curr != head:
		temp = curr.next
		curr.next = prev
		prev = curr
		curr = temp
	curr.next = prev


def deleteNode(head, key):
	curr = head
	prev = None
	while curr.data != key:
		prev = curr
		curr = curr.next
	if prev == None:
		head = head.next
	else:
		prev.next = curr.next

def reverse(head):
	dp = []
	curr = head
	while curr.next != head:
		dp.append(curr.data)
		curr = curr.next
	dp.append(curr.data)
	dp.reverse()
	curr = head
	i = 0
	while curr.next != head:
		curr.data = dp[i]
		i += 1
		curr = curr.next
	curr.data = dp[i]


def deleteNode(head, key):
	if head == key and head.next == head:
		return None
	curr = head.next
	prev = head
	while curr != head:
		if curr.data == key:
			prev.next = curr.next
		prev = curr
		curr = curr.next

def reverse(head):
	prev = head
	curr = head.next
	while curr != head:
		nxt = curr.next
		curr.next = prev
		prev = curr
		curr = nxt
	head.next = prev
	curr = head.next
	saved = head.next.data
	while curr != head:
		curr.data = curr.next.data
		curr = curr.next
	curr.data = saved

class Node:

	def __init__(self, data):
		self.data = data
		self.next = None

def push(data, prev):
	if prev == None:
		prev = Node(data)
		return prev
	tmp = Node(data)
	prev.next = tmp
	return tmp

def printList(head):
	flg = False
	tmp = head
	while flg == False or tmp != head:
		flg = True
		print(tmp.data, end=' ')
		tmp = tmp.next
	print()
if __name__ == '__main__':
	for _ in range(int(input())):
		n = int(input())
		arr = [int(i) for i in input().split()]
		delNode = int(input())
		head = Node(None)
		prev = head
		for i in arr:
			prev = push(i, prev)
		head = head.next
		prev.next = head
		deleteNode(head, delNode)
		reverse(head)
		printList(head)


def deleteNode(head, key):
	if head.data == key:
		head = head.next
		node = head
	else:
		node = head
		while head.next and head.next.data != key:
			head = head.next
		if head.next:
			head.next = head.next.next
	return node

def reverse(head):
	i = head
	flag = True
	stack = []
	while i != head or flag:
		stack.append(i.data)
		i = i.next
		flag = False
	i = head
	flag = True
	while i != head or flag:
		i.data = stack.pop()
		i = i.next
		flag = False
	return head

class Node:

	def __init__(self, data):
		self.data = data
		self.next = None

def push(data, prev):
	if prev == None:
		prev = Node(data)
		return prev
	tmp = Node(data)
	prev.next = tmp
	return tmp

def printList(head):
	flg = False
	tmp = head
	while flg == False or tmp != head:
		flg = True
		print(tmp.data, end=' ')
		tmp = tmp.next
	print()
if __name__ == '__main__':
	for _ in range(int(input())):
		n = int(input())
		arr = [int(i) for i in input().split()]
		delNode = int(input())
		head = Node(None)
		prev = head
		for i in arr:
			prev = push(i, prev)
		head = head.next
		prev.next = head
		deleteNode(head, delNode)
		reverse(head)
		printList(head)


def deleteNode(head, key):
	tmpprev = head
	tmp = head.next
	while tmp:
		if tmp.data == key:
			tmpprev.next = tmp.next
			break
		tmpprev = tmp
		tmp = tmp.next
		if tmp == head:
			break
	return head

def reverse(head):
	arr = []
	tmp = head
	while tmp:
		arr.append(tmp.data)
		tmp = tmp.next
		if tmp == head:
			break
	arr = arr[::-1]
	tmp = head
	for i in range(len(arr)):
		tmp.data = arr[i]
		if i == len(arr) - 1:
			tmp.next = head
			break
		else:
			tmp = tmp.next
	return head

class Node:

	def __init__(self, data):
		self.data = data
		self.next = None

def push(data, prev):
	if prev == None:
		prev = Node(data)
		return prev
	tmp = Node(data)
	prev.next = tmp
	return tmp

def printList(head):
	flg = False
	tmp = head
	while flg == False or tmp != head:
		flg = True
		print(tmp.data, end=' ')
		tmp = tmp.next
	print()
if __name__ == '__main__':
	for _ in range(int(input())):
		n = int(input())
		arr = [int(i) for i in input().split()]
		delNode = int(input())
		head = Node(None)
		prev = head
		for i in arr:
			prev = push(i, prev)
		head = head.next
		prev.next = head
		deleteNode(head, delNode)
		reverse(head)
		printList(head)


def deleteNode(head, key):
	if head == None:
		return
	n = head
	d = None
	while n.next != head:
		if n.next.data == key:
			d = n.next
			n.next = d.next
		n = n.next

def reverse(temp_head):
	newhead = temp_head
	curr = temp_head
	while curr.next != newhead:
		curr = curr.next
	curr.next = None
	newhead = temp_head
	curr = temp_head
	prev = None
	next = None
	while curr.next:
		next = curr.next
		curr.next = prev
		prev = curr
		curr = next
	curr.next = prev
	temp_head.next = curr
	global head
	head = curr

class Node:

	def __init__(self, data):
		self.data = data
		self.next = None

def push(data, prev):
	if prev == None:
		prev = Node(data)
		return prev
	tmp = Node(data)
	prev.next = tmp
	return tmp

def printList(head):
	flg = False
	tmp = head
	while flg == False or tmp != head:
		flg = True
		print(tmp.data, end=' ')
		tmp = tmp.next
	print()
if __name__ == '__main__':
	for _ in range(int(input())):
		n = int(input())
		arr = [int(i) for i in input().split()]
		delNode = int(input())
		head = Node(None)
		prev = head
		for i in arr:
			prev = push(i, prev)
		head = head.next
		prev.next = head
		deleteNode(head, delNode)
		reverse(head)
		printList(head)


def deleteNode(head, key):
	curr = head
	while curr.next.data != key:
		curr = curr.next
	curr.next = curr.next.next

def reverse(temp_head):
	newhead = temp_head
	curr = temp_head
	while curr.next != newhead:
		curr = curr.next
	curr.next = None
	newhead = temp_head
	curr = temp_head
	prev = None
	next = None
	while curr.next:
		next = curr.next
		curr.next = prev
		prev = curr
		curr = next
	curr.next = prev
	temp_head.next = curr
	global head
	head = curr


global head

def deleteNode(head, key):
	curr = head
	prev = head
	while curr.next:
		if curr.data == key:
			prev.next = curr.next
			break
		prev = curr
		curr = curr.next

def reverse(temp_head):
	curr = temp_head
	prev = None
	while curr.next:
		next = curr.next
		curr.next = prev
		prev = curr
		curr = next
	curr.next = prev
	global head
	head = prev


def deleteNode(head, key):
	if head != None and head.next != None:
		temp = head
		while temp.next.data != key:
			temp = temp.next
		temp.next = temp.next.next

def reverse(head):
	if head != None or head.next != None:
		prev = head
		curr = head.next
		after = head.next.next
		while after != head:
			curr.next = prev
			prev = curr
			curr = after
			after = after.next
		curr.next = prev
		after.next = curr
		temp1 = head.next.data
		temp = head.next
		while temp != head:
			temp.data = temp.next.data
			temp = temp.next
		temp.data = temp1


def deleteNode(head, key):
	if head is None:
		return head
	p = head
	c = head.next
	while c != head:
		if c.data == key:
			p.next = c.next
			break
		p = c
		c = c.next
	if c == head:
		head = c.next
	return head
head = None

def reverse(head_):
	global head
	if head == None:
		return head
	p = head
	c = head.next
	while c != head:
		next = c.next
		c.next = p
		p = c
		c = next
	c.next = p
	head = head.next