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: interview

Starter code:

Solutions:
# cook your dish here
for _ in range(int(input())):
    N, M = [int(x) for x in input().split()]
    edges = [0]*M
    dir = {}
    nodes = [[] for j in range(N+1)]
    ind = [0]*(N+1)
    graph = {}
    final_edges = []
    for i in range(M):
        u, v = [int(x) for x in input().split()]
        nodes[u].append(v)
        nodes[v].append(u)
        dir[(u,v)]=1
        dir[(v,u)] = 0
        ind[v] += 1
        graph[(u,v)] = graph[(v,u)] = i
        final_edges.append([u,v])
    if M%2!=0:
        print(-1)
        continue
    for i in range(M):
        u, v = final_edges[i]
        if ind[u]%2!=0 and ind[v]%2!=0:
            d = dir[(u,v)]
            if d:
                ind[u] += 1
                ind[v] -= 1
                dir[(u,v)] = 0
                dir[(v,u)] = 1
                edges[i] = abs(edges[i]-1)
            else:
                ind[u] -= 1
                ind[v] += 1
                dir[(u, v)] = 1
                dir[(v, u)] = 0
                edges[i] = abs(edges[i]-1)
    s = []
    for i in range(1, N+1):
        if ind[i]%2:
            s.append(i)
    while s:
        set1 = set()
        for u in s:
            if ind[u]%2:
                v = nodes[u][0]
                d = dir[(u,v)]
                index = graph[(u, v)]
                set1.add(v)
                if d:
                    ind[u] += 1
                    ind[v] -= 1
                    dir[(u, v)] = 1
                    dir[(v, u)] = 1
                    edges[index] = abs(edges[index]-1)
                else:
                    ind[u] -= 1
                    ind[v] += 1
                    dir[(u, v)] = 1
                    dir[(v, u)] = 0
                    edges[index] = abs(edges[index]-1)

        s = set1
    print(*edges)

for __ in range(int(input())):
    n,m=map(int,input().split())
    g=[[]for _ in range(n+1)]
    long=[]
    di={}
    stable=[0 for _ in range(n+1)]
    edges=[0 for _ in range(m)]
    for i in range(m):
        x,y=map(int,input().split())
        g[x].append(y)
        g[y].append(x)
        stable[y]+=1
        long.append([x,y])
        di[(x,y)]=i
        di[(y,x)]=i
    f=1
    if m%2:
        f=0
    for i in range(m):
        c,d=long[i][0],long[i][1]
        if stable[c]%2==1 and stable[d]%2==1:
            stable[c]+=1
            stable[d]-=1
            edges[i]=1
    s=[]
    for i in range(1,n+1):
        if stable[i]%2==1:
            s.append(i)
    while s and f:
        set1=set()
        for i in s:
            if stable[i]%2:
                y=g[i][0]
                w=di[(i,y)]
                stable[y]+=1
                stable[i]-=1

                set1.add(y)
                edges[w]=abs(edges[w]-1)
        s=set1
    if(f):
        print(*edges)
    else:
        print(-1)


import sys
from collections import deque

croot = dict()
setnumber = dict()

def finalroot(v):
    if croot[v] != v:
        v = finalroot(croot[v])
    return v

def union(v1,v2):
    r1 = finalroot(v1)
    r2 = finalroot(v2)
    if r1 != r2:
        if setnumber[r1] > setnumber[r2]:
            croot[r2] = r1
        else:
            croot[r1] = r2
            if setnumber[r1] == setnumber[r2]:
                setnumber[r2] += 1

def minspan(n,edges):

    finale = []
    edgecount = {}

    for i in range(n):
        edgecount[i+1] = 0
        setnumber[i+1] = 0
        croot[i+1] = i+1

    edges.sort()

    for e in edges:
        u,v = e
        if finalroot(u) != finalroot(v):
            edgecount[u] += 1
            edgecount[v] += 1
            finale.append((u,v))
            union(u,v)

    root = max(edgecount.keys(), key=(lambda k: edgecount[k]))
    return set(finale),root

def main():

    t = int(input())

    for i in range(t):

        n,m = [int(item) for item in input().split()]
        e = set()
        elist = []

        for i in range(m):
            u,v = [int(item) for item in input().split()]
            elist.append((u,v))
            e.add((u,v))

        finale,root = minspan(n,list(e))
        sroot = root
        tofix = e - finale

        indegree = {}
        for i in range(n):
            indegree[i+1] = 0

        decided = set()

        # randomly assign directions to edges not in min span
        for t in tofix:
            indegree[t[1]] += 1
            decided.add((t[0],t[1]))

        cf = {}
        pf = {}

        for i in range(n):
            cf[i+1] = set()
            pf[i+1] = set()

        # minspan finale- create tree
        # 2 dics cf - contains each node with it's child
        # pf - each node with it's parent

        for f in finale:
            u,v = f
            cf[u].add(v)
            cf[v].add(u)

        r = deque()
        r.append(root)

        while len(r) != 0:
            root = r.pop()
            for c in cf[root]:
                r.append(c)
                cf[c].remove(root)
                pf[c] = root


        q = deque([sroot])
        st = deque()
        # tree traversal
        while len(q) != 0:
            node = q.pop()
            st.append(node)
            for e in cf[node]:
                q.append(e)

        while len(st) != 0:
            c = st.pop()
            if c != sroot:
                p = pf[c]
                if indegree[c]%2 == 0:
                    #outgoing
                    decided.add((c,p))
                    indegree[p] += 1
                else:
                    decided.add((p,c))
                    indegree[c] += 1

        possible = (indegree[sroot]%2 == 0)
        if possible:
            ans = []
            for i in elist:
                if i in decided:
                    ans.append(0)
                else:
                    ans.append(1)
            print(*ans)
        else:
            print(-1)

    return 0

