There is only little time left until the New Year! For that reason, Chef decided to make a present for his best friend. He made a connected and undirected simple graph with N$N$ vertices and M$M$ edges.
Since Chef does not like odd numbers and his friend does not like undirected graphs, Chef decided to direct each edge in one of two possible directions in such a way that the indegrees of all vertices of the resulting graph are even. The indegree of a vertex is the number of edges directed to that vertex from another vertex. As usual, Chef is busy in the kitchen, so you need to help him with directing the edges. Find one possible way to direct them or determine that it is impossible under the given conditions, so that Chef could bake a cake as a present instead.

-----Input-----
- The first line of the input contains a single integer T$T$ denoting the number of test cases. The description of T$T$ test cases follows.
- The first line of each test case contains two space-separated integers N$N$ and M$M$.
- M$M$ lines follow. For each i$i$ (1≤i≤M$1 \le i \le M$), the i$i$-th of these lines contains two space-separated integers ui$u_i$ and vi$v_i$ denoting an edge between vertices ui$u_i$ and vi$v_i$.

-----Output-----
For each test case:
- If a valid way to direct the edges does not exist, print a single line containing one integer −1$-1$.
- Otherwise, print a single line containing M$M$ space-separated integers. For each valid i$i$, the i$i$-th of these integers should be 0$0$ if edge i$i$ is directed from ui$u_i$ to vi$v_i$ or 1$1$ if it is directed from vi$v_i$ to ui$u_i$.

-----Constraints-----
- 1≤T≤5$1 \le T \le 5$
- 1≤N,M≤105$1 \le N, M \le 10^5$
- 1≤ui,vi≤N$1 \le u_i, v_i \le N$ for each valid i$i$
- the graph on the input is connected, does not contain multiple edges or self-loops

-----Subtasks-----
Subtask #1 (30 points): 1≤N,M≤1,000$1 \le N, M \le 1,000$
Subtask #2 (70 points): original constraints

-----Example Input-----
2
4 4
1 2
1 3
2 4
3 4
3 3
1 2
2 3
1 3

-----Example Output-----
0 0 1 1
-1

Difficulty: HARD

Solutions:
from collections import deque
from heapq import *
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	graph = [set() for i in range(n)]
	newgraph = [set() for i in range(n)]
	edges = []
	for i in range(m):
		(ui, vi) = map(int, input().split())
		edges.append((ui - 1, vi - 1))
		graph[ui - 1].add(vi - 1)
		graph[vi - 1].add(ui - 1)
	if m & 1:
		print(-1)
		continue
	nodes = [(len(graph[i]), -i) for i in range(n)]
	indegs = [0] * n
	heapify(nodes)
	while nodes:
		(l, node) = heappop(nodes)
		node = -1 * node
		clist = list(graph[node])
		graph[node] = set()
		for cnode in clist:
			if indegs[node] & 1:
				newgraph[cnode].add(node)
				indegs[node] += 1
			else:
				newgraph[node].add(cnode)
				indegs[cnode] += 1
			graph[cnode].remove(node)
			heappush(nodes, (len(graph[cnode]), -cnode))
	ans = []
	for (ui, vi) in edges:
		if ui in newgraph[vi]:
			ans.append(1)
		else:
			ans.append(0)
	print(*ans)


