Student Vladislav came to his programming exam completely unprepared as usual. He got a question about some strange algorithm on a graph — something that will definitely never be useful in real life. He asked a girl sitting next to him to lend him some cheat papers for this questions and found there the following definition:

The minimum spanning tree T of graph G is such a tree that it contains all the vertices of the original graph G, and the sum of the weights of its edges is the minimum possible among all such trees.

Vladislav drew a graph with n vertices and m edges containing no loops and multiple edges. He found one of its minimum spanning trees and then wrote for each edge its weight and whether it is included in the found tree or not. Unfortunately, the piece of paper where the graph was painted is gone and the teacher is getting very angry and demands to see the original graph. Help Vladislav come up with a graph so that the information about the minimum spanning tree remains correct.

Input

The first line of the input contains two integers n and m (<image>) — the number of vertices and the number of edges in the graph.

Each of the next m lines describes an edge of the graph and consists of two integers aj and bj (1 ≤ aj ≤ 109, bj = {0, 1}). The first of these numbers is the weight of the edge and the second number is equal to 1 if this edge was included in the minimum spanning tree found by Vladislav, or 0 if it was not.

It is guaranteed that exactly n - 1 number {bj} are equal to one and exactly m - n + 1 of them are equal to zero.

Output

If Vladislav has made a mistake and such graph doesn't exist, print  - 1.

Otherwise print m lines. On the j-th line print a pair of vertices (uj, vj) (1 ≤ uj, vj ≤ n, uj ≠ vj), that should be connected by the j-th edge. The edges are numbered in the same order as in the input. The graph, determined by these edges, must be connected, contain no loops or multiple edges and its edges with bj = 1 must define the minimum spanning tree. In case there are multiple possible solutions, print any of them.

Examples

Input

4 5
2 1
3 1
4 0
1 1
5 0


Output

2 4
1 4
3 4
3 1
3 2


Input

3 3
1 0
2 1
3 1


Output

-1

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Graph algorithms

Data structures

Constructive algorithms


Solutions:
(n, m) = (int(x) for x in input().split())
edges = []
for i in range(m):
	(w, in_tree) = input().split()
	edges.append([int(w), i, int(in_tree)])
sorted_edges = sorted(edges, key=lambda e: (e[0], -e[2]))
print

def free_edge():
	for y in range(3, n + 1):
		for x in range(2, y):
			yield [x, y]
f = free_edge()
k = 2
for e in sorted_edges:
	if e[2]:
		e += [1, k]
		k += 1
	else:
		free = next(f)
		if free[1] >= k:
			print(-1)
			break
		else:
			e += free
else:
	sorted_edges.sort(key=lambda e: e[1])
	for e in sorted_edges:
		print(e[3], e[4])


(n, m) = map(int, input().split())
a = []
for i in range(m):
	(x, y) = map(int, input().split())
	a.append((x, y, i))
a.sort()
(b, c, d) = ([object()] * m, 2, [0] * (n + 1))
for i in range(m):
	if a[i][1]:
		b[i] = (a[i][2], 1, c)
		d[c] = a[i][0]
		c += 1
(x, y, f) = (2, 3, 0)
for i in range(m):
	if not a[i][1]:
		if d[x] <= a[i][0] and d[y] <= a[i][0]:
			b[i] = (a[i][2], x, y)
		else:
			f = 1
			break
		x += 1
		if x == y:
			x = 2
			y += 1
if f:
	print('-1')
else:
	b.sort()
	for (_, i, j) in b:
		print(i, j)


f = lambda : map(int, input().split())
(n, m) = f()
p = []
for i in range(m):
	(a, b) = f()
	p.append((a, 1 - b, i))
p.sort()
k = j = 0
s = [0] * m
(u, v) = (1, 3)
for (a, b, i) in p:
	if not b:
		k += j
		s[i] = (j + 1, j + 2)
		j += 1
	elif k:
		k -= 1
		s[i] = (u, v)
		if v - u - 2:
			u += 1
		else:
			(u, v) = (1, v + 1)
	else:
		print(-1)
		exit(0)
for (x, y) in s:
	print(x, y)


d_ = {}
(n, m) = map(int, input().split())
edge = [list(map(int, input().split())) + [i] for i in range(m)]
edge = sorted(edge, key=lambda x: (x[0], -x[1]))
cur = 0
flg = True
minstone_f = 2
minstone_t = 0
for (w, b, ind) in edge:
	if b == 1:
		d_[ind] = [cur, cur + 1]
		cur += 1
	else:
		if minstone_t == -1:
			minstone_f += 1
			minstone_t = minstone_f - 2
		if minstone_f > cur:
			flg = False
			break
		d_[ind] = [minstone_f, minstone_t]
		minstone_t -= 1
