Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline
from bisect import bisect_left 
from collections import deque

def count_edge(a,b):
    global edge,AB
    if a>b:
        a,b=b,a
    edge[bisect_left(AB,(a,b))]+=1

def main():
    global edge,AB
    N,M=map(int,input().split())
    data=[[] for i in range(N+1)]
    AB=[]
    edge=[0]*M
    count=[0]*(N+1)
    for i in range(M):
        A,B=map(int,input().split())
        if A>B:
            A,B=B,A
        AB.append((A,B))
        data[A].append(B)
        data[B].append(A)
        count[A]+=1
    AB.sort()

    if M%2==1:
        print(-1)
        sys.exit()

    parent=[0]*(N+1)
    que=[1]
    flag=[0]*(N+1)
    flag[1]=1
    parent[1]=1
    lsls=deque([])
    while que:
        h=[]
        for u in que:
            for v in data[u]:
                if flag[v]==0:
                    flag[v]=1
                    parent[v]=u
                    h.append(v)
                    if count[v]%2==1:
                        lsls.appendleft(v)
        que=h

    for i in lsls:
        if count[i]%2==0:
            continue
        else:
            count[i]+=1
            while True:
                j=parent[i]
                if i==j:
                    break
                count_edge(i,j)
                if count[j]%2==1:
                    count[j]+=1
                    break
                i=j
            
    for i in range(M):
        if edge[i]%2==0:
            print(*AB[i])
        else:
            print(*AB[i][::-1])
    
if __name__=="__main__":
    main()

Fixed submission:
import sys
input=sys.stdin.readline
from bisect import bisect_left 
from collections import deque

def count_edge(a,b):
    global edge,AB
    if a>b:
        a,b=b,a
    edge[bisect_left(AB,(a,b))]+=1

def main():
    global edge,AB
    N,M=map(int,input().split())
    data=[[] for i in range(N+1)]
    AB=[]
    edge=[0]*M
    count=[0]*(N+1)
    for i in range(M):
        A,B=map(int,input().split())
        if A>B:
            A,B=B,A
        AB.append((A,B))
        data[A].append(B)
        data[B].append(A)
        count[A]^=1
    AB.sort()

    if M%2==1:
        print(-1)
        sys.exit()

    parent=[0]*(N+1)
    que=[1]
    parent[1]=1
    lsls=deque([])
    while que:
        h=[]
        for u in que:
            for v in data[u]:
                if parent[v]==0:
                    parent[v]=u
                    h.append(v)
                    if count[v]%2==1:
                        lsls.appendleft(v)
        que=h

    for i in lsls:
        if count[i]==0:
            continue
        else:
            count[i]=0
            while True:
                j=parent[i]
                if i==j:
                    break
                count_edge(i,j)
                if count[j]==1:
                    count[j]=0
                    break
                i=j
            
    for i in range(M):
        if edge[i]%2==0:
            print(*AB[i])
        else:
            print(*AB[i][::-1])
    
if __name__=="__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline
from bisect import bisect_left 
from collections import deque

def count_edge(a,b):
    if a>b:
        a,b=b,a
    edge[bisect_left(AB,(a,b))]+=1

N,M=map(int,input().split())
data=[[] for i in range(N+1)]
AB=[]
edge=[0]*M
count=[0]*(N+1)
for i in range(M):
    A,B=map(int,input().split())
    if A>B:
        A,B=B,A
    AB.append((A,B))
    data[A].append(B)
    data[B].append(A)
    count[A]+=1
AB.sort()

if M%2==1:
    print(-1)
    sys.exit()

parent=[0]*(N+1)
que=[1]
flag=[0]*(N+1)
flag[1]=1
parent[1]=1
lsls=deque([])
while que:
    h=[]
    for u in que:
        for v in data[u]:
            if flag[v]==0:
                flag[v]=1
                parent[v]=u
                h.append(v)
                if count[v]%2==1:
                    lsls.appendleft(v)
    que=h

for i in lsls:
    if count[i]%2==0:
        continue
    else:
        count[i]+=1
        while True:
            j=parent[i]
            if i==j:
                break
            count_edge(i,j)
            if count[j]%2==1:
                count[j]+=1
                break
            i=j
            
for i in range(M):
    if edge[i]%2==0:
        print(*AB[i])
    else:
        print(*AB[i][::-1])

Fixed submission:
import sys
input=sys.stdin.readline
from bisect import bisect_left 
from collections import deque

def count_edge(a,b):
    global edge,AB
    if a>b:
        a,b=b,a
    edge[bisect_left(AB,(a,b))]+=1

def main():
    global edge,AB
    N,M=map(int,input().split())
    data=[[] for i in range(N+1)]
    AB=[]
    edge=[0]*M
    count=[0]*(N+1)
    for i in range(M):
        A,B=map(int,input().split())
        if A>B:
            A,B=B,A
        AB.append((A,B))
        data[A].append(B)
        data[B].append(A)
        count[A]^=1
    AB.sort()

    if M%2==1:
        print(-1)
        sys.exit()

    parent=[0]*(N+1)
    que=[1]
    parent[1]=1
    lsls=deque([])
    while que:
        h=[]
        for u in que:
            for v in data[u]:
                if parent[v]==0:
                    parent[v]=u
                    h.append(v)
                    if count[v]%2==1:
                        lsls.appendleft(v)
        que=h

    for i in lsls:
        if count[i]==0:
            continue
        else:
            count[i]=0
            while True:
                j=parent[i]
                if i==j:
                    break
                count_edge(i,j)
                if count[j]==1:
                    count[j]=0
                    break
                i=j
            
    for i in range(M):
        if edge[i]%2==0:
            print(*AB[i])
        else:
            print(*AB[i][::-1])
    