T = int(input())
for t in range(T):
	(N, M) = map(int, input().split())
	tab = dict()
	sasiedzi = [[] for i in range(N)]
	wchodzs = [[] for i in range(N)]
	visited = [-1] * N
	kraws = []
	for m in range(M):
		(x, y) = map(int, input().split())
		x -= 1
		y -= 1
		kraws.append((x, y))
		tab[x, y] = 1
		tab[y, x] = 1
		sasiedzi[x].append(y)
		sasiedzi[y].append(x)
	for vis in range(N):
		if visited[vis] >= 0:
			continue
		krawedzi = set()
		stack = [vis]
		visited[vis] = 0
		ind = 0
		while stack:
			nr = stack[-1]
			bylo = []
			for j in range(visited[nr], len(sasiedzi[nr])):
				i = sasiedzi[nr][j]
				if visited[i] >= 0:
					continue
				stack.append(i)
				visited[nr] = j + 1
				visited[i] = 0
				bylo = True
				break
			if bylo:
				continue
			stack.pop()
			if not stack:
				break
			wchodzacych = 0
			nieustalonych = 0
			for i in sasiedzi[nr]:
				if tab[nr, i] == -1:
					wchodzacych += 1
				if tab[nr, i] == tab[i, nr]:
					nieustalonych += 1
					nieustnr = i
			if nieustalonych == 1:
				if wchodzacych % 2 == 0:
					tab[nieustnr, nr] = -1
				else:
					tab[nr, nieustnr] = -1
			else:
				for i in sasiedzi[nr]:
					if nieustalonych == 0:
						break
					if tab[nr, i] != tab[i, nr]:
						continue
					if nieustalonych == 1 and wchodzacych % 2 == 0:
						tab[i, nr] = -1
						nieustalonych -= 1
						break
					tab[nr, i] = -1
					nieustalonych -= 1
					wchodzacych += 1
		if len(krawedzi) % 2 == 1:
			niepa = True
	res = [0] * M
	nr = 0
	for (x, y) in kraws:
		if tab[x, y] == -1:
			res[nr] = 1
		nr += 1
	ok = True
	for y in range(N):
		chk = 0
		for sas in sasiedzi[y]:
			if tab[y, sas] == -1:
				chk += 1
		if chk % 2 != 0:
			ok = False
	if ok:
		print(' '.join(map(str, res)))
	else:
		print(-1)


from collections import defaultdict
import sys
sys.setrecursionlimit(1000000)
global edgesdone

def f():
	sys.stdout.flush()

def solve(key, nodes, track, result):
	node = nodes[key]
	track[key] = 1
	tmpindg = 0
	global edgesdone
	for sub in node:
		(k, s) = (int(key), int(sub))
		if track.get(sub, 0) == 0:
			edgesdone += 1
			indg = solve(sub, nodes, track, result)
			if indg == 0:
				result[k][s] = '1'
				result[s][k] = '0'
				tmpindg ^= 1
			else:
				result[k][s] = '0'
				result[s][k] = '1'
		elif track[sub] == 2:
			result[k][s] = '1'
			result[s][k] = '0'
			tmpindg ^= 1
			edgesdone += 1
		else:
			nodes[sub].append(key)
	track[key] = 2
	return tmpindg
tests = int(input())
result = defaultdict(lambda : defaultdict(int))
for t in range(tests):
	edgesdone = 0
	(n, m) = map(int, input().split())
	if m % 2 == 1:
		for i in range(m):
			tmp = input()
		print(-1)
	else:
		nodes = {}
		track = {}
		order = []
		for i in range(0, m):
			(v1, v2) = sys.stdin.readline().split()
			if i == 0:
				startkey = v1
			n1 = nodes.get(v1)
			n2 = nodes.get(v2)
			if n1 is None:
				if n2 is None:
					nodes[v1] = [v2]
					nodes[v2] = []
				else:
					n2.append(v1)
					nodes[v2] = n2
					nodes[v1] = []
			else:
				n1.append(v2)
				nodes[v1] = n1
				if n2 is None:
					nodes[v2] = []
			order.append((int(v1), int(v2)))
		tmp = solve(startkey, nodes, track, result)
		res = []
		for query in order:
			res.append(result[query[0]][query[1]])
		print(' '.join(res))
	f()


t = int(input())
for z in range(t):
	(n, m) = map(int, input().split())
	edges = [[0] * 2 for i in range(m)]
	degrees = [0] * (n + 1)
	dgcp = [0] * (n + 1)
	ans = [0] * m
	less = [-1] * (n + 1)
	great = [-1] * (n + 1)
	for i in range(m):
		(u, v) = map(int, input().split())
		edges[i] = [u, v]
		if u > v:
			if great[v] == -1:
				great[v] = i
			if less[u] == -1:
				less[u] = i
		else:
			if great[u] == -1:
				great[u] = i
			if less[v] == -1:
				less[v] = i
		degrees[v] += 1
		dgcp[v] += 1
	flag = 1
	for i in range(1, n):
		if degrees[i] % 2 != 0:
			j = great[i]
			if j > -1:
				if edges[j][1] == i and edges[j][0] > i:
					degrees[i] -= 1
					degrees[edges[j][0]] += 1
					ans[j] = 1
				elif edges[j][1] > i and edges[j][0] == i:
					degrees[i] += 1
					degrees[edges[j][1]] -= 1
					ans[j] = 1
		if degrees[i] % 2 != 0:
			flag = 0
	if degrees[n] % 2 == 0 and flag == 1:
		str1 = ' '.join((str(e) for e in ans))
		print(str1)
	else:
		ans = [0] * m
		flag = 1
		for i in range(n, 1, -1):
			if dgcp[i] % 2 != 0:
				j = less[i]
				if j > -1:
					if edges[j][1] == i and edges[j][0] < i:
						dgcp[i] -= 1
						dgcp[edges[j][0]] += 1
						ans[j] = 1
					elif edges[j][1] < i and edges[j][0] == i:
						dgcp[i] += 1
						dgcp[edges[j][1]] -= 1
						ans[j] = 1
			if dgcp[i] % 2 != 0:
				flag = 0
		if dgcp[1] % 2 == 0 and flag == 1:
			str1 = ' '.join((str(e) for e in ans))
			print(str1)
		else:
			print(-1)


import time
import sys
import gc
from collections import deque

class Graph(object):

	def __init__(self, N, M):
		self.N = N
		self.M = M
		self.vec = {}
		self.edgeDirs = [0] * self.M
		self.spanning_edges = {}
		self.edge2index = {}
		self.index = 0

	def addEdge(self, u, v):
		if u not in self.vec:
			self.vec[u] = set()
		self.vec[u].add(v)
		if v not in self.vec:
			self.vec[v] = set()
		self.vec[v].add(u)
		self.edge2index[u, v] = self.index
		self.index += 1

	def makeEven(self):
		post_order = []
		stack = [1]
		visited = set()
		while len(stack) != 0:
			u = stack.pop()
			if u in visited:
				continue
			else:
				visited.add(u)
				post_order.append(u)
			for v in self.vec[u]:
				if v not in visited:
					self.spanning_edges[v] = u
					stack.append(v)
		while len(post_order) != 0:
			u = post_order.pop()
			odd_edges = len(self.vec[u]) % 2 == 1
			if odd_edges:
				span_node = self.spanning_edges[u]
				self.vec[u].remove(span_node)
			while len(self.vec[u]) > 0:
				v1 = self.vec[u].pop()
				v2 = self.vec[u].pop()
				self.direct(v1, u)
				self.direct(v2, u)
				self.vec[v1].remove(u)
				self.vec[v2].remove(u)
			if odd_edges:
				self.vec[u].add(span_node)

	def direct(self, u, v):
		if (u, v) not in self.edge2index:
			self.edgeDirs[self.edge2index[v, u]] = 1

	def getIndex(self, edge):
		(u, v) = edge
		if (u, v) in self.edge2index:
			return self.edge2index[u, v]
		else:
			return self.edge2index[v, u]
T = int(input())
for _ in range(T):
	[N, M] = list(map(int, input().split()))
	if M & 1:
		for _ in range(M):
			input()
		print(-1)
		sys.stdout.flush()
	else:
		graph = Graph(N, M)
		for _ in range(M):
			[ui, vi] = list(map(int, input().split()))
			graph.addEdge(ui, vi)
		graph.makeEven()
		print(' '.join(map(str, graph.edgeDirs)))
		sys.stdout.flush()


import sys
sys.setrecursionlimit(20000)

def leftedges(v):
	arr = []
	for i in dic[v]:
		if ans.get(str(i) + '-' + str(v), -1) == None or ans.get(str(v) + '-' + str(i), -1) == None:
			arr.append(i)
	return arr

