You are given a set of $n$ ($n$ is always a power of $2$) elements containing all integers $0, 1, 2, \ldots, n-1$ exactly once.

Find $\frac{n}{2}$ pairs of elements such that:

Each element in the set is in exactly one pair.

The sum over all pairs of the bitwise AND of its elements must be exactly equal to $k$. Formally, if $a_i$ and $b_i$ are the elements of the $i$-th pair, then the following must hold: $$\sum_{i=1}^{n/2}{a_i \& b_i} = k,$$ where $\&$ denotes the bitwise AND operation.

If there are many solutions, print any of them, if there is no solution, print $-1$ instead.


-----Input-----

The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 400$) — the number of test cases. Description of the test cases follows.

Each test case consists of a single line with two integers $n$ and $k$ ($4 \leq n \leq 2^{16}$, $n$ is a power of $2$, $0 \leq k \leq n-1$).

The sum of $n$ over all test cases does not exceed $2^{16}$. All test cases in each individual input will be pairwise different.


-----Output-----

For each test case, if there is no solution, print a single line with the integer $-1$.

Otherwise, print $\frac{n}{2}$ lines, the $i$-th of them must contain $a_i$ and $b_i$, the elements in the $i$-th pair.

If there are many solutions, print any of them. Print the pairs and the elements in the pairs in any order.


-----Examples-----

Input
4
4 0
4 1
4 2
4 3
Output
0 3
1 2
0 2
1 3
0 1
2 3
-1


-----Note-----

In the first test, $(0\&3)+(1\&2) = 0$.

In the second test, $(0\&2)+(1\&3) = 1$.

In the third test, $(0\&1)+(2\&3) = 2$.

In the fourth test, there is no solution.

Difficulty: MEDIUM
Tags: Bit manipulation

Constructive algorithms
Skill types: Bit manipulation

Starter code:

Solutions:
def solve(n, k):
	n = n - 1
	pairs = [(i, n - i) for i in range(n // 2 + 1)]
	if k == 0:
		pass
	elif k == n:
		if n == 3:
			return '-1'
		pairs[0] = (n - 1, n)
		pairs[1] = (1, 3)
		pairs[3] = (n - 3, 0)
	elif k <= n // 2:
		pairs[k] = (k, n)
		pairs[0] = (n - k, 0)
	else:
		pairs[n - k] = (k, n)
		pairs[0] = (n - k, 0)
	return '\n'.join([' '.join([str(k) for k in pair]) for pair in pairs])
T = int(input())
for _ in range(T):
	(n, k) = [int(x) for x in input().split()]
	print(solve(n, k))


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	a = [[i, n - i - 1] for i in range(n // 2)]
	if k == n - 1:
		if n == 4:
			print(-1)
			continue
		else:
			(a[0][1], a[1][1], a[3][0], a[3][1]) = (a[3][1], a[3][0], a[1][1], a[0][1])
	elif k < n // 2:
		(a[k][1], a[0][1]) = (a[0][1], a[k][1])
	else:
		(a[n - k - 1][0], a[0][1]) = (a[0][1], a[n - k - 1][0])
	print('\n'.join(map(lambda x: str(x[0]) + ' ' + str(x[1]), a)))


cases = int(input())
for _ in range(cases):
	(n, k) = map(int, input().split())
	if k == 0:
		for i in range(n // 2):
			print('%d %d' % (i, n - 1 ^ i))
	elif k == n - 1:
		if n == 4:
			print('-1')
		else:
			print('%d %d' % (n - 1, n - 2))
			print('1 %d' % (n - 3))
			print('0 2')
			for i in range(3, n // 2):
				print('%d %d' % (i, i ^ n - 1))
	else:
		print('%d %d' % (k, n - 1))
		print('%d %d' % (0, k ^ n - 1))
		for i in range(1, n // 2):
			if i != k and i != k ^ n - 1:
				print('%d %d' % (i, i ^ n - 1))


test_cases = int(input())
for _ in range(test_cases):
	a = list(map(int, input().split()))
	n = a[0]
	k = a[1]
	if n == 4 and k == 3:
		print(-1)
	elif k == 0:
		for i in range(0, n // 2):
			print(f'{i} {n - i - 1}')
	elif k < n - 1:
		print(f'{n - 1} {k}')
		print(f'{n - k - 1} 0')
		for i in range(1, n // 2):
			if i != k and i != n - k - 1:
				print(f'{i} {n - i - 1}')
	elif k == n - 1:
		print(f'{n - 1} {n - 2}')
		print(f'0 2')
		print(f'1 {n - 3}')
		for i in range(3, n // 2):
			print(f'{i} {n - i - 1}')


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().strip().split())
	if k == n - 1:
		if n == 4:
			print('-1')
			continue
		print(f'{n - 2} {n - 1}')
		print(f'1 3')
		print(f'0 {n - 4}')
		print(f'2 {n - 3}')
		for x in range(4, n // 2):
			print(f'{x} {n - x - 1}')
		continue
	if k == 0:
		for x in range(n // 2):
			print(f'{x} {n - x - 1}')
		continue
	print(f'{k} {n - 1}')
	print(f'0 {n - k - 1}')
	for x in range(1, n // 2):
		if x in (k, n - k - 1):
			continue
		print(f'{x} {n - x - 1}')


def find_pairs(n, k):
	if n == 2:
		if k == 1:
			print(0, 1)
		else:
			print(-1)
	elif k == 0:
		for i in range(n // 2):
			print(i, n - i - 1)
	elif n == 4 and k == 3:
		print(-1)
	elif k != n - 1:
		print(k, n - 1)
		print(0, n - k - 1)
		for i in range(1, n // 2):
			if i != k and i != n - k - 1:
				print(i, n - i - 1)
	else:
		print(n - 1, n - 2)
		print(1, n // 2 - 1)
		print(0, n // 2)
		for i in range(2, n // 2 - 1):
			print(i, n - i - 1)
T = int(input())
for t in range(T):
	(n, k) = [int(j) for j in input().split()]
	find_pairs(n, k)


import sys
input = sys.stdin.readline
num_test_cases = int(input())
for test_case_ittr in range(num_test_cases):
	(n, target) = [int(x) for x in input().split()]
	if n == 4 and target == 3:
		print(-1)
		continue
	range_max = n - 1
	if target == range_max:
		print(n - 1, n - 2)
		print(n - 3, 1)
		print(2, 0)
		for x in range(3, n // 2):
			print(x, range_max ^ x)
		continue
	print(target, range_max)
	target_compliment = None
	if target:
		target_compliment = range_max ^ target
		print(target_compliment, 0)
	s = {target, target_compliment} if target else set()
	for element in range(1, n // 2):
		if element in s:
			continue
		compliment = range_max ^ element
		print(element, compliment)


for i in range(int(input())):
	(n, k) = map(int, input().split())
	a = [int(x) for x in range(n)]
	if k < n - 1:
		print(k, n - 1)
		if k != 0:
			print(0, n - 1 - k)
		for i in range(1, n // 2):
			if i == k or i == n - 1 - k:
				continue
			print(i, n - 1 - i)
	elif k == 3:
		print(-1)
	else:
		print(n - 2, n - 1)
		print(1, 3)
		print(0, n - 4)
		for i in range(2, n // 2):
			if i == 3:
				continue
			print(i, n - 1 - i)


def c(n, i):
	return n - i - 1

def solve(n, k):
	if k == 0:
		for i in range(n // 2):
			print(i, c(n, i))
	elif k < n - 1:
		ignore = [0, k, c(n, k), n - 1]
		print(0, c(n, k))
		print(k, n - 1)
		for i in range(n // 2):
			if i in ignore:
				continue
			print(i, c(n, i))
	elif n == 4:
		print(-1)
	else:
		print(0, 2)
		print(1, n - 3)
		print(n - 1, n - 2)
		for i in range(3, n // 2):
			print(i, c(n, i))
for case in range(int(input())):
	(n, k) = map(int, input().split())
	solve(n, k)


def main():
	(N, K) = [int(i) for i in input().split()]
	l = [(i, N - i - 1) for i in range(N // 2)]
	if K == 3 and N == 4:
		print(-1)
		return
	elif K == 2 and N == 4:
		print('0 1')
		print('2 3')
		return
	try:
		if K == 0:
			pass
		elif K < N - 1:
			l.remove(tuple(sorted((K, N - K - 1))))
			l.remove(tuple(sorted((0, N - 1))))
			l.append(tuple(sorted((K, N - 1))))
			l.append(tuple(sorted((0, N - K - 1))))
		else:
			l.remove(tuple(sorted((1, N - 2))))
			l.remove(tuple(sorted((0, N - 1))))
			l.append(tuple(sorted((N - 2, N - 1))))
			l.append(tuple(sorted((0, 1))))
			l.remove(tuple(sorted((2, N - 3))))
			l.remove(tuple(sorted((3, N - 4))))
			l.append(tuple(sorted((2, N - 4))))
			l.append(tuple(sorted((3, N - 3))))
	except Exception as e:
		print(l)
		print((K, N - K - 1))
		raise e
	for x in l:
		print(' '.join([str(y) for y in x]))
num_of_cases = [int(i) for i in input().split()][0]
for p in range(num_of_cases):
	output = main()


def solve():
	(n, k) = map(int, input().split())
	if n == 4 and k == 3:
		print(-1)
		return
	arr = []
	for i in range(n // 2):
		arr.append([i, n - 1 - i])
	if k < n // 2:
		(arr[0][0], arr[k][0]) = (arr[k][0], arr[0][0])
	elif k < n - 1:
		(arr[0][0], arr[n - 1 - k][1]) = (arr[n - 1 - k][1], arr[0][0])
	else:
		arr[0][0] = n - 2
		arr[1][1] = n - 3
		arr[2][1] = 0
	for i in arr:
		print(i[0], i[1])
for _ in range(int(input())):
	solve()


def main():
	test_no = int(input())
	for _ in range(test_no):
		line = input().split()
		(n, k) = (int(line[0]), int(line[1]))
		if k == n - 1 and n == 4:
			print(-1)
			continue
		elif k == n - 1 and n != 4:
			print(n - 1, n - 2)
			print(1, n // 2 - 1)
			print(0, n // 2)
			for i in range(2, n // 2):
				if i != n // 2 - 1 and i != n // 2:
					print(i, n - 1 - i)
		elif k == 0:
			for i in range(n // 2):
				print(i, n - 1 - i)
		else:
			print(k, n - 1)
			print(n - 1 - k, 0)
			for i in range(1, n // 2):
				if i != k and i != n - 1 - k:
					print(i, n - 1 - i)
main()


def solve():
	(n, k) = map(int, input().split())
	if n == 4 and k == 3:
		print(-1)
		return
	if k == 0:
		for i in range(n // 2):
			print(i, n - i - 1)
	elif k == n - 1:
		print(0, n - 4)
		print(1, 3)
		print(2, n - 3)
		print(n - 2, n - 1)
		for i in range(4, n // 2):
			print(i, n - i - 1)
	else:
		print(0, n - k - 1)
		print(k, n - 1)
		for i in range(1, n // 2):
			if i != k and i != n - k - 1:
				print(i, n - i - 1)
tst = int(input())
for tt in range(tst):
	solve()


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	arr = [i for i in range(n)]
	if n == 4 and m == 3:
		print(-1)
		continue
	if n == m + 1:
		(arr[0], arr[m - 1]) = (arr[m - 1], arr[0])
		(arr[1], arr[n - 4]) = (arr[n - 4], arr[1])
	else:
		(arr[0], arr[m]) = (arr[m], arr[0])
	for i in range(n // 2):
		print(arr[i], arr[n - 1 - i])


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	arr1 = [i for i in range(n)]
	if k == 0:
		for i in range(n // 2):
			print(arr1[i], arr1[n - i - 1])
	if k > 0 and k < n - 1:
		(arr1[k & n - 1], arr1[0]) = (arr1[0], arr1[k & n - 1])
		for i in range(n // 2):
			print(arr1[i], arr1[n - i - 1])
	if k == n - 1:
		if k == 3:
			print(-1)
		else:
			(arr1[n - 2], arr1[0]) = (arr1[0], arr1[n - 2])
			(arr1[n - 3], arr1[n - 2]) = (arr1[n - 2], arr1[n - 3])
			for i in range(n // 2):
				print(arr1[i], arr1[n - i - 1])


def solve(n, k):
	if k == n - 1:
		if n == 4:
			print(-1)
		else:
			a = n >> 1
			b = n >> 1 | n >> 2
			for i in range(1, n // 2):
				if i not in [a, b, n - 1 ^ a, n - 1 ^ b]:
					print(i, n - 1 ^ i)
			print(a, b)
			print(n - 1 ^ a, n - 1)
			print(0, n - 1 ^ b)
		return
	if k == 0:
		for i in range(n // 2):
			print(i, n - 1 ^ i)
		return
	for i in range(1, n // 2):
		if i == k:
			print(i, n - 1)
			print(0, n - 1 ^ i)
		elif n - 1 ^ i == k:
			print(n - 1 ^ i, n - 1)
			print(0, i)
		else:
			print(i, n - 1 ^ i)
	return
t = int(input())
while t:
	(n, k) = list(map(int, input().split(' ')))
	solve(n, k)
	t -= 1


def executeCases():
	(n, k) = list(map(int, input().split()))
	pairs = list()
	for i in range(n // 2):
		pairs.append([i, n - i - 1])
	if k < n // 2:
		swap(pairs, k, 0)
	elif k < n - 1:
		swap(pairs, n - k - 1, 1)
	elif len(pairs) > 2:
		swap(pairs, n - k, 1)
		temp = pairs[1][0]
		pairs[1][0] = pairs[2][0]
		pairs[2][0] = temp
	else:
		print(-1)
		return
	for i in range(n // 2):
		print(pairs[i][0], pairs[i][1])
	return

def swap(pairs, a, i):
	temp = pairs[a][i]
	pairs[a][i] = pairs[0][0]
	pairs[0][0] = temp
t = int(input())
for i in range(t):
	executeCases()


T = int(input())
for k in range(T):
	(n, k) = map(int, input().split())
	if n == 4 and k == 3:
		print(-1)
	elif k == 0:
		for i in range(n // 2):
			print(i, n - i - 1)
	elif k == n - 1:
		print(0, n - 4)
		print(1, 3)
		print(2, n - 3)
		print(n - 2, n - 1)
		for i in range(4, n // 2):
			print(i, n - i - 1)
	else:
		print(0, n - k - 1)
		print(k, n - 1)
		for i in range(1, n // 2):
			if i != k and i != n - k - 1:
				print(i, n - i - 1)


t = int(input())
while t > 0:
	t -= 1
	li = list(map(int, input().split()))
	a = li[0]
	b = li[1]
	if a == 4 and b == 3:
		print(-1)
		continue
	if a - b == 1:
		print(a - 1, a // 2 - 1)
		print(1, a // 2 - 2)
		print(a // 2 + 1, a - 2)
		print(0, a // 2)
		li = [a - 1, a // 2 - 1, 1, a // 2 - 2, a // 2 + 1, a - 2, 0, a // 2]
		for i in range(2, a // 2):
			if i not in li and a - 1 - i not in li:
				print(i, a - i - 1)
		print()
		continue
	print(b, a - 1)
	if b != 0:
		print(0, a - 1 - b)
	for i in range(1, a // 2):
		if i != b and b != a - 1 - i:
			print(i, a - 1 - i)


for tc in range(int(input())):
	(n, m) = map(int, input().split())
	if m < n - 1:
		print(m, n - 1)
		for i in range(1, n // 2):
			if i == m:
				print(0, n - 1 - i)
			elif i == n - m - 1:
				print(0, i)
			else:
				print(i, n - 1 - i)
	elif n > 4:
		print(n - 1, n - 2)
		print(n - 3, 1)
		print(0, 2)
		for i in range(3, n // 2):
			print(i, n - 1 - i)
	else:
		print(-1)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	if k == n - 1 and n == 4:
		print(-1)
	elif k == n - 1:
		lis = []
		for i in range(n // 2):
			lis.append((i, n - 1 - i))
		lis[0] = (n - 2, n - 1)
		lis[1] = (0, n - 3 - 1)
		lis[3] = (1, 3)
		for j in lis:
			print(*j)
	else:
		lis = []
		for i in range(1, n // 2):
			lis.append((i, n - 1 - i))
		lis = [(0, n - k - 1)] + lis
		if k == 0:
			pass
		elif k >= n // 2:
			lis[n - k - 1] = (k, n - 1)
		else:
			lis[k] = (k, n - 1)
		for j in lis:
			print(*j)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	if n == 4 and k == 3:
		print(-1)
		continue
	if k == n - 1:
		print(n - 1, n - 2)
		print(1, 3)
		print(0, n - 4)
		for i in range(2, n // 2):
			if i == 3:
				pass
			else:
				print(i, n - 1 - i)
	else:
		print(k, n - 1)
		if k != 0:
			print(0, n - k - 1)
		for i in range(1, n // 2):
			if i == k or n - i - 1 == k:
				continue
			print(i, n - i - 1)


import sys
from math import sqrt, gcd
from collections import deque, Counter
sys.setrecursionlimit(10 ** 8)
I = lambda : int(input())
S = lambda : input().strip()
M = lambda : map(int, input().strip().split())
L = lambda : list(map(int, input().strip().split()))
mod = 1000000007
for _ in range(I()):
	(n, k) = M()
	if k == n - 1 and n == 4:
		print(-1)
		continue
	if k == n - 1:
		print(n - 1, n - 2)
		print(1, 3)
		print(0, n - 1 - 3)
		for i in range(2, n // 2):
			if i != 3:
				print(i, n - 1 - i)
		continue
	print(n - 1, k)
	if k != 0:
		print(0, n - 1 - k)
	for i in range(1, n // 2):
		if i != k and i != n - 1 - k:
			print(i, n - 1 - i)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	if k == 3 and n == 4:
		print(-1)
	elif k == n - 1:
		print(n - 1, n - 2)
		print(1, 3)
		print(0, n - 1 - 3)
		for j in range(n // 2):
			if j == 0 or j == 1 or j == 3:
				continue
			print(j, n - 1 - j)
	elif k == 0:
		for j in range(n // 2):
			print(j, n - 1 - j)
	else:
		print(0, n - 1 - k)
		print(n - 1, k)
		for j in range(1, n // 2):
			if j == k or j == n - 1 - k:
				continue
			print(j, n - 1 - j)


tc = int(input())
for t in range(tc):
	(n, k) = map(int, input().split())
	if n == 4 and k == 3:
		print(-1)
	elif n == 2:
		if k == 0:
			print('0 1')
		else:
			print(-1)
	else:
		nums = list()
		for i in range(n // 2):
			nums.append([0, 0])
		for i in range(n // 2):
			nums[i][0] = n - 1 - i
			nums[i][1] = i
		if k > 0:
			a = 0
			if k < n // 2:
				nums[0][1] = nums[k][1]
				nums[k][1] = 0
			else:
				l = n - k - 1
				if k == n - 1:
					l = n - (n - 2) - 1
				nums[0][1] = nums[l][0]
				nums[l][0] = 0
				if k == n - 1:
					l = n // 2 - 1
					nums[1][0] = nums[l][1]
					nums[l][1] = 0
		for pair in nums:
			print('{} {}'.format(pair[0], pair[1]))


t = int(input())
while t != 0:
	(n, summ) = [int(x) for x in input().split()]
	if summ == n - 1:
		if summ == 3:
			print(-1)
		else:
			print(n - 2, n - 1)
			print(1, n - 3)
			print(0, 2)
			l = 3
			while l < n // 2:
				if l != summ and l != n - 1 - summ:
					print(l, n - 1 - l)
				l += 1
	else:
		print(summ, n - 1)
		if summ != 0:
			print(0, n - 1 - summ)
		l = 1
		while l < n // 2:
			if l != summ and l != n - 1 - summ:
				print(l, n - 1 - l)
			l += 1
	t -= 1


T = int(input())
for t in range(T):
	(n, k) = list(map(int, input().split()))
	if k == n - 1:
		if n == 4:
			print(-1)
		else:
			print(n - 2, n - 1)
			print(1, 3)
			print(0, n - 4)
			(a, b) = (2, n - 3)
			for i in range(n // 2 - 2):
				if a != 3:
					print(a, b)
				a += 1
				b -= 1
		continue
	if k == 0:
		(a, b) = (0, n - 1)
		for i in range(n // 2):
			print(a, b)
			a += 1
			b -= 1
		continue
	print(k, n - 1)
	print(0, n - k - 1)
	(a, b) = (1, n - 2)
	for i in range(n // 2 - 1):
		if a != k and b != k:
			print(a, b)
		a += 1
		b -= 1


from collections import Counter
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	if n == 4 and k == n - 1:
		print(-1)
	elif k == 0:
		for i in range(n // 2):
			print(i, n - i - 1)
	elif k == 1:
		print(1, n - 1)
		print(0, n - 2)
		for i in range(2, n // 2):
			print(i, n - i - 1)
	elif k == n - 1:
		print(k - 1, k)
		print(k - 2, 1)
		print(0, 2)
		ans = [1, 0, 2, k, k - 1, k - 2]
		for i in range(n // 2):
			if i not in ans:
				print(i, n - i - 1)
	else:
		print(k, n - 1)
		print(0, n - k - 1)
		ans = [k, n - 1, 0, n - k - 1]
		for i in range(n // 2):
			if i not in ans and n - 1 - i not in ans:
				print(i, n - 1 - i)


import sys
for _ in range(int(sys.stdin.readline())):
	(n, k) = map(int, sys.stdin.readline().split())
	if k > n * (n - 2) / 4:
		print(-1)
	else:
		if k != 0 and k != n - 1:
			print(k, n - 1)
			print(0, k ^ n - 1)
		elif k == 0:
			print(0, n - 1)
		else:
			print(n - 1, n - 2)
			print(3, 1)
			print(0, n - 1 ^ 3)
		for i in range(1, n // 2):
			if i == k or i == k ^ n - 1:
				continue
			elif k == n - 1:
				if i in [1, 3]:
					continue
				else:
					print(i, i ^ n - 1)
			else:
				print(i, i ^ n - 1)


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
t = inp()
for _ in range(t):
	[n, k] = inlt()
	if k >= n:
		print(-1)
		continue
	elif k == 0:
		for i in range(n // 2):
			print(i, n - 1 - i)
	elif k < n - 1:
		ck = n - 1 ^ k
		print(0, ck)
		print(k, n - 1)
		for i in range(n // 2):
			if i == k or i == ck or i == 0:
				continue
			print(i, n - 1 - i)
	elif n == 4:
		print(-1)
	else:
		print(0, 2)
		print(n - 1, n - 2)
		print(1, n - 3)
		for i in range(3, n // 2):
			print(i, n - 1 - i)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	l = []
	for i in range(int(n // 2)):
		l.append([i, n - i - 1])
	if n == 4 and k == 3:
		print(-1)
		continue
	if k != n - 1:
		if k < n / 2:
			(l[0][0], l[k][0]) = (l[k][0], l[0][0])
		else:
			(l[0][0], l[n - k - 1][1]) = (l[n - k - 1][1], l[0][0])
	else:
		(l[0][0], l[1][1]) = (l[1][1], l[0][0])
		(l[1][0], l[2][0]) = (l[2][0], l[1][0])
	for i in l:
		print(*i)


from collections import defaultdict
import math
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	if k == 3 and k == n - 1:
		print(-1)
	else:
		if k == 0:
			ans = []
			for i in range(n // 2):
				ans.append([i, n - i - 1])
		elif k < n - 1:
			ans = [[k, n - 1]]
			for i in range(1, n // 2):
				if i == k or n - i - 1 == k:
					continue
				ans.append([i, n - i - 1])
			left = n - 1 - k
			ans.append([0, left])
		else:
			ans = []
			ans.append([1, 3])
			ans.append([n - 2, n - 1])
			ans.append([0, n - 4])
			ans.append([2, n - 3])
			for i in range(4, n // 2):
				if i == 1 or i == 3:
					continue
				ans.append([i, n - i - 1])
		for i in ans:
			print(*i)


def func():
	(n, k) = map(int, input().split())
	a = [0 for i in range(n // 2)]
	b = [0 for i in range(n // 2)]
	if k == 0:
		for i in range(n // 2):
			(a[i], b[i]) = (i, i ^ n - 1)
	elif k > 0 and k < n - 1:
		itr = min(k, k ^ n - 1)
		for i in range(n // 2):
			if i != 0 or i != itr:
				(a[i], b[i]) = (i, i ^ n - 1)
		(a[0], b[0]) = (0, k ^ n - 1)
		(a[itr], b[itr]) = (k, n - 1)
	else:
		if n == 4:
			print(-1)
			return
		(a[0], b[0]) = (n - 2, n - 1)
		(a[1], b[1]) = (1, n - 3)
		(a[2], b[2]) = (0, 2)
		for i in range(3, n // 2):
			(a[i], b[i]) = (i, i ^ n - 1)
	for i in range(n // 2):
		print(a[i], b[i])
t = int(input())
for i in range(t):
	func()


from itertools import count
from os import sep
from time import process_time_ns
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	if n == 4:
		if k == 0:
			print(0, 3)
			print(1, 2)
		if k < 3 and k > 0:
			print(n - 1, k)
			print(0, n - k - 1)
		elif k == 3:
			print('-1')
	else:
		d = []
		for i in range(n // 2):
			d.append([i, n - i - 1])
		if k > 0 and k < n - 1:
			if k < n // 2:
				d[0][1] = n - k - 1
				d[k][1] = n - 1
			else:
				d[0][1] = n - k - 1
				d[n - k - 1][0] = n - 1
		elif k == n - 1:
			d[0][0] = n - 2
			d[2][1] = 0
			d[1][1] = n - 3
		for i in range(n // 2):
			print(*d[i])


import random

def tInt():
	n = int(input())
	return n

def tList():
	a = map(int, input().split())
	return a

def tStr():
	s = input()
	return s

def solve():
	for _ in range(tInt()):
		(n, k) = tList()
		flag = False
		if n == 4 and k == 3 or k >= n:
			flag = True
			print(-1)
		elif k == 0:
			pair = []
			for i in range(n // 2):
				pair.append([i, n - 1 - i])
		elif k > 0 and k < n - 1:
			pair = []
			small_k = min(k, n - 1 - k)
			for i in range(0, n // 2):
				if i != 0 and i != small_k:
					pair.append([i, n - 1 - i])
			pair.append([0, n - 1 - k])
			pair.append([k, n - 1])
		elif k == n - 1:
			pair = []
			pair.append([n - 2, n - 1])
			pair.append([1, n - 3])
			pair.append([0, 2])
			for i in range(3, n // 2):
				pair.append([i, n - 1 - i])
		if flag == False:
			for (x, y) in pair:
				print(x, y)
solve()


import sys, collections

def solve(n, k):
	res = []
	if not k:
		for i in range(n // 2):
			res.append([i, n - 1 - i])
	elif n == 4 and k == 3:
		return []
	elif k == n - 1:
		res.append([n - 2, n - 1])
		res.append([1, n // 2 - 1])
		res.append([0, n // 2])
		for i in range(2, n // 2 - 1):
			res.append([i, n - 1 - i])
	else:
		res.append([k, n - 1])
		res.append([0, n - 1 - k])
		for i in range(1, n // 2):
			if i != k and i != n - 1 - k:
				res.append([i, n - 1 - i])
	return res
input = sys.stdin.readline
tests = int(input()[:-1])
for test in range(tests):
	(n, k) = [int(s) for s in input()[:-1].split()]
	res = solve(n, k)
	if res == []:
		print(-1)
	else:
		for pair in res:
			print(*pair)


import sys
input = sys.stdin.readline
t = int(input().strip())

def comp(x, n):
	return n - 1 ^ x
for _ in range(t):
	(n, k) = map(int, input().split())
	if k == 0:
		for i in range(n // 2):
			print(i, comp(i, n))
	elif k < n - 1:
		print(k, n - 1)
		print(0, comp(k, n))
		for i in range(n // 2):
			if i != 0 and i != comp(k, n) and (i != k):
				print(i, comp(i, n))
	elif n > 4:
		print(n - 1, n - 2)
		print(0, 2)
		print(1, n - 3)
		for i in range(n // 2):
			if i != 0 and i != 1 and (i != 2):
				print(i, comp(i, n))
	else:
		print(-1)


import sys

def solution(n, k):
	if k != n - 1:
		for i in range(n // 2):
			if i == 0:
				print(i, n - k - 1)
			elif i == k:
				print(i, n - 1)
			elif i == n - k - 1:
				print(k, n - 1)
			else:
				print(i, n - i - 1)
		return
	if n <= 4:
		print(-1)
		return
	else:
		for i in range(n // 2):
			if i == 0:
				print(0, 2)
			elif i == 1:
				print(1, n - 3)
			elif i == 2:
				print(n - 1, n - 2)
			else:
				print(i, n - i - 1)
	return
t = None
a = []
b = []
input = sys.stdin.read().splitlines()
t = int(input[0])
offset = 1
for test in range(t):
	data = input[test + offset].split(' ')
	n = data[0]
	k = data[1]
	solution(int(n), int(k))


for _ in range(int(input())):
	n = []
	(a, b) = map(int, input().split())
	for z in range(a):
		n.append(z)
	if a == b + 1 and a == 4:
		print(-1)
	elif b == 0:
		for zzz in range(len(n) // 2):
			print(n[0 + zzz], n[-1 - zzz])
	elif a == b + 1:
		print(a - 1, a - 2)
		print(a - 3, 1)
		print(0, 2)
		n.remove(a - 1)
		n.remove(a - 2)
		n.remove(a - 3)
		n.remove(0)
		n.remove(1)
		n.remove(2)
		for i in range(len(n) // 2):
			print(n[0 + i], n[-1 - i])
	else:
		print(a - 1, b)
		print(0, a - b - 1)
		n.remove(0)
		n.remove(a - b - 1)
		n.remove(a - 1)
		n.remove(b)
		if len(n) == 0:
			zzzzz = 5
		else:
			for i in range(len(n) // 2):
				print(n[0 + i], n[-1 - i])


def solve(n, k):
	result = []
	for i in range(n // 2):
		result.append([i, n - 1 - i])
	if k == n - 1:
		result[0][0] = n - 2
		result[1][1] = n - 3
		result[2][1] = 0
		return result
	replace_index = min(k, n - 1 - k)
	if replace_index == k:
		result[replace_index][0] = 0
	else:
		result[replace_index][1] = 0
	result[0][0] = k
	return result
t = int(input())
for _ in range(t):
	(n, k) = [int(x) for x in input().split()]
	if k == n - 1 and n == 4:
		print(-1)
	else:
		result = solve(n, k)
		for i in result:
			print(i[0], i[1])


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	if k == n - 1:
		if n == 4:
			print(-1)
			continue
		print(0, 2)
		print(1, n - 3)
		for i in range(3, n // 2):
			print(i, n - i - 1)
		print(k, k - 1)
		continue
	use = [False] * n
	for i in range(n):
		if use[i]:
			continue
		if i == 0:
			print(i, n - k - 1)
			use[i] = use[n - k - 1] = True
		elif i == k:
			print(i, n - 1)
			use[i] = use[n - 1] = True
		else:
			print(i, n - i - 1)
			use[i] = use[n - i - 1] = True


t = int(input())
while t:
	t = t - 1
	(n, k) = list(map(int, input().split()))
	a = [1] * n
	if k == n - 1:
		if n == 4:
			print(-1)
			continue
		p = 0
		l1 = []
		l2 = []
		while 2 ** p < n:
			l1 += [2 ** p]
			l2 += [n - 1 - 2 ** p]
			p = p + 1
		l1 = l1[1:] + l1[:1]
		for i in range(p):
			print(l1[i], l2[i])
			a[l1[i]] = 0
			a[l2[i]] = 0
		for i in range(n):
			if a[i]:
				j = n - 1 - i
				print(i, j)
				a[j] = 0
		continue
	k2 = n - 1 - k
	if k:
		print(0, k2)
	print(k, n - 1)
	a[0] = 0
	a[k] = 0
	a[k2] = 0
	a[n - 1] = 0
	for i in range(n):
		if a[i]:
			j = n - 1 - i
			print(i, j)
			a[j] = 0


import math
from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	if k == n - 1 and n - 1 == 3:
		print(-1)
		continue
	l = []
	for i in range(n // 2):
		l.append([i, n - 1 - i])
	if k != 0 and k != n - 1:
		id = min(k, n - 1 - k)
		l[0] = [0, n - 1 - k]
		l[id] = [k, n - 1]
	if k == n - 1:
		l[0][0] = n - 2
		l[1][1] = l[-1][0]
		l[-1][0] = 0
	for i in range(len(l)):
		print(*l[i])


def decode(n, k):
	res = []
	for i in range(n // 2):
		res.append([i, n - 1 - i])
	if k == n - 1:
		res[0][0] = n - 2
		res[1][1] = n - 3
		res[2][1] = 0
		return res
	replace_index = min(k, n - 1 - k)
	if replace_index == k:
		res[replace_index][0] = 0
	else:
		res[replace_index][1] = 0
	res[0][0] = k
	return res
x = int(input())
for _ in range(x):
	(n, k) = map(int, input().split())
	if k == n - 1 and n == 4:
		print(-1)
	else:
		result = decode(n, k)
		for i in result:
			print(i[0], i[1])


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	l = [i for i in range(n)]
	if n == 4 and k == 3:
		print(-1)
		continue
	if n - 1 == k:
		(l[0], l[k - 1]) = (l[k - 1], l[0])
		(l[k - 2], l[k - 1]) = (l[k - 1], l[k - 2])
	else:
		(l[k], l[0]) = (l[0], l[k])
	for i in range(n // 2):
		print(l[i], l[n - 1 - i])
num_inp = lambda : int(input())
arr_inp = lambda : list(map(int, input().split()))
sp_inp = lambda : map(int, input().split())
str_inp = lambda : input()


for t in range(int(input())):
	(n, k) = map(int, input().split())
	if n == 4 and k == 3:
		print(-1)
	else:
		A = []
		for i in range(n // 2):
			A.append([i, n - i - 1])
		if k == n - 1:
			(A[0][1], A[1][0]) = (A[1][0], A[0][1])
			(A[-1][1], A[-2][1]) = (A[-2][1], A[-1][1])
		elif k > A[-1][0]:
			(A[0][1], A[n - k - 1][0]) = (A[n - k - 1][0], A[0][1])
		else:
			(A[0][1], A[k][1]) = (A[k][1], A[0][1])
		for i in range(len(A)):
			print(*A[i])


for _ in range(int(input())):
	(n, k) = list(map(int, input().split()))
	mapping = {}
	for i in range(n // 2):
		mapping[i] = n - i - 1
	if k == 0:
		for (key, val) in mapping.items():
			print(key, val)
	elif k < n - 1:
		print(k, n - 1)
		print(0, n - k - 1)
		for (key, val) in mapping.items():
			if key in [0, k, n - k - 1, n - 1] or val in [0, k, n - k - 1, n - 1]:
				pass
			else:
				print(key, val)
	elif n == 4:
		print(-1)
	else:
		print(n - 2, n - 1)
		print(1, n - 3)
		print(0, 2)
		for (key, val) in mapping.items():
			if key in [0, 1, 2, n - 3, n - 2, n - 1] or val in [0, 1, 2, n - 3, n - 2, n - 1]:
				pass
			else:
				print(key, val)


from sys import stdin
from collections import Counter, defaultdict

def rei():
	return list(map(int, stdin.readline().strip().split(' ')))

def res():
	return stdin.readline().rstrip()

def prno():
	print('NO')

def pryes():
	print('YES')

def isEnd(a):
	return len(set(a)) == 1

def solve():
	(n, k) = rei()
	a = [i for i in range(n // 2)]
	if k == 0:
		for i in a:
			print(i, ~i & n - 1)
	elif k == n - 1:
		if n == 4:
			print(-1)
			return
		print(n - 1, n - 2)
		print(1, n - 3)
		print(0, 2)
		for i in a[3:]:
			print(i, ~i & n - 1)
	else:
		exclude = set()
		print(n - 1, k)
		print(0, ~k & n - 1)
		exclude.add(k)
		exclude.add(~k & n - 1)
		for i in a[1:]:
			if i in exclude:
				continue
			print(i, ~i & n - 1)
n = rei()[0]
for _ in range(n):
	solve()


def swap_partners(a, b, l):
	a_index = -1
	b_index = -1
	for (i, pair) in enumerate(l):
		if a in pair:
			a_index = i
		if b in pair:
			b_index = i
	if l[a_index][0] != a:
		l[a_index] = l[a_index][::-1]
	if l[b_index][0] != b:
		l[b_index] = l[b_index][::-1]
	l[b_index][0] = l[a_index][1]
	l[a_index][1] = b
for _ in range(int(input())):
	(n, k) = list(map(int, input().split()))
	if k == n - 1 and n <= 4:
		print(-1)
		continue
	pairs = []
	l = 0
	r = n - 1
	while l < r:
		pairs.append([l, r])
		l += 1
		r -= 1
	if k < n - 1:
		swap_partners(k, n - 1, pairs)
		for pair in pairs:
			print(pair[0], pair[1])
	elif k == n - 1:
		swap_partners(k - 1, n - 1, pairs)
		swap_partners(1, 3, pairs)
		for pair in pairs:
			print(pair[0], pair[1])


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	if n == 4 and k == 3:
		print(-1)
	else:
		ans = []
		visted = [False] * n
		if k != n - 1:
			visted[k] = visted[n - 1 - k] = visted[0] = visted[n - 1] = True
			ans.extend([(k, n - 1), (0, n - 1 - k)])
			if ans[0] == ans[1]:
				ans.pop()
			for i in range(n):
				if not visted[i]:
					visted[i] = visted[n - 1 - i] = True
					ans.append((i, n - 1 - i))
		else:
			visted[0] = visted[n - 1] = visted[1] = visted[n - 2] = visted[2] = visted[n - 3] = True
			ans.extend([(n - 2, n - 1), (1, n - 3), (0, 2)])
			for i in range(n):
				if not visted[i]:
					visted[i] = visted[n - 1 - i] = True
					ans.append((i, n - 1 - i))
			pass
		for i in ans:
			print(*i)


import sys
from collections import defaultdict, deque
from bisect import bisect_left
from itertools import permutations
import math
input = sys.stdin.readline

def inp():
	l = list(map(int, input().split()))
	return l
for _ in range(int(input())):
	(n, kk) = inp()
	if 0 < kk < n - 1:
		print(kk, n - 1)
		print(0, n - kk - 1)
		for i in range(1, n // 2):
			if i != n - kk - 1 and i != kk:
				print(i, n - i - 1)
	elif kk == 0:
		for i in range(n // 2):
			print(i, n - i - 1)
	else:
		if n == 4 and kk == 3:
			print(-1)
			continue
		print(n - 1, n - 2)
		print(3, 1)
		print(0, n - 4)
		for i in range(n - 3, n // 2 - 1, -1):
			if i != n - 4:
				print(i, n - i - 1)


from sys import stdin, stdout

def decimalToBinary(n):
	return bin(n).replace('0b', '')
for _ in range(int(stdin.readline())):
	(n, k) = map(int, stdin.readline().split())
	ans = []
	if n == 4 and k == 3:
		print('-1')
		continue
	aa = []
	if k != n - 1 and k != 0:
		ans.append([k, n - 1])
		ans.append([0, n - k - 1])
		aa = [k, n - 1, 0, n - k - 1]
	elif k != 0:
		ans.append([n - 2, n - 1])
		ans.append([n - 3, 1])
		ans.append([0, 2])
		aa = [n - 2, n - 1, n - 3, 1, 0, 2]
	for i in range(n // 2):
		if i not in aa and n - i - 1 not in aa:
			ans.append([i, n - i - 1])
	for i in ans:
		print(i[0], i[1])


for t in range(int(input())):
	(n, k) = map(int, input().split())
	if k == n - 1 and n == 4:
		print(-1)
		continue
	used = set()
	if k == n - 1:
		for i in range(n - 1):
			if i in used:
				continue
			if i == 0:
				print(i, n - 1 - 3)
				used.add(n - 1 - 3)
			elif i == 1:
				print(i, 3)
				used.add(3)
			elif i == n - 2:
				print(n - 1, n - 2)
				used.add(n - 2)
			else:
				print(i, n - 1 - i)
				used.add(n - 1 - i)
	else:
		for i in range(n):
			if i in used:
				continue
			if i == 0:
				print(i, n - 1 - k)
				used.add(n - 1 - k)
			elif i == k:
				print(i, n - 1)
				used.add(n - 1)
			else:
				print(i, n - 1 - i)
				used.add(n - 1 - i)


import sys
ip = sys.stdin.readline
for _ in range(int(ip())):
	(n, k) = map(int, ip().split())
	vis = [False for _ in range(n)]
	ans = []
	if n - 1 == k:
		if n - 1 == 3:
			print(-1)
		else:
			vis[n - 2] = vis[n - 1] = True
			ans.append((n - 2, n - 1))
			vis[1] = vis[3] = True
			ans.append((1, 3))
			vis[n - 1 - 3] = vis[0] = True
			ans.append((n - 1 - 3, 0))
			for i in range(n - 3, 1, -1):
				if not vis[i]:
					vis[i] = True
					vis[n - 1 - i] = True
					ans.append((i, n - 1 - i))
			for i in range(n // 2):
				print(ans[i][0], ans[i][1])
	else:
		vis[n - 1] = vis[k] = vis[n - 1 - k] = vis[0] = True
		ans.append((n - 1, k))
		if k != 0:
			ans.append((n - 1 - k, 0))
		for i in range(n - 2, 0, -1):
			if not vis[i]:
				vis[i] = True
				vis[n - 1 - i] = True
				ans.append((i, n - 1 - i))
		for i in range(n // 2):
			print(ans[i][0], ans[i][1])


from tabnanny import check
t = int(input())
for tt in range(t):
	(n, k) = [int(i) for i in input().split(' ')]
	temp = n
	m = -1
	while temp:
		temp //= 2
		m += 1
	if k == 0:
		for i in range(n // 2):
			print(i, n - 1 - i)
	elif k != n - 1:
		cc = [0 for i in range(n)]
		print(k, n - 1)
		cc[k] = 1
		cc[n - 1] = 1
		nk = ~k & n - 1
		print(0, nk)
		cc[0] = 1
		cc[nk] = 1
		for i in range(n):
			if cc[i] == 0:
				print(i, ~i & n - 1)
				cc[i] = 1
				cc[~i & n - 1] = 1
	elif n == 4:
		print(-1)
	else:
		cc = [0 for i in range(n)]
		print(n - 2, n - 1)
		print(1, 3)
		print(0, ~3 & n - 1)
		cc[n - 2] = 1
		cc[n - 1] = 1
		cc[1] = 1
		cc[3] = 1
		cc[0] = 1
		cc[~3 & n - 1] = 1
		for i in range(n):
			if cc[i] == 0:
				print(i, ~i & n - 1)
				cc[i] = 1
				cc[~i & n - 1] = 1


test_case = int(input())
for i in range(test_case):
	(n, k) = input().split()
	(n, k) = (int(n), int(k))
	isvisited = set()
	i = 0
	if k == n - 1:
		if n == 4:
			print(-1)
		else:
			k -= 1
			while i < n:
				if i in isvisited:
					i += 1
				else:
					isvisited.add(i)
					if i == 0:
						print(i, n - 1 - 3)
						isvisited.add(n - 1 - 3)
					elif i == 1:
						i += 1
					elif i == 3:
						print(i, 1)
						isvisited.add(1)
					elif i == k:
						print(i, n - 1)
						isvisited.add(n - 1)
					else:
						print(i, n - 1 - i)
						isvisited.add(n - 1 - i)
	else:
		while i < n:
			if i in isvisited:
				i += 1
			else:
				isvisited.add(i)
				if i == 0:
					print(i, n - 1 - k)
					isvisited.add(n - 1 - k)
				elif i == k:
					print(i, n - 1)
					isvisited.add(n - 1)
				else:
					print(i, n - 1 - i)
					isvisited.add(n - 1 - i)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	if n == 4 and k == 3:
		print(-1)
		continue
	(vis, ans) = ([0] * n, [])
	if k == n - 1:
		k -= 3
		ans.append([k, n - 1])
		ans.append([n - 1 - k, 0])
		vis[k] = vis[n - 1] = vis[n - 1 - k] = vis[0] = 1
		ans.append([1, n - 3])
		ans.append([2, n - 2])
		vis[1] = vis[2] = vis[n - 3] = vis[n - 2] = 1
	elif k:
		ans.append([k, n - 1])
		ans.append([n - 1 - k, 0])
		vis[k] = vis[n - 1] = vis[n - 1 - k] = vis[0] = 1
	for i in range(n):
		if vis[i]:
			continue
		ans.append([i, n - 1 - i])
		vis[i] = vis[n - 1 - i] = 1
	for item in ans:
		print(*item)


from traceback import print_tb
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	c = []
	for i in range(n):
		c.append(n - i - 1)
	if k == 0:
		for i in range(n // 2):
			print(i, n - i - 1)
	elif n - 1 > k > 0:
		print(k, n - 1)
		print(0, c[k])
		for i in range(n // 2):
			if i not in (0, k, n - 1, c[k]):
				print(i, c[i])
	elif k == n - 1:
		if n == 4:
			print(-1)
			continue
		print(n - 1, n - 2)
		print(n - 3, 1)
		print(0, 2)
		for i in range(n // 2):
			if i not in (n - 1, n - 2, n - 3, 1, 0, 2):
				print(i, c[i])


from collections import deque
for _ in range(int(input())):
	(n, k) = map(int, input().split())

	def c(a):
		return a ^ n - 1
	ans = []
	vis = set()
	if k == 0:
		for i in range(n):
			if i not in vis and c(i) not in vis:
				vis.add(c(i))
				vis.add(i)
				ans.append([i, c(i)])
	elif 0 < k < n - 1:
		ans.append([0, c(k)])
		ans.append([n - 1, k])
		vis |= {0, k, c(k), n - 1}
		for i in range(n):
			if i not in vis and c(i) not in vis:
				vis.add(c(i))
				vis.add(i)
				ans.append([i, c(i)])
	elif n >= 8:
		ans.append([n - 2, n - 1])
		ans.append([1, 3])
		vis |= {1, 3, n - 2, n - 1}
		for i in range(n):
			if i not in vis and c(i) not in vis:
				vis.add(c(i))
				vis.add(i)
				ans.append([i, c(i)])
		p = []
		for i in range(n):
			if i not in vis:
				p.append(i)
		for i in range(0, len(p), 2):
			ans.append([p[i], p[i + 1]])
	else:
		print(-1)
	[print(*i) for i in ans]


ans = []
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	otv = []
	if k != n - 1:
		otv.append([k, n - 1])
		if k != 0:
			otv.append([n - k - 1, 0])
		i = 1
		while i < n - i - 1:
			if i != k and i != n - k - 1 and (n - i - 1 != k) and (n - i - 1 != n - k - 1):
				otv.append([i, n - i - 1])
			i += 1
	elif n == 4:
		otv.append([-1])
	else:
		otv.append([n - 1, n - 2])
		otv.append([1, 3])
		otv.append([2, n - 3])
		otv.append([0, n - 4])
		i = 4
		while i < n - i - 1:
			otv.append([i, n - i - 1])
			i += 1
	ans.append(otv)
for i in range(len(ans)):
	for j in range(len(ans[i])):
		if len(ans[i][j]) == 1:
			print(ans[i][j][0])
		else:
			print(ans[i][j][0], ans[i][j][1])


def solve():
	(n, k) = map(int, input().split())
	res = 0
	if n == 4 and k == 3:
		print(-1)
		return
	pool = set([i for i in range(n)])
	pairs = []
	if k < n - 1:
		pairs.append((k, n - 1))
		print(k, n - 1)
		res += k & n - 1
		pool.remove(k)
		pool.remove(n - 1)
	else:
		pairs.append([k - 1, k])
		pairs.append([1, k - 2])
		res += k - 1 & k
		res += 1 & k - 2
		print(k - 1, k)
		print(1, k - 2)
		pool.remove(k)
		pool.remove(k - 1)
		pool.remove(1)
		pool.remove(k - 2)
	for i in range(n // 2):
		if i in pool and n - i - 1 in pool:
			pairs.append([i, n - i - 1])
			print(i, n - i - 1)
			res += i & n - i - 1
			pool.remove(i)
			pool.remove(n - i - 1)
	if len(pool) == 2:
		pairs.append(list(pool))
		print(*list(pool))
		res += list(pool)[0] & list(pool)[1]
for _ in range(int(input())):
	solve()


t = int(input())

def solve(n, k):
	ans = [[k, n - 1]]
	ans2 = []
	if k == n - 1:
		ans2.append([n - 2, n - 1])
		ans2.append([1, 5])
		ans2.append([0, n - 6])
		for i in range(2, n // 2):
			if i not in [n - 6, 5]:
				ans2.append([i, n - i - 1])
		return ans2
	if k == 0:
		for i in range(1, n // 2):
			ans.append([i, n - 1 - i])
		return ans
	ans.append([0, n - 1 - k])
	i = 0
	while len(ans) != n // 2:
		if i in [0, k, n - 1, n - k - 1]:
			i += 1
			continue
		ans.append([i, n - 1 - i])
		i += 1
	return ans
for _ in range(t):
	(n, k) = list(map(int, input().split()))
	ans = solve(n, k)
	if n == 4 and k == n - 1:
		print(-1)
		continue
	for i in ans:
		for j in i:
			print(j, end=' ')
		print('\n')


import sys
import math
input = sys.stdin.readline
from collections import Counter
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	if k == n - 1:
		if n == 4:
			print(-1)
			continue
		lst = []
		lst.append([n - 1, n - 2])
		lst.append([0, 2])
		lst.append([1, n - 3])
		for i in range(3, n // 2):
			lst.append([i, n - 1 - i])
		for i in lst:
			print(*i)
	else:
		lst = []
		lst.append([0, n - k - 1])
		for i in range(1, n // 2):
			if i == k or i == n - k - 1:
				lst.append([n - 1, k])
				continue
			else:
				lst.append([i, n - i - 1])
		for i in lst:
			print(*i)