if flg == False:
	print(-1)
else:
	ans = '\n'.join([str(d_[i][0] + 1) + ' ' + str(d_[i][1] + 1) for i in range(m)])
	print(ans)


import sys
from operator import itemgetter
lines = sys.stdin.readlines()
(n, m) = map(int, lines[0].split(' '))

def build_edge(i, row):
	parts = row.split(' ')
	return (int(parts[0]), int(parts[1]), i)

def edge_key(a):
	return (a[0], -a[1])
edges = [build_edge(i, row) for (i, row) in enumerate(lines[1:])]
edges = sorted(edges, key=edge_key)
(x, y) = (1, 2)
vertex = 1
color = [0 for x in range(n)]
color[0] = 1
ans = []
for (weight, used, index) in edges:
	if used == 1:
		color[vertex] = 1
		ans.append((0, vertex, index))
		vertex += 1
	else:
		if color[x] != 1 or color[y] != 1:
			print(-1)
			exit(0)
		ans.append((x, y, index))
		x += 1
		if x == y:
			x = 1
			y += 1
ans = sorted(ans, key=itemgetter(2))
for edge in ans:
	print('%s %s' % (edge[0] + 1, edge[1] + 1))


def main():
	(n, m) = map(int, input().split())
	tmp = ([], [])
	for i in range(m):
		(a, b) = input().split()
		tmp[b == '1'].append((int(a), i))
	for l in tmp:
		l.sort()
	(additional, spanning) = tmp
	tmp = []

	def concurrent_way():
		for (y, (ww, _)) in enumerate(spanning, 2):
			for x in range(2, y):
				yield (ww, x, y)
	for ((w, e), (wmax, v1, v2)) in zip(additional, concurrent_way()):
		if w < wmax:
			print('-1')
			return
		else:
			tmp.append((e, v1, v2))
	tmp.extend(((n, 1, v) for (v, (_, n)) in enumerate(spanning, 2)))
	tmp.sort()
	f = ' '.join
	print('\n'.join((f((str(j), str(k))) for (_, j, k) in tmp)))
main()


import sys
from math import gcd, sqrt, ceil, log2
from collections import defaultdict, Counter, deque
from bisect import bisect_left, bisect_right
import math
sys.setrecursionlimit(2 * 10 ** 5 + 10)
import heapq
from itertools import permutations
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
aa = 'abcdefghijklmnopqrstuvwxyz'

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def get_sum(bit, i):
	s = 0
	i += 1
	while i > 0:
		s += bit[i]
		i -= i & -i
	return s

def update(bit, n, i, v):
	i += 1
	while i <= n:
		bit[i] += v
		i += i & -i

def modInverse(b, m):
	g = math.gcd(b, m)
	if g != 1:
		return -1
	else:
		return pow(b, m - 2, m)