if __name__=="__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline
from bisect import bisect_left 
from collections import deque

def count_edge(a,b):
    if a>b:
        a,b=b,a
    edge[bisect_left(AB,(a,b))]+=1

N,M=map(int,input().split())
data=[[] for i in range(N+1)]
AB=[]
edge=[0]*M
count=[0]*(N+1)
for i in range(M):
    A,B=map(int,input().split())
    if A>B:
        A,B=B,A
    AB.append((A,B))
    data[A].append(B)
    data[B].append(A)
    count[A]+=1
AB.sort()

if M%2==1:
    print(-1)
    sys.exit()

parent=[0]*(N+1)
que=[1]
flag=[0]*(N+1)
flag[1]=1
parent[1]=1
lsls=deque([])
while que:
    h=[]
    for u in que:
        for v in data[u]:
            if flag[v]==0:
                flag[v]=1
                parent[v]=u
                h.append(v)
                if count[v]%2==1:
                    lsls.appendleft(v)
    que=h

for i in lsls:
    if count[i]%2==0:
        continue
    else:
        count[i]+=1
        while True:
            j=parent[i]
            if i==j:
                break
            count_edge(i,j)
            if count[j]%2==1:
                count[j]+=1
                break
            i=j
            
for i in range(M):
    if edge[i]%2==0:
        print(*AB[i])
    else:
        print(*AB[i][::-1])

Fixed submission:
import sys
input=sys.stdin.readline
from bisect import bisect_left 
from collections import deque

def count_edge(a,b):
    global edge,AB
    if a>b:
        a,b=b,a
    edge[bisect_left(AB,(a,b))]+=1

def main():
    global edge,AB
    N,M=map(int,input().split())
    data=[[] for i in range(N+1)]
    AB=[]
    edge=[0]*M
    count=[0]*(N+1)
    for i in range(M):
        A,B=map(int,input().split())
        if A>B:
            A,B=B,A
        AB.append((A,B))
        data[A].append(B)
        data[B].append(A)
        count[A]^=1
    AB.sort()

    if M%2==1:
        print(-1)
        sys.exit()

    parent=[0]*(N+1)
    que=[1]
    parent[1]=1
    lsls=deque([])
    while que:
        h=[]
        for u in que:
            for v in data[u]:
                if parent[v]==0:
                    parent[v]=u
                    h.append(v)
                    if count[v]%2==1:
                        lsls.appendleft(v)
        que=h

    for i in lsls:
        if count[i]==0:
            continue
        else:
            count[i]=0
            while True:
                j=parent[i]
                if i==j:
                    break
                count_edge(i,j)
                if count[j]==1:
                    count[j]=0
                    break
                i=j
            
    for i in range(M):
        if edge[i]%2==0:
            print(*AB[i])
        else:
            print(*AB[i][::-1])
    
if __name__=="__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from bisect import bisect_left 

def count_edge(a,b):
    if a>b:
        a,b=b,a
    edge[bisect_left(AB,[a,b])]+=1

N,M=map(int,input().split())
data=[[] for i in range(N+1)]
AB=[]
edge=[0]*M
count=[0]*(N+1)
for i in range(M):
    A,B=map(int,input().split())
    if A>B:
        A,B=B,A
    AB.append([A,B])
    data[A].append(B)
    data[B].append(A)
    count[A]+=1
AB.sort()

if M%2==1:
    print(-1)
    sys.exit()

parent=[0]*(N+1)
que=[1]
flag=[0]*(N+1)
flag[1]=1
parent[1]=1
while que:
    h=[]
    for u in que:
        for v in data[u]:
            if flag[v]==0:
                flag[v]=1
                parent[v]=u
                h.append(v)
    que=h

for i in range(1,N+1):
    if count[i]%2==0:
        continue
    else:
        count[i]+=1
        while True:
            j=parent[i]
            if i==j:
                break
            count_edge(i,j)
            if count[j]%2==1:
                count[j]+=1
                break
            i=j
            
for i in range(M):
    if edge[i]%2==0:
        print(*AB[i])
    else:
        print(*AB[i][::-1])

Fixed submission:
import sys
input=sys.stdin.readline
from bisect import bisect_left 
from collections import deque

def count_edge(a,b):
    global edge,AB
    if a>b:
        a,b=b,a
    edge[bisect_left(AB,(a,b))]+=1

def main():
    global edge,AB
    N,M=map(int,input().split())
    data=[[] for i in range(N+1)]
    AB=[]
    edge=[0]*M
    count=[0]*(N+1)
    for i in range(M):
        A,B=map(int,input().split())
        if A>B:
            A,B=B,A
        AB.append((A,B))
        data[A].append(B)
        data[B].append(A)
        count[A]^=1
    AB.sort()

    if M%2==1:
        print(-1)
        sys.exit()

    parent=[0]*(N+1)
    que=[1]
    parent[1]=1
    lsls=deque([])
    while que:
        h=[]
        for u in que:
            for v in data[u]:
                if parent[v]==0:
                    parent[v]=u
                    h.append(v)
                    if count[v]%2==1:
                        lsls.appendleft(v)
        que=h

    for i in lsls:
        if count[i]==0:
            continue
        else:
            count[i]=0
            while True:
                j=parent[i]
                if i==j:
                    break
                count_edge(i,j)
                if count[j]==1:
                    count[j]=0
                    break
                i=j
            
    for i in range(M):
        if edge[i]%2==0:
            print(*AB[i])
        else:
            print(*AB[i][::-1])
    
if __name__=="__main__":
    main()