def dfs(v, visited, ans, dic, check):
	visited[v] = True
	for i in dic[v]:
		if visited[i] == False:
			dfs(i, visited, ans, dic, check)
	arr = leftedges(v)
	l = len(arr)
	if l == 0:
		return
	elif l % 2 == 1 and check[v] % 2 == 0:
		a = arr[-1]
		if ans.get(str(v) + '-' + str(a), -1) == None:
			ans[str(v) + '-' + str(a)] = 0
		else:
			ans[str(a) + '-' + str(v)] = 1
		check[a] += 1
		check[v] += l - 1
		for j in range(l - 1):
			a = arr[j]
			if ans.get(str(a) + '-' + str(v), -1) == None:
				ans[str(a) + '-' + str(v)] = 0
			else:
				ans[str(v) + '-' + str(a)] = 1
	elif l % 2 == 1 and check[v] % 2 == 1:
		check[v] += l
		for j in range(l):
			a = arr[j]
			if ans.get(str(a) + '-' + str(v), -1) == None:
				ans[str(a) + '-' + str(v)] = 0
			else:
				ans[str(v) + '-' + str(a)] = 1
	elif l % 2 == 0 and check[v] % 2 == 1:
		a = arr[-1]
		if ans.get(str(v) + '-' + str(a), -1) == None:
			ans[str(v) + '-' + str(a)] = 0
		else:
			ans[str(a) + '-' + str(v)] = 1
		check[a] += 1
		check[v] += l - 1
		for j in range(l - 1):
			a = arr[j]
			if ans.get(str(a) + '-' + str(v), -1) == None:
				ans[str(a) + '-' + str(v)] = 0
			else:
				ans[str(v) + '-' + str(a)] = 1
	elif l % 2 == 0 and check[v] % 2 == 0:
		check[v] += l
		for j in range(l):
			a = arr[j]
			if ans.get(str(a) + '-' + str(v), -1) == None:
				ans[str(a) + '-' + str(v)] = 0
			else:
				ans[str(v) + '-' + str(a)] = 1
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	dic = {i: [] for i in range(1, n + 1)}
	ans = {}
	for i in range(m):
		(u, v) = map(int, input().split())
		dic[u].append(v)
		dic[v].append(u)
		ans[str(u) + '-' + str(v)] = None
	if m % 2 == 1:
		print(-1)
	else:
		visited = {i: False for i in range(1, n + 1)}
		check = {i: 0 for i in range(1, n + 1)}
		dfs(1, visited, ans, dic, check)
		print(*ans.values())


import sys

def fprint(x):
	print(x, flush=True)

def qprint(x):
	sys.stdout.write(str(x) + '\n')

def input():
	return sys.stdin.readline()
import itertools
t = int(input())

class vertex:

	def __init__(self, id):
		self.num = id
		self.connections = []
		self.treeConnections = []
		self.inDeg = 0
		self.included = False
for _ in range(t):
	(N, M) = map(int, input().split())
	if M % 2:
		for x in range(M):
			(n, m) = map(int, input().split())
		print('-1')
	else:
		edges = []
		for x in range(M):
			edges.append([0, 0])
		vertices = []
		for i in range(0, N + 1):
			vertices.append(vertex(i))
		for x in range(M):
			(edges[x][0], edges[x][1]) = map(int, input().split())
			vertices[edges[x][0]].connections.append([edges[x][1], x])
			vertices[edges[x][1]].connections.append([edges[x][0], x])
		answer = list(itertools.repeat(-1, M))
		inST = list(itertools.repeat(0, M))
		queue = [1]
		vertices[1].included = True
		stack = [1]
		while len(queue) != 0:
			cur_ver = queue.pop()
			for conn in vertices[cur_ver].connections:
				if not vertices[conn[0]].included:
					vertices[cur_ver].treeConnections.append(conn)
					queue.append(conn[0])
					stack.append(conn[0])
					vertices[conn[0]].included = True
					inST[conn[1]] = 1
		for (index, edge) in enumerate(edges):
			if not inST[index]:
				answer[index] = 1
				vertices[edge[0]].inDeg += 1
		for ind in range(len(stack) - 1, -1, -1):
			cur_ver = stack[ind]
			for conn in vertices[cur_ver].treeConnections:
				(index, edge) = (conn[1], edges[conn[1]])
				if vertices[conn[0]].inDeg % 2:
					vertices[conn[0]].inDeg += 1
					if cur_ver == edge[0]:
						answer[index] = 0
					else:
						answer[index] = 1
				else:
					vertices[cur_ver].inDeg += 1
					if cur_ver == edge[0]:
						answer[index] = 1
					else:
						answer[index] = 0
		print(' '.join(map(str, answer)))


def find_edge_directions():
	if M % 2:
		return '-1'
	undirDict = {i: [] for i in range(N + 1)}
	degCtr = [0 for i in range(N + 1)]
	indegCtr = [0 for i in range(N + 1)]
	lenList = [[] for i in range(N + 1)]
	result = [None for i in range(M)]
	for i in range(M):
		undirDict[uvList[i][0]].append(uvList[i])
		undirDict[uvList[i][1]].append(uvList[i])
		degCtr[uvList[i][0]] += 1
		degCtr[uvList[i][1]] += 1
	for i in range(1, N + 1):
		lenList[len(undirDict[i])].append(i)
	(i, ctr) = (1, 0)
	while ctr < M:
		while i < 1 or lenList[i] == []:
			i += 1
		vert = lenList[i][0]
		if undirDict[vert] == []:
			del lenList[i][0]
			continue
		(u, v, idx) = undirDict[vert][0]
		if result[idx] != None:
			del undirDict[vert][0]
			continue
		elif u == vert and indegCtr[vert] % 2 or (v == vert and indegCtr[vert] % 2 == 0):
			result[idx] = '1'
			indegCtr[u] += 1
		else:
			result[idx] = '0'
			indegCtr[v] += 1
		del undirDict[vert][0]
		del lenList[i][0]
		lenList[i - 1].append(vert)
		if vert == v:
			lenList[degCtr[u] - 1].append(u)
		else:
			lenList[degCtr[v] - 1].append(v)
		(degCtr[u], degCtr[v]) = (degCtr[u] - 1, degCtr[v] - 1)
		i = i - 1
		ctr += 1
	return ' '.join(result)
T = int(input())
opList = []
for x in range(T):
	(N, M) = map(int, input().split())
	uvList = [list(map(int, input().split())) + [_] for _ in range(M)]
	opList.append(find_edge_directions())
print('\n'.join(opList))


class Nodes:

	def __init__(self, k, p, q):
		self.value = k
		self.next = None
		self.reverse = p
		self.edgenumber = q

class GraphNodes:

	def __init__(self, k):
		self.value = k
		self.next = None
		self.visited = 0

class AdjascencyList:

	def __init__(self, n):
		self.adj_lst = [None] * n
		for i in range(n):
			self.adj_lst[i] = GraphNodes(i)

	def insert(self, x, y, p, q):
		temp = Nodes(y, p, q)
		a = self.adj_lst[x]
		temp.next = a.next
		a.next = temp

def solve(G, Q, n, m):
	ans = [0 for i in range(m)]
	for i in range(n - 1, 0, -1):
		v = i
		if Q[v] == 0:
			a = G.adj_lst[v]
			while a.next != None:
				if G.adj_lst[a.next.value].visited == 0:
					ans[a.next.edgenumber] = a.next.reverse
					Q[a.next.value] = Q[a.next.value] ^ 1
				a = a.next
		else:
			a = G.adj_lst[v]
			counts = 0
			while a.next != None and counts == 0:
				if G.adj_lst[a.next.value].visited == 0:
					ans[a.next.edgenumber] = a.next.reverse ^ 1
					counts = 1
				a = a.next
			while a.next != None:
				if G.adj_lst[a.next.value].visited == 0:
					ans[a.next.edgenumber] = a.next.reverse
					Q[a.next.value] = Q[a.next.value] ^ 1
				a = a.next
		G.adj_lst[v].visited = 1
	return ans

def main():
	t = int(input())
	for p in range(t):
		(n, m) = input().split()
		(n, m) = (int(n), int(m))
		if m % 2 == 1:
			for i in range(m):
				(a, b) = input().split()
			print(-1)
		else:
			G = AdjascencyList(n)
			Q = [0 for i in range(n)]
			for i in range(m):
				(a, b) = input().split()
				(a, b) = (int(a) - 1, int(b) - 1)
				G.insert(a, b, 0, i)
				G.insert(b, a, 1, i)
			final = solve(G, Q, n, m)
			str1 = ' '.join((str(e) for e in final))
			print(str1)
main()