def primeFactors(n):
	sa = []
	while n % 2 == 0:
		sa.append(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			sa.append(i)
			n = n // i
	if n > 2:
		sa.append(n)
	return sa

def seive(n):
	pri = [True] * (n + 1)
	p = 2
	while p * p <= n:
		if pri[p] == True:
			for i in range(p * p, n + 1, p):
				pri[i] = False
		p += 1
	return pri

def check_prim(n):
	if n < 0:
		return False
	for i in range(2, int(sqrt(n)) + 1):
		if n % i == 0:
			return False
	return True

def getZarr(string, z):
	n = len(string)
	(l, r, k) = (0, 0, 0)
	for i in range(1, n):
		if i > r:
			(l, r) = (i, i)
			while r < n and string[r - l] == string[r]:
				r += 1
			z[i] = r - l
			r -= 1
		else:
			k = i - l
			if z[k] < r - i + 1:
				z[i] = z[k]
			else:
				l = i
				while r < n and string[r - l] == string[r]:
					r += 1
				z[i] = r - l
				r -= 1

def search(text, pattern):
	concat = pattern + '$' + text
	l = len(concat)
	z = [0] * l
	getZarr(concat, z)
	ha = []
	for i in range(l):
		if z[i] == len(pattern):
			ha.append(i - len(pattern) - 1)
	return ha

def mergeIntervals(arr):
	arr.sort(key=lambda x: x[0])
	m = []
	s = -10000
	max = -100000
	for i in range(len(arr)):
		a = arr[i]
		if a[0] > max:
			if i != 0:
				m.append([s, max])
			max = a[1]
			s = a[0]
		elif a[1] >= max:
			max = a[1]
	if max != -100000 and [s, max] not in m:
		m.append([s, max])
	return m

class SortedList:

	def __init__(self, iterable=[], _load=200):
		values = sorted(iterable)
		self._len = _len = len(values)
		self._load = _load
		self._lists = _lists = [values[i:i + _load] for i in range(0, _len, _load)]
		self._list_lens = [len(_list) for _list in _lists]
		self._mins = [_list[0] for _list in _lists]
		self._fen_tree = []
		self._rebuild = True

	def _fen_build(self):
		self._fen_tree[:] = self._list_lens
		_fen_tree = self._fen_tree
		for i in range(len(_fen_tree)):
			if i | i + 1 < len(_fen_tree):
				_fen_tree[i | i + 1] += _fen_tree[i]
		self._rebuild = False

	def _fen_update(self, index, value):
		if not self._rebuild:
			_fen_tree = self._fen_tree
			while index < len(_fen_tree):
				_fen_tree[index] += value
				index |= index + 1

	def _fen_query(self, end):
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		x = 0
		while end:
			x += _fen_tree[end - 1]
			end &= end - 1
		return x

	def _fen_findkth(self, k):
		_list_lens = self._list_lens
		if k < _list_lens[0]:
			return (0, k)
		if k >= self._len - _list_lens[-1]:
			return (len(_list_lens) - 1, k + _list_lens[-1] - self._len)
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		idx = -1
		for d in reversed(range(len(_fen_tree).bit_length())):
			right_idx = idx + (1 << d)
			if right_idx < len(_fen_tree) and k >= _fen_tree[right_idx]:
				idx = right_idx
				k -= _fen_tree[idx]
		return (idx + 1, k)

	def _delete(self, pos, idx):
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len -= 1
		self._fen_update(pos, -1)
		del _lists[pos][idx]
		_list_lens[pos] -= 1
		if _list_lens[pos]:
			_mins[pos] = _lists[pos][0]
		else:
			del _lists[pos]
			del _list_lens[pos]
			del _mins[pos]
			self._rebuild = True

	def _loc_left(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(lo, pos) = (-1, len(_lists) - 1)
		while lo + 1 < pos:
			mi = lo + pos >> 1
			if value <= _mins[mi]:
				pos = mi
			else:
				lo = mi
		if pos and value <= _lists[pos - 1][-1]:
			pos -= 1
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value <= _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def _loc_right(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(pos, hi) = (0, len(_lists))
		while pos + 1 < hi:
			mi = pos + hi >> 1
			if value < _mins[mi]:
				hi = mi
			else:
				pos = mi
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value < _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def add(self, value):
		_load = self._load
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len += 1
		if _lists:
			(pos, idx) = self._loc_right(value)
			self._fen_update(pos, 1)
			_list = _lists[pos]
			_list.insert(idx, value)
			_list_lens[pos] += 1
			_mins[pos] = _list[0]
			if _load + _load < len(_list):
				_lists.insert(pos + 1, _list[_load:])
				_list_lens.insert(pos + 1, len(_list) - _load)
				_mins.insert(pos + 1, _list[_load])
				_list_lens[pos] = _load
				del _list[_load:]
				self._rebuild = True
		else:
			_lists.append([value])
			_mins.append(value)
			_list_lens.append(1)
			self._rebuild = True

	def discard(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_right(value)
			if idx and _lists[pos][idx - 1] == value:
				self._delete(pos, idx - 1)

	def remove(self, value):
		_len = self._len
		self.discard(value)
		if _len == self._len:
			raise ValueError('{0!r} not in list'.format(value))

	def pop(self, index=-1):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		value = self._lists[pos][idx]
		self._delete(pos, idx)
		return value

	def bisect_left(self, value):
		(pos, idx) = self._loc_left(value)
		return self._fen_query(pos) + idx

	def bisect_right(self, value):
		(pos, idx) = self._loc_right(value)
		return self._fen_query(pos) + idx

	def count(self, value):
		return self.bisect_right(value) - self.bisect_left(value)

	def __len__(self):
		return self._len

	def __getitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		return self._lists[pos][idx]

	def __delitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		self._delete(pos, idx)

	def __contains__(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_left(value)
			return idx < len(_lists[pos]) and _lists[pos][idx] == value
		return False

	def __iter__(self):
		return (value for _list in self._lists for value in _list)

	def __reversed__(self):
		return (value for _list in reversed(self._lists) for value in reversed(_list))

	def __repr__(self):
		return 'SortedList({0})'.format(list(self))

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def sol(n):
	seti = set()
	for i in range(1, int(sqrt(n)) + 1):
		if n % i == 0:
			seti.add(n // i)
			seti.add(i)
	return seti

def lcm(a, b):
	return a * b // gcd(a, b)

def moore_voting(l):
	count1 = 0
	count2 = 0
	first = 10 ** 18
	second = 10 ** 18
	n = len(l)
	for i in range(n):
		if l[i] == first:
			count1 += 1
		elif l[i] == second:
			count2 += 1
		elif count1 == 0:
			count1 += 1
			first = l[i]
		elif count2 == 0:
			count2 += 1
			second = l[i]
		else:
			count1 -= 1
			count2 -= 1
	for i in range(n):
		if l[i] == first:
			count1 += 1
		elif l[i] == second:
			count2 += 1
	if count1 > n // 3:
		return first
	if count2 > n // 3:
		return second
	return -1

def find_parent(u, parent):
	if u != parent[u]:
		parent[u] = find_parent(parent[u], parent)
	return parent[u]

def dis_union(n):
	par = [i for i in range(n + 1)]
	rank = [1] * (n + 1)
	k = int(input())
	for i in range(k):
		(a, b) = map(int, input().split())
		(z1, z2) = (find_parent(a, par), find_parent(b, par))
		if z1 != z2:
			par[z1] = z2
			rank[z2] += rank[z1]

def dijkstra(n, tot, hash):
	hea = [[0, n]]
	dis = [10 ** 18] * (tot + 1)
	dis[n] = 0
	boo = defaultdict(bool)
	check = defaultdict(int)
	while hea:
		(a, b) = heapq.heappop(hea)
		if boo[b]:
			continue
		boo[b] = True
		for (i, w) in hash[b]:
			if b == 1:
				c = 0
				if (1, i, w) in nodes:
					c = nodes[1, i, w]
					del nodes[1, i, w]
				if dis[b] + w < dis[i]:
					dis[i] = dis[b] + w
					check[i] = c
				elif dis[b] + w == dis[i] and c == 0:
					dis[i] = dis[b] + w
					check[i] = c
			elif dis[b] + w <= dis[i]:
				dis[i] = dis[b] + w
				check[i] = check[b]
			heapq.heappush(hea, [dis[i], i])
	return check

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res *= x
		x = x * x
		y = y >> 1
	return res
(n, m) = map(int, input().split())
ans = set()
cur = 2
rest = []
now = []
ba = []
hash = defaultdict(int)
for i in range(m):
	(a, b) = map(int, input().split())
	now.append([a, b, i])
now.sort()
yo = [[0, 0]] * m
for i in range(m):
	(a, b, x) = now[i]
	ba.append([x, a])
	if b == 1:
		hash[1, cur] = a
		yo[x] = [1, cur]
		cur += 1
	else:
		rest.append(a)
rest.sort()
ba.sort()
ha = n - 1
to = 2
fro = 3
while ha != m:
	z = rest.pop(0)
	(a, b) = (to, fro)
	(z1, z2) = (hash[1, to], hash[1, fro])
	if z < z1 or z < z2:
		print(-1)
		exit()
	ans.add((z, a, b))
	if rest == []:
		break
	to += 1
	if to == fro:
		fro += 1
		to = 2
hash = defaultdict(list)
for (a, b, c) in ans:
	hash[a].append((b, c))
for (x, a) in ba:
	if yo[x] == [0, 0]:
		(c, d) = hash[a].pop(0)
		yo[x] = (c, d)
for (a, b) in yo:
	print(a, b)


readInts = lambda : list(map(int, input().split()))
(n, m) = readInts()
edge = []
for _ in range(m):
	(l, f) = readInts()
	if f == 1:
		f = -1
	edge += [(l, f, _)]
edge.sort()
ok = True
cnt = 0
ret = [(0, 0)] * m
u = 2
v = 3
t = 2
for e in edge:
	if e[1] == -1:
		ret[e[2]] = (1, t)
		cnt += t - 2
		t += 1
	elif cnt <= 0:
		ok = False
		break
	else:
		ret[e[2]] = (u, v)
		cnt -= 1
		u += 1
		if u == v:
			u = 2
			v += 1
if ok == False:
	print(-1)
else:
	for e in ret:
		print(*e)


def graph(e):
	e = [(w, i, b) for (i, (w, b)) in enumerate(e)]
	e.sort()
	g = [None] * len(e)
	mst = [(w, i) for (w, i, b) in e if b]
	for (n, (w, i)) in enumerate(mst, 2):
		g[i] = (1, n)
	cm = 0
	available = []
	for (w, i, b) in e:
		if not b:
			if not available:
				cm += 1
				if mst[cm][0] > w:
					return None
				available = [(u, cm + 2) for u in range(2, cm + 2)]
			g[i] = available.pop()
	return g
(n, m) = map(int, input().split())
e = []
for _ in range(m):
	(a, b) = map(int, input().split())
	e.append((a, b))
g = graph(e)
if g:
	for (u, v) in g:
		print('{} {}'.format(u, v))
else:
	print(-1)


__author__ = 'MoonBall'
import sys
import functools
T = 1

def process():
	(N, M) = list(map(int, input().split()))
	e = []
	for _ in range(M):
		e.append(list(map(int, input().split())) + [_])
	e = sorted(e, key=functools.cmp_to_key(lambda x, y: y[1] - x[1] if x[0] == y[0] else x[0] - y[0]))
	ans = [0] * M
	i = 3
	j = 1
	has = 1
	ok = True
	for _e in e:
		if _e[1] == 1:
			has = has + 1
			ans[_e[2]] = (has, has - 1)
		else:
			if has < i:
				ok = False
			else:
				ans[_e[2]] = (i, j)
			if j == i - 2:
				i = i + 1
				j = 1
			else:
				j = j + 1
	if not ok:
		print(-1)
	else:
		for (u, v) in ans:
			print(u, v)
for _ in range(T):
	process()


def next():
	global fr
	global to
	fr += 1
	if fr == to:
		fr = 2
		to += 1
(n, m) = tuple(map(int, input().split()))
lenths = []
for i in range(m):
	(a, b) = tuple(map(int, input().split()))
	if b == 1:
		b = 0
	else:
		b = 1
	lenths.append((a, b, i))
res = [(0, 0)] * m
fr = 2
to = 3
my = 0
len_sorted = sorted(lenths)
f = 1
for i in range(m):
	if len_sorted[i][1] == 0:
		res[len_sorted[i][2]] = (1, my + 2)
		my += 1
	else:
		if my <= to - 2:
			f = 0
			break
		res[len_sorted[i][2]] = (fr, to)
		next()
	if my + 1 > n:
		f = 0
		break
if f:
	for i in range(m):
		print(' '.join(list(map(str, res[i]))))
else:
	print('-1')


def parse(i):
	t = input().split()
	return (int(t[0]), int(t[1]), i)
(n, m) = [int(x) for x in input().split()]
list = [parse(i) for i in range(m)]
list.sort(key=lambda x: (x[0], -x[1]))
f = 0
t = 1
fakeFrom = 1
fakeTo = 2
graph = []
for i in range(m):
	if list[i][1] == 1:
		graph.append((f + 1, t + 1, list[i][2]))
		t += 1
	else:
		graph.append((fakeFrom + 1, fakeTo + 1, list[i][2]))
		if fakeTo >= t:
			print(-1)
			exit(0)
		fakeFrom += 1
		if fakeFrom == fakeTo:
			fakeFrom = 1
			fakeTo += 1
graph.sort(key=lambda x: x[2])
for x in graph:
	print(x[0], x[1])


def read_data():
	(n, m) = map(int, input().split())
	ABs = []
	for i in range(m):
		(a, b) = map(int, input().split())
		ABs.append((a, b))
	return (n, m, ABs)

def solve(n, m, ABs):
	edges = [(a, -b, i) for (i, (a, b)) in enumerate(ABs)]
	edges.sort()
	ans = [(0, 0) for i in range(m)]
	v = 0
	count = 0
	s = 1
	t = 2
	for (a, mb, i) in edges:
		count += 1
		if mb == -1:
			v += 1
			ans[i] = (0, v)
		else:
			if t > v:
				return (False, [])
			ans[i] = (s, t)
			if t == s + 1:
				s = 1
				t += 1
			else:
				s += 1
	return (True, ans)
(n, m, ABs) = read_data()
(res, ans) = solve(n, m, ABs)
if res:
	for (i, j) in ans:
		print(i + 1, j + 1)
else:
	print(-1)


(n, m) = map(int, input().split())
arr = []
for i in range(m):
	(a, b) = map(int, input().split())
	arr.append([a, -b, i + 1])
arr.sort()
ans = [[-1, -1] for i in range(m)]
ok = True
at = 2
last = 3
curr = 2
for i in range(m):
	idx = arr[i][2] - 1
	inc = arr[i][1]
	if inc:
		ans[idx] = [1, curr]
		curr += 1
	elif last < curr:
		ans[idx] = [at, last]
		at += 1
		if at == last:
			last += 1
			at = 2
	else:
		ok = False
if ok:
	for i in ans:
		print(*i)
else:
	print(-1)