def __starting_point():
    main()

__starting_point()

import sys
from collections import deque

def minspan(n,edges):

    nodelist = []
    finale = []
    edgecount = {}

    for i in range(n):
        edgecount[i+1] = 0

    for i in range(n):
        nodelist.append(set([i+1]))

    for e in edges:

        u = e[0]
        v = e[1]

        for each_set in nodelist:
            if u in each_set:
                save1 = each_set
            if v in each_set:
                save2 = each_set

        if save1 != save2:
            finale.append((u,v))
            edgecount[u] += 1
            edgecount[v] += 1
            merged = save1 | save2
            nodelist.remove(save1)
            nodelist.remove(save2)
            nodelist.append(merged)

        if len(nodelist) == 1:
            break

    root = max(edgecount.keys(), key=(lambda k: edgecount[k]))
    return set(finale),root

def main():

    t = int(input())

    for i in range(t):

        n,m = [int(item) for item in input().split()]
        e = set()
        elist = []

        for i in range(m):
            u,v = [int(item) for item in input().split()]
            elist.append((u,v))
            e.add((u,v))

        finale,root = minspan(n,e)
        sroot = root
        tofix = e - finale

        indegree = {}
        for i in range(n):
            indegree[i+1] = 0

        decided = set()

        # randomly assign directions to edges not in min span
        for t in tofix:
            indegree[t[1]] += 1
            decided.add((t[0],t[1]))

        cf = {}
        pf = {}

        for i in range(n):
            cf[i+1] = set()
            pf[i+1] = set()

        # minspan finale- create tree
        # 2 dics cf - contains each node with it's child
        # pf - each node with it's parent

        for f in finale:
            u,v = f
            cf[u].add(v)
            cf[v].add(u)

        r = deque()
        r.append(root)

        while len(r) != 0:
            root = r.pop()
            for c in cf[root]:
                r.append(c)
                cf[c].remove(root)
                pf[c] = root


        q = deque([sroot])
        st = deque()
        # tree traversal
        while len(q) != 0:
            node = q.pop()
            st.append(node)
            for e in cf[node]:
                q.append(e)

        while len(st) != 0:
            c = st.pop()
            if c != sroot:
                p = pf[c]
                if indegree[c]%2 == 0:
                    #outgoing
                    decided.add((c,p))
                    indegree[p] += 1
                else:
                    decided.add((p,c))
                    indegree[c] += 1


        possible = (indegree[sroot]%2 == 0)
        if possible:
            ans = []
            for i in elist:
                if i in decided:
                    ans.append(0)
                else:
                    ans.append(1)
            print(*ans)
        else:
            print(-1)

    return 0

def __starting_point():
    main()

__starting_point()

import sys
from collections import deque

def minspan(n,edges):

    nodelist = []
    finale = []

    for i in range(n):
        nodelist.append(set([i+1]))

    for e in edges:
        u = e[0]
        v = e[1]

        for each_set in nodelist:
            if u in each_set:
                save1 = each_set
            if v in each_set:
                save2 = each_set

        if save1 != save2:
            finale.append((u,v))
            merged = save1 | save2
            nodelist.remove(save1)
            nodelist.remove(save2)
            nodelist.append(merged)

        if len(nodelist) == 1:
            break

    return finale

def main():

    t = int(input())

    for i in range(t):

        n,m = [int(item) for item in input().split()]
        e = set()
        elist = []

        for i in range(m):
            u,v = [int(item) for item in input().split()]
            elist.append((u,v))
            e.add((u,v))

        finale = set(minspan(n,e))
        tofix = e - finale

        indegree = {}
        for i in range(n):
            indegree[i+1] = 0

        decided = set()

        # randomly assign directions to edges not in min span
        for t in tofix:
            indegree[t[1]] += 1
            decided.add((t[0],t[1]))

        ## find leaf in finale
        ## fix random root i = 1
        cf = {}
        pf = {}

        for i in range(n):
            cf[i+1] = set()
            pf[i+1] = set()

        for f in finale:
            cf[f[0]].add(f[1])
            cf[f[1]].add(f[0])

        root = 1
        visit = set([root])
        visited = set()
        parent = {}

        #create the min span tree , every child with one parent
        # root is 1
        while len(visited) < n and len(visit) > 0:

            v = visit.pop()
            d = cf[v]
            visited.add(v)

            for i in d:
                if i not in visited:
                    parent[i] = v
                    pf[v].add(i)
                    visit.add(i)


        leaf = set()
        for k,v in cf.items():
            if len(v) == 1:
                leaf.add(k)


        st = [1]
        sc = [1]

        # get traversal order from kids to parents
        while len(st) < n+1:
            if len(sc) == 0:
                break
            j = sc.pop()
            for i in pf[j]:
                st.append(i)
                sc.append(i)

        while len(st) != 1:

            g = st.pop()
            h = parent[g]
            pair1 = (g,h)
            pair2 = (h,g)

            if indegree[g]%2 == 0:
                #outgoing
                decided.add(pair1)
                indegree[h] += 1
            else:
                #incoming
                decided.add(pair2)
                indegree[g] += 1


        possible = (indegree[root]%2 == 0)
        if possible:
            ans = []
            for i in elist:
                if i in decided:
                    ans.append(0)
                else:
                    ans.append(1)
            print(*ans)
        else:
            print(-1)


    return 0


def __starting_point():
    main()

__starting_point()

for t in range(int(input())):
    n,m=map(int,input().split())
    g=[[]for _ in range(n+1)]
    long=[]
    di={}
    stable=[0]*(n+1)
    edges=[0]*(m)
    for i in range(m):
        x,y=map(int,input().split())
        g[x].append(y)
        g[y].append(x)
        stable[y]+=1
        long.append([x,y])
        di[(x,y)]=i
        di[(y,x)]=i
    f=1
    if m%2:
        f=0
    for i in range(m):
        c,d=long[i][0],long[i][1]
        if stable[c]%2==1 and stable[d]%2==1:
            stable[c]+=1
            stable[d]-=1
            edges[i]=1
    s=[]
    for i in range(1,n+1):
        if stable[i]%2==1:
            s.append(i)
    while s and f:
        set1=set()
        for i in s:
            if stable[i]%2:
                y=g[i][0]
                w=di[(i,y)]
                if edges[x]:
                    stable[y]+=1
                    stable[i]-=1
                else:
                    stable[i]+=1
                    stable[y]-=1
                set1.add(y)
                edges[w]=abs(edges[w]-1)
        s=set1
    if(f):
        print(*edges)
    else:
        print(-1)

for _ in range(int(input())):
    N, M = [int(x) for x in input().split()]
    edges = [0]*M
    dir = {}
    nodes = [[] for j in range(N+1)]
    ind = [0]*(N+1)
    graph = {}
    final_edges = []
    for i in range(M):
        u, v = [int(x) for x in input().split()]
        nodes[u].append(v)
        nodes[v].append(u)
        dir[(u,v)]=1
        dir[(v,u)] = 0
        ind[v] += 1
        graph[(u,v)] = graph[(v,u)] = i
        final_edges.append([u,v])
    if M%2!=0:
        print(-1)
        continue
    for i in range(M):
        u, v = final_edges[i]
        if ind[u]%2!=0 and ind[v]%2!=0:
            d = dir[(u,v)]
            if d:
                ind[u] += 1
                ind[v] -= 1
                dir[(u,v)] = 0
                dir[(v,u)] = 1
                edges[i] = abs(edges[i]-1)
            else:
                ind[u] -= 1
                ind[v] += 1
                dir[(u, v)] = 1
                dir[(v, u)] = 0
                edges[i] = abs(edges[i]-1)
    s = []
    for i in range(1, N+1):
        if ind[i]%2:
            s.append(i)
    while s:
        set1 = set()
        for u in s:
            if ind[u]%2:
                v = nodes[u][0]
                d = dir[(u,v)]
                index = graph[(u, v)]
                set1.add(v)
                if d:
                    ind[u] += 1
                    ind[v] -= 1
                    dir[(u, v)] = 0
                    dir[(v, u)] = 1
                    edges[index] = abs(edges[index]-1)
                else:
                    ind[u] -= 1
                    ind[v] += 1
                    dir[(u, v)] = 1
                    dir[(v, u)] = 0
                    edges[index] = abs(edges[index]-1)

        s = set1
    print(*edges)

for _ in range(int(input())):
    l=[]
    mp={}
    cr={}
    ans=[]
    n,m=map(int,input().split())
    for i in range(1,n+1):
        mp[i]=0
        cr[i]=False
    for _ in range(m):
        f,t=map(int,input().split())
        l.append((f,t))
        mp[f]+=1
        mp[t]+=1
    if m%2>0:
        print(-1)
        continue
    else:
        for _ in range(m):
            cr[l[_][1]]=not cr[l[_][1]]
            ans.append(0)
        for _ in range(m-1,-1,-1):
            if cr[l[_][1]]==True:
                cr[l[_][1]]=not True
                ans[_]=1
                if cr[l[_][0]]:
                    cr[l[_][0]]=False
                else:
                    cr[l[_][0]]=True
        for i in range(len(ans)):
            print(ans[i],end=' ')
        print()