There are $n$ left boots and $n$ right boots. Each boot has a color which is denoted as a lowercase Latin letter or a question mark ('?'). Thus, you are given two strings $l$ and $r$, both of length $n$. The character $l_i$ stands for the color of the $i$-th left boot and the character $r_i$ stands for the color of the $i$-th right boot.

A lowercase Latin letter denotes a specific color, but the question mark ('?') denotes an indefinite color. Two specific colors are compatible if they are exactly the same. An indefinite color is compatible with any (specific or indefinite) color.

For example, the following pairs of colors are compatible: ('f', 'f'), ('?', 'z'), ('a', '?') and ('?', '?'). The following pairs of colors are not compatible: ('f', 'g') and ('a', 'z').

Compute the maximum number of pairs of boots such that there is one left and one right boot in a pair and their colors are compatible.

Print the maximum number of such pairs and the pairs themselves. A boot can be part of at most one pair.


-----Input-----

The first line contains $n$ ($1 \le n \le 150000$), denoting the number of boots for each leg (i.e. the number of left boots and the number of right boots).

The second line contains the string $l$ of length $n$. It contains only lowercase Latin letters or question marks. The $i$-th character stands for the color of the $i$-th left boot.

The third line contains the string $r$ of length $n$. It contains only lowercase Latin letters or question marks. The $i$-th character stands for the color of the $i$-th right boot.


-----Output-----

Print $k$ — the maximum number of compatible left-right pairs of boots, i.e. pairs consisting of one left and one right boot which have compatible colors.

The following $k$ lines should contain pairs $a_j, b_j$ ($1 \le a_j, b_j \le n$). The $j$-th of these lines should contain the index $a_j$ of the left boot in the $j$-th pair and index $b_j$ of the right boot in the $j$-th pair. All the numbers $a_j$ should be distinct (unique), all the numbers $b_j$ should be distinct (unique).

If there are many optimal answers, print any of them.


-----Examples-----
Input
10
codeforces
dodivthree

Output
5
7 8
4 9
2 2
9 10
3 1

Input
7
abaca?b
zabbbcc

Output
5
6 5
2 3
4 6
7 4
1 2

Input
9
bambarbia
hellocode

Output
0

Input
10
code??????
??????test

Output
10
6 2
1 6
7 3
3 5
4 8
9 7
5 1
2 4
10 9
8 10

Source: codeforces | Difficulty: MEDIUM
Tags: Implementation

Greedy algorithms


Solutions:
if True:
	n = int(input())
	s = input()
	t = input()
else:
	s = 'code??????'
	t = '??????test'
	n = len(s)
X = [[i + 1, s[i]] for i in range(n)]
Y = [[i + 1, t[i]] for i in range(n)]
X = sorted(X, key=lambda x: ord(x[1]) if x[1] != '?' else 200)
Y = sorted(Y, key=lambda x: ord(x[1]) if x[1] != '?' else 200)
i = 0
j = 0
ANS = []
XX = [0] * n
YY = [0] * n
while i < n and j < n:
	if X[i][1] == '?' or Y[j][1] == '?':
		break
	if X[i][1] == Y[j][1]:
		ANS.append([X[i][0], Y[j][0]])
		XX[i] = 1
		YY[j] = 1
		i += 1
		j += 1
	elif X[i][1] < Y[j][1]:
		i += 1
	else:
		j += 1
while i < n and X[i][1] != '?':
	i += 1
while j < n and Y[j][1] != '?':
	j += 1
jj = 0
ii = 0
while len(ANS) < n and i < n:
	while YY[jj] == 1 and jj < n:
		jj += 1
	if jj >= n:
		break
	ANS.append([X[i][0], Y[jj][0]])
	XX[i] = 1
	YY[jj] = 1
	i += 1
	jj += 1
while len(ANS) < n and j < n:
	while XX[ii] == 1 and ii < n:
		ii += 1
	while YY[j] == 1 and j < n:
		j += 1
	if ii >= n:
		break
	ANS.append([X[ii][0], Y[j][0]])
	XX[ii] = 1
	YY[j] = 1
	j += 1
	ii += 1
print(len(ANS))
for ans in ANS:
	print(*ans)


n = int(input())
l = input()
r = input()
ldict = [[] for i in range(27)]
rdict = [[] for i in range(27)]
for i in range(n):
	if l[i] == '?':
		ldict[26].append(i + 1)
	else:
		ldict[ord(l[i]) - ord('a')].append(i + 1)
	if r[i] == '?':
		rdict[26].append(i + 1)
	else:
		rdict[ord(r[i]) - ord('a')].append(i + 1)
ans = []
for i in range(26):
	if len(ldict[i]) < len(rdict[i]):
		smaller = 0
		mn = len(ldict[i])
	else:
		smaller = 1
		mn = len(rdict[i])
	for j in range(mn):
		ans.append([ldict[i][j], rdict[i][j]])
	if smaller == 0 and ldict[26] != []:
		for j in range(mn, len(rdict[i])):
			ans.append([ldict[26][-1], rdict[i][j]])
			del ldict[26][-1]
			if ldict[26] == []:
				break
	elif smaller == 1 and rdict[26] != []:
		for j in range(mn, len(ldict[i])):
			ans.append([ldict[i][j], rdict[26][-1]])
			del rdict[26][-1]
			if rdict[26] == []:
				break
mn = min(len(ldict[26]), len(rdict[26]))
for i in range(mn):
	ans.append([ldict[26][i], rdict[26][i]])
print(len(ans))
for i in ans:
	print(i[0], i[1])


n = int(input())
a = input()
b = input()
da = {}
db = {}
l1 = []
l2 = []
for i in range(n):
	if a[i] == '?':
		l1.append(i)
	elif a[i] in da:
		da[a[i]].append(i)
	else:
		da[a[i]] = [i]
	if b[i] == '?':
		l2.append(i)
	elif b[i] in db:
		db[b[i]].append(i)
	else:
		db[b[i]] = [i]
ans = []
for key in da.keys():
	if key in db:
		while len(da[key]) > 0 and len(db[key]) > 0:
			(x, y) = (da[key].pop(), db[key].pop())
			ans.append((x + 1, y + 1))
for key in db.keys():
	while len(l1) > 0 and len(db[key]) > 0:
		(x, y) = (l1.pop(), db[key].pop())
		ans.append((x + 1, y + 1))
for key in da.keys():
	while len(l2) > 0 and len(da[key]) > 0:
		(y, x) = (l2.pop(), da[key].pop())
		ans.append((x + 1, y + 1))
while len(l1) > 0 and len(l2) > 0:
	(x, y) = (l1.pop(), l2.pop())
	ans.append((x + 1, y + 1))
print(len(ans))
for i in ans:
	print(i[0], i[1])


import collections
n = int(input())
l = input()
r = input()
lcount = collections.defaultdict(int)
rcount = collections.defaultdict(int)
l_indexes = collections.defaultdict(list)
r_indexes = collections.defaultdict(list)
for i in range(n):
	lcount[l[i]] += 1
	rcount[r[i]] += 1
	l_indexes[l[i]].append(i + 1)
	r_indexes[r[i]].append(i + 1)
matches = []
for i in lcount.keys():
	if lcount[i] > 0 and rcount[i] > 0 and (i != '?'):
		while lcount[i] > 0 and rcount[i] > 0:
			lcount[i] -= 1
			rcount[i] -= 1
			matches.append((l_indexes[i].pop(), r_indexes[i].pop()))
if lcount['?'] > 0:
	for i in rcount.keys():
		while lcount['?'] > 0 and rcount[i] > 0 and (i != '?'):
			lcount['?'] -= 1
			rcount[i] -= 1
			matches.append((l_indexes['?'].pop(), r_indexes[i].pop()))
if rcount['?'] > 0:
	for i in lcount.keys():
		while rcount['?'] > 0 and lcount[i] > 0 and (i != '?'):
			rcount['?'] -= 1
			lcount[i] -= 1
			matches.append((l_indexes[i].pop(), r_indexes['?'].pop()))
if lcount['?'] > 0:
	for i in rcount.keys():
		while lcount['?'] > 0 and rcount[i] > 0:
			lcount['?'] -= 1
			rcount[i] -= 1
			matches.append((l_indexes['?'].pop(), r_indexes[i].pop()))
if rcount['?'] > 0:
	for i in lcount.keys():
		while rcount['?'] > 0 and lcount[i] > 0:
			rcount['?'] -= 1
			lcount[i] -= 1
			matches.append((l_indexes[i].pop(), r_indexes['?'].pop()))
print(len(matches))
for (lp, rp) in matches:
	print(str(lp) + ' ' + str(rp))


n = int(input())
s1 = input()
s2 = input()

def s_to_m(s):
	m = {}
	for (i, c) in enumerate(s, 1):
		if c not in m:
			m[c] = []
		m[c].append(i)
	return m

def extract_next(m, c):
	res = m[c].pop()
	if len(m[c]) == 0:
		m.pop(c)
	return res
m1 = s_to_m(s1)
m2 = s_to_m(s2)
pairs = []
keys = set(m1.keys())
for key in keys:
	if key == '?':
		continue
	while key in m2 and key in m1:
		pairs.append((extract_next(m1, key), extract_next(m2, key)))
keys = set(m1.keys())
for key in keys:
	if key == '?':
		continue
	while '?' in m2 and key in m1:
		pairs.append((extract_next(m1, key), extract_next(m2, '?')))
keys = set(m2.keys())
for key in keys:
	while '?' in m1 and key in m2:
		pairs.append((extract_next(m1, '?'), extract_next(m2, key)))
print(len(pairs))
for (x, y) in pairs:
	print(x, y)


from collections import deque
n = int(input())
a = input()
b = input()
va = [[] for i in range(26)]
qa = deque([])
vb = [[] for i in range(26)]
qb = deque([])
ca = [0 for i in range(n)]
cb = [0 for i in range(n)]
for i in range(n):
	if a[i] == '?':
		qa.append(i + 1)
		ca[i] = -1
	else:
		s = ord(a[i]) - ord('a')
		va[s].append(i + 1)
for i in range(n):
	if b[i] == '?':
		qb.append(i + 1)
		cb[i] = -1
	else:
		s = ord(b[i]) - ord('a')
		vb[s].append(i + 1)
ans = []
for i in range(26):
	t = min(len(va[i]), len(vb[i]))
	for j in range(t):
		xa = va[i][j]
		xb = vb[i][j]
		ans.append([xa, xb])
		ca[xa - 1] = 1
		cb[xb - 1] = 1
(ga, gb) = (deque([]), deque([]))
for i in range(n):
	if ca[i] == 0:
		ga.append(i + 1)
	if cb[i] == 0:
		gb.append(i + 1)
while qa:
	if len(gb) == 0:
		break
	x1 = qa.popleft()
	x2 = gb.popleft()
	ans.append([x1, x2])
while qb:
	if len(ga) == 0:
		break
	x2 = qb.popleft()
	x1 = ga.popleft()
	ans.append([x1, x2])
t = min(len(qa), len(qb))
for i in range(t):
	x1 = qa.popleft()
	x2 = qb.popleft()
	ans.append([x1, x2])
print(len(ans))
for (i, j) in ans:
	print(i, j)


import sys
lines = sys.stdin.readlines()
lines = [line.strip() for line in lines]
n = int(lines[0])
(a1, a2) = (lines[1], lines[2])
(d1, d2) = ({}, {})
(id1, id2) = ({}, {})
for (idx, elem) in enumerate(a1):
	if elem not in d1:
		d1[elem] = 1
		id1[elem] = [idx]
	else:
		d1[elem] += 1
		id1[elem].append(idx)
for (idx, elem) in enumerate(a2):
	if elem not in d2:
		d2[elem] = 1
		id2[elem] = [idx]
	else:
		d2[elem] += 1
		id2[elem].append(idx)
ans = []
for (k, v) in d1.items():
	if k == '?':
		continue
	if k in d2:
		for (i, j) in zip(id1[k], id2[k]):
			ans.append((i, j))
		mins = min(v, d2[k])
		id1[k] = id1[k][mins:]
		id2[k] = id2[k][mins:]
		d1[k] -= mins
		d2[k] -= mins
if '?' in d1 and d1['?'] >= 0:
	val = d1['?']
	for (k, v) in d2.items():
		if k == '?':
			continue
		if v != 0:
			for (i, j) in zip(id1['?'], id2[k]):
				ans.append((i, j))
			mins = min(val, v)
			id1['?'] = id1['?'][mins:]
			id2[k] = id2[k][mins:]
			d1['?'] -= mins
			d2[k] -= mins
if '?' in d2 and d2['?'] >= 0:
	val = d2['?']
	for (k, v) in d1.items():
		if v != 0:
			for (i, j) in zip(id2['?'], id1[k]):
				ans.append((j, i))
			mins = min(val, v)
			id2['?'] = id2['?'][mins:]
			id1[k] = id1[k][mins:]
			d2['?'] -= mins
			d1[k] -= mins
print(len(ans))
for k in ans:
	print('{} {}'.format(k[0] + 1, k[1] + 1))


from sys import stdin, stdout
from collections import defaultdict
for t in range(1):
	n = int(stdin.readline())
	s1 = input()
	s2 = input()
	g1 = defaultdict(list)
	g2 = defaultdict(list)
	for i in range(n):
		g1[s1[i]] += [i]
		g2[s2[i]] += [i]
	vis1 = [0] * n
	vis2 = [0] * n
	ans = []
	for ascc in range(97, 123):
		ch = chr(ascc)
		for (p1, p2) in zip(g1[ch], g2[ch]):
			ans += [[p1 + 1, p2 + 1]]
			vis1[p1] = 1
			vis2[p2] = 1
	p = 0
	for ind in g1['?']:
		if vis1[ind]:
			continue
	for ind in g1['?']:
		if vis1[ind]:
			continue
		while p < n and (vis2[p] or s2[p] == '?'):
			p += 1
		if p >= n:
			break
		ans += [[ind + 1, p + 1]]
		vis2[p] = 1
		vis1[ind] = 1
	p = 0
	for ind in g2['?']:
		if vis2[ind]:
			continue
		while p < n and vis1[p]:
			p += 1
		if p >= n:
			break
		ans += [[p + 1, ind + 1]]
		vis1[p] = 1
		vis2[ind] = 1
	print(len(ans))
	for (x, y) in ans:
		print(x, y)


n = int(input())
l = str(input())
r = str(input())
ind1 = [[] for i in range(27)]
ind2 = [[] for i in range(27)]
c1 = [0] * 27
c2 = [0] * 27
ans = []
for i in range(n):
	if l[i] != '?':
		ind1[ord(l[i]) - 97].append(i + 1)
		c1[ord(l[i]) - 97] += 1
	else:
		ind1[-1].append(i + 1)
		c1[-1] += 1
for i in range(n):
	if r[i] != '?':
		ind2[ord(r[i]) - 97].append(i + 1)
		c2[ord(r[i]) - 97] += 1
	else:
		ind2[-1].append(i + 1)
		c2[-1] += 1
for i in range(26):
	while c1[i] > 0 and c2[i] > 0:
		ans.append([ind1[i].pop(), ind2[i].pop()])
		c1[i] -= 1
		c2[i] -= 1
for i in range(26):
	while c1[i] > 0 and c2[-1] > 0:
		ans.append([ind1[i].pop(), ind2[-1].pop()])
		c1[i] -= 1
		c2[-1] -= 1
for i in range(26):
	while c1[-1] > 0 and c2[i] > 0:
		ans.append([ind1[-1].pop(), ind2[i].pop()])
		c1[-1] -= 1
		c2[i] -= 1
while c1[-1] > 0 and c2[-1] > 0:
	ans.append([ind1[-1].pop(), ind2[-1].pop()])
	c1[-1] -= 1
	c2[-1] -= 1
k = len(ans)
print(k)
for i in range(k):
	print(ans[i][0], ans[i][1])


from sys import stdin, stdout
from itertools import combinations
from collections import defaultdict
import math

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

def stringListIn():
	return [x for x in stdin.readline().split()]

def intIn():
	return int(stdin.readline())

def stringIn():
	return stdin.readline().strip()
n = intIn()
a = '*' + stringIn()
b = '*' + stringIn()
lia = {}
lib = {}
for i in range(1, n + 1):
	if a[i] not in lia:
		lia[a[i]] = [i]
	else:
		lia[a[i]].append(i)
	if b[i] not in lib:
		lib[b[i]] = [i]
	else:
		lib[b[i]].append(i)
ans = []
(nca, ncb) = ([], [])
for ch1 in lia:
	if ch1 != '?':
		a1 = lia[ch1]
		if ch1 in lib:
			a2 = lib[ch1]
			l1 = len(a1)
			l2 = len(a2)
			for i in range(min(l1, l2)):
				ans.append([a1[i], a2[i]])
			if l1 < l2:
				for i in range(l1, l2):
					ncb.append(a2[i])
			elif l1 > l2:
				for i in range(l2, l1):
					nca.append(a1[i])
		else:
			for i in range(len(a1)):
				nca.append(a1[i])
for ch1 in lib:
	if ch1 != '?':
		a1 = lib[ch1]
		if ch1 not in lia:
			for i in range(len(a1)):
				ncb.append(a1[i])
(qla, qlb) = ([], [])
(lla, llb) = (len(nca), len(ncb))
if '?' in lia:
	qla = lia['?']
	qa = len(qla)
	ncq = []
	if llb > 0:
		for i in range(min(qa, llb)):
			ans.append([qla[i], ncb[i]])
		if qa > llb:
			for i in range(llb, qa):
				ncq.append(qla[i])
		qla = ncq
if '?' in lib:
	qlb = lib['?']
	qb = len(qlb)
	ncq = []
	if lla > 0:
		for i in range(min(qb, lla)):
			ans.append([nca[i], qlb[i]])
		if qb > lla:
			for i in range(lla, qb):
				ncq.append(qlb[i])
			qlb = ncq
for i in range(min(len(qla), len(qlb))):
	ans.append([qla[i], qlb[i]])
print(len(ans))
for ele in ans:
	print(*ele)


n = int(input())
inp_1 = input()
inp_2 = input()

def str_dict(string):
	od = dict()
	for c in range(ord('a'), ord('z') + 1):
		od[chr(c)] = []
	od['?'] = []
	for i in range(len(string)):
		od[string[i]].append(i + 1)
	return od

def ans(n, str_1, str_2):
	od_1 = str_dict(str_1)
	od_2 = str_dict(str_2)
	res_list = []
	for c in range(ord('a'), ord('z') + 1):
		c_min = min(len(od_1[chr(c)]), len(od_2[chr(c)]))
		if c_min > 0:
			for _ in range(c_min):
				res_list.append((od_1[chr(c)].pop(), od_2[chr(c)].pop()))
	for c in range(ord('a'), ord('z') + 1):
		c_min = min(len(od_1[chr(c)]), len(od_2['?']))
		if c_min > 0:
			for _ in range(c_min):
				res_list.append((od_1[chr(c)].pop(), od_2['?'].pop()))
	for c in range(ord('a'), ord('z') + 1):
		c_min = min(len(od_1['?']), len(od_2[chr(c)]))
		if c_min > 0:
			for _ in range(c_min):
				res_list.append((od_1['?'].pop(), od_2[chr(c)].pop()))
	c_min = min(len(od_1['?']), len(od_2['?']))
	if c_min > 0:
		for _ in range(c_min):
			res_list.append((od_1['?'].pop(), od_2['?'].pop()))
	return res_list
a = ans(n, inp_1, inp_2)
print(len(a))
for v in a:
	print(v[0], ' ', v[1])


import sys
from collections import deque
input = sys.stdin.readline

def One(type):
	return type(input())

def List(type):
	return list(map(type, input().split()))

def Str():
	return list(input())

def Many(type):
	return map(type, input().split())

def main():
	n = One(int)
	l = Str()
	r = Str()
	l.pop()
	r.pop()
	for i in range(n):
		if l[i] == '?':
			l[i] = '{'
		if r[i] == '?':
			r[i] = '{'
		l[i] = list(l[i])
		l[i].append(i)
		r[i] = list(r[i])
		r[i].append(i)
	l.sort()
	r.sort()
	indl = indr = 0
	sr = deque()
	sl = deque()
	ans = []
	while indl < n and indr < n and (l[indl][0] != '{') and (r[indr][0] != '{'):
		(lef, rig) = (l[indl], r[indr])
		if lef[0] == rig[0]:
			ans.append([lef[1] + 1, rig[1] + 1])
			indl += 1
			indr += 1
		elif lef[0] < rig[0]:
			sl.append(lef[1])
			indl += 1
		else:
			sr.append(rig[1])
			indr += 1
		continue
	while indl < n and l[indl][0] != '{':
		sl.append(l[indl][1])
		indl += 1
	while indr < n and r[indr][0] != '{':
		sr.append(r[indr][1])
		indr += 1
	indl = indr = n - 1
	while indl >= 0 and l[indl][0] == '{' and (len(sr) > 0):
		ans.append([l[indl][1] + 1, sr.pop() + 1])
		indl -= 1
	while indr >= 0 and r[indr][0] == '{' and (len(sl) > 0):
		ans.append([sl.pop() + 1, r[indr][1] + 1])
		indr -= 1
	while indr >= 0 and indl >= 0 and (r[indr][0] == '{') and (l[indl][0] == '{'):
		ans.append([l[indl][1] + 1, r[indr][1] + 1])
		indr -= 1
		indl -= 1
	print(len(ans))
	for i in ans:
		print(i[0], i[1])
main()


from collections import defaultdict
p = 'abcdefghijklmnopqrstuvwxyz'
n = int(input())
l = defaultdict(int)
r = defaultdict(int)
lv = defaultdict(list)
rv = defaultdict(list)
k = 0
sc = 0
rsl = []
ojl = []
ojr = []
for i in input():
	sc += 1
	lv[i].append(sc)
	l[i] += 1
sc = 0
for i in input():
	sc += 1
	rv[i].append(sc)
	r[i] += 1
x = l['?']
y = r['?']
ls = 0
rs = 0
for i in p:
	xr = min(l[i], r[i])
	k += xr
	l[i] -= xr
	r[i] -= xr
	ls += l[i]
	rs += r[i]
	for j in range(xr):
		rsl.append((lv[i][j], rv[i][j]))
	for j in range(l[i]):
		ojl.append(lv[i][xr + j])
	for j in range(r[i]):
		ojr.append(rv[i][xr + j])
wl = min(rs, x)
k += wl
x -= wl
for i in range(wl):
	rsl.append((lv['?'][i], ojr[i]))
wr = min(ls, y)
y -= wr
k += wr
for i in range(wr):
	rsl.append((ojl[i], rv['?'][i]))
aq = min(x, y)
k += aq
for i in range(aq):
	rsl.append((lv['?'][wl + i], rv['?'][wr + i]))
print(len(rsl))
for el in rsl:
	print(el[0], el[1])


import sys
input = sys.stdin.readline
from collections import defaultdict
n = int(input())
sl = input()[:-1]
sr = input()[:-1]
dl = defaultdict(list)
dr = defaultdict(list)
for i in range(n):
	dl[sl[i]].append(i)
	dr[sr[i]].append(i)
ans0 = 0
ans1 = []
match_l = [False] * n
match_r = [False] * n
for alpha in 'abcdefghijklmnopqrstuvwxyz':
	m = min(len(dl[alpha]), len(dr[alpha]))
	for i in range(m):
		j = dl[alpha][i]
		k = dr[alpha][i]
		ans0 += 1
		ans1.append((j + 1, k + 1))
		match_l[j] = True
		match_r[k] = True
(l_idx, r_idx) = (0, 0)
for i in range(n):
	if sl[i] != '?' and (not match_l[i]):
		if r_idx < len(dr['?']):
			ans0 += 1
			ans1.append((i + 1, dr['?'][r_idx] + 1))
			r_idx += 1
for i in range(n):
	if sr[i] != '?' and (not match_r[i]):
		if l_idx < len(dl['?']):
			ans0 += 1
			ans1.append((dl['?'][l_idx] + 1, i + 1))
			l_idx += 1
m = min(len(dl['?']) - l_idx, len(dr['?']) - r_idx)
for i in range(m):
	ans0 += 1
	ans1.append((dl['?'][l_idx + i] + 1, dr['?'][r_idx + i] + 1))
print(ans0)
for (i, j) in ans1:
	print(i, j)


n = int(input())
l = list(input())
r = list(input())
dic1 = {chr(i): [] for i in range(97, 123)}
dic2 = {chr(i): [] for i in range(97, 123)}
cnt1 = []
cnt2 = []
for i in range(n):
	if l[i] == '?':
		cnt1.append(i)
	else:
		dic1[l[i]].append(i)
	if r[i] == '?':
		cnt2.append(i)
	else:
		dic2[r[i]].append(i)
for i in dic1:
	if len(dic1[i]) > len(dic2[i]):
		while len(cnt2) != 0 and len(dic1[i]) != len(dic2[i]):
			dic2[i].append(cnt2[-1])
			cnt2.pop()
	elif len(dic1[i]) < len(dic2[i]):
		while len(cnt1) != 0 and len(dic2[i]) != len(dic1[i]):
			dic1[i].append(cnt1[-1])
			cnt1.pop()
indexes = []
for k in dic1:
	for i in range(min((len(dic2[k]), len(dic1[k])))):
		indexes.append((dic1[k][i] + 1, dic2[k][i] + 1))
for k in range(min(len(cnt1), len(cnt2))):
	indexes.append((cnt1[k] + 1, cnt2[k] + 1))
print(len(indexes))
for (x, y) in indexes:
	print(x, y)


n = int(input())
l = input()
r = input()
ml = {}
mr = {}
for i in range(n):
	if l[i] not in ml:
		ml[l[i]] = []
	ml[l[i]].append(i)
	if r[i] not in mr:
		mr[r[i]] = []
	mr[r[i]].append(i)
matched = []
remains_l = 0
remains_r = 0
for i in range(ord('a'), ord('z') + 1):
	c = chr(i)
	if c in ml and c in mr:
		t = min(len(ml[c]), len(mr[c]))
		for i in range(t):
			matched.append((ml[c][i], mr[c][i]))
		while t < len(ml[c]) and '?' in mr and (len(mr['?']) > 0):
			matched.append((ml[c][t], mr['?'].pop()))
			t += 1
		while t < len(mr[c]) and '?' in ml and (len(ml['?']) > 0):
			matched.append((ml['?'].pop(), mr[c][t]))
			t += 1
	elif c in ml:
		t = 0
		while t < len(ml[c]) and '?' in mr and (len(mr['?']) > 0):
			matched.append((ml[c][t], mr['?'].pop()))
			t += 1
	elif c in mr:
		t = 0
		while t < len(mr[c]) and '?' in ml and (len(ml['?']) > 0):
			matched.append((ml['?'].pop(), mr[c][t]))
			t += 1
while '?' in ml and len(ml['?']) > 0 and ('?' in mr) and (len(mr['?']) > 0):
	matched.append((ml['?'].pop(), mr['?'].pop()))
if len(matched) > 0:
	print(len(matched))
	for match in matched:
		print(match[0] + 1, match[1] + 1)
else:
	print(0)


import sys
from math import *

def minp():
	return sys.stdin.readline().strip()

def mint():
	return int(minp())

def mints():
	return list(map(int, minp().split()))
n = mint()
l = [[] for i in range(27)]
r = [[] for i in range(27)]
s = minp()
for i in range(n):
	if s[i] == '?':
		l[26].append(i)
	else:
		l[ord(s[i]) - ord('a')].append(i)
s = minp()
for i in range(n):
	if s[i] == '?':
		r[26].append(i)
	else:
		r[ord(s[i]) - ord('a')].append(i)
res = [0] * n
rp = 0
best = 0
for i in range(26):
	for j in range(min(len(l[i]), len(r[i]))):
		res[rp] = (l[i].pop() + 1, r[i].pop() + 1)
		rp += 1
		best += 1
for i in range(26):
	while len(l[i]) > 0 and len(r[26]) > 0:
		res[rp] = (l[i].pop() + 1, r[26].pop() + 1)
		rp += 1
		best += 1
for i in range(26):
	while len(r[i]) > 0 and len(l[26]) > 0:
		res[rp] = (l[26].pop() + 1, r[i].pop() + 1)
		rp += 1
		best += 1
i = 0
j = 0
while i < 27 or j < 27:
	if i < 27 and len(l[i]) == 0:
		i += 1
	elif j < 27 and len(r[j]) == 0:
		j += 1
	elif i == j or i == 26 or j == 26:
		best += 1
		res[rp] = (l[i].pop() + 1, r[j].pop() + 1)
		rp += 1
	else:
		l[i].pop() + 1
		r[j].pop() + 1
print(best)
for i in range(rp):
	print(*res[i])


n = int(input())
l = input().strip()
r = input().strip()
cntl = [[] for x in range(27)]
cntr = [[] for x in range(27)]
for i in range(n):
	if l[i] == '?':
		cntl[26].append(i)
	else:
		cntl[ord(l[i]) - ord('a')].append(i)
for i in range(n):
	if r[i] == '?':
		cntr[26].append(i)
	else:
		cntr[ord(r[i]) - ord('a')].append(i)
ovrL = []
ovrR = []
r = []
for i in range(26):
	to = min(len(cntl[i]), len(cntr[i]))
	for j in range(min(len(cntl[i]), len(cntr[i]))):
		r.append((cntl[i][j], cntr[i][j]))
	ovrL[len(ovrL):] = cntl[i][to:]
	ovrR[len(ovrR):] = cntr[i][to:]
for i in range(min(len(ovrL), len(cntr[26]))):
	r.append((ovrL[i], cntr[26].pop()))
for i in range(min(len(ovrR), len(cntl[26]))):
	r.append((cntl[26].pop(), ovrR[i]))
for i in range(min(len(cntl[26]), len(cntr[26]))):
	r.append((cntl[26][i], cntr[26][i]))
r = [str(a + 1) + ' ' + str(b + 1) for (a, b) in r]
print(len(r))
print('\n'.join(r))


n = int(input())
lq = []
rq = []
l = [[] for g in range(26)]
r = [[] for g in range(26)]
i = input()
for f in range(n):
	if i[f] == '?':
		lq.append(f + 1)
	else:
		l[ord(i[f]) - ord('a')].append(f + 1)
i = input()
for f in range(n):
	if i[f] == '?':
		rq.append(f + 1)
	else:
		r[ord(i[f]) - ord('a')].append(f + 1)
ans = []

def merge(a, b):
	m = min(len(a), len(b))
	for j in range(m):
		ans.append((a[j], b[j]))
	del a[:m]
	del b[:m]
for i in range(26):
	merge(l[i], r[i])
	if len(l[i]):
		merge(l[i], rq)
	if len(r[i]):
		merge(lq, r[i])
merge(lq, rq)
print(len(ans))
for x in ans:
	print(x[0], x[1])


from __future__ import print_function
import os, sys, atexit
if sys.version_info[0] < 3:
	range = xrange
	from cStringIO import StringIO as BytesIO
	sys.stdout = BytesIO()
else:
	from io import BytesIO
	sys.stdout = BytesIO()
	_write = sys.stdout.write
	sys.stdout.write = lambda s: _write(s.encode())
atexit.register(lambda : os.write(1, sys.stdout.getvalue()))
input = BytesIO(os.read(0, os.fstat(0).st_size)).readline
n = int(input())
L = input()
R = input()
Lcount = [[] for _ in range(200)]
Rcount = [[] for _ in range(200)]
for (i, l) in enumerate(L):
	Lcount[l].append(i)
for (i, r) in enumerate(R):
	Rcount[r].append(i)
out = []
for c in range(ord('a'), ord('z') + 1):
	Ltmp = Lcount[c]
	Rtmp = Rcount[c]
	while Ltmp and Rtmp:
		i = Ltmp.pop()
		j = Rtmp.pop()
		out.append((i + 1, j + 1))
Ltmp = Lcount[ord('?')]
for c in range(ord('a'), ord('z') + 1):
	Rtmp = Rcount[c]
	while Ltmp and Rtmp:
		i = Ltmp.pop()
		j = Rtmp.pop()
		out.append((i + 1, j + 1))
Rtmp = Rcount[ord('?')]
for c in range(ord('a'), ord('z') + 1):
	Ltmp = Lcount[c]
	while Ltmp and Rtmp:
		i = Ltmp.pop()
		j = Rtmp.pop()
		out.append((i + 1, j + 1))
Ltmp = Lcount[ord('?')]
Rtmp = Rcount[ord('?')]
while Ltmp and Rtmp:
	i = Ltmp.pop()
	j = Rtmp.pop()
	out.append((i + 1, j + 1))
print(len(out))
for (x, y) in out:
	sys.stdout.write(str(x))
	sys.stdout.write(' ')
	sys.stdout.write(str(y))
	sys.stdout.write('\n')


n = int(input())
l = input()
r = input()
dl = dict()
dr = dict()
bs = ord('a')
for i in range(0, 26):
	idx = chr(bs + i)
	dl[idx] = []
	dr[idx] = []
dl['?'] = []
dr['?'] = []
for i in range(n):
	dl[l[i]].append(i)
	dr[r[i]].append(i)
pairs = []
co = 0
for i in dl:
	while dl[i] and dr[i]:
		pairs.append((1 + dl[i].pop(), 1 + dr[i].pop()))
		co += 1
	while dl[i] and dr['?']:
		pairs.append((1 + dl[i].pop(), 1 + dr['?'].pop()))
		co += 1
	while dl['?'] and dr[i]:
		pairs.append((1 + dl['?'].pop(), 1 + dr[i].pop()))
		co += 1
print(co)
for (i, j) in pairs:
	print(i, j)


N = int(input())
L = input()
R = input()
left = {}
right = {}
for i in range(26):
	left[chr(97 + i)] = []
	right[chr(97 + i)] = []
left['?'] = []
right['?'] = []
ans = []
for i in range(N):
	letter = L[i]
	left[letter].append(i + 1)
	letter = R[i]
	right[letter].append(i + 1)
res = 0
for i in range(26):
	letter = chr(97 + i)
	while left[letter] and right[letter]:
		p1 = left[letter].pop()
		p2 = right[letter].pop()
		res += 1
		ans.append([p1, p2])
if left['?']:
	for i in range(26):
		letter = chr(97 + i)
		while left['?'] and right[letter]:
			p1 = left['?'].pop()
			p2 = right[letter].pop()
			res += 1
			ans.append([p1, p2])
if right['?']:
	for i in range(26):
		letter = chr(97 + i)
		while right['?'] and left[letter]:
			p2 = right['?'].pop()
			p1 = left[letter].pop()
			res += 1
			ans.append([p1, p2])
while right['?'] and left['?']:
	p2 = right['?'].pop()
	p1 = left['?'].pop()
	res += 1
	ans.append([p1, p2])
print(res)
for pair in ans:
	print(pair[0], pair[1])


import re, math, decimal, bisect

def read():
	return input().strip()

def iread():
	return int(input().strip())

def viread():
	return [int(_) for _ in input().strip().split()]
n = iread()
s1 = read()
s2 = read()
lb = {}
rb = {}
for c in 'abcdefghijklmnopqrstuvwxyz?':
	lb[c] = []
	rb[c] = []
for i in range(n):
	lb[s1[i]].append(i + 1)
	rb[s2[i]].append(i + 1)
pairs = []
for c in 'abcdefghijklmnopqrstuvwxyz':
	if len(lb[c]) >= len(rb[c]):
		for i in range(len(rb[c])):
			pairs.append([lb[c][i], rb[c][i]])
		lb[c] = lb[c][len(rb[c]):]
		rb[c] = []
	elif len(lb[c]) < len(rb[c]):
		for i in range(len(lb[c])):
			pairs.append([lb[c][i], rb[c][i]])
		rb[c] = rb[c][len(lb[c]):]
		lb[c] = []
	if type(rb[c]) is int:
		rb[c] = [rb[c]]
	if type(lb[c]) is int:
		lb[c] = [lb[c]]
for c in 'abcdefghijklmnopqrstuvwxyz?':
	if len(lb[c]) >= len(rb['?']):
		for i in range(len(rb['?'])):
			pairs.append([lb[c][i], rb['?'][i]])
		lb[c] = lb[c][len(rb['?']):]
		rb['?'] = []
	elif len(lb[c]) < len(rb['?']):
		for i in range(len(lb[c])):
			pairs.append([lb[c][i], rb['?'][i]])
		rb['?'] = rb['?'][len(lb[c]):]
		lb[c] = []
	if type(rb['?']) is int:
		rb['?'] = [rb['?']]
	if type(lb[c]) is int:
		lb[c] = [lb[c]]
for c in 'abcdefghijklmnopqrstuvwxyz?':
	if len(lb['?']) >= len(rb[c]):
		for i in range(len(rb[c])):
			pairs.append([lb['?'][i], rb[c][i]])
		lb['?'] = lb['?'][len(rb[c]):]
		rb[c] = []
	elif len(lb['?']) < len(rb[c]):
		for i in range(len(lb['?'])):
			pairs.append([lb['?'][i], rb[c][i]])
		rb[c] = rb[c][len(lb['?']):]
		lb['?'] = []
	if type(rb[c]) is int:
		rb[c] = [rb[c]]
	if type(lb['?']) is int:
		lb['?'] = [lb['?']]
print(len(pairs))
for p in pairs:
	print('{} {}'.format(p[0], p[1]))


from collections import defaultdict
boots = int(input())
l = input()
r = input()
all_indexes = set(range(1, boots + 1))
l_boots = defaultdict(list)
for (index, item) in enumerate(l):
	l_boots[item].append(index + 1)
r_boots = defaultdict(list)
for (index, item) in enumerate(r):
	r_boots[item].append(index + 1)
res = []
for (color, indexes) in l_boots.items():
	if color != '?' and color in r_boots:
		for j in range(min(len(l_boots[color]), len(r_boots[color]))):
			res.append((l_boots[color][j], r_boots[color][j]))
r_indexes = list(all_indexes - set([x[1] for x in res]))
r_indexes.sort(key=lambda x: r[x - 1], reverse=True)
for i in range(len(l_boots.get('?', []))):
	res.append((l_boots['?'][i], r_indexes[i]))
l_indexes = list(all_indexes - set([x[0] for x in res]))
r_set = set([x[1] for x in res])
k = 0
for i in range(len(r_boots.get('?', []))):
	if r_boots['?'][i] not in r_set:
		res.append((l_indexes[k], r_boots['?'][i]))
		k += 1
print(len(res))
for (x, y) in res:
	print(x, y)


n = int(input())
l = list(input())
r = list(input())
(d1, d2) = ({}, {})
for (j, i) in enumerate(l, 1):
	if i not in d1:
		d1[i] = []
	d1[i].append(j)
for (j, i) in enumerate(r, 1):
	if i not in d2:
		d2[i] = []
	d2[i].append(j)
res = []
for i in range(26):
	b = chr(i + ord('a'))
	if b not in d1:
		d1[b] = []
	if b not in d2:
		d2[b] = []
	while d1[b] and d2[b]:
		res.append([d1[b].pop(), d2[b].pop()])
if '?' not in d1:
	d1['?'] = []
if '?' not in d2:
	d2['?'] = []
p1 = 0
while p1 < 26:
	b = chr(p1 + ord('a'))
	while d1['?'] and d2[b]:
		res.append([d1['?'].pop(), d2[b].pop()])
	p1 += 1
p1 = 0
while p1 < 26:
	b = chr(p1 + ord('a'))
	while d2['?'] and d1[b]:
		res.append([d1[b].pop(), d2['?'].pop()])
	p1 += 1
while d1['?'] and d2['?']:
	res.append([d1['?'].pop(), d2['?'].pop()])
k = min(len(res), n)
print(k)
for i in range(k):
	print(*res[i])


import sys
from collections import defaultdict, deque
from string import ascii_lowercase

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

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

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7
N = INT()
L = input()
R = input()
C1 = defaultdict(deque)
for i in range(N):
	C1[L[i]].append(i + 1)
C2 = defaultdict(deque)
for i in range(N):
	C2[R[i]].append(i + 1)
ans = []
for s in ascii_lowercase:
	while len(C1[s]) and len(C2[s]):
		a = C1[s].popleft()
		b = C2[s].popleft()
		ans.append((a, b))
for s in ascii_lowercase:
	while len(C1['?']) and len(C2[s]):
		a = C1['?'].popleft()
		b = C2[s].popleft()
		ans.append((a, b))
for s in ascii_lowercase:
	while len(C2['?']) and len(C1[s]):
		b = C2['?'].popleft()
		a = C1[s].popleft()
		ans.append((a, b))
while len(C1['?']) and len(C2['?']):
	a = C1['?'].popleft()
	b = C2['?'].popleft()
	ans.append((a, b))
print(len(ans))
for v in ans:
	print(*v)


from collections import defaultdict
from collections import Counter

def main():
	n = int(input())
	a = input()
	b = input()
	letter_dict = defaultdict(list)
	letter_counter = Counter(b)
	for (i, letter) in enumerate(b):
		letter_dict[letter].append(i + 1)
	ret = []
	for i in range(n):
		if a[i] != '?':
			if letter_counter[a[i]]:
				letter_counter[a[i]] -= 1
				index = letter_dict[a[i]].pop()
				ret.append([i + 1, index])
			elif letter_counter['?']:
				letter_counter['?'] -= 1
				index = letter_dict['?'].pop()
				ret.append([i + 1, index])
	not_use = []
	for (k, v) in letter_dict.items():
		not_use.extend(v)
	for i in range(n):
		if a[i] == '?':
			ret.append([i + 1, not_use.pop()])
	print(len(ret))
	for i in range(len(ret)):
		print(*ret[i])
main()


import atexit
import io
import sys
_I_B = sys.stdin.read().splitlines()
input = iter(_I_B).__next__
_O_B = io.StringIO()
sys.stdout = _O_B

@atexit.register
def write():
	sys.__stdout__.write(_O_B.getvalue())
n = int(input())
l = input()
r = input()
dl = dict()
dr = dict()
bs = ord('a')
for i in range(0, 26):
	idx = chr(bs + i)
	dl[idx] = []
	dr[idx] = []
dl['?'] = []
dr['?'] = []
for i in range(n):
	dl[l[i]].append(i)
	dr[r[i]].append(i)
pairs = []
co = 0
for i in dl:
	while dl[i] and dr[i]:
		pairs.append((1 + dl[i].pop(), 1 + dr[i].pop()))
		co += 1
	while dl[i] and dr['?']:
		pairs.append((1 + dl[i].pop(), 1 + dr['?'].pop()))
		co += 1
	while dl['?'] and dr[i]:
		pairs.append((1 + dl['?'].pop(), 1 + dr[i].pop()))
		co += 1
print(co)
for (i, j) in pairs:
	print(i, j)


import os
import sys
from io import BytesIO, IOBase
from collections import defaultdict

def main():
	n = int(input())
	a = list(input())
	b = list(input())
	aa = defaultdict(lambda : [])
	bb = defaultdict(lambda : [])
	for x in range(n):
		aa[a[x]].append(x + 1)
	for x in range(n):
		bb[b[x]].append(x + 1)
	ans = []
	for x in aa.keys():
		if x in bb.keys() and x != '?':
			for z in range(min(aa[x].__len__(), bb[x].__len__())):
				ans.append([aa[x][len(aa[x]) - 1], bb[x][len(bb[x]) - 1]])
				aa[x].pop()
				bb[x].pop()
	for x in bb.keys():
		if x != '?':
			for z in range(min(aa['?'].__len__(), bb[x].__len__())):
				ans.append([aa['?'][len(aa[x]) - 1], bb[x][len(bb[x]) - 1]])
				aa['?'].pop()
				bb[x].pop()
	for x in aa.keys():
		if x != '?':
			for z in range(min(aa[x].__len__(), bb['?'].__len__())):
				ans.append([aa[x][len(aa[x]) - 1], bb['?'][len(bb[x]) - 1]])
				aa[x].pop()
				bb['?'].pop()
	for x in bb.keys():
		if x == '?':
			for z in range(min(aa['?'].__len__(), bb[x].__len__())):
				ans.append([aa['?'][len(aa[x]) - 1], bb[x][len(bb[x]) - 1]])
				aa[x].pop()
				bb[x].pop()
	print(len(ans))
	for x in ans:
		print(*x)
BUFSIZE = 8192

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')
main()


def fastio():
	import sys
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
MOD = 10 ** 9 + 7
I = lambda : list(map(int, input().split()))
from collections import defaultdict as dd
(n,) = I()
l = input()
r = input()
ans = 0
res = []
d1 = {}
d2 = {}
s = 'abcdefghijklmnopqrstuvwxyz'
d1 = dd(list)
d2 = dd(list)
for (i, j) in enumerate(l):
	d1[j].append(i + 1)
for (i, j) in enumerate(r):
	d2[j].append(i + 1)
du1 = []
du2 = []
for i in s:
	m = min(len(d1[i]), len(d2[i]))
	res += list(zip(d1[i], d2[i]))
	du1 += d1[i][m:]
	du2 += d2[i][m:]
res += list(zip(du1, d2['?']))
du1 = d2['?'][min(len(du1), len(d2['?'])):]
res += list(zip(d1['?'], du2))
du2 = d1['?'][min(len(du2), len(d1['?'])):]
res += list(zip(du2, du1))
print(len(res))
for i in res:
	print(*i)


def charToInt(char):
	if char == '?':
		return 20000
	else:
		return ord(char)
s_l = int(input())
ls = [(charToInt(val), idx) for (idx, val) in enumerate(input())]
rs = [(charToInt(val), idx) for (idx, val) in enumerate(input())]
sls = sorted(ls, key=lambda val: val[0])
srs = sorted(rs, key=lambda val: val[0])
anc = 0
an = []
li = 0
ri = 0
while len(sls) > li and len(srs) > ri:
	l = sls[li]
	r = srs[ri]
	if l[0] == r[0]:
		anc += 1
		an.append([l[1] + 1, r[1] + 1])
		li += 1
		ri += 1
	elif l[0] < r[0]:
		if srs[len(srs) - 1][0] == 20000:
			l_q = srs.pop(len(srs) - 1)
			anc += 1
			an.append([l[1] + 1, l_q[1] + 1])
			li += 1
		else:
			li += 1
	elif sls[len(sls) - 1][0] == 20000:
		l_q = sls.pop(len(sls) - 1)
		anc += 1
		an.append([l_q[1] + 1, r[1] + 1])
		ri += 1
	else:
		ri += 1
print(anc)
for i in an:
	print(*i, sep=' ')


from collections import defaultdict
import sys
input = sys.stdin.readline
n = int(input())
a = input()
b = input()
oa = defaultdict(set)
ob = defaultdict(set)
for i in range(n):
	oa[a[i]].add(i)
	ob[b[i]].add(i)
pairs = []
for i in range(n):
	if a[i] == '?':
		continue
	if len(ob[a[i]]) == 0:
		if len(ob['?']):
			pairs.append((i, ob['?'].pop()))
		continue
	pairs.append((i, ob[a[i]].pop()))
rest = set()
for item in ob:
	rest.update(ob[item])
for i in range(n):
	if a[i] != '?':
		continue
	pairs.append((i, rest.pop()))
print(len(pairs))
for p in pairs:
	print(p[0] + 1, p[1] + 1)


def replaceX(s, i):
	s = s[0:i] + 'X' + s[i + 1:]
	return s

def match(c1, c2):
	if c1 == c2 or (c1 == '?' and c2 != 'X') or (c2 == '?' and c1 != 'X'):
		return True
	else:
		return False
n = int(input())
s1 = input()
s2 = input()
a1 = [[] for i in range(0, 27)]
a2 = [[] for i in range(0, 27)]
sol = []
for i in range(0, n):
	if s1[i] != '?':
		a1[ord(s1[i]) - 96].append(i)
	else:
		a1[0].append(i)
	if s2[i] != '?':
		a2[ord(s2[i]) - 96].append(i)
	else:
		a2[0].append(i)
for i in range(1, 27):
	while len(a1[i]) != 0 and len(a2[i]) != 0:
		sol.append((a1[i][-1] + 1, a2[i][-1] + 1))
		a1[i].pop()
		a2[i].pop()
for i in range(1, 27):
	while len(a1[0]) != 0 and len(a2[i]) != 0:
		sol.append((a1[0][-1] + 1, a2[i][-1] + 1))
		a1[0].pop()
		a2[i].pop()
	while len(a2[0]) != 0 and len(a1[i]) != 0:
		sol.append((a1[i][-1] + 1, a2[0][-1] + 1))
		a2[0].pop()
		a1[i].pop()
while len(a1[0]) != 0 and len(a2[0]) != 0:
	sol.append((a1[0][-1] + 1, a2[0][-1] + 1))
	a1[0].pop()
	a2[0].pop()
print(len(sol))
for ele in sol:
	print(ele[0], ele[1])


def getItemCount(Arr):
	cnt = {}
	for (idx, ele) in enumerate(Arr):
		if ele in cnt:
			cnt[ele].append(idx)
		else:
			cnt[ele] = [idx]
	return cnt

def pairBoots(L_cnt, R_cnt):
	pairs = []
	indefinite = '?'
	for l_boot in L_cnt:
		if l_boot != indefinite and l_boot in R_cnt:
			matching = min(len(R_cnt[l_boot]), len(L_cnt[l_boot]))
			for i in range(0, matching):
				pairs.append((L_cnt[l_boot][i], R_cnt[l_boot][i]))
			R_cnt[l_boot] = R_cnt[l_boot][matching:]
			L_cnt[l_boot] = L_cnt[l_boot][matching:]
	if indefinite in L_cnt:
		for (r_boot, r_indexes) in R_cnt.items():
			if len(L_cnt[indefinite]) != 0:
				if r_boot != indefinite:
					matching = min(len(L_cnt[indefinite]), len(r_indexes))
					for i in range(0, matching):
						pairs.append((L_cnt[indefinite][i], R_cnt[r_boot][i]))
					R_cnt[r_boot] = R_cnt[r_boot][matching:]
					L_cnt[indefinite] = L_cnt[indefinite][matching:]
			else:
				break
	if indefinite in R_cnt:
		for (l_boot, l_indexes) in L_cnt.items():
			if len(R_cnt[indefinite]) != 0:
				if l_boot != indefinite:
					matching = min(len(R_cnt[indefinite]), len(l_indexes))
					for i in range(matching):
						pairs.append((L_cnt[l_boot][i], R_cnt[indefinite][i]))
					L_cnt[l_boot] = L_cnt[l_boot][matching:]
					R_cnt[indefinite] = R_cnt[indefinite][matching:]
			else:
				break
	if indefinite in L_cnt and indefinite in R_cnt:
		matching = min(len(L_cnt[indefinite]), len(R_cnt[indefinite]))
		for i in range(matching):
			pairs.append((L_cnt[indefinite][i], R_cnt[indefinite][i]))
		R_cnt[indefinite] = R_cnt[indefinite][matching:]
		L_cnt[indefinite] = L_cnt[indefinite][matching:]
	return pairs
n = int(input().rstrip())
L = input().rstrip()
R = input().rstrip()
L_cnt = getItemCount(L)
R_cnt = getItemCount(R)
pairs = pairBoots(L_cnt, R_cnt)
print(len(pairs))
for (a, b) in pairs:
	print(a + 1, b + 1)


from collections import defaultdict
n = int(input())
s1 = input()
s2 = input()
hash1 = defaultdict(list)
hash2 = defaultdict(list)
ans = []
baga1 = set()
baga2 = set()
posn1 = []
posn2 = []
for i in range(n):
	hash1[s1[i]].append(i)
	hash2[s2[i]].append(i)
	if s1[i] == '?':
		posn1.append(i)
		baga1.add(i)
	if s2[i] == '?':
		posn2.append(i)
		baga2.add(i)
for i in hash1.keys():
	if i != '?':
		z1 = len(hash1[i])
		z2 = len(hash2[i])
		k = min(z1, z2)
		for j in range(k):
			ans.append([hash1[i][j], hash2[i][j]])
			baga1.add(hash1[i][j])
			baga2.add(hash2[i][j])
flip1 = []
flip2 = []
for i in range(n):
	if i not in baga1:
		flip1.append(i)
	if i not in baga2:
		flip2.append(i)
(count1, count2) = (0, 0)
if len(posn1) >= 1:
	z = min(len(posn1), len(flip2))
	for i in range(z):
		ans.append([posn1[i], flip2[i]])
		count1 += 1
if len(posn2) >= 1:
	z = min(len(posn2), len(flip1))
	for i in range(z):
		ans.append([flip1[i], posn2[i]])
		count2 += 1
if len(posn2) >= 1 and len(posn1) >= 1:
	z1 = len(posn1) - count1
	z2 = len(posn2) - count2
	if z1 > 0 and z2 > 0:
		posn1.reverse()
		posn2.reverse()
		k1 = min(z2, z1)
		for i in range(k1):
			ans.append([posn1[i], posn2[i]])
print(len(ans))
for i in ans:
	print(i[0] + 1, i[1] + 1)


bl = {}
br = {}
num = int(input())
l = input()
r = input()
for i in range(26):
	ab = chr(97 + i)
	bl[ab] = []
	br[ab] = []
bl['?'] = []
br['?'] = []
for i in range(num):
	bl[l[i]].append(i)
	br[r[i]].append(i)
pairs = []
co = 0
for i in bl:
	while bl[i] and br[i]:
		pairs.append((1 + bl[i].pop(), 1 + br[i].pop()))
		co += 1
	while bl[i] and br['?']:
		pairs.append((1 + bl[i].pop(), 1 + br['?'].pop()))
		co += 1
	while bl['?'] and br[i]:
		pairs.append((1 + bl['?'].pop(), 1 + br[i].pop()))
		co += 1
print(co)
for (i, j) in pairs:
	print(i, j)


n = int(input())
l = {}
r = {}
num = 1
for i in input():
	if i not in l:
		l[i] = [num]
	else:
		l[i].append(num)
	num += 1
num = 1
for i in input():
	if i not in r:
		r[i] = [num]
	else:
		r[i].append(num)
	num += 1
ab = []
for i in l.keys():
	if i == '?':
		continue
	if i in l and i in r:
		z = min(len(l[i]), len(r[i]))
		for j in range(z):
			ab.append((l[i][j], r[i][j]))
		if len(l[i]) > len(r[i]):
			r[i] = []
			l[i] = l[i][z:]
		elif len(l[i]) == len(r[i]):
			r[i] = []
			l[i] = []
		else:
			r[i] = r[i][z:]
			l[i] = []
	if i in l and '?' in r:
		z = min(len(l[i]), len(r['?']))
		for j in range(z):
			ab.append((l[i][j], r['?'][j]))
		if len(l[i]) > len(r['?']):
			r['?'] = []
			l[i] = l[i][z:]
		elif len(l[i]) == len(r['?']):
			r['?'] = []
			l[i] = []
		else:
			r['?'] = r['?'][z:]
			l[i] = []
for i in r.keys():
	if i == '?':
		continue
	if '?' in l and i in r:
		z = min(len(l['?']), len(r[i]))
		for j in range(z):
			ab.append((l['?'][j], r[i][j]))
		if len(l['?']) > len(r[i]):
			r[i] = []
			l['?'] = l['?'][z:]
		elif len(l['?']) == len(r[i]):
			r[i] = []
			l['?'] = []
		else:
			r[i] = r[i][z:]
			l['?'] = []
if '?' in l and '?' in r:
	z = min(len(l['?']), len(r['?']))
	for j in range(z):
		ab.append((l['?'][j], r['?'][j]))
	if len(l['?']) > len(r['?']):
		r['?'] = []
		l['?'] = l['?'][z:]
	elif len(l['?']) == len(r['?']):
		r['?'] = []
		l['?'] = []
	else:
		r['?'] = r['?'][z:]
		l['?'] = []
print(len(ab))
for i in ab:
	print(i[0], i[1])


from collections import Counter

def make_dict(s):
	d = {}
	for (i, c) in enumerate(s):
		try:
			d[c].append(i)
		except:
			d[c] = [i]
	return d
n = input()
first = input()
second = input()
q1 = first.count('?')
q2 = second.count('?')
first = list(first)
second = list(second)
c1 = make_dict(first)
c2 = make_dict(second)
c1['?'] = []
c2['?'] = []
pairs = []
for k in c1:
	try:
		c2[k]
		m = min(len(c1[k]), len(c2[k]))
		for j in range(m):
			i1 = c1[k][j]
			i2 = c2[k][j]
			pairs.append((i1, i2))
			first[i1] = 0
			second[i2] = 0
		c1[k] = c1[k][j:]
		c2[k] = c2[k][j:]
	except:
		pass
QUESTION = 0
AVAILABLE = 1
one = [[], []]
two = [[], []]
oai = 0
oqi = 0
tai = 0
tqi = 0
for i in range(len(first)):
	if first[i] == '?':
		one[QUESTION].append(i)
	elif first[i] != 0:
		one[AVAILABLE].append(i)
	if second[i] == '?':
		two[QUESTION].append(i)
	elif second[i] != 0:
		two[AVAILABLE].append(i)
for i in range(min(len(one[QUESTION]), len(two[AVAILABLE]) + len(two[QUESTION]))):
	if tai < len(two[AVAILABLE]):
		pairs.append((one[QUESTION][oqi], two[AVAILABLE][tai]))
		tai += 1
	elif tqi < len(two[QUESTION]):
		pairs.append((one[QUESTION][oqi], two[QUESTION][tqi]))
		tqi += 1
	else:
		break
	oqi += 1
for i in range(min(len(two[QUESTION]), len(one[AVAILABLE]) + len(one[QUESTION]))):
	if oai < len(one[AVAILABLE]):
		pairs.append((one[AVAILABLE][oai], two[QUESTION][tqi]))
		oai += 1
	elif oqi < len(one[QUESTION]):
		pairs.append((one[QUESTION][oqi], two[QUESTION][tqi]))
		oqi += 1
	else:
		break
	tqi += 1
print(len(pairs))
for p in pairs:
	print(p[0] + 1, p[1] + 1)


from collections import defaultdict, deque

def solve():
	N = int(input())
	boot1 = input().strip()
	boot2 = input().strip()
	freq1 = defaultdict(list)
	freq2 = defaultdict(list)
	for i in range(N):
		freq1[boot1[i]].append(i + 1)
		freq2[boot2[i]].append(i + 1)
	results = []
	Keys = list(freq1.keys())
	for x in Keys:
		if x != '?':
			while len(freq1[x]) > 0 and len(freq2[x]) > 0:
				a = freq1[x].pop()
				b = freq2[x].pop()
				results.append(str(a) + ' ' + str(b))
			while len(freq1[x]) > 0 and len(freq2['?']) > 0:
				a = freq1[x].pop()
				b = freq2['?'].pop()
				results.append(str(a) + ' ' + str(b))
			while len(freq2[x]) > 0 and len(freq1['?']) > 0:
				a = freq1['?'].pop()
				b = freq2[x].pop()
				results.append(str(a) + ' ' + str(b))
	Keys2 = list(freq2.keys())
	if len(freq1['?']) > 0:
		for x in Keys2:
			if x == '?':
				continue
			while freq1['?'] and freq2[x]:
				a = freq1['?'].pop()
				b = freq2[x].pop()
				results.append(str(a) + ' ' + str(b))
	if len(freq2['?']) > 0:
		for x in Keys:
			if x == '?':
				continue
			while freq2['?'] and freq1[x]:
				a = freq1[x].pop()
				b = freq2['?'].pop()
				results.append(str(a) + ' ' + str(b))
	while freq1['?'] and freq2['?']:
		a = freq1['?'].pop()
		b = freq2['?'].pop()
		results.append(str(a) + ' ' + str(b))
	cnt = len(results)
	print(cnt)
	print('\n'.join((k for k in results)))
solve()


from sys import stdin, stdout
from collections import Counter
alp_li = 'abcdefghijklmnopqrstuvwxyz'

def getctr(l, r):
	ctr = 0
	l = Counter(l)
	r = Counter(r)
	for i in alp_li:
		if i in l and i in r:
			i_t = min(l[i], r[i])
			ctr += i_t
			l[i] -= i_t
			r[i] -= i_t
	l_nv = 0
	l_v = 0
	r_nv = 0
	r_v = 0
	if '?' in l:
		l_v = l['?']
	if '?' in r:
		r_v = r['?']
	for i in alp_li:
		if i in l:
			l_nv += l[i]
		if i in r:
			r_nv += r[i]
	p1 = min(l_v, r_nv)
	l_v -= p1
	r_nv -= p1
	p2 = min(r_v, l_nv)
	r_v -= p2
	l_nv -= p2
	p3 = min(l_v, r_v)
	l_v -= p3
	r_v -= p3
	ctr += p1 + p2 + p3
	stdout.write(str(ctr) + '\n')
n = int(stdin.readline().strip())
l = stdin.readline().strip()
r = stdin.readline().strip()
ctr = 0
getctr(l, r)
l_arr = {}
r_arr = {}
for i in range(len(l)):
	if l[i] in l_arr:
		l_arr[l[i]].append(i + 1)
	else:
		l_arr[l[i]] = [i + 1]
for i in range(len(r)):
	if r[i] in r_arr:
		r_arr[r[i]].append(i + 1)
	else:
		r_arr[r[i]] = [i + 1]
l = Counter(l)
r = Counter(r)
for i in alp_li:
	if i in l and i in r:
		i_t = min(l[i], r[i])
		ctr += i_t
		l[i] -= i_t
		r[i] -= i_t
		for j in range(i_t):
			stdout.write(str(l_arr[i].pop()) + ' ' + str(r_arr[i].pop()) + '\n')
l_nv = 0
l_v = 0
r_nv = 0
r_v = 0
if '?' in l:
	l_v = l['?']
if '?' in r:
	r_v = r['?']
for i in alp_li:
	if i in l:
		l_nv += l[i]
	if i in r:
		r_nv += r[i]
p1 = min(l_v, r_nv)
l_v -= p1
r_nv -= p1
loop_ctr = 0
alp_ctr = 0
while loop_ctr < p1:
	if alp_li[alp_ctr] in r:
		if len(r_arr[alp_li[alp_ctr]]) > 0:
			stdout.write(str(l_arr['?'].pop()) + ' ' + str(r_arr[alp_li[alp_ctr]].pop()) + '\n')
			loop_ctr += 1
		else:
			alp_ctr += 1
	else:
		alp_ctr += 1
p2 = min(r_v, l_nv)
r_v -= p2
l_nv -= p2
loop_ctr = 0
alp_ctr = 0
while loop_ctr < p2:
	if alp_li[alp_ctr] in l:
		if len(l_arr[alp_li[alp_ctr]]) > 0:
			stdout.write(str(l_arr[alp_li[alp_ctr]].pop()) + ' ' + str(r_arr['?'].pop()) + '\n')
			loop_ctr += 1
		else:
			alp_ctr += 1
	else:
		alp_ctr += 1
p3 = min(l_v, r_v)
l_v -= p3
r_v -= p3
loop_ctr = 0
alp_ctr = 0
while loop_ctr < p3:
	stdout.write(str(l_arr['?'].pop()) + ' ' + str(r_arr['?'].pop()) + '\n')
	loop_ctr += 1


import sys
sys.setrecursionlimit(20000000)
input = sys.stdin.readline
n = int(input())
a = input()
b = input()
al = [[] for i in range(27)]
bl = [[] for i in range(27)]
for i in range(n):
	if a[i] == '?':
		al[26].append(i + 1)
	else:
		al[ord(a[i]) - ord('a')].append(i + 1)
	if b[i] == '?':
		bl[26].append(i + 1)
	else:
		bl[ord(b[i]) - ord('a')].append(i + 1)
ans = []
for i in range(26):
	while al[i] and bl[i]:
		ans.append([al[i].pop(), bl[i].pop()])
alz = []
blz = []
for i in range(26):
	while al[i]:
		alz.append(al[i].pop())
	while bl[i]:
		blz.append(bl[i].pop())
while al[26] and blz:
	ans.append([al[26].pop(), blz.pop()])
while bl[26] and alz:
	ans.append([alz.pop(), bl[26].pop()])
while al[26] and bl[26]:
	ans.append([al[26].pop(), bl[26].pop()])
print(len(ans))
for i in range(len(ans)):
	print(ans[i][0], ans[i][1])


from sys import stdin
from collections import defaultdict
input = stdin.readline
n = int(input())
l = input().rstrip()
r = input().rstrip()
ll = defaultdict(list)
rr = defaultdict(list)
for i in range(n):
	ll[l[i]].append(i)
	rr[r[i]].append(i)
ans = []
for k in ll:
	if k == '?':
		continue
	while len(ll[k]) > 0 and len(rr[k]) > 0:
		ans.append((ll[k].pop() + 1, rr[k].pop() + 1))
for k in ll:
	if len(rr['?']) == 0:
		break
	if k == '?':
		continue
	while len(ll[k]) > 0 and len(rr['?']) > 0:
		ans.append((ll[k].pop() + 1, rr['?'].pop() + 1))
for k in rr:
	if len(ll['?']) == 0:
		break
	if k == '?':
		continue
	while len(rr[k]) > 0 and len(ll['?']) > 0:
		ans.append((ll['?'].pop() + 1, rr[k].pop() + 1))
while len(rr['?']) > 0 and len(ll['?']) > 0:
	ans.append((ll['?'].pop() + 1, rr['?'].pop() + 1))
print(len(ans))
for i in ans:
	print(*i)


n = int(input())
s = input()
s1 = input()
d1 = {}
d2 = {}
for i in range(n):
	j = s[i]
	if j not in d1:
		d1[j] = []
	d1[j].append(i + 1)
	j = s1[i]
	if j not in d2:
		d2[j] = []
	d2[j].append(i + 1)
ans = []
ost1 = []
ost2 = []
ne1 = []
ne2 = []
for i in d1:
	if i == '?':
		ne1 = d1[i][:]
		continue
	if i not in d2:
		ost1 += d1[i]
		continue
	while d1[i] and d2[i]:
		ans.append((d1[i].pop(), d2[i].pop()))
	ost1 += d1[i]
	ost2 += d2[i]
for i in d2:
	if i != '?':
		ost2 += d2[i]
	else:
		ne2 = d2[i]
while ne1 and ost2:
	ans.append((ne1.pop(), ost2.pop()))
while ne2 and ost1:
	ans.append((ost1.pop(), ne2.pop()))
while ne1 and ne2:
	ans.append((ne1.pop(), ne2.pop()))
print(len(ans))
for i in ans:
	print(i[0], i[1])


from collections import defaultdict
n = int(input())
l = input()
r = input()
dl = defaultdict(list)
dr = defaultdict(list)
for i in range(n):
	dl[l[i]].append(i + 1)
for i in range(n):
	dr[r[i]].append(i + 1)
s = 'abcdefghijklmnopqrstuvwxyz'
ans = []
for i in s:
	while dl[i] and dr[i]:
		ans.append((dl[i].pop(), dr[i].pop()))
for i in s:
	while dl['?'] and dr[i]:
		ans.append((dl['?'].pop(), dr[i].pop()))
for i in s:
	while dr['?'] and dl[i]:
		ans.append((dl[i].pop(), dr['?'].pop()))
while dr['?'] and dl['?']:
	ans.append((dl['?'].pop(), dr['?'].pop()))
print(len(ans))
for i in ans:
	print(*i)


n = int(input())
s1 = input()
s2 = input()
val1 = {}
val2 = {}
for i in range(n):
	if s1[i] in val1.keys():
		val1[s1[i]] += [i + 1]
	else:
		val1[s1[i]] = [i + 1]
	if s2[i] in val2.keys():
		val2[s2[i]] += [i + 1]
	else:
		val2[s2[i]] = [i + 1]
ans = [[None, None]]
for elem in val1.keys():
	if elem != '?':
		if elem not in val2.keys():
			continue
		while val1[elem] and val2[elem]:
			ans.append((val1[elem].pop(-1), val2[elem].pop(-1)))
if '?' in val1:
	for elem in val2:
		if elem != '?':
			while val1['?'] and val2[elem]:
				ans.append((val1['?'].pop(-1), val2[elem].pop(-1)))
if '?' in val2:
	for elem in val1:
		if elem != '?':
			while val2['?'] and val1[elem]:
				ans.append((val1[elem].pop(-1), val2['?'].pop(-1)))
if '?' in val2 and '?' in val1:
	while val1['?'] and val2['?']:
		ans.append((val1['?'].pop(-1), val2['?'].pop(-1)))
ans.pop(0)
print(len(ans))
for elem in ans:
	print(elem[0], elem[1])


def mp():
	return map(int, input().split())
n = int(input())
il = input()
ir = input()
cnt = 0
ans = []
l = []
r = []
for i in range(n):
	l.append([il[i], i])
	r.append([ir[i], i])
l.sort()
r.sort()
y = dict()
for i in range(n):
	if l[i][0] == '?':
		continue
	ll = 0
	rr = n
	while ll + 1 < rr:
		mm = (ll + rr) // 2
		if r[mm][0] <= l[i][0]:
			ll = mm
		else:
			rr = mm
	if r[ll][0] == l[i][0] and r[ll][1] == -1:
		ll = y[r[ll][0]]
	if r[ll][0] == l[i][0] and r[ll][1] != -1:
		ans.append([l[i][1] + 1, r[ll][1] + 1])
		y[r[ll][0]] = ll - 1
		r[ll][1] = -1
		l[i][1] = -1
bl = []
br = []
zl = []
zr = []
for i in range(n):
	if l[i][0] != '?' and l[i][1] != -1:
		bl.append(l[i][1])
	if r[i][0] != '?' and r[i][1] != -1:
		br.append(r[i][1])
	if l[i][0] == '?':
		zl.append(l[i][1])
	if r[i][0] == '?':
		zr.append(r[i][1])
while min(len(bl), len(zr)) > 0:
	ans.append([bl[-1] + 1, zr[-1] + 1])
	bl.pop()
	zr.pop()
while min(len(br), len(zl)) > 0:
	ans.append([zl[-1] + 1, br[-1] + 1])
	br.pop()
	zl.pop()
while min(len(zl), len(zr)) > 0:
	ans.append([zl[-1] + 1, zr[-1] + 1])
	zl.pop()
	zr.pop()
print(len(ans))
for i in ans:
	print(i[0], i[1])


amount = int(input())
string_1 = input()
string_2 = input()
array_1 = [[] for i in range(27)]
array_2 = [[] for i in range(27)]
for i in range(len(string_1)):
	if string_1[i] == '?':
		array_1[26].append(i)
	else:
		array_1[ord(string_1[i]) - ord('a')].append(i)
	if string_2[i] == '?':
		array_2[26].append(i)
	else:
		array_2[ord(string_2[i]) - ord('a')].append(i)
ans = ''
counter = 0
for i in range(27):
	while len(array_1[i]) and len(array_2[i]):
		first = array_1[i].pop()
		second = array_2[i].pop()
		ans += str(first + 1) + ' ' + str(second + 1) + '\n'
		counter += 1
	if len(array_1[i]):
		while len(array_1[i]) and len(array_2[26]):
			first = array_1[i].pop()
			second = array_2[26].pop()
			ans += str(first + 1) + ' ' + str(second + 1) + '\n'
			counter += 1
	if len(array_2[i]):
		while len(array_2[i]) and len(array_1[26]):
			first = array_1[26].pop()
			second = array_2[i].pop()
			ans += str(first + 1) + ' ' + str(second + 1) + '\n'
			counter += 1
if len(array_1[26]):
	while len(array_1[26]) and len(array_2[26]):
		first = array_1[26].pop()
		second = array_2[26].pop()
		ans += str(first + 1) + ' ' + str(second + 1) + '\n'
		counter += 1
if len(array_2[26]):
	while len(array_2[26]) and len(array_1[26]):
		first = array_1[26].pop()
		second = array_2[26].pop()
		ans += str(first + 1) + ' ' + str(second + 1) + '\n'
		counter += 1
print(counter)
print(ans)


n = int(input())
s1 = list(input())
s2 = list(input())
dic1 = {}
dic2 = {}
index1 = index2 = 0
i = 1
reserve1 = []
reserve2 = []
for item in s1:
	if item not in dic1:
		dic1[item] = [i]
	else:
		dic1[item].append(i)
	i += 1
i = 1
answer = []
for item in s2:
	if item not in dic2:
		dic2[item] = [i]
	else:
		dic2[item].append(i)
	i += 1
for item in dic1:
	if item != '?' and item in dic2:
		len1 = len(dic1[item])
		len2 = len(dic2[item])
		if len1 <= len2:
			for i in range(len1):
				answer.append((dic1[item][i], dic2[item][i]))
			for i in range(len1, len2):
				reserve2.append(dic2[item][i])
		else:
			for i in range(len2):
				answer.append((dic1[item][i], dic2[item][i]))
			for i in range(len2, len1):
				reserve1.append(dic1[item][i])
	elif item != '?':
		for i in dic1[item]:
			reserve1.append(i)
for item in dic2:
	if item not in dic1:
		for i in dic2[item]:
			reserve2.append(i)
if '?' in dic1:
	for i in range(min(len(dic1['?']), len(reserve2))):
		answer.append((dic1['?'][i], reserve2[i]))
	if len(reserve2) < len(dic1['?']):
		for i in range(len(reserve2), len(dic1['?'])):
			reserve1.append(dic1['?'][i])
if '?' in dic2:
	for i in range(min(len(dic2['?']), len(reserve1))):
		answer.append((reserve1[i], dic2['?'][i]))
if len(answer) > 0:
	print(len(answer))
	for i in range(len(answer)):
		print(*answer[i])
else:
	print(0)


n = int(input())
s = input()
t = input()
s1 = []
t1 = []
for i in range(1, n + 1):
	s1.append([s[i - 1], i])
	t1.append([t[i - 1], i])
s1.sort()
t1.sort()
s2 = []
t2 = []
p = []
i = 0
j = 0
x = 0
while i < n and j < n:
	if s1[i][0] == t[j][0] == '?':
		s2.append(s1[i])
		t2.append(t1[j])
		i += 1
		j += 1
	elif s1[i][0] == '?':
		s2.append(s1[i])
		i += 1
	elif t1[j][0] == '?':
		t2.append(t1[j])
		j += 1
	elif s1[i][0] == t1[j][0]:
		p.append([s1[i][1], t1[j][1]])
		i += 1
		j += 1
		x += 1
	elif ord(s1[i][0]) < ord(t1[j][0]):
		s2.append(s1[i])
		i += 1
	else:
		t2.append(t1[j])
		j += 1
while i < n:
	s2.append(s1[i])
	i += 1
while j < n:
	t2.append(t1[j])
	j += 1
t2.reverse()
while len(t2) > 0 and t2[-1][0] == '?':
	p.append([s2[-1][1], t2[-1][1]])
	s2.pop()
	t2.pop()
	x += 1
s2.reverse()
while len(s2) > 0 and s2[-1][0] == '?':
	p.append([s2[-1][1], t2[-1][1]])
	s2.pop()
	t2.pop()
	x += 1
print(x)
for i in range(x):
	print(*p[i])


n = int(input())
s = input()
s1 = input()
d = {}
d1 = {}
for i in range(n):
	e = d.get(s[i], [])
	e.append(i)
	d[s[i]] = e
for i in range(n):
	e = d1.get(s1[i], [])
	e.append(i)
	d1[s1[i]] = e
k1 = len(d.get('?', [])) - 1
k2 = len(d1.get('?', [])) - 1
ans = []
for x in d:
	if x != '?':
		e = d1.get(x, [])
		v = min(len(e), len(d[x]))
		for i in range(v):
			ans.append(str(d[x][i] + 1) + ' ' + str(e[i] + 1))
		for i in range(v, len(e)):
			if k1 >= 0:
				ans.append(str(d['?'][k1] + 1) + ' ' + str(e[i] + 1))
				k1 -= 1
			else:
				break
		for i in range(v, len(d[x])):
			if k2 >= 0:
				ans.append(str(d[x][i] + 1) + ' ' + str(d1['?'][k2] + 1))
				k2 -= 1
			else:
				break
		if d1.get(x, 1) != 1:
			del d1[x]
for x in d1:
	if x != '?':
		for i in range(len(d1[x])):
			if k1 >= 0:
				ans.append(str(d['?'][k1] + 1) + ' ' + str(d1[x][i] + 1))
				k1 -= 1
			else:
				break
for i in range(min(k1, k2) + 1):
	ans.append(str(d['?'][i] + 1) + ' ' + str(d1['?'][i] + 1))
print(len(ans))
print('\n'.join(map(str, ans)))


def main():
	n = int(input())
	l = list(input())
	r = list(input())
	lleft = [0] * 26
	rleft = [0] * 26
	qsL = 0
	qsR = 0
	pairs = 0
	res = []
	usedL = [False] * n
	usedR = [False] * n
	for c in l:
		if c == '?':
			qsL += 1
		else:
			lleft[ord(c) - 97] += 1
	for c in r:
		if c == '?':
			qsR += 1
		else:
			rleft[ord(c) - 97] += 1
	for i in range(26):
		mn = min(lleft[i], rleft[i])
		pairs += mn
		lleft[i] -= mn
		rleft[i] -= mn
		lefts = []
		pos = -1
		while len(lefts) < mn:
			pos += 1
			if l[pos] == str(chr(97 + i)):
				usedL[pos] = True
				lefts.append(pos + 1)
		rights = []
		pos = -1
		while len(rights) < mn:
			pos += 1
			if r[pos] == str(chr(97 + i)):
				usedR[pos] = True
				rights.append(pos + 1)
		res.extend(zip(lefts, rights))
	for i in range(26):
		mn1 = min(lleft[i], qsR)
		if mn1 > 0:
			lleft[i] -= mn1
			qsR -= mn1
			pairs += mn1
			lefts = []
			pos = -1
			while len(lefts) < mn1:
				pos += 1
				if l[pos] == str(chr(97 + i)) and (not usedL[pos]):
					usedL[pos] = True
					lefts.append(pos + 1)
			rights = []
			pos = -1
			while len(rights) < mn1:
				pos += 1
				if r[pos] == '?' and (not usedR[pos]):
					usedR[pos] = True
					rights.append(pos + 1)
			res.extend(zip(lefts, rights))
		mn2 = min(rleft[i], qsL)
		if mn2 > 0:
			rleft[i] -= mn2
			qsL -= mn2
			pairs += mn2
			lefts = []
			pos = -1
			while len(lefts) < mn2:
				pos += 1
				if l[pos] == '?' and (not usedL[pos]):
					usedL[pos] = True
					lefts.append(pos + 1)
			rights = []
			pos = -1
			while len(rights) < mn2:
				pos += 1
				if r[pos] == str(chr(97 + i)) and (not usedR[pos]):
					usedR[pos] = True
					rights.append(pos + 1)
			res.extend(zip(lefts, rights))
	mn3 = min(qsL, qsR)
	if mn3 > 0:
		pairs += mn3
		lefts = []
		pos = -1
		while len(lefts) < mn3:
			pos += 1
			if l[pos] == '?' and (not usedL[pos]):
				usedL[pos] = True
				lefts.append(pos + 1)
		rights = []
		pos = -1
		while len(rights) < mn3:
			pos += 1
			if r[pos] == '?' and (not usedR[pos]):
				usedR[pos] = True
				rights.append(pos + 1)
		res.extend(zip(lefts, rights))
	print(pairs)
	for p in res:
		print(*p)
main()


n = int(input())
l = input()
r = input()
left_char_pos = dict()
right_char_pos = dict()
left_rem = 0
right_rem = 0
for i in range(n):
	li = l[i]
	ri = r[i]
	if li in left_char_pos:
		left_char_pos[li].append(i + 1)
	else:
		left_char_pos[li] = [i + 1]
	if li != '?':
		left_rem += 1
	if ri != '?':
		right_rem += 1
	if ri in right_char_pos:
		right_char_pos[ri].append(i + 1)
	else:
		right_char_pos[ri] = [i + 1]
all_chars = set(list(l) + list(r))
pairs_to_print = list()
for char in all_chars:
	if char != '?':
		if char in left_char_pos and char in right_char_pos:
			while len(left_char_pos[char]) > 0 and len(right_char_pos[char]) > 0:
				left_pos_to_pop = left_char_pos[char].pop()
				right_pos_to_pop = right_char_pos[char].pop()
				left_rem -= 1
				right_rem -= 1
				pairs_to_print.append('{} {}'.format(left_pos_to_pop, right_pos_to_pop))
left_pos_remaining = list()
right_pos_remaining = list()
for char in all_chars:
	if char != '?':
		if char in left_char_pos:
			left_pos_remaining.extend(left_char_pos[char])
		if char in right_char_pos:
			right_pos_remaining.extend(right_char_pos[char])
if '?' in left_char_pos:
	m1 = len(left_char_pos['?'])
else:
	m1 = 0
if '?' in right_char_pos:
	m2 = len(right_char_pos['?'])
else:
	m2 = 0
left_qm_match_pos = min(m1, right_rem)
right_qm_match_pos = min(m2, left_rem)
for _ in range(left_qm_match_pos):
	m1 -= 1
	right_match = right_pos_remaining.pop()
	left_match = left_char_pos['?'].pop()
	pairs_to_print.append('{} {}'.format(left_match, right_match))
for _ in range(right_qm_match_pos):
	m2 -= 1
	right_match = right_char_pos['?'].pop()
	left_match = left_pos_remaining.pop()
	pairs_to_print.append('{} {}'.format(left_match, right_match))
qm_qm_match = min(m1, m2)
for _ in range(qm_qm_match):
	left_match = left_char_pos['?'].pop()
	right_match = right_char_pos['?'].pop()
	pairs_to_print.append('{} {}'.format(left_match, right_match))
print(len(pairs_to_print))
for line in pairs_to_print:
	print(line)


n = int(input())
dict1 = {}
dict2 = {}
s1 = str(input())
s2 = str(input())
for i in range(n):
	try:
		dict1[s1[i]].append(i + 1)
	except:
		KeyError
		dict1[s1[i]] = [i + 1]
	try:
		dict2[s2[i]].append(i + 1)
	except:
		KeyError
		dict2[s2[i]] = [i + 1]
ansarr = []
ans = 0
flags1 = [0] * n
flags2 = [0] * n
for i in dict1.keys():
	if i != '?':
		count = 0
		try:
			while count < len(dict1[i]) and count < len(dict2[i]):
				ansarr.append(dict1[i][count])
				ansarr.append(dict2[i][count])
				ans += 1
				flags1[dict1[i][count] - 1] = 1
				flags2[dict2[i][count] - 1] = 1
				count += 1
		except:
			KeyError
try:
	count1 = 0
	index = 0
	while count1 < len(dict1['?']) and index < n:
		if flags2[index] == 0 and s2[index] != '?':
			ans += 1
			flags2[index] = 1
			flags1[dict1['?'][count1] - 1] = 1
			ansarr.append(dict1['?'][count1])
			ansarr.append(index + 1)
			count1 += 1
			index += 1
		else:
			index += 1
except:
	KeyError
try:
	count1 = 0
	index = 0
	while count1 < len(dict2['?']) and index < n:
		if flags1[index] == 0:
			ans += 1
			flags1[index] = 1
			flags2[dict2['?'][count1] - 1] = 1
			ansarr.append(index + 1)
			ansarr.append(dict2['?'][count1])
			count1 += 1
			index += 1
		else:
			index += 1
except:
	KeyError
print(ans)
print(*ansarr)


import string

def ind(x):
	sample = list(string.ascii_lowercase)
	if x == '?':
		return 26
	else:
		return sample.index(x)

def main():
	n = int(input())
	l = input()
	r = input()
	l_arr = [[] for x in range(27)]
	r_arr = [[] for x in range(27)]
	for x in range(n):
		l_arr[ind(l[x])].append(x + 1)
		r_arr[ind(r[x])].append(x + 1)
	count = 0
	pair = []
	for x in range(26):
		while len(l_arr[x]) != 0 and len(r_arr[x]) != 0:
			pair.append((l_arr[x][-1], r_arr[x][-1]))
			del l_arr[x][-1]
			del r_arr[x][-1]
			count += 1
	for x in range(26):
		while len(l_arr[-1]) != 0 and len(r_arr[x]) != 0:
			pair.append((l_arr[-1][-1], r_arr[x][-1]))
			del l_arr[-1][-1]
			del r_arr[x][-1]
			count += 1
	for x in range(26):
		while len(l_arr[x]) != 0 and len(r_arr[-1]) != 0:
			pair.append((l_arr[x][-1], r_arr[-1][-1]))
			del l_arr[x][-1]
			del r_arr[-1][-1]
			count += 1
	for x in range(26):
		while len(l_arr[-1]) != 0 and len(r_arr[-1]) != 0:
			pair.append((l_arr[-1][-1], r_arr[-1][-1]))
			del l_arr[-1][-1]
			del r_arr[-1][-1]
			count += 1
	print(count)
	for x in range(count):
		print(pair[x][0], end=' ')
		print(pair[x][1])
main()


n = int(input())
l = input()
r = input()
le = []
re = []
for i in range(26):
	le.append([])
	re.append([])
ql = []
qr = []
for i in range(len(l)):
	ind = ord(l[i]) - ord('a')
	if ind >= 0 and ind < 26:
		le[ind].append(i + 1)
	else:
		ql.append(i + 1)
for i in range(len(r)):
	ind = ord(r[i]) - ord('a')
	if ind >= 0 and ind < 26:
		re[ind].append(i + 1)
	else:
		qr.append(i + 1)
ans = 0
for i in range(26):
	ans += min(len(le[i]), len(re[i]))
ans += len(ql) + len(qr)
if ans >= n:
	ans = n
print(ans)
req = ans
f = False
for i in range(26):
	for j in range(min(len(le[i]), len(re[i]))):
		print(le[i][j], re[i][j])
		req -= 1
		if req == 0:
			f = True
	if f:
		break
if f:
	exit()
il = 0
ir = 0
for i in range(26):
	for j in range(min(len(le[i]), len(re[i])), len(le[i])):
		if ir >= len(qr):
			break
		print(le[i][j], qr[ir])
		ir += 1
		req -= 1
		if req == 0:
			f = True
			break
	for j in range(min(len(le[i]), len(re[i])), len(re[i])):
		if il >= len(ql):
			break
		print(ql[il], re[i][j])
		il += 1
		req -= 1
		if req == 0:
			f = True
			break
	if f:
		break
for i in range(max(il, ir), max(len(ql), len(qr))):
	print(ql[i], qr[i])
	req -= 1
	if req == 0:
		break


n = int(input())
l = input()
r = input()
rr = {'?': []}
ll = {'?': []}
for i in range(len(l)):
	if l[i] in ll:
		ll[l[i]].append(i + 1)
	else:
		ll[l[i]] = [i + 1]
	if r[i] in rr:
		rr[r[i]].append(i + 1)
	else:
		rr[r[i]] = [i + 1]
ans = []
for i in ll:
	if i == '?':
		continue
	elif i in rr:
		while len(ll[i]) > 0 and len(rr[i]) > 0:
			ans.append([ll[i].pop(), rr[i].pop()])
	if len(rr['?']) > 0:
		while len(ll[i]) > 0 and len(rr['?']) > 0:
			ans.append([ll[i].pop(), rr['?'].pop()])
while len(rr['?']) > 0:
	for i in ll:
		if len(rr['?']) <= 0:
			break
		while len(ll[i]) > 0:
			if len(rr['?']) <= 0:
				break
			ans.append([ll[i].pop(), rr['?'].pop()])
while len(ll['?']) > 0:
	for i in rr:
		if len(ll['?']) <= 0:
			break
		while len(rr[i]) > 0:
			if len(ll['?']) <= 0:
				break
			ans.append([ll['?'].pop(), rr[i].pop()])
print(len(ans))
for i in range(0, len(ans)):
	print(ans[i][0], ans[i][1])


n = int(input())
l = input()
r = input()
l_d = {}
l_q = []
for (i, c) in enumerate(l, 1):
	if c == '?':
		l_q.append(i)
		continue
	if c not in l_d:
		l_d[c] = []
	l_d[c].append(i)
r_d = {}
r_q = []
for (i, c) in enumerate(r, 1):
	if c == '?':
		r_q.append(i)
		continue
	if c not in r_d:
		r_d[c] = []
	r_d[c].append(i)
kk = set(l_d.keys()).intersection(set(r_d.keys()))
r = []
ost_r = []
ost_l = []
for k in kk:
	m = min(len(r_d[k]), len(l_d[k]))
	for i in range(m):
		r.append((l_d[k][i], r_d[k][i]))
	if len(l_d[k]) > m:
		ost_l.extend(l_d[k][m:])
	else:
		del l_d[k]
	if len(r_d[k]) > m:
		ost_r.extend(r_d[k][m:])
	else:
		del r_d[k]
for v in l_d.values():
	ost_l.extend(v)
for v in r_d.values():
	ost_r.extend(v)
m_l = min(len(l_q), len(ost_r))
r.extend(zip(l_q, ost_r))
m_r = min(len(r_q), len(ost_l))
r.extend(zip(ost_l, r_q))
r.extend(zip(l_q[m_l:], r_q[m_r:]))
print(len(r))
for rr in r:
	print(*rr)


from collections import defaultdict
n = int(input())
l = input()
r = input()
d_l = defaultdict(list)
d_r = defaultdict(list)
for i in range(n):
	d_l[l[i]].append(i + 1)
	d_r[r[i]].append(i + 1)
indices = []
for i in range(97, 97 + 26):
	c = chr(i)
	m = min(len(d_l[c]), len(d_r[c]))
	for _ in range(m):
		i1 = d_l[c].pop()
		i2 = d_r[c].pop()
		indices.append((i1, i2))
flag1 = True
flag2 = True
for i in range(97, 97 + 26):
	c = chr(i)
	tmp1 = len(d_l[c])
	tmp2 = len(d_r[c])
	for _ in range(tmp1):
		if d_r['?'] == []:
			flag1 = False
			break
		if flag1:
			i1 = d_l[c].pop()
			i2 = d_r['?'].pop()
			indices.append((i1, i2))
	for _ in range(tmp2):
		if d_l['?'] == []:
			flag2 = False
			break
		if flag2:
			i1 = d_r[c].pop()
			i2 = d_l['?'].pop()
			indices.append((i2, i1))
	if flag1 or flag2:
		continue
	else:
		break
m = min(len(d_l['?']), len(d_r['?']))
for _ in range(m):
	i1 = d_l['?'].pop()
	i2 = d_r['?'].pop()
	indices.append((i1, i2))
k = len(indices)
print(k)
for _ in range(k):
	print(*indices[_])


n = int(input())
(la, ra) = ({}, {})
(lq, rq) = ([], [])
s = input()
for i in range(len(s)):
	if s[i] != '?':
		if s[i] not in la:
			la[s[i]] = []
		la[s[i]].append(i)
	else:
		lq.append(i)
s = input()
for i in range(len(s)):
	if s[i] != '?':
		if s[i] not in ra:
			ra[s[i]] = []
		ra[s[i]].append(i)
	else:
		rq.append(i)
ansa = []
for i in la:
	if i in ra:
		while len(la[i]) and len(ra[i]):
			ansa.append((la[i].pop(), ra[i].pop()))
lr = []
for i in la:
	lr.extend(la[i])
rr = []
for i in ra:
	rr.extend(ra[i])
while len(lq) and len(rr):
	ansa.append((lq.pop(), rr.pop()))
while len(lr) and len(rq):
	ansa.append((lr.pop(), rq.pop()))
while len(lq) and len(rq):
	ansa.append((lq.pop(), rq.pop()))
print(len(ansa))
for (i, j) in ansa:
	print(i + 1, j + 1)


n = int(input())
leftShoes = ' ' + input()
rightShoes = ' ' + input()
leftCounts = {'a': [], 'b': [], 'c': [], 'd': [], 'e': [], 'f': [], 'g': [], 'h': [], 'i': [], 'j': [], 'k': [], 'l': [], 'm': [], 'n': [], 'o': [], 'p': [], 'q': [], 'r': [], 's': [], 't': [], 'u': [], 'v': [], 'w': [], 'x': [], 'y': [], 'z': [], '?': []}
rightCounts = {'a': [], 'b': [], 'c': [], 'd': [], 'e': [], 'f': [], 'g': [], 'h': [], 'i': [], 'j': [], 'k': [], 'l': [], 'm': [], 'n': [], 'o': [], 'p': [], 'q': [], 'r': [], 's': [], 't': [], 'u': [], 'v': [], 'w': [], 'x': [], 'y': [], 'z': [], '?': []}
pairCounts = {'a': [], 'b': [], 'c': [], 'd': [], 'e': [], 'f': [], 'g': [], 'h': [], 'i': [], 'j': [], 'k': [], 'l': [], 'm': [], 'n': [], 'o': [], 'p': [], 'q': [], 'r': [], 's': [], 't': [], 'u': [], 'v': [], 'w': [], 'x': [], 'y': [], 'z': []}
for index in range(1, len(leftShoes)):
	leftCounts[leftShoes[index]].append(index)
for index in range(1, len(rightShoes)):
	rightCounts[rightShoes[index]].append(index)
leftRemaining = []
rightRemaining = []
for shoe in pairCounts.keys():
	while len(leftCounts[shoe]) > 0 and len(rightCounts[shoe]) > 0:
		pairCounts[shoe].append((leftCounts[shoe].pop(), rightCounts[shoe].pop()))
	leftRemaining.extend(leftCounts[shoe])
	rightRemaining.extend(rightCounts[shoe])
while len(leftCounts['?']) > 0 and len(rightRemaining) > 0:
	pairCounts['a'].append((leftCounts['?'].pop(), rightRemaining.pop()))
while len(rightCounts['?']) > 0 and len(leftRemaining) > 0:
	pairCounts['a'].append((leftRemaining.pop(), rightCounts['?'].pop()))
while len(leftCounts['?']) > 0 and len(rightCounts['?']) > 0:
	pairCounts['a'].append((leftCounts['?'].pop(), rightCounts['?'].pop()))
totalPairs = 0
for (_, count) in pairCounts.items():
	totalPairs += len(count)
print(totalPairs)
for key in pairCounts.keys():
	for (left, right) in pairCounts[key]:
		print(left, right)


n = int(input())
l = input()
r = input()
lc = [[] for i in range(27)]
rc = [[] for i in range(27)]
x = 0
for i in l:
	if i == '?':
		lc[26].append(x)
	else:
		lc[ord(i) - 97].append(x)
	x += 1
x = 0
for i in r:
	if i == '?':
		rc[26].append(x)
	else:
		rc[ord(i) - 97].append(x)
	x += 1
ans = []
ql = []
qr = []
for i in range(26):
	if len(lc[i]) > len(rc[i]):
		for t in range(len(lc[i]) - len(rc[i])):
			ql.append(lc[i].pop())
	else:
		for t in range(len(rc[i]) - len(lc[i])):
			qr.append(rc[i].pop())
	for j in range(len(lc[i])):
		ans.append([lc[i].pop(), rc[i].pop()])
for i in range(min(len(ql), len(rc[26]))):
	ans.append([ql.pop(), rc[26].pop()])
for i in range(min(len(qr), len(lc[26]))):
	ans.append([lc[26].pop(), qr.pop()])
for i in range(min(len(lc[26]), len(rc[26]))):
	ans.append([lc[26].pop(), rc[26].pop()])
print(len(ans))
for i in ans:
	print(i[0] + 1, i[1] + 1)


(n, s1, s2) = (int(input()), input(), input())
(dl, dr) = ({'?': 0}, {'?': 0})
(indl, indr) = ({'?': []}, {'?': []})
for i in range(n):
	if s1[i] not in dl:
		dl[s1[i]] = dr[s1[i]] = 0
		(indl[s1[i]], indr[s1[i]]) = ([], [])
	if s2[i] not in dl:
		dl[s2[i]] = dr[s2[i]] = 0
		(indl[s2[i]], indr[s2[i]]) = ([], [])
	dl[s1[i]] += 1
	dr[s2[i]] += 1
	indl[s1[i]].append(i + 1)
	indr[s2[i]].append(i + 1)
pairs = 0
printv = []
for k in dl:
	if k == '?':
		continue
	new = min(dl[k], dr[k])
	pairs += new
	dl[k] -= new
	dr[k] -= new
	printv.extend([(k, k)] * new)
v = dr['?']
for k in dl:
	if k == '?':
		continue
	if v <= 0:
		break
	new = min(v, dl[k])
	pairs += new
	v -= new
	dl[k] -= new
	printv.extend([(k, '?')] * new)
dr['?'] = v
v = dl['?']
for k in dr:
	if k == '?':
		continue
	if v <= 0:
		break
	new = min(v, dr[k])
	pairs += new
	v -= new
	dr[k] -= new
	printv.extend([('?', k)] * new)
dl['?'] = v
new = min(dr['?'], dl['?'])
pairs += new
printv.extend([('?', '?')] * new)
print(pairs)
print('\n'.join([str(indl[s[0]].pop()) + ' ' + str(indr[s[1]].pop()) for s in printv]))


import sys
DBG = not True
n = int(input())
sl = input()
sr = input()
alpha = 'abcdefghijklmnopqrstuvwxyz?'
hl = {}
hr = {}
for i in range(27):
	hl[alpha[i]] = []
	hr[alpha[i]] = []
for i in range(n):
	hl[sl[i]].append(i)
	hr[sr[i]].append(i)
usedl = []
usedr = []
pairs = []
for i in range(26):
	x = alpha[i]
	mn = min(len(hl[x]), len(hr[x]))
	for j in range(mn):
		pairs.append([hl[x][j], hr[x][j]])
		usedl.append(hl[x][j])
		usedr.append(hr[x][j])
if DBG:
	print(pairs)
	print(usedl)
	print(usedr)
restl = list(set([i for i in range(n)]) - set(usedl) - set(hl['?']))
restr = list(set([i for i in range(n)]) - set(usedr) - set(hr['?']))
top = 0
mn = min(len(hl['?']), len(restr))
for i in range(mn):
	pairs.append([hl['?'][i], restr[top]])
	top += 1
topql = top
top = 0
mn = min(len(hr['?']), len(restl))
for i in range(mn):
	pairs.append([restl[top], hr['?'][i]])
	top += 1
topqr = top
if DBG:
	print('post')
	print(pairs)
	print(restl)
	print(restr)
	print(topql)
	print(topqr)
if topql < len(hl['?']):
	for i in range(len(hl['?']) - topql):
		pairs.append([hl['?'][topql + i], hr['?'][topqr + i]])
print(len(pairs))
for i in range(len(pairs)):
	print('{} {}'.format(pairs[i][0] + 1, pairs[i][1] + 1))


n = int(input())
a = list(input())
b = list(input())
cnt_a = 0
cnt_b = 0
for i in range(n):
	if a[i] != '?':
		a[i] = [ord(a[i]), i + 1]
	else:
		a[i] = [1000, i + 1]
		cnt_a += 1
	if b[i] != '?':
		b[i] = [ord(b[i]), i + 1]
	else:
		b[i] = [1000, i + 1]
		cnt_b += 1
a.sort()
b.sort()
ans = 0
j = 0
i = 0
used_a = [0] * n
used_b = [0] * n
ans_m = []
while i < n and j < n:
	if a[i][0] == b[j][0] and a[i][0] != 1000 and (used_a[i] == 0) and (used_b[j] == 0):
		ans_m.append([a[i][1], b[j][1]])
		used_a[i] = 1
		used_b[j] = 1
		ans += 1
		j += 1
		i += 1
	elif a[i][0] > b[j][0]:
		j += 1
	else:
		i += 1
print(min(n, ans + cnt_a + cnt_b))
for q in range(ans):
	print(ans_m[q][0], ans_m[q][1])
j = 0
for i in range(n):
	if a[i][0] == 1000 and used_a[i] == 0:
		while used_b[j] == 1 and j < n:
			j += 1
			if j == n:
				break
		if j == n:
			break
		if used_b[j] == 0 and used_a[i] == 0:
			print(a[i][1], b[j][1])
			used_a[i] = 1
			used_b[j] = 1
j = 0
for i in range(n):
	if b[i][0] == 1000 and used_b[i] == 0:
		while used_a[j] == 1 and j < n:
			j += 1
			if j == n:
				break
		if j == n:
			break
		if used_a[j] == 0 and used_b[i] == 0:
			print(a[j][1], b[i][1])
			used_a[j] = 1
			used_b[i] = 1


n = int(input())
l = [x for x in input()]
r = [x for x in input()]
L = {}
R = {}
for (i, e) in enumerate(l):
	i += 1
	if e in L:
		L[e].append(i)
	else:
		L[e] = [i]
for (i, e) in enumerate(r):
	i += 1
	if e in R:
		R[e].append(i)
	else:
		R[e] = [i]
res = []
q = '?'
for e in L:
	if e == q:
		continue
	if e in R:
		while len(R[e]) and len(L[e]):
			res.append((L[e].pop(), R[e].pop()))
if q in L:
	for e in R:
		if e == q:
			continue
		while L[q] and R[e]:
			res.append((L[q].pop(), R[e].pop()))
if q in R:
	for e in L:
		while L[e] and R[q]:
			res.append((L[e].pop(), R[q].pop()))
print(len(res))
for e in res:
	print(*e)


n = int(input())
l = input()
r = input()
left = {'?': 0, 'a': 0, 'c': 0, 'b': 0, 'e': 0, 'd': 0, 'g': 0, 'f': 0, 'i': 0, 'h': 0, 'k': 0, 'j': 0, 'm': 0, 'l': 0, 'o': 0, 'n': 0, 'q': 0, 'p': 0, 's': 0, 'r': 0, 'u': 0, 't': 0, 'w': 0, 'v': 0, 'y': 0, 'x': 0, 'z': 0}
right = {'?': 0, 'a': 0, 'c': 0, 'b': 0, 'e': 0, 'd': 0, 'g': 0, 'f': 0, 'i': 0, 'h': 0, 'k': 0, 'j': 0, 'm': 0, 'l': 0, 'o': 0, 'n': 0, 'q': 0, 'p': 0, 's': 0, 'r': 0, 'u': 0, 't': 0, 'w': 0, 'v': 0, 'y': 0, 'x': 0, 'z': 0}
leftIndex = {i: [] for i in left.keys()}
rightIndex = {i: [] for i in right.keys()}
for i in range(n):
	left[l[i]] += 1
	right[r[i]] += 1
	leftIndex[l[i]].append(i + 1)
	rightIndex[r[i]].append(i + 1)
matchCount = 0
needBufferLeft = 0
needBufferRight = 0
strLeft = ''
strRight = ''
for i in left.keys():
	if i != '?':
		matchCount += min(left[i], right[i])
		if left[i] < right[i]:
			needBufferRight += right[i] - left[i]
			strRight += i * (right[i] - left[i])
		else:
			needBufferLeft += left[i] - right[i]
			strLeft += i * (left[i] - right[i])
leftPairs = min(needBufferLeft, right['?'])
rightPairs = min(needBufferRight, left['?'])
matchCount += leftPairs + rightPairs
matchCount += min(right['?'] - leftPairs, left['?'] - rightPairs)
print(matchCount)
for i in left.keys():
	if i != '?':
		for j in range(min(left[i], right[i])):
			print(leftIndex[i].pop(), rightIndex[i].pop())
count = 0
flag = True
for i in leftIndex.keys():
	if i != '?':
		for j in leftIndex[i]:
			if count == leftPairs:
				flag = False
				break
			print(j, rightIndex['?'].pop())
			count += 1
		if not flag:
			break
count = 0
flag = True
for i in rightIndex.keys():
	if i != '?':
		for j in rightIndex[i]:
			if count == rightPairs:
				flag = False
				break
			print(leftIndex['?'].pop(), j)
			count += 1
		if not flag:
			break
for i in range(min(right['?'] - leftPairs, left['?'] - rightPairs)):
	print(leftIndex['?'].pop(), rightIndex['?'].pop())


am = int(input())
l = list(input())
r = list(input())
secL = []
secR = []
bootsL = []
bootsR = []
lMore = []
rMore = []
pairs = []

def makePair(x, y):
	pairs.append(str(x + 1) + ' ' + str(y + 1))
for i in range(26):
	bootsL.append([])
	bootsR.append([])
for i in range(am):
	if l[i] == '?':
		secL.append(i)
	else:
		bootsL[ord(l[i]) - 97].append(i)
	if r[i] == '?':
		secR.append(i)
	else:
		bootsR[ord(r[i]) - 97].append(i)
for i in range(26):
	for g in range(min(len(bootsL[i]), len(bootsR[i]))):
		makePair(bootsL[i][g], bootsR[i][g])
	if len(bootsL[i]) > len(bootsR[i]):
		for g in range(min(len(bootsL[i]), len(bootsR[i])), len(bootsL[i])):
			lMore.append(bootsL[i][g])
	else:
		for g in range(min(len(bootsL[i]), len(bootsR[i])), len(bootsR[i])):
			rMore.append(bootsR[i][g])
secRInd = 0
secLInd = 0
for i in range(min(len(lMore), len(secR))):
	secRInd += 1
	makePair(lMore[i], secR[i])
for i in range(min(len(rMore), len(secL))):
	secLInd += 1
	makePair(secL[i], rMore[i])
for i in range(min(len(secR) - secRInd, len(secL) - secLInd)):
	pairs.append(str(secL[secLInd + i] + 1) + ' ' + str(secR[secRInd + i] + 1))
print(len(pairs))
print(*pairs, sep='\n')


n = int(input())
a = input()
b = input()
(da, db) = ({}, {})

def add(d, k, v):
	if k in d:
		d[k].append(v)
	else:
		d[k] = [v]

def sub(d, k):
	if k in d:
		v = d[k].pop()
		if len(d[k]) <= 0:
			del d[k]
		return v
for i in range(0, len(a)):
	add(da, a[i], i + 1)
for i in range(0, len(b)):
	add(db, b[i], i + 1)
p = []
for k in [*da.keys()]:
	while k != '?' and k in db and (k in da):
		p.append((sub(da, k), sub(db, k)))
for k in [*da.keys()]:
	while k != '?' and k in da and ('?' in db):
		p.append((sub(da, k), sub(db, '?')))
for k in [*db.keys()]:
	while k != '?' and k in db and ('?' in da):
		p.append((sub(da, '?'), sub(db, k)))
while '?' in da and '?' in db:
	p.append((sub(da, '?'), sub(db, '?')))
print(len(p))
print('\n'.join(map(lambda x: '%s %s' % (x[0], x[1]), p)))


n = int(input())
l = input()
r = input()
larray = [[] for i in range(27)]
rarray = [[] for i in range(27)]
for i in range(len(l)):
	if ord(l[i]) != 63:
		larray[ord(l[i]) - 97].append(i)
	else:
		larray[26].append(i)
for i in range(len(r)):
	if ord(r[i]) != 63:
		rarray[ord(r[i]) - 97].append(i)
	else:
		rarray[26].append(i)
ans = []
for i in range(26):
	c = len(larray[i]) if len(larray[i]) < len(rarray[i]) else len(rarray[i])
	for j in range(c):
		tmp = [larray[i][len(larray[i]) - 1], rarray[i][len(rarray[i]) - 1]]
		ans.append(tmp)
		larray[i].pop()
		rarray[i].pop()
for i in range(27):
	c = len(rarray[i])
	for j in range(c):
		if len(larray[26]) > 0:
			tmp = [larray[26][len(larray[26]) - 1], rarray[i][len(rarray[i]) - 1]]
			ans.append(tmp)
			larray[26].pop()
			rarray[i].pop()
for i in range(27):
	c = len(larray[i])
	for j in range(c):
		if len(rarray[26]) > 0:
			tmp = [larray[i][len(larray[i]) - 1], rarray[26][len(rarray[26]) - 1]]
			ans.append(tmp)
			rarray[26].pop()
			larray[i].pop()
print(len(ans))
for i in range(len(ans)):
	print('{0} {1}'.format(ans[i][0] + 1, ans[i][1] + 1))


import re, math, decimal, bisect

def read():
	return input().strip()

def iread():
	return int(input().strip())

def viread():
	return [int(_) for _ in input().strip().split()]

def round(n):
	return int(n + 0.5)
n = iread()
l = sorted([list(z) for z in zip(read() + '~', range(n + 1), [False] * (n + 1))])
r = sorted([list(z) for z in zip(read() + '~', range(n + 1), [False] * (n + 1))])
(i, j, lqs, rqs) = (0, 0, [], [])
pairs = []
while i < n or j < n:
	if l[i][0] == '?':
		lqs.append(l[i][1])
		i += 1
		continue
	if r[j][0] == '?':
		rqs.append(r[j][1])
		j += 1
		continue
	if l[i][0] == r[j][0]:
		l[i][2] = True
		r[j][2] = True
		pairs.append((l[i][1] + 1, r[j][1] + 1))
		i += 1
		j += 1
	elif l[i][0] < r[j][0]:
		i += 1
	else:
		j += 1
(i, j) = (0, 0)
while (i < n or j < n) and (lqs or rqs):
	if i < n and (l[i][2] or l[i][0] == '?'):
		i += 1
		continue
	elif j < n and (r[j][2] or r[j][0] == '?'):
		j += 1
		continue
	elif j < n and (not r[j][2]) and lqs:
		pairs.append((lqs.pop() + 1, r[j][1] + 1))
		j += 1
	elif i < n and (not l[i][2]) and rqs:
		pairs.append((l[i][1] + 1, rqs.pop() + 1))
		i += 1
i = 0
while lqs and rqs:
	pairs.append((lqs.pop() + 1, rqs.pop() + 1))
print(len(pairs))
for p in pairs:
	print(*p)


from collections import defaultdict
n = int(input())
s = input()
t = input()
col1 = defaultdict(list)
col2 = defaultdict(list)
any1 = []
any2 = []
a = ord('a')
for i in range(n):
	if s[i] == '?':
		any1.append(i)
	else:
		col1[ord(s[i]) - a].append(i)
for i in range(n):
	if t[i] == '?':
		any2.append(i)
	else:
		col2[ord(t[i]) - a].append(i)
rec = []
mo1 = []
mo2 = []
for i in range(26):
	for j in range(max(len(col1[i]), len(col2[i]))):
		if j < min(len(col1[i]), len(col2[i])):
			rec.append((col1[i][j], col2[i][j]))
		elif len(col1[i]) > len(col2[i]):
			mo1.append(col1[i][j])
		else:
			mo2.append(col2[i][j])
while len(mo1) > 0 and len(any2) > 0:
	(a, b) = (mo1.pop(), any2.pop())
	rec.append((a, b))
while len(mo2) > 0 and len(any1) > 0:
	(a, b) = (any1.pop(), mo2.pop())
	rec.append((a, b))
for i in range(min(len(any1), len(any2))):
	rec.append((any1[i], any2[i]))
print(len(rec))
for (i, j) in rec:
	print(i + 1, j + 1)


n = int(input())
left = list(input())
right = list(input())
d_left = {}
d_right = {}
for (i, x) in enumerate(right):
	if d_right.get(x):
		d_right[x][0] += 1
		d_right[x][1].append(i + 1)
	else:
		d_right[x] = [1, [i + 1]]
ans = 0
pairs = []
question_mark_num = 0
question_mark_num_list = []
for (i, x) in enumerate(left):
	if x == '?':
		question_mark_num += 1
		question_mark_num_list.append(i + 1)
		continue
	if d_right.get(x):
		if d_right[x][0] > 0:
			d_right[x][0] -= 1
			pairs.append([i + 1, d_right[x][1].pop()])
			ans += 1
			continue
	if d_right.get('?'):
		if d_right['?'][0] > 0:
			d_right['?'][0] -= 1
			pairs.append([i + 1, d_right['?'][1].pop()])
			ans += 1
			continue
number_of_last = 0
z = 0
for key in d_right.keys():
	for j in range(d_right[key][0]):
		if number_of_last == question_mark_num:
			break
		number_of_last += 1
		pairs.append([question_mark_num_list[z], d_right[key][1][j]])
		z += 1
print(ans + number_of_last)
for i in pairs:
	print(*i)


import bisect
import collections
import copy
import functools
import heapq
import itertools
import math
import random
import re
import string
import sys
import time
sys.setrecursionlimit(99999)
n = int(input())
l = input()
r = input()
csl = collections.defaultdict(lambda : collections.deque())
csr = collections.defaultdict(lambda : collections.deque())
cntl = 0
cntr = 0
ans = []
for (i, c) in enumerate(l):
	csl[c].append(i + 1)
for (i, c) in enumerate(r):
	if c != '?':
		if csl[c]:
			ans.append([csl[c].popleft(), i + 1])
		elif csl['?']:
			ans.append([csl['?'].popleft(), i + 1])
	else:
		csr[c].append(i + 1)
for (k, v) in csl.items():
	while v and csr['?']:
		ans.append([v.popleft(), csr['?'].popleft()])
print(len(ans))
for (x, y) in ans:
	print(x, y)


ii = lambda : int(input())
mi = lambda : map(int, input().split())
li = lambda : list(mi())
from collections import defaultdict as dd
n = ii()
(a, b) = (input().strip(), input().strip())
(da, db) = (dd(list), dd(list))
(qa, qb) = ([], [])
for i in range(n):
	if a[i] == '?':
		qa.append(i)
	else:
		da[a[i]].append(i)
	if b[i] == '?':
		qb.append(i)
	else:
		db[b[i]].append(i)
ans = []
for c in 'abcdefghijklmnopqrstuvwxyz':
	(u, v) = (da[c], db[c])
	while u and v:
		ans.append((u.pop(), v.pop()))
	while u and qb:
		ans.append((u.pop(), qb.pop()))
	while v and qa:
		ans.append((qa.pop(), v.pop()))
while qa and qb:
	ans.append((qa.pop(), qb.pop()))
print(len(ans))
print(*('%d %d' % (i + 1, j + 1) for (i, j) in ans), sep='\n')


n = int(input())
darling = input()
darling = list(darling)
hiro = input()
hiro = list(hiro)
kaguya = []
miyuki = []
for i in range(0, n):
	darling[i] = (darling[i], i + 1)
	hiro[i] = (hiro[i], i + 1)
darling.sort()
hiro.sort()
Jian = []
le = ri = n - 1
while le >= 0 and ri >= 0 and (darling[le][0] != '?' or hiro[ri][0] != '?'):
	if darling[le][0] == hiro[ri][0] or darling[le][0] == '?' or hiro[ri][0] == '?':
		Jian.append((darling[le][1], hiro[ri][1]))
		darling[le] = ('0', darling[le][1])
		hiro[ri] = ('0', hiro[ri][1])
		le -= 1
		ri -= 1
	elif le == 0:
		ri -= 1
	elif ri == 0:
		le -= 1
	elif darling[le][0] < hiro[ri][0]:
		ri -= 1
	else:
		le -= 1
for i in range(0, n):
	if darling[i][0] != '0':
		kaguya.append(darling[i])
for i in range(n, 0, -1):
	if hiro[i - 1][0] != '0':
		miyuki.append(hiro[i - 1])
le = len(kaguya) - 1
ri = len(miyuki) - 1
while le >= 0 and ri >= 0:
	if kaguya[le][0] == '?' or miyuki[ri][0] == '?':
		Jian.append((kaguya[le][1], miyuki[ri][1]))
		le -= 1
		ri -= 1
	elif le != 0 and ri != 0:
		le -= 1
		ri -= 1
	elif le == 0:
		ri -= 1
	elif ri == 0:
		le -= 1
print(len(Jian))
for i in Jian:
	print(i[0], i[1])


from sys import stdin, stdout
input = stdin.readline
t = 1
for _ in range(t):
	n = int(input())
	l = input().strip()
	r = input().strip()
	a = {}
	b = {}
	for i in range(n):
		if l[i] not in a:
			a[l[i]] = []
		a[l[i]].append(i)
	for i in range(n):
		if r[i] not in b:
			b[r[i]] = []
		b[r[i]].append(i)
	ans = 0
	out = []
	done_a = set()
	done_b = set()
	for i in range(0, 26):
		if chr(i + 97) in a and chr(i + 97) in b:
			val = min(len(a[chr(i + 97)]), len(b[chr(i + 97)]))
			ans += val
			for j in range(val):
				out.append((a[chr(i + 97)].pop(), b[chr(i + 97)].pop()))
				done_a.add(out[-1][0])
				done_b.add(out[-1][1])
	for i in range(n):
		if i not in done_a:
			if '?' in b and len(b['?']) and (l[i] != '?'):
				out.append((i, b['?'].pop()))
				ans += 1
				done_a.add(i)
	for i in range(n):
		if i not in done_b:
			if '?' in a and len(a['?']) and (r[i] != '?'):
				out.append((a['?'].pop(), i))
				ans += 1
				done_b.add(i)
	if '?' in a and '?' in b:
		while len(a['?']) and len(b['?']):
			out.append((a['?'].pop(), b['?'].pop()))
			ans += 1
	print(ans)
	for i in out:
		print(i[0] + 1, i[1] + 1)


from collections import Counter, defaultdict, deque
import itertools
n = int(input())
a = input()
b = input()
da = defaultdict(deque)
db = defaultdict(deque)
for i in range(n):
	da[a[i]].append(i)
	db[b[i]].append(i)
letters = set(da.keys()) | set(db.keys())
result = []
for l in letters:
	if l == '?':
		continue
	dal = da[l]
	dbl = db[l]
	min_len = min(len(dal), len(dbl))
	max_len = max(len(dal), len(dbl))
	for i in range(min_len):
		result.append((dal.popleft(), dbl.popleft()))
if da['?']:
	q = da['?']
	for (letter, indexes) in db.items():
		if letter != '?':
			while indexes and q:
				result.append((q.popleft(), indexes.popleft()))
if db['?']:
	q = db['?']
	for (letter, indexes) in da.items():
		if letter != '?':
			while indexes and q:
				result.append((indexes.popleft(), q.popleft()))
while da['?'] and db['?']:
	result.append((da['?'].popleft(), db['?'].popleft()))
print(len(result))
for (ai, bi) in result:
	print(ai + 1, bi + 1)


from collections import defaultdict

def boot(l, r):
	dl = defaultdict(list)
	dr = defaultdict(list)
	for i in range(n):
		dl[l[i]].append(i + 1)
		dr[r[i]].append(i + 1)
	(pair, count) = ([], 0)
	for k in list(dr):
		if k != '?' and len(dl[k]) == 0:
			while dr[k] and dl['?']:
				pair.append((dl['?'].pop(), dr[k].pop()))
				count += 1
	for k in list(dl):
		while dl[k] and dr[k]:
			pair.append((dl[k].pop(), dr[k].pop()))
			count += 1
		while dl[k] and dr['?']:
			pair.append((dl[k].pop(), dr['?'].pop()))
			count += 1
		while dl['?'] and dr[k]:
			pair.append((dl['?'].pop(), dr[k].pop()))
			count += 1
	print(count)
	for (i, j) in pair:
		print(i, j)
n = int(input())
l = input()
r = input()
boot(l, r)


from collections import *
(n, s1, s2, ans, arr, mem1, mem2) = (int(input()), input(), input(), 0, [], defaultdict(deque), defaultdict(deque))
for i in range(n):
	mem1[s1[i]].append(i + 1)
	mem2[s2[i]].append(i + 1)
for i in range(n):
	if mem2[s1[i]] and s1[i] != '?':
		ans += 1
		arr.append([i + 1, mem2[s1[i]].popleft()])
		mem1[s1[i]].popleft()
	elif mem2['?'] and s1[i] != '?':
		ans += 1
		arr.append([i + 1, mem2['?'].popleft()])
		mem1[s1[i]].popleft()
for i in range(n - 1, -1, -1):
	if mem2[s2[i]] and mem1['?'] and (s2[i] != '?'):
		ans += 1
		arr.append([mem1['?'].pop(), i + 1])
		mem2[s2[i]].pop()
for i in range(n):
	if s1[i] == '?' and mem1['?'] and mem2['?']:
		ans += 1
		arr.append([mem1['?'].popleft(), mem2['?'].pop()])
print(len(arr))
for i in range(len(arr)):
	print(*arr[i])


n = int(input())
a = input()
b = input()
A = [[] for i in range(27)]
B = [[] for i in range(27)]
for i in range(n):
	if a[i] == '?':
		A[26].append(i)
	else:
		A[ord(a[i]) - ord('a')].append(i)
	if b[i] == '?':
		B[26].append(i)
	else:
		B[ord(b[i]) - ord('a')].append(i)
matches = []
for i in range(27):
	while A[i] and B[i]:
		matches.append((A[i].pop(), B[i].pop()))
	while A[i] and B[26]:
		matches.append((A[i].pop(), B[26].pop()))
	while A[26] and B[i]:
		matches.append((A[26].pop(), B[i].pop()))
print(len(matches))
for i in matches:
	print(i[0] + 1, i[1] + 1)


from collections import defaultdict
d1 = defaultdict(list)
d2 = defaultdict(list)
n = int(input())
s1 = input()
cnt1 = cnt2 = n
s2 = input()
for i in range(n):
	d1[s1[i]].append(i + 1)
	d2[s2[i]].append(i + 1)
lst = []
dp1 = [1] * n
dp2 = [1] * n
cnt = 0
for i in range(27):
	if i == 26:
		ln1 = len(d1['?'])
		ln2 = len(d2['?'])
		mm1 = n - cnt - ln1
		mm2 = n - cnt - ln2
		minn1 = min(ln1, mm2)
		pp = 0
		for k in range(n):
			if dp2[k] == 1 and s2[k] != '?':
				if minn1 == 0:
					break
				temp1 = d1['?'].pop()
				lst.append([temp1, k + 1])
				minn1 -= 1
		minn1 = min(ln2, mm1)
		for k in range(n):
			if dp1[k] == 1 and s1[k] != '?':
				if minn1 == 0:
					break
				temp1 = d2['?'].pop()
				lst.append([k + 1, temp1])
				minn1 -= 1
		ln1 = len(d1['?'])
		ln2 = len(d2['?'])
		fnl = min(ln1, ln2)
		for i in range(fnl):
			temp1 = d1['?'].pop()
			temp2 = d2['?'].pop()
			lst.append([temp1, temp2])
	else:
		t = min(len(d1[chr(i + 97)]), len(d2[chr(i + 97)]))
		for j in range(t):
			cnt += 1
			tp1 = d1[chr(i + 97)].pop()
			tp2 = d2[chr(i + 97)].pop()
			dp1[tp1 - 1] = 0
			dp2[tp2 - 1] = 0
			lst.append([tp1, tp2])
print(len(lst))
for i in lst:
	print(*i)


import copy

def f(n, s1, s2):
	count = 0
	s1_dict = {}
	s1_dict_count = 0
	s1_count_dict = {}
	s2_dict = {}
	s2_dict_count = 0
	s1_wild = []
	s2_wild = []
	i = 1
	for letter in s1:
		if not letter == '?':
			s1_dict.setdefault(letter, [0, []])
			s1_count_dict.setdefault(letter, 0)
			s1_dict[letter][1].append(i)
			s1_dict_count += 1
		else:
			s1_wild.append(i)
		i += 1
	d = {}
	i = 1
	for letter in s2:
		if not letter == '?':
			if not s1_dict_count == 0:
				try:
					if not s1_dict[letter] == []:
						j = s1_dict[letter][0]
						d.setdefault(letter, [])
						a = s1_dict[letter][1][j]
						d[letter].append([a, i])
						s1_dict[letter][0] += 1
						count += 1
						s1_dict_count -= 1
				except:
					s2_dict.setdefault(letter, [0, []])
					s2_dict[letter][1].append(i)
					s2_dict_count += 1
			else:
				s2_dict.setdefault(letter, [0, []])
				s2_dict[letter][1].append(i)
				s2_dict_count += 1
		else:
			s2_wild.append(i)
		i += 1
	s2_wild_count = len(s2_wild)
	s1_wild_count = len(s1_wild)
	j = 0
	d.setdefault('?', [])
	for item in s2_wild:
		if s1_dict_count == 0:
			break
		for key in s1_dict.keys():
			b = s1_dict[key][0]
			c = len(s1_dict[key][1])
			if b < c and s2_wild_count > 0:
				for num in s1_dict[key][1][b:]:
					if s1_dict_count == 0:
						break
					if b < c and s2_wild_count > 0:
						d.setdefault('?', [])
						d['?'].append([num, s2_wild[j]])
						s1_dict[key][0] += 1
						s1_dict_count -= 1
						s2_wild_count -= 1
						b += 1
						count += 1
						j += 1
					else:
						break
	k = 0
	for item in s1_wild:
		if s2_dict_count == 0:
			break
		for key in s2_dict.keys():
			b = s2_dict[key][0]
			c = len(s2_dict[key][1])
			if b < c and s1_wild_count > 0:
				for num in s2_dict[key][1][b:]:
					if s2_dict_count == 0:
						break
					if b < c and s1_wild_count > 0:
						d.setdefault('?', [])
						d['?'].append([s1_wild[k], num])
						s2_dict[key][0] += 1
						s2_dict_count -= 1
						s1_wild_count -= 1
						b += 1
						count += 1
						k += 1
					else:
						break
	while s1_wild_count > 0 and s2_wild_count > 0:
		d.setdefault('?', [])
		d['?'].append([s1_wild[k], s2_wild[j]])
		count += 1
		s1_wild_count -= 1
		s2_wild_count -= 1
		k += 1
		j += 1
	if count == 0:
		print(0)
	else:
		print(count)
		for item in d.values():
			if len(item) > 0:
				for pair in item:
					print(pair[0], end='')
					print(' ', end='')
					print(pair[1])
a = int(input())
b = input()
c = input()
f(a, b, c)


n = int(input())
a = list(input())
b = list(input())
la = [[] for i in range(26)]
lb = [[] for i in range(26)]
(qa, qb) = ([], [])
for i in range(n):
	if a[i] != '?':
		la[ord(a[i]) - 97].append(i)
	else:
		qa.append(i)
	if b[i] != '?':
		lb[ord(b[i]) - 97].append(i)
	else:
		qb.append(i)
ans = []
for i in range(26):
	while len(la[i]) and len(lb[i]):
		(x, y) = (la[i].pop(), lb[i].pop())
		ans.append([x + 1, y + 1])
for i in range(26):
	while len(la[i]) and len(qb):
		(x, y) = (la[i].pop(), qb.pop())
		ans.append([x + 1, y + 1])
for i in range(26):
	while len(lb[i]) and len(qa):
		(x, y) = (qa.pop(), lb[i].pop())
		ans.append([x + 1, y + 1])
while len(qa) and len(qb):
	(x, y) = (qa.pop(), qb.pop())
	ans.append([x + 1, y + 1])
print(len(ans))
for i in ans:
	print(*i)


n = int(input())
s = input()
t = input()
p = [[] for _ in range(27)]
q = [[] for _ in range(27)]
for i in range(n):
	if s[i].isalpha():
		p[ord(s[i]) - 97].append(i)
	else:
		p[26].append(i)
for i in range(n):
	if t[i].isalpha():
		q[ord(t[i]) - 97].append(i)
	else:
		q[26].append(i)
ans = []
for i in range(26):
	while p[i] and q[i]:
		ans.append([p[i][-1], q[i][-1]])
		p[i].pop()
		q[i].pop()
for i in range(26):
	while p[i] and q[26]:
		ans.append([p[i][-1], q[26][-1]])
		p[i].pop()
		q[26].pop()
for i in range(26):
	while p[26] and q[i]:
		ans.append([p[26][-1], q[i][-1]])
		p[26].pop()
		q[i].pop()
while p[26] and q[26]:
	ans.append([p[26][-1], q[26][-1]])
	p[26].pop()
	q[26].pop()
print(len(ans))
for i in ans:
	print(i[0] + 1, i[1] + 1)


n = int(input())
a = list(input())
b = list(input())
l = dict()
r = dict()
for i in range(n):
	if a[i] in l:
		l[a[i]].append(i + 1)
	else:
		l[a[i]] = [i + 1]
	if b[i] in r:
		r[b[i]].append(i + 1)
	else:
		r[b[i]] = [i + 1]
ans = []
for i in list(l.keys()):
	if i in r and i != '?':
		for j in range(min(len(l[i]), len(r[i]))):
			ans.append(str(l[i].pop()) + ' ' + str(r[i].pop()))
p1 = []
for i in list(l.keys()):
	if i != '?':
		for el in l[i]:
			p1.append(el)
p2 = []
for i in list(r.keys()):
	if i != '?':
		for el in r[i]:
			p2.append(el)
if '?' in l:
	for i in range(min(len(p2), len(l['?']))):
		ans.append(str(l['?'].pop()) + ' ' + str(p2.pop()))
if '?' in r:
	for i in range(min(len(p1), len(r['?']))):
		ans.append(str(p1.pop()) + ' ' + str(r['?'].pop()))
if '?' in r and '?' in l:
	for i in range(min(len(l['?']), len(r['?']))):
		ans.append(str(l['?'].pop()) + ' ' + str(r['?'].pop()))
print(len(ans))
print('\n'.join(ans))


from itertools import chain
from collections import Counter
alp = [chr(i) for i in range(97, 97 + 26)]
N = int(input())
L = input()
R = input()
LC = Counter(L)
RC = Counter(R)
TL = [0] * 29
for (i, v) in enumerate(alp):
	TL[i] = min(LC[v], RC[v])
stl = sum(TL)
TL[26] = min(N - stl - LC['?'], RC['?'])
TL[27] = min(LC['?'], N - stl - RC['?'])
TL[28] = min(LC['?'] - TL[27], RC['?'] - TL[26])
(AnsL, AnsR) = [[[] for _ in range(29)] for _ in range(2)]
TR = TL.copy()
for (i, v) in enumerate(L, 1):
	if v == '?':
		if TL[27] > 0:
			TL[27] -= 1
			AnsL[27].append(i)
		else:
			AnsL[28].append(i)
	else:
		v = ord(v) - 97
		if TL[v] > 0:
			TL[v] -= 1
			AnsL[v].append(i)
		elif TL[26] > 0:
			TL[26] -= 1
			AnsL[26].append(i)
for (i, v) in enumerate(R, 1):
	if v == '?':
		if TR[26] > 0:
			TR[26] -= 1
			AnsR[26].append(i)
		else:
			AnsR[28].append(i)
	else:
		v = ord(v) - 97
		if TR[v] > 0:
			TR[v] -= 1
			AnsR[v].append(i)
		elif TR[27] > 0:
			TR[27] -= 1
			AnsR[27].append(i)
print(sum([len(i) for i in AnsL]))
for (i, j) in zip(chain(*AnsL), chain(*AnsR)):
	print(i, j)


def process(s):
	result = {}
	for (i, v) in enumerate(s):
		result.setdefault(v, []).append(i)
	return result

def flatten(d):
	result = []
	for indices in d.values():
		result.extend(indices)
	return result

def flatten_wild(d, wild_first):
	wild = d.pop('?', [])
	if wild_first:
		return (wild + flatten(d), len(wild))
	return (flatten(d) + wild, len(wild))

def solve(a, b):
	a_count = process(a)
	b_count = process(b)
	result = []
	for (char, a_i) in a_count.items():
		if char == '?':
			continue
		if char not in b_count:
			continue
		count = 0
		for (i, j) in zip(a_i, b_count[char]):
			result.append((i, j))
			count += 1
		a_count[char] = a_count[char][count:]
		b_count[char] = b_count[char][count:]
	(a_flat, a_wild) = flatten_wild(a_count, wild_first=True)
	(b_flat, b_wild) = flatten_wild(b_count, wild_first=False)
	if a_wild + b_wild >= len(a_flat):
		result.extend(zip(a_flat, b_flat))
		return result
	for (i, j, _) in zip(a_flat, b_flat, range(a_wild)):
		result.append((i, j))
	for (i, j, _) in zip(reversed(a_flat), reversed(b_flat), range(b_wild)):
		result.append((i, j))
	return result

def main() -> None:
	n = int(input())
	a = input()
	b = input()
	result = solve(a, b)
	print(len(result))
	for (i, j) in result:
		print(i + 1, j + 1)
main()


n = int(input())
l = str(input())
r = str(input())
from collections import defaultdict
dl = defaultdict(lambda : [])
dr = defaultdict(lambda : [])
L = []
R = []
for i in range(n):
	if l[i] != '?':
		dl[l[i]].append(i)
	else:
		L.append(i)
	if r[i] != '?':
		dr[r[i]].append(i)
	else:
		R.append(i)
ans = []
for i in range(26):
	c = chr(i + ord('a'))
	if len(dl[c]) >= len(dr[c]):
		for j in range(len(dr[c])):
			a = dl[c].pop()
			b = dr[c].pop()
			ans.append((a + 1, b + 1))
		for j in range(len(dl[c])):
			if R:
				a = dl[c].pop()
				b = R.pop()
				ans.append((a + 1, b + 1))
			else:
				continue
	if len(dl[c]) < len(dr[c]):
		for j in range(len(dl[c])):
			a = dl[c].pop()
			b = dr[c].pop()
			ans.append((a + 1, b + 1))
		for j in range(len(dr[c])):
			if L:
				a = L.pop()
				b = dr[c].pop()
				ans.append((a + 1, b + 1))
			else:
				continue
if len(L) >= len(R):
	for j in range(len(R)):
		a = L[j]
		b = R[j]
		ans.append((a + 1, b + 1))
else:
	for j in range(len(L)):
		a = L[j]
		b = R[j]
		ans.append((a + 1, b + 1))
print(len(ans))
for i in range(len(ans)):
	print(*ans[i])


from collections import defaultdict
n = int(input())
left = input()
right = input()
colors = set()
left_colors = defaultdict(list)
right_colors = defaultdict(list)
for (i, boot) in enumerate(left):
	colors.add(boot)
	if boot in left_colors:
		left_colors[boot].append(i + 1)
	else:
		left_colors[boot] = [i + 1]
for (i, boot) in enumerate(right):
	colors.add(boot)
	if boot in right_colors:
		right_colors[boot].append(i + 1)
	else:
		right_colors[boot] = [i + 1]
pairs = []
left_left = []
left_right = []
for color in colors:
	if color != '?':
		while len(left_colors[color]) > 0 and len(right_colors[color]) > 0:
			pairs.append((left_colors[color].pop(), right_colors[color].pop()))
		while len(left_colors[color]) > 0:
			left_left.append(left_colors[color].pop())
		while len(right_colors[color]) > 0:
			left_right.append(right_colors[color].pop())
while len(left_colors['?']) > 0 and len(left_right) > 0:
	pairs.append((left_colors['?'].pop(), left_right.pop()))
while len(right_colors['?']) > 0 and len(left_left) > 0:
	pairs.append((left_left.pop(), right_colors['?'].pop()))
while len(left_colors['?']) > 0 and len(right_colors['?']) > 0:
	pairs.append((left_colors['?'].pop(), right_colors['?'].pop()))
print(len(pairs))
for pair in pairs:
	print(pair[0], pair[1])


n = int(input())
l = list(input())
k = list(input())
ar = [[] for i in range(27)]
ar1 = [[] for i in range(27)]
for i in range(n):
	if l[i] == '?':
		ar[26].append(i)
	else:
		ar[ord(l[i]) - 97].append(i)
for i in range(n):
	if k[i] == '?':
		ar1[26].append(i)
	else:
		ar1[ord(k[i]) - 97].append(i)
trash = []
trash1 = []
trash2 = []
trash3 = []
io = []
for i in range(26):
	for j in range(min(len(ar[i]), len(ar1[i]))):
		io.append([ar[i][j] + 1, ar1[i][j] + 1])
	for j in range(min(len(ar[i]), len(ar1[i])), max(len(ar[i]), len(ar1[i]))):
		if len(ar[i]) > len(ar1[i]):
			trash.append(ar[i][j])
		else:
			trash1.append(ar1[i][j])
for i in range(min(len(trash1), len(ar[26]))):
	io.append([ar[26][i] + 1, trash1[i] + 1])
for i in range(len(trash1), len(ar[26])):
	trash2.append(ar[26][i])
for i in range(min(len(trash), len(ar1[26]))):
	io.append([trash[i] + 1, ar1[26][i] + 1])
for i in range(len(trash), len(ar1[26])):
	trash3.append(ar1[26][i])
for i in range(min(len(trash2), len(trash3))):
	io.append([trash2[i] + 1, trash3[i] + 1])
print(len(io))
for i in range(len(io)):
	print(io[i][0], io[i][1])


l = int(input())
le = input().strip()
ri = input().strip()
la = list(sorted(((y, x) for (x, y) in enumerate(le)), reverse=True))
lb = list(sorted(((y, x) for (x, y) in enumerate(ri)), reverse=True))
(i, j) = (0, 0)
res = 0
resp = []
(ui, ul) = (l - 1, l - 1)
while i <= ui and j <= ul:
	(ai, aa) = la[i]
	(bi, bb) = lb[j]
	if ai == bi or ai == '?' or bi == '?':
		resp.append('{} {}'.format(aa + 1, bb + 1))
		i += 1
		j += 1
	elif ai > bi:
		i += 1
		if lb[ul][0] == '?':
			resp.append('{} {}'.format(aa + 1, lb[ul][1] + 1))
			ul -= 1
	else:
		j += 1
		if la[ui][0] == '?':
			resp.append('{} {}'.format(la[ui][1] + 1, bb + 1))
			ui -= 1
print(len(resp))
for r in resp:
	print(r)


from collections import deque
n = int(input())
l = input()
r = input()
a = [0 for i in range(0, ord('z') + 1)]
b = [0 for i in range(0, ord('z') + 1)]
c = [deque() for i in range(0, ord('z') + 1)]
d = [deque() for i in range(0, ord('z') + 1)]
for i in range(len(l)):
	if l[i] == '?':
		a[0] += 1
		c[0].append(i + 1)
	else:
		a[ord(l[i])] += 1
		c[ord(l[i])].append(i + 1)
for i in range(len(l)):
	if r[i] == '?':
		b[0] += 1
		d[0].append(i + 1)
	else:
		b[ord(r[i])] += 1
		d[ord(r[i])].append(i + 1)
vopra = a[0]
voprb = b[0]
sch = 0
out = []
for i in range(1, len(a)):
	sch += min(a[i], b[i])
	sch += min(voprb, a[i] - min(a[i], b[i]))
	for j in range(min(a[i], b[i])):
		if min(a[i], b[i]) > 0:
			out.append([c[i].popleft(), d[i].popleft()])
	for j in range(min(voprb, a[i] - min(a[i], b[i]))):
		if min(voprb, a[i] - min(a[i], b[i])) > 0:
			out.append([c[i].popleft(), d[0].popleft()])
	voprb -= min(voprb, a[i] - min(a[i], b[i]))
	sch += min(vopra, b[i] - min(a[i], b[i]))
	for j in range(min(vopra, b[i] - min(a[i], b[i]))):
		if min(vopra, b[i] - min(a[i], b[i])) > 0:
			out.append([c[0].popleft(), d[i].popleft()])
	vopra -= min(vopra, b[i] - min(a[i], b[i]))
if vopra > 0 and voprb > 0:
	sch += min(vopra, voprb)
	for j in range(min(vopra, voprb)):
		if min(vopra, voprb) > 0:
			out.append([c[0].popleft(), d[0].popleft()])
print(sch)
for i in out:
	print(*i)


from collections import defaultdict
n = int(input())
l = input().strip()
r = input().strip()
left = defaultdict(list)
right = defaultdict(list)
for i in range(n):
	left[l[i]].append(i + 1)
for i in range(n):
	right[r[i]].append(i + 1)
table = []
s = ''
for i in range(26):
	s += chr(97 + i)
for i in s:
	while left[i] and right[i]:
		table.append((left[i].pop(), right[i].pop()))
for i in s:
	while left[i] and right['?']:
		table.append((left[i].pop(), right['?'].pop()))
for i in s:
	while left['?'] and right[i]:
		table.append((left['?'].pop(), right[i].pop()))
for i in s:
	while left['?'] and right['?']:
		table.append((left['?'].pop(), right['?'].pop()))
print(len(table))
for i in table:
	print(*i)


import sys
inf = float('inf')
mod = 1000000007

def get_array():
	return list(map(int, sys.stdin.readline().split()))

def get_ints():
	return map(int, sys.stdin.readline().split())

def input():
	return sys.stdin.readline()

def main():
	n = int(input())
	L = input().strip()
	R = input().strip()
	(d1, d2) = ({}, {})
	for j in range(n):
		i = L[j]
		if i not in d1:
			d1[i] = []
		d1[i].append(j)
	for j in range(n):
		i = R[j]
		if i not in d2:
			d2[i] = []
		d2[i].append(j)
	ans = 0
	op = []
	rn1 = 0
	rn2 = 0
	ind1 = []
	ind2 = []
	for i in d1:
		if i == '?':
			continue
		if i in d2:
			ans += min(len(d1[i]), len(d2[i]))
			x = min(len(d1[i]), len(d2[i]))
			for l in range(x):
				op.append((d1[i][l] + 1, d2[i][l] + 1))
			if len(d1[i]) > len(d2[i]):
				rn1 += len(d1[i]) - len(d2[i])
				for l in range(x, len(d1[i])):
					ind1.append(d1[i][l] + 1)
			elif len(d2[i]) > len(d1[i]):
				rn2 += len(d2[i]) - len(d1[i])
				for l in range(x, len(d2[i])):
					ind2.append(d2[i][l] + 1)
		else:
			rn1 += len(d1[i])
			for l in range(len(d1[i])):
				ind1.append(d1[i][l] + 1)
	for i in d2:
		if i == '?':
			continue
		if i in d1:
			continue
		rn2 += len(d2[i])
		for l in range(len(d2[i])):
			ind2.append(d2[i][l] + 1)
	rm1 = 0
	rm2 = 0
	if '?' in d1:
		ans += min(rn2, len(d1['?']))
		x = min(len(d1['?']), rn2)
		for l in range(x):
			op.append((d1['?'][l] + 1, ind2[l]))
		if len(d1['?']) > rn2:
			rm1 = len(d1['?']) - rn2
	if '?' in d2:
		ans += min(rn1, len(d2['?']))
		x = min(rn1, len(d2['?']))
		for l in range(x):
			op.append((ind1[l], d2['?'][l] + 1))
		if len(d2['?']) > rn1:
			rm2 = len(d2['?']) - rn1
	ans += min(rm1, rm2)
	for l in range(min(rm1, rm2)):
		op.append((d1['?'][-l - 1] + 1, d2['?'][-l - 1] + 1))
	print(ans)
	for i in op:
		print(i[0], i[1])
main()


n = int(input())
l = input()
r = input()
d = {}
for (i, x) in enumerate(r):
	if d.get(x) == None:
		d[x] = [0, 0]
	d[x].append(i + 1)
	d[x][0] += 1
(a, b) = (0, 0)
if d.get('?') != None:
	a = 1
(pairs, another, questionsl, k) = ([], [], [], 0)
for (i, x) in enumerate(l):
	if x != '?':
		if d.get(x) == None:
			if a == 1:
				if d['?'][0] > d['?'][1]:
					pairs.append([i + 1, d['?'][d['?'][1] + 2]])
					d['?'][1] += 1
		elif d[x][0] > d[x][1]:
			pairs.append([i + 1, d[x][d[x][1] + 2]])
			d[x][1] += 1
		elif a == 1:
			if d['?'][0] > d['?'][1]:
				pairs.append([i + 1, d['?'][d['?'][1] + 2]])
				d['?'][1] += 1
	else:
		k += 1
		questionsl.append(i + 1)
e = 0
for (i, x) in enumerate(d):
	if e == k:
		break
	for (j, y) in enumerate(d[x][d[x][1] + 2:]):
		pairs.append([questionsl[e], y])
		e += 1
		if e == k:
			break
print(len(pairs))
for (i, x) in enumerate(pairs):
	print(*x)


n = int(input())
a = input()
b = input()
da = {'?': 0}
db = {'?': 0}
pa = {'?': []}
pb = {'?': []}
ans = []
for i in range(n):
	if a[i] not in da:
		da[a[i]] = 1
		pa[a[i]] = [i]
	else:
		da[a[i]] += 1
		pa[a[i]].append(i)
	if b[i] not in db:
		db[b[i]] = 1
		pb[b[i]] = [i]
	else:
		db[b[i]] += 1
		pb[b[i]].append(i)
todo1 = []
todo2 = []
for i in da:
	if i == '?':
		continue
	if i in db:
		while len(pa[i]) != 0 and len(pb[i]) != 0:
			x = pa[i].pop()
			y = pb[i].pop()
			ans.append([x, y])
		while len(pa[i]) != 0:
			todo1.append(pa[i].pop())
		while len(pb[i]) != 0:
			todo2.append(pb[i].pop())
	while len(pa[i]) != 0:
		todo1.append(pa[i].pop())
for i in pb:
	if i == '?':
		continue
	while len(pb[i]) != 0:
		todo2.append(pb[i].pop())
n = len(pb['?'])
for i in range(n):
	if todo1:
		ans.append([todo1.pop(), pb['?'].pop()])
	else:
		break
n = len(pa['?'])
for i in range(n):
	if todo2:
		ans.append([pa['?'].pop(), todo2.pop()])
	else:
		break
while len(pb['?']) > 0 and len(pa['?']) > 0:
	ans.append([pa['?'].pop(), pb['?'].pop()])
print(len(ans))
for i in ans:
	print(i[0] + 1, i[1] + 1)


import string
n = int(input())
l = input()
r = input()
lefts = [[] for _ in range(256)]
rights = [[] for _ in range(256)]
oq = ord('?')
pairs = []
for i in range(n):
	lefts[ord(l[i])].append(i + 1)
	rights[ord(r[i])].append(i + 1)
q_store = [lefts[oq], rights[oq]]
q_used = [0, 0]
for c in string.ascii_lowercase:
	oc = ord(c)
	same_pairs = min(len(lefts[oc]), len(rights[oc]))
	for i in range(same_pairs):
		pairs.append((lefts[oc][i], rights[oc][i]))
	if same_pairs == len(rights[oc]):
		extras = lefts[oc]
		q_idx = 1
	else:
		extras = rights[oc]
		q_idx = 0
	for i in range(same_pairs, len(extras)):
		if q_used[q_idx] >= len(q_store[q_idx]):
			break
		if q_idx == 0:
			pairs.append((q_store[q_idx][q_used[q_idx]], extras[i]))
		else:
			pairs.append((extras[i], q_store[q_idx][q_used[q_idx]]))
		q_used[q_idx] += 1
while q_used[0] < len(q_store[0]) and q_used[1] < len(q_store[1]):
	pairs.append((q_store[0][q_used[0]], q_store[1][q_used[1]]))
	q_used[0] += 1
	q_used[1] += 1
print(len(pairs))
for pair in pairs:
	print(*pair)


from collections import Counter as cou
n = int(input())
ans = []
dl = {}
dl['?'] = set()
dr = {}
dr['?'] = set()
for i in range(ord('a'), ord('z') + 1):
	dl[chr(i)] = set()
	dr[chr(i)] = set()
for i in range(2):
	x = input()
	if not i:
		l = dict(cou(x))
		for (ind, j) in enumerate(x):
			dl[j].add(ind + 1)
	else:
		r = dict(cou(x))
		for (ind, j) in enumerate(x):
			dr[j].add(ind + 1)
del x, ind, j, i
op = 0
for i in l.keys():
	if '?' == i:
		op = 1
		continue
	lc = l.get(i, 0)
	rc = r.get(i, 0)
	vc = r.get('?', 0)
	diff = min(lc, rc)
	lc -= diff
	l[i] = max(0, l.get(i, 0) - diff)
	rc -= diff
	r[i] = max(0, r.get(i, 0) - diff)
	for j in range(diff):
		ans.append([dl[i].pop(), dr[i].pop()])
	if lc:
		diff = min(lc, vc)
		lc -= diff
		l[i] = max(0, l.get(i, 0) - diff)
		vc -= diff
		r['?'] = max(0, r.get('?', 0) - diff)
		for j in range(diff):
			ans.append([dl[i].pop(), dr['?'].pop()])
if op:
	x = l['?']
	for i in dr.keys():
		if len(dr[i]):
			for j in dr[i]:
				if not x:
					break
				x -= 1
				ans.append([dl['?'].pop(), j])
print(len(ans))
for i in ans:
	print(' '.join(map(str, i)))


import sys
n = input()
l = sys.stdin.readline()
r = sys.stdin.readline()
alle = set(l + r) - set(('?', ' ', '\n'))
(indsr, indsl) = ({'?': []}, {'?': []})
indsr.update(((c, []) for c in alle))
indsl.update(((c, []) for c in alle))
for (i, c) in enumerate(l, start=1):
	indsl.setdefault(c, []).append(i)
for (i, c) in enumerate(r, start=1):
	indsr.setdefault(c, []).append(i)
(paarel, paarer) = ([], [])
(restl, restr) = ([], [])
summe = 0
for c in alle:
	listl = indsl[c]
	listr = indsr[c]
	nl = len(listl)
	nr = len(listr)
	anz = min(nr, nl)
	paarel += listl[:anz]
	paarer += listr[:anz]
	restl += listl[anz:]
	restr += listr[anz:]
	summe += anz
fragel = indsl['?']
frager = indsr['?']
mischl = min(len(restl), len(frager))
mischr = min(len(restr), len(fragel))
doppelfrage = max(len(frager) - mischl, len(fragel) - mischr)
summe = summe + mischr + mischl + doppelfrage
print(summe)
for (i, j) in zip(paarel, paarer):
	print('%s %s' % (i, j))
for (i, j) in zip(fragel[:mischr], restr):
	print('%s %s' % (i, j))
for (i, j) in zip(restl, frager[:mischl]):
	print('%s %s' % (i, j))
for (i, j) in zip(fragel[mischr:], frager[mischl:]):
	print('%s %s' % (i, j))


n = int(input())
l = list(input())
r = list(input())
from collections import defaultdict
(l1, l2) = (defaultdict(list), defaultdict(list))
for i in range(n):
	l1[l[i]].append(i + 1)
	l2[r[i]].append(i + 1)
a = list('abcdefghijklmnopqrstuvwxyz')
p = []
for i in a:
	while l1[i] and l2[i]:
		p.append((l1[i].pop(), l2[i].pop()))
for i in a:
	while l1['?'] and l2[i]:
		p.append((l1['?'].pop(), l2[i].pop()))
for i in a:
	while l1[i] and l2['?']:
		p.append((l1[i].pop(), l2['?'].pop()))
while l1['?'] and l2['?']:
	p.append((l1['?'].pop(), l2['?'].pop()))
print(len(p))
for i in p:
	print(*i)


n = int(input())
l = input()
r = input()
rr = {'?': []}
for i in range(n):
	if r[i] in rr:
		rr[r[i]].append(i)
	else:
		rr[r[i]] = [i]
ll = []
for i in range(n):
	if l[i] == '?':
		ll.append(i)
q = []
res = []
for i in range(n):
	c = l[i]
	if c != '?':
		if c in rr and len(rr[c]):
			res.append([i, rr[c].pop()])
		elif len(rr['?']):
			res.append([i, rr['?'].pop()])
for i in rr:
	for j in rr[i]:
		if len(ll):
			res.append([ll.pop(), j])
print(len(res))
for i in res:
	(a, b) = i
	print(a + 1, b + 1)


n = int(input())
l = list(input())
r = list(input())
d = {chr(97 + i): [] for i in range(26)}
e = {chr(97 + i): [] for i in range(26)}
d['?'] = []
e['?'] = []
for i in range(n):
	d[l[i]].append(i + 1)
for i in range(n):
	e[r[i]].append(i + 1)
count = 0
c = []
for i in d.keys():
	if i != '?':
		for j in range(min(len(d[i]), len(e[i]))):
			c.append([d[i].pop(), e[i].pop()])
for i in d.keys():
	x = min(len(e['?']), len(d[i]))
	for j in range(x):
		c.append([d[i].pop(), e['?'].pop()])
for i in e.keys():
	x = min(len(e[i]), len(d['?']))
	for j in range(x):
		c.append([d['?'].pop(), e[i].pop()])
print(len(c))
for i in range(len(c)):
	print(*c[i])


import sys, os
from io import BytesIO, IOBase
import collections, itertools, bisect, heapq, math, string
from decimal import *
BUFSIZE = 8192
BUFSIZE = 8192

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 main():
	n = int(input())
	s = str(input())
	t = str(input())
	ds = collections.defaultdict(list)
	dt = collections.defaultdict(list)
	d = collections.defaultdict(list)
	for i in range(n):
		ds[s[i]].append(i)
		dt[t[i]].append(i)
	ans = []
	q = '?'
	(k1, k2) = (0, 0)
	for x in list(string.ascii_lowercase):
		(i, j) = (0, 0)
		while i < len(ds[x]) or j < len(dt[x]):
			if i < len(ds[x]) and j < len(dt[x]):
				ans.append((ds[x][i] + 1, dt[x][j] + 1))
				i += 1
				j += 1
			if i >= len(ds[x]) and j < len(dt[x]):
				if q in ds.keys() and k1 < len(ds[q]):
					ans.append((ds[q][k1] + 1, dt[x][j] + 1))
					k1 += 1
				else:
					break
				j += 1
			if i < len(ds[x]) and j >= len(dt[x]):
				if q in dt.keys() and k2 < len(dt[q]):
					ans.append((ds[x][i] + 1, dt[q][k2] + 1))
					k2 += 1
				else:
					break
				i += 1
	while k1 < len(ds[q]) and k2 < len(dt[q]):
		ans.append((ds[q][k1] + 1, dt[q][k2] + 1))
		k1 += 1
		k2 += 1
	print(len(ans))
	for a in ans:
		print(a[0], a[1])
main()


n = int(input())
s = input()
t = input()
fs = [0] * 27
ft = [0] * 27
for i in range(27):
	fs[i] = []
	ft[i] = []
for i in range(n):
	if s[i] == '?':
		fs[26].append(i + 1)
	else:
		fs[ord(s[i]) - ord('a')].append(i + 1)
	if t[i] == '?':
		ft[26].append(i + 1)
	else:
		ft[ord(t[i]) - ord('a')].append(i + 1)
pos1 = [-1] * (n + 1)
pos2 = [0] * n
ct = 0
for i in range(26):
	ct += min(len(fs[i]), len(ft[i]))
	for j in range(min(len(fs[i]), len(ft[i]))):
		pos1[fs[i][j]] = ft[i][j]
		pos2[ft[i][j] - 1] = 1
j = 0
for i in range(len(fs[26])):
	while j < n:
		if t[j] != '?' and pos2[j] == 0:
			ct += 1
			pos1[fs[26][i]] = j + 1
			j += 1
			break
		j += 1
j = 1
for i in range(len(ft[26])):
	while j < n + 1:
		if pos1[j] == -1:
			ct += 1
			pos1[j] = ft[26][i]
			j += 1
			break
		j += 1
print(ct)
for i in range(n + 1):
	if pos1[i] > 0:
		print(i, pos1[i])


from collections import defaultdict
n = int(input())
(left, right) = (input(), input())
mpl = defaultdict(set)
mpr = defaultdict(set)
for ind in range(n):
	mpr[right[ind]].add(ind)
	mpl[left[ind]].add(ind)

def q():
	ans = ''
	for i in range(n):
		if left[i] != '?' and left[i] in mpr:
			if mpr[left[i]]:
				ans += '%d %d\n' % (mpl[left[i]].pop() + 1, mpr[left[i]].pop() + 1)
				if not mpr[left[i]]:
					del mpr[left[i]]
				if not mpl[left[i]]:
					del mpl[left[i]]
	return ans
ans = q()
while mpl['?']:
	for elem2 in mpr:
		if elem2 != '?' and mpr[elem2]:
			ans += '%d %d\n' % (mpl['?'].pop() + 1, mpr[elem2].pop() + 1)
			break
	else:
		break
while mpr['?']:
	for elem2 in mpl:
		if mpl[elem2]:
			ans += '%d %d\n' % (mpl[elem2].pop() + 1, mpr['?'].pop() + 1)
			break
	else:
		break
for i in range(len(mpr['?'])):
	ans += '%d %d\n' % (mpl['?'].pop() + 1, mpr['?'].pop() + 1)
print(ans.count('\n'))
print(ans)


import sys
import itertools as it
import math as mt
import bisect as bi
import collections as cc
import itertools as it
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
bl = {}
br = {}
num = int(input())
l = input()
r = input()
for i in range(26):
	ab = chr(97 + i)
	bl[ab] = []
	br[ab] = []
bl['?'] = []
br['?'] = []
for i in range(num):
	bl[l[i]].append(i)
	br[r[i]].append(i)
pairs = []
co = 0
for i in bl:
	while bl[i] and br[i]:
		pairs.append((1 + bl[i].pop(), 1 + br[i].pop()))
		co += 1
	while bl[i] and br['?']:
		pairs.append((1 + bl[i].pop(), 1 + br['?'].pop()))
		co += 1
	while bl['?'] and br[i]:
		pairs.append((1 + bl['?'].pop(), 1 + br[i].pop()))
		co += 1
print(co)
for (i, j) in pairs:
	print(i, j)


n = int(input())
s = input()
t = input()
ans = []
vec1 = [[] for i in range(500)]
vec2 = [[] for i in range(500)]
for i in range(n):
	vec1[ord(s[i])].append(i + 1)
	vec2[ord(t[i])].append(i + 1)
for i in range(ord('a'), ord('z') + 1):
	while len(vec1[i]) and len(vec2[i]):
		ans.append([vec1[i][-1], vec2[i][-1]])
		vec1[i].pop()
		vec2[i].pop()
O = ord('?')
for i in range(ord('a'), ord('z') + 1):
	while len(vec1[O]) and len(vec2[i]):
		ans.append([vec1[O][-1], vec2[i][-1]])
		vec1[O].pop()
		vec2[i].pop()
for i in range(ord('a'), ord('z') + 1):
	while len(vec1[i]) and len(vec2[O]):
		ans.append([vec1[i][-1], vec2[O][-1]])
		vec1[i].pop()
		vec2[O].pop()
while len(vec1[O]) and len(vec2[O]):
	ans.append([vec1[O][-1], vec2[O][-1]])
	vec1[O].pop()
	vec2[O].pop()
print(len(ans))
for i in ans:
	print(i[0], i[1])


import sys
import math

def getAndParseInt(num=1):
	string = sys.stdin.readline().strip()
	if num == 1:
		return int(string)
	else:
		return [int(part) for part in string.split()]

def getAndParseString(num=1, delim=' '):
	string = sys.stdin.readline().strip()
	if num == 1:
		return string
	else:
		return [part for part in string.split(delim)]
n = getAndParseInt()
s1 = getAndParseString()
s2 = getAndParseString()
dict1 = {chr(97 + i): [] for i in range(26)}
q1 = []
for (index, char) in enumerate(s1):
	if char != '?':
		dict1[char].append(index)
	else:
		q1.append(index)
output = []
chars2 = []
q2 = []
for (index, char) in enumerate(s2):
	if char == '?':
		q2.append(index)
	elif len(dict1[char]) == 0:
		chars2.append(index)
	else:
		l_index = dict1[char].pop()
		output.append([l_index + 1, index + 1])
chars1 = []
for i in range(26):
	chars1 += dict1[chr(97 + i)]
while q2 and chars1:
	l_index = chars1.pop()
	r_index = q2.pop()
	output.append([l_index + 1, r_index + 1])
while q1 and chars2:
	l_index = q1.pop()
	r_index = chars2.pop()
	output.append([l_index + 1, r_index + 1])
while q1 and q2:
	l_index = q1.pop()
	r_index = q2.pop()
	output.append([l_index + 1, r_index + 1])
print(len(output))
for out in output:
	print(out[0], out[1])


from collections import defaultdict
n = int(input())
left = input()
right = input()

def count_element_frequency(s):
	d = defaultdict(list)
	for (i, el) in enumerate(s):
		d[el].append(i + 1)
	return d
left_dict = count_element_frequency(left)
right_dict = count_element_frequency(right)
left_pointer = defaultdict(int)
right_pointer = defaultdict(int)
total_pairs = 0
pairs = []
for k in left_dict:
	(lf, rf) = (len(left_dict[k]) - left_pointer[k], len(right_dict[k]) - right_pointer[k])
	num_matches = min(lf, rf)
	total_pairs += num_matches
	for i in range(num_matches):
		pairs.append((left_dict[k][left_pointer[k]], right_dict[k][right_pointer[k]]))
		left_pointer[k] += 1
		right_pointer[k] += 1
	if '?' in left_dict and len(left_dict['?']) > left_pointer['?'] and (lf < rf):
		rem_wildcard_f = len(left_dict['?']) - left_pointer['?']
		rem_rf = rf - lf
		rem_num_matches = min(rem_wildcard_f, rem_rf)
		total_pairs += rem_num_matches
		for i in range(rem_num_matches):
			pairs.append((left_dict['?'][left_pointer['?']], right_dict[k][right_pointer[k]]))
			left_pointer['?'] += 1
			right_pointer[k] += 1
	if '?' in right_dict and len(right_dict['?']) > right_pointer['?'] and (lf > rf):
		rem_wildcard_f = len(right_dict['?']) - right_pointer['?']
		rem_lf = lf - rf
		rem_num_matches = min(rem_wildcard_f, rem_lf)
		total_pairs += rem_num_matches
		for i in range(rem_num_matches):
			pairs.append((left_dict[k][left_pointer[k]], right_dict['?'][right_pointer['?']]))
			left_pointer[k] += 1
			right_pointer['?'] += 1
for k in right_dict:
	if '?' not in left_dict or len(left_dict['?']) <= left_pointer['?']:
		break
	if len(right_dict[k]) > right_pointer[k]:
		rem_wildcard_f = len(left_dict['?']) - left_pointer['?']
		rem_rf = len(right_dict[k]) - right_pointer[k]
		rem_num_matches = min(rem_wildcard_f, rem_rf)
		total_pairs += rem_num_matches
		for i in range(rem_num_matches):
			pairs.append((left_dict['?'][left_pointer['?']], right_dict[k][right_pointer[k]]))
			left_pointer['?'] += 1
			right_pointer[k] += 1
print(total_pairs)
for p in pairs:
	print('%s %s' % p)


n = int(input())
l = input()
r = input()
l2 = [[l[i], i + 1] for i in range(n)]
r2 = [[r[i], i + 1] for i in range(n)]
l2.sort(reverse=True)
r2.sort(reverse=True)
l_i = 0
r_i = 0
ANS = []
check_l = [0] * n
check_r = [0] * n
while l_i < n and r_i < n and (l2[l_i][0] != '?') and (r2[r_i][0] != '?'):
	if l2[l_i][0] == r2[r_i][0]:
		ANS.append([l2[l_i][1], r2[r_i][1]])
		check_l[l_i] = 1
		check_r[r_i] = 1
		l_i += 1
		r_i += 1
	elif l2[l_i][0] > r2[r_i][0]:
		l_i += 1
	elif l2[l_i][0] < r2[r_i][0]:
		r_i += 1
j = 0
for i in range(n - 1, -1, -1):
	if l2[i][0] != '?':
		break
	if check_l[i] == 1:
		continue
	while check_r[j] == 1:
		j += 1
	ANS.append([l2[i][1], r2[j][1]])
	check_l[i] = 1
	check_r[j] = 1
j = 0
for i in range(n - 1, -1, -1):
	if r2[i][0] != '?':
		break
	if check_r[i] == 1:
		continue
	while check_l[j] == 1:
		j += 1
	ANS.append([l2[j][1], r2[i][1]])
	check_l[j] = 1
	check_r[i] = 1
print(len(ANS))
for ans in ANS:
	print(*ans)


import sys

def unique(lst):
	return dict(((o, o) for o in lst)).values()

class Main:

	def __init__(self):
		self.buff = None
		self.index = 0

	def next(self):
		if self.buff is None or self.index == len(self.buff):
			self.buff = sys.stdin.readline().split()
			self.index = 0
		val = self.buff[self.index]
		self.index += 1
		return val

	def next_int(self):
		return int(self.next())

	def solve(self):
		n = self.next_int()
		a = self.next()
		b = self.next()
		aa = sorted([i for i in range(0, n)], key=lambda i: a[i])
		bb = sorted([i for i in range(0, n)], key=lambda i: b[i])
		rst = []
		ai = 0
		bi = 0
		aj = n - 1
		bj = n - 1
		while ai <= aj and bi <= bj:
			if a[aa[aj]] == b[bb[bj]]:
				rst.append((aa[aj], bb[bj]))
				aj -= 1
				bj -= 1
			elif a[aa[aj]] > b[bb[bj]]:
				if b[bb[bi]] == '?':
					rst.append((aa[aj], bb[bi]))
					bi += 1
				aj -= 1
			else:
				if a[aa[ai]] == '?':
					rst.append((aa[ai], bb[bj]))
					ai += 1
				bj -= 1
		print(len(rst))
		for pr in rst:
			print(pr[0] + 1, pr[1] + 1)
Main().solve()


from collections import defaultdict
n = int(input().lstrip())
(l, r) = (input().lstrip(), input().lstrip())
ls = rs = defaultdict(list)
res = []
(l_qi, r_qi) = ([], [])
r_left = []
for (i, c) in enumerate(l):
	if c == '?':
		l_qi.append(i)
	else:
		ls[c].append(i)
for (i, c) in enumerate(r):
	if c == '?':
		r_qi.append(i)
	elif len(ls[c]) > 0:
		res.append((ls[c].pop(), i))
	else:
		r_left.append(i)
i = 0
l_left = [li for lis in ls.values() for li in lis]
res.extend(list(zip(l_qi, r_left)))
res.extend(list(zip(l_left, r_qi)))
lq = len(l_qi) - len(r_left)
rq = len(r_qi) - len(l_left)
if lq > 0 and rq > 0:
	res.extend(list(zip(l_qi[-lq:], r_qi[-rq:])))
print(len(res))
for (li, ri) in res:
	print('{} {}'.format(li + 1, ri + 1))


from collections import deque
n = int(input())
l = input()
r = input()
lc = []
rc = []
lb = dict()
rb = dict()
for i in range(0, n):
	if l[i] not in lb:
		lb[l[i]] = deque([i + 1])
	else:
		lb[l[i]].append(i + 1)
for i in range(0, n):
	if r[i] not in rb:
		rb[r[i]] = deque([i + 1])
	else:
		rb[r[i]].append(i + 1)
ans = []
for i in range(97, 123):
	c = chr(i)
	if c in lb and c in rb:
		while len(lb[c]) != 0 and len(rb[c]) != 0:
			(ll, rr) = (lb[c].popleft(), rb[c].popleft())
			ans.append((ll, rr))
	if c in lb:
		lc += lb[c]
	if c in rb:
		rc += rb[c]
if '?' in lb and len(rc) != 0:
	while len(lb['?']) != 0 and len(rc) != 0:
		(ll, rr) = (lb['?'].popleft(), rc.pop())
		ans.append((ll, rr))
if '?' in rb and len(lc) != 0:
	while len(rb['?']) != 0 and len(lc) != 0:
		(ll, rr) = (lc.pop(), rb['?'].popleft())
		ans.append((ll, rr))
if '?' in lb and '?' in rb:
	if len(lb['?']) != 0 and len(rb['?']) != 0:
		while len(lb['?']) != 0 and len(rb['?']) != 0:
			(ll, rr) = (lb['?'].popleft(), rb['?'].popleft())
			ans.append((ll, rr))
print(len(ans))
for i in ans:
	print(i[0], i[1])


from collections import defaultdict as ddict
n = int(input())
l = input()
r = input()
ld = ddict(list)
rd = ddict(list)
ans = list()
lq = list()
rq = list()
ky = 'qwertyuiopasdfghjklzxcvbnm'
for (i, it) in enumerate(l):
	ld[it].append(i)
for (i, it) in enumerate(r):
	rd[it].append(i)
for k in ky:
	rng = min(len(ld[k]), len(rd[k]))
	for i in range(rng):
		ans.append((ld[k][i], rd[k][i]))
	lq += ld[k][rng:]
	rq += rd[k][rng:]
lqq = list()
rqq = list()
lminq = min(len(lq), len(rd['?']))
rminq = min(len(rq), len(ld['?']))
for i in range(lminq):
	ans.append((lq[i], rd['?'][i]))
rqq += rd['?'][lminq:]
for i in range(rminq):
	ans.append((ld['?'][i], rq[i]))
lqq += ld['?'][rminq:]
for i in range(min(len(rqq), len(lqq))):
	ans.append((lqq[i], rqq[i]))
print(len(ans))
for it in ans:
	print(it[0] + 1, it[1] + 1)


n = int(input())
l = input()
r = input()
rd = {}
for i in range(n):
	if r[i] not in rd.keys():
		rd[r[i]] = []
		rd[r[i]].append(1)
		rd[r[i]].append(i + 1)
	else:
		rd[r[i]][0] += 1
		rd[r[i]].append(i + 1)
max = 0
dic = {}
for i in range(n):
	if l[i] != '?':
		if l[i] in rd.keys():
			rd[l[i]][0] -= 1
			dic[i + 1] = rd[l[i]].pop()
			max += 1
			if rd[l[i]][0] == 0:
				del rd[l[i]]
		elif '?' in rd.keys():
			rd['?'][0] -= 1
			dic[i + 1] = rd['?'].pop()
			max += 1
			if rd['?'][0] == 0:
				del rd['?']
tmp = list(rd.keys())
length = len(tmp)
for i in range(n):
	if l[i] == '?':
		if length > 0:
			rd[tmp[0]][0] -= 1
			dic[i + 1] = rd[tmp[0]].pop()
			max += 1
			if rd[tmp[0]][0] == 0:
				del rd[tmp[0]]
				tmp.pop(0)
				length -= 1
print(max)
for i in dic.items():
	print(i[0], i[1])


import string
n = int(input())
l = [ord(c) for c in input()]
r = [ord(c) for c in input()]
lefts = [[] for _ in range(256)]
rights = [[] for _ in range(256)]
oq = ord('?')
pairs = []
for i in range(n):
	lefts[l[i]].append(i + 1)
	rights[r[i]].append(i + 1)
(ql, qr) = (lefts[oq], rights[oq])
for c in string.ascii_lowercase:
	oc = ord(c)
	loc = lefts[oc]
	roc = rights[oc]
	while len(loc) and len(roc):
		pairs.append((loc.pop(), roc.pop()))
	while len(loc) and len(qr):
		pairs.append((loc.pop(), qr.pop()))
	while len(roc) and len(ql):
		pairs.append((ql.pop(), roc.pop()))
while len(ql) and len(qr):
	pairs.append((ql.pop(), qr.pop()))
print(len(pairs))
for pair in pairs:
	print(*pair)


import sys
import string
from collections import defaultdict
fin = sys.stdin.readline
n = int(fin())
l = fin().rstrip('\n')
r = fin().rstrip('\n')
l_char_to_indices = defaultdict(list)
r_char_to_indices = defaultdict(list)
hatenas_l = []
hatenas_r = []
for (i, (l_char, r_char)) in enumerate(zip(l, r)):
	if l_char != '?':
		l_char_to_indices[l_char].append(i)
	else:
		hatenas_l.append(i)
	if r_char != '?':
		r_char_to_indices[r_char].append(i)
	else:
		hatenas_r.append(i)
pairs = set()
for char in string.ascii_lowercase:
	while l_char_to_indices[char] and r_char_to_indices[char]:
		pair = (l_char_to_indices[char].pop(), r_char_to_indices[char].pop())
		pairs.add(pair)
	if not l_char_to_indices[char]:
		del l_char_to_indices[char]
	if not r_char_to_indices[char]:
		del r_char_to_indices[char]
while hatenas_l and r_char_to_indices:
	char = next(iter(r_char_to_indices))
	pairs.add((hatenas_l.pop(), r_char_to_indices[char].pop()))
	if not r_char_to_indices[char]:
		del r_char_to_indices[char]
while hatenas_r and l_char_to_indices:
	char = next(iter(l_char_to_indices))
	pairs.add((l_char_to_indices[char].pop(), hatenas_r.pop()))
	if not l_char_to_indices[char]:
		del l_char_to_indices[char]
while hatenas_l and hatenas_r:
	pairs.add((hatenas_l.pop(), hatenas_r.pop()))
print(len(pairs))
for (l_idx, r_idx) in pairs:
	print(l_idx + 1, r_idx + 1)


n = int(input())
(l, r) = [input(), input()]
loc = {}
roc = {}
lq = []
rq = []
for (i, [lc, rc]) in enumerate(zip(l, r)):
	if lc == '?':
		lq.append(i)
	else:
		loc.setdefault(lc, []).append(i)
	if rc == '?':
		rq.append(i)
	else:
		roc.setdefault(rc, []).append(i)
ans = []
for (color, lidx) in loc.items():
	matches = list(zip(lidx, roc.get(color, [])))
	if len(matches) > 0:
		ans += matches
		loc[color] = loc[color][len(matches):]
		roc[color] = roc[color][len(matches):]
lleft = [x for ids in loc.values() for x in ids]
rleft = [x for ids in roc.values() for x in ids]
lmatches = list(zip(lq, rleft))
rmatches = list(zip(lleft, rq))
ans += lmatches + rmatches + list(zip(lq[len(lmatches):], rq[len(rmatches):]))
print(len(ans))
for (x, y) in ans:
	print(str(x + 1) + ' ' + str(y + 1))


n = int(input())
l = input()
r = input()
s = 'abcdefghijklmnopqrstuvwxyz'
d1 = {}
d2 = {}
for c in s:
	d1[c] = []
	d2[c] = []
d1['?'] = []
d2['?'] = []
for i in range(n):
	d1[l[i]].append(i)
	d2[r[i]].append(i)
ansleft = []
ansright = []
checkedleft = [0 for i in range(n)]
checkedright = [0 for j in range(n)]
for j in range(26):
	let = s[j]
	l1 = list(d1[let])
	l2 = list(d2[let])
	c1 = len(l1)
	c2 = len(l2)
	cnt = min(c1, c2)
	i = 0
	while i < cnt:
		ansleft.append(l1[i])
		ansright.append(l2[i])
		checkedright[l2[i]] = 1
		checkedleft[l1[i]] = 1
		i += 1
nonques1 = []
nonques2 = []
ques1 = list(d1['?'])
ques2 = list(d2['?'])
for i in range(n):
	if l[i] != '?' and checkedleft[i] == 0:
		nonques1.append(i)
	if r[i] != '?' and checkedright[i] == 0:
		nonques2.append(i)
j = 0
k = 0
for i in range(len(ques1)):
	if ques1[i] != -1:
		if j < len(nonques2):
			ansleft.append(ques1[i])
			ansright.append(nonques2[j])
			nonques2[j] = -1
			j += 1
			ques1[i] = -1
		elif k < len(ques2):
			ansleft.append(ques1[i])
			ansright.append(ques2[k])
			ques2[k] = -1
			k += 1
			ques1[i] = -1
j = 0
k = 0
for i in range(len(ques2)):
	if ques2[i] != -1:
		if j < len(nonques1):
			ansright.append(ques2[i])
			ansleft.append(nonques1[j])
			nonques1[j] = -1
			j += 1
			ques2[i] = -1
		elif k < len(ques1):
			ansright.append(ques2[i])
			ansleft.append(ques1[k])
			ques1[k] = -1
			k += 1
			ques2[i] = -1
print(len(ansleft))
for i in range(len(ansleft)):
	print(ansleft[i] + 1, ansright[i] + 1)


import sys
input = sys.stdin.readline
n = int(input())
l = list(input().rstrip())
r = list(input().rstrip())
xl = [[] for _ in range(26)]
xr = [[] for _ in range(26)]
(l0, r0) = ([], [])
for i in range(n):
	if l[i] == '?':
		l0.append(i + 1)
	else:
		xl[ord(l[i]) - 97].append(i + 1)
for i in range(n):
	if r[i] == '?':
		r0.append(i + 1)
	else:
		xr[ord(r[i]) - 97].append(i + 1)
ans = []
(l1, r1) = ([], [])
for i in range(26):
	(xli, xri) = (xl[i], xr[i])
	mi = min(len(xli), len(xri))
	for j in range(mi):
		ans.append((xli[j], xri[j]))
	for j in range(mi, len(xli)):
		l1.append(xli[j])
	for j in range(mi, len(xri)):
		r1.append(xri[j])
(l2, r2) = ([], [])
for i in range(min(len(l0), len(r1))):
	ans.append((l0[i], r1[i]))
if len(l0) > len(r1):
	for i in range(len(r1), len(l0)):
		l2.append(l0[i])
for i in range(min(len(r0), len(l1))):
	ans.append((l1[i], r0[i]))
if len(r0) > len(l1):
	for i in range(len(l1), len(r0)):
		r2.append(r0[i])
for i in range(min(len(l2), len(r2))):
	ans.append((l2[i], r2[i]))
k = len(ans)
print(k)
for (a, b) in ans:
	print(a, b)


import string
from collections import Counter
alpha = string.ascii_lowercase + '?'
n = int(input())
l = input()
r = input()
l_counter = Counter(l)
r_counter = Counter(r)
l_pos = {a: [] for a in alpha}
r_pos = {a: [] for a in alpha}
for i in range(n):
	c_l = l[i]
	c_r = r[i]
	l_pos[c_l].append(i)
	r_pos[c_r].append(i)
res_count = 0
res = []
l_unsorted = set()
r_unsorted = set()
for c in alpha[:-1]:
	c_l = l_counter.get(c, 0)
	c_r = r_counter.get(c, 0)
	if c_l < c_r:
		c_pairs = c_l
		r_unsorted |= set(r_pos[c][c_pairs:])
	else:
		c_pairs = c_r
		l_unsorted |= set(l_pos[c][c_pairs:])
	res_count += c_pairs
	for i in range(c_pairs):
		res.append(f'{l_pos[c][i] + 1} {r_pos[c][i] + 1}')
for i in l_pos['?']:
	if r_unsorted:
		res.append(f'{i + 1} {r_unsorted.pop() + 1}')
	else:
		if not r_pos['?']:
			break
		res.append(f"{i + 1} {r_pos['?'].pop() + 1}")
	res_count += 1
for i in r_pos['?']:
	if l_unsorted:
		res.append(f'{l_unsorted.pop() + 1} {i + 1}')
	else:
		if not l_pos['?']:
			break
		res.append(f"{l_pos['?'].pop() + 1} {i + 1}")
	res_count += 1
print(res_count)
for x in res:
	print(x)


n = int(input())
a = input()
b = input()
a_ind = [[] for i in range(27)]
b_ind = [[] for i in range(27)]
ans = []
for i in range(n):
	a_ind[ord(a[i]) - ord('a') if a[i] != '?' else -1].append(i)
	b_ind[ord(b[i]) - ord('a') if b[i] != '?' else -1].append(i)
for i in range(26):
	while a_ind[i] and b_ind[i]:
		ans.append((a_ind[i].pop(), b_ind[i].pop()))
k1 = 0
for i in range(26):
	for j in b_ind[i]:
		if k1 == len(a_ind[26]):
			break
		ans.append((a_ind[26][k1], j))
		k1 += 1
k2 = 0
for i in range(26):
	for j in a_ind[i]:
		if k2 == len(b_ind[26]):
			break
		ans.append((j, b_ind[26][k2]))
		k2 += 1
while k1 != len(a_ind[26]) and k2 != len(b_ind[26]):
	ans.append((a_ind[26][k1], b_ind[26][k2]))
	k1 += 1
	k2 += 1
print(len(ans))
for i in ans:
	print(i[0] + 1, i[1] + 1)


n = int(input())
s1 = input()
s2 = input()
d1 = {}
for i in range(n):
	if s1[i] in d1:
		d1[s1[i]].append(i + 1)
	else:
		d1[s1[i]] = [i + 1]
d2 = {}
for i in range(n):
	if s2[i] in d2:
		d2[s2[i]].append(i + 1)
	else:
		d2[s2[i]] = [i + 1]
st = ''
for i in range(26):
	st += chr(i + 97)
ans = []
for i in st:
	if i in d1 and i in d2:
		(a, b) = (d1[i], d2[i])
		m = min(len(a), len(b))
		for i in range(m):
			ans.append([a.pop(), b.pop()])
if '?' in d1:
	a = d1['?']
	for i in d2:
		if '?' != i:
			b = d2[i]
			for i in range(len(b)):
				if len(a) == 0:
					break
				ans.append([a.pop(), b.pop()])
if '?' in d2:
	b = d2['?']
	for i in d1:
		if '?' != i:
			a = d1[i]
			for i in range(len(a)):
				if len(b) == 0:
					break
				ans.append([a.pop(), b.pop()])
if '?' in d1 and '?' in d2:
	(a, b) = (d1['?'], d2['?'])
	while len(a) > 0 and len(b) > 0:
		ans.append([a.pop(), b.pop()])
print(len(ans))
for i in ans:
	print(*i)


from sys import stdin, stdout
import cProfile, math
from collections import Counter
from bisect import bisect_left, bisect, bisect_right
import itertools
from copy import deepcopy
from fractions import Fraction
import sys, threading
import operator as op
from functools import reduce
sys.setrecursionlimit(10 ** 6)
threading.stack_size(2 ** 27)
fac_warmup = False
printHeap = str()
memory_constrained = False
P = 10 ** 9 + 7
import sys

class merge_find:

	def __init__(self, n):
		self.parent = list(range(n))
		self.size = [1] * n
		self.num_sets = n
		self.lista = [[_] for _ in range(n)]

	def find(self, a):
		to_update = []
		while a != self.parent[a]:
			to_update.append(a)
			a = self.parent[a]
		for b in to_update:
			self.parent[b] = a
		return self.parent[a]

	def merge(self, a, b):
		a = self.find(a)
		b = self.find(b)
		if a == b:
			return
		if self.size[a] < self.size[b]:
			(a, b) = (b, a)
		self.num_sets -= 1
		self.parent[b] = a
		self.size[a] += self.size[b]
		self.lista[a] += self.lista[b]

	def set_size(self, a):
		return self.size[self.find(a)]

	def __len__(self):
		return self.num_sets

def display(string_to_print):
	stdout.write(str(string_to_print) + '\n')

def primeFactors(n):
	factors = dict()
	for i in range(2, math.ceil(math.sqrt(n)) + 1):
		while n % i == 0:
			if i in factors:
				factors[i] += 1
			else:
				factors[i] = 1
			n = n // i
	if n > 2:
		factors[n] = 1
	return factors

def fibonacci_modP(n, MOD):
	if n < 2:
		return 1
	return (cached_fn(fibonacci_modP, (n + 1) // 2, MOD) * cached_fn(fibonacci_modP, n // 2, MOD) + cached_fn(fibonacci_modP, (n - 1) // 2, MOD) * cached_fn(fibonacci_modP, (n - 2) // 2, MOD)) % MOD

def factorial_modP_Wilson(n, p):
	if p <= n:
		return 0
	res = p - 1
	for i in range(n + 1, p):
		res = res * cached_fn(InverseEuler, i, p) % p
	return res

def binary(n, digits=20):
	b = bin(n)[2:]
	b = '0' * (20 - len(b)) + b
	return b

def isprime(n):
	if n < 4:
		return True
	if n % 2 == 0:
		return False
	if n % 3 == 0:
		return False
	i = 5
	w = 2
	while i * i <= n:
		if n % i == 0:
			return False
		i += w
		w = 6 - w
	return True
factorial_modP = []

def warm_up_fac(MOD):
	nonlocal factorial_modP, fac_warmup
	if fac_warmup:
		return
	factorial_modP = [1 for _ in range(fac_warmup_size + 1)]
	for i in range(2, fac_warmup_size):
		factorial_modP[i] = factorial_modP[i - 1] * i % MOD
	fac_warmup = True

def InverseEuler(n, MOD):
	return pow(n, MOD - 2, MOD)

def nCr(n, r, MOD):
	nonlocal fac_warmup, factorial_modP
	if not fac_warmup:
		warm_up_fac(MOD)
		fac_warmup = True
	return factorial_modP[n] * (pow(factorial_modP[r], MOD - 2, MOD) * pow(factorial_modP[n - r], MOD - 2, MOD) % MOD) % MOD

def test_print(*args):
	if testingMode:
		print(args)

def display_list(list1, sep=' '):
	stdout.write(sep.join(map(str, list1)) + '\n')

def display_2D_list(li):
	for i in li:
		print(i)

def get_int():
	return int(stdin.readline().strip())

def get_tuple():
	return map(int, stdin.readline().split())

def get_list():
	return list(map(int, stdin.readline().split()))
import heapq, itertools
pq = []
entry_finder = {}
REMOVED = '<removed-task>'

def add_task(task, priority=0):
	if task in entry_finder:
		remove_task(task)
	count = next(counter)
	entry = [priority, count, task]
	entry_finder[task] = entry
	heapq.heappush(pq, entry)

def remove_task(task):
	entry = entry_finder.pop(task)
	entry[-1] = REMOVED

def pop_task():
	while pq:
		(priority, count, task) = heapq.heappop(pq)
		if task is not REMOVED:
			del entry_finder[task]
			return task
	raise KeyError('pop from an empty priority queue')
memory = dict()

def clear_cache():
	nonlocal memory
	memory = dict()

def cached_fn(fn, *args):
	nonlocal memory
	if args in memory:
		return memory[args]
	else:
		result = fn(*args)
		memory[args] = result
		return result

def ncr(n, r):
	return math.factorial(n) / (math.factorial(n - r) * math.factorial(r))

def binary_serach(i, li):
	fn = lambda x: li[x] - x // i
	x = -1
	b = len(li)
	while b >= 1:
		while b + x < len(li) and fn(b + x) > 0:
			x += b
		b = b // 2
	return x
TestCases = False
testingMode = False
fac_warmup_size = 10 ** 5 + 100
optimiseForReccursion = True
from math import factorial

def main():
	n = get_int()
	s1 = list(input())
	s2 = list(input())
	li = [[] for _ in range(27)]
	lj = [[] for _ in range(27)]
	for (index, i) in enumerate(s1):
		if i == '?':
			li[0].append(index)
		else:
			li[ord(i) - ord('a') + 1].append(index)
	for (index, i) in enumerate(s2):
		if i == '?':
			lj[0].append(index)
		else:
			lj[ord(i) - ord('a') + 1].append(index)
	res = []
	for i in range(1, 27):
		while len(li[i]) > 0 and len(lj[i]) > 0:
			res.append([li[i].pop() + 1, lj[i].pop() + 1])
		if len(li[i]) > 0:
			while len(li[i]) > 0 and len(lj[0]) > 0:
				res.append([li[i].pop() + 1, lj[0].pop() + 1])
		elif len(lj[i]) > 0:
			while len(lj[i]) > 0 and len(li[0]) > 0:
				res.append([li[0].pop() + 1, lj[i].pop() + 1])
	while len(li[0]) > 0 and len(lj[0]) > 0:
		res.append([li[0].pop() + 1, lj[0].pop() + 1])
	print(len(res))
	for li in res:
		display_list(li)
if TestCases:
	for _ in range(get_int()):
		cProfile.run('main()') if testingMode else main()
else:
	(cProfile.run('main()') if testingMode else main()) if not optimiseForReccursion else threading.Thread(target=main).start()


n = int(input())
arr1 = list(input())
arr2 = list(input())
alphabet = list('abcdefghijklmnopqrstuvwxyz?')
alphabetdict = {}
for letter in alphabet:
	alphabetdict[letter] = [[], []]
for i in range(n):
	alphabetdict[arr1[i]][0].append(i)
for i in range(n):
	alphabetdict[arr2[i]][1].append(i)
pairs = []
for i in range(26):
	current = alphabetdict[alphabet[i]]
	while len(alphabetdict[alphabet[i]][0]) and len(alphabetdict[alphabet[i]][1]):
		(x, y) = (alphabetdict[alphabet[i]][0].pop(), alphabetdict[alphabet[i]][1].pop())
		pairs.append((x, y))
arr1any = alphabetdict['?'][0]
arr2any = alphabetdict['?'][1]
combinedarr1 = []
for i in range(26):
	combinedarr1 += alphabetdict[alphabet[i]][0]
combinedarr2 = []
for i in range(26):
	combinedarr2 += alphabetdict[alphabet[i]][1]
for i in range(min(len(combinedarr1), len(arr2any))):
	pairs.append((combinedarr1.pop(), arr2any.pop()))
for i in range(min(len(combinedarr2), len(arr1any))):
	pairs.append((arr1any.pop(), combinedarr2.pop()))
for i in range(min(len(arr1any), len(arr2any))):
	pairs.append((arr1any.pop(), arr2any.pop()))
print(len(pairs))
for pair in pairs:
	print(pair[0] + 1, pair[1] + 1)


num_boots = int(input())
left_boots = [[x] for x in list(input())]
right_boots = [[x] for x in list(input())]
for x in range(1, num_boots + 1):
	left_boots[x - 1].append(x)
	right_boots[x - 1].append(x)
left_boots.sort(reverse=True)
right_boots.sort(reverse=True)
num_matches = 0
match_list = []
unmatched_list = [[], []]
question_mark_list = [[], []]
i = 0
j = 0
left_had_hit_question_mark = False
right_had_hit_question_mark = False
while i < num_boots and j < num_boots:
	left_present_chr = left_boots[i][0]
	right_present_chr = right_boots[j][0]
	if left_present_chr == '?':
		left_had_hit_question_mark = True
		for x in range(i, num_boots):
			question_mark_list[0].append(left_boots[x][1])
		i = num_boots
	if right_present_chr == '?':
		right_had_hit_question_mark = True
		for x in range(j, num_boots):
			question_mark_list[1].append(right_boots[x][1])
		j = num_boots
	question_mark_reached = left_had_hit_question_mark or right_had_hit_question_mark
	if not (left_had_hit_question_mark and right_had_hit_question_mark) and question_mark_reached:
		left_or_right = 0
		start = i
		which_boots = left_boots
		if right_had_hit_question_mark:
			left_or_right = 1
			start = j
			which_boots = right_boots
		for x in range(start, num_boots):
			unmatched_list[left_or_right].append(which_boots[x][1])
	if question_mark_reached:
		break
	if left_present_chr == right_present_chr:
		num_matches += 1
		match_list.append([left_boots[i][1], right_boots[j][1]])
		i += 1
		j += 1
	else:
		present_chr = min(left_present_chr, right_present_chr)
		if left_present_chr > present_chr:
			unmatched_list[0].append(left_boots[i][1])
			i += 1
		else:
			unmatched_list[1].append(right_boots[j][1])
			j += 1
if i != j:
	if i > j:
		affected_counter = j
		affected_boots = right_boots
		j = num_boots
		left_or_right = 1
	else:
		affected_counter = i
		affected_boots = left_boots
		i = num_boots
		left_or_right = 0
	for x in range(affected_counter, num_boots):
		if affected_boots[x][0] == '?':
			for h in range(x, num_boots):
				question_mark_list[left_or_right].append(affected_boots[h][1])
			affected_counter = num_boots
			break
		else:
			unmatched_list[left_or_right].append(affected_boots[x][1])
left_question_counter = 0
right_question_counter = 0
if question_mark_list[1]:
	for x in range(0, len(question_mark_list[1])):
		if x == len(unmatched_list[0]):
			break
		match_list.append([unmatched_list[0][x], question_mark_list[1][x]])
		num_matches += 1
		right_question_counter += 1
if question_mark_list[0]:
	for x in range(0, len(question_mark_list[0])):
		if x == len(unmatched_list[1]):
			break
		match_list.append([question_mark_list[0][x], unmatched_list[1][x]])
		num_matches += 1
		left_question_counter += 1
num_left_extra_question = len(question_mark_list[0]) - left_question_counter
num_right_extra_question = len(question_mark_list[1]) - right_question_counter
extra_question_num = min(num_left_extra_question, num_right_extra_question)
num_matches += extra_question_num
print(num_matches)
for x in range(0, extra_question_num):
	match_list.append([question_mark_list[0][left_question_counter], question_mark_list[1][right_question_counter]])
	left_question_counter += 1
	right_question_counter += 1
for x in match_list:
	print('{} {}'.format(x[0], x[1]))


import sys
import collections
import math
import heapq
from operator import itemgetter

def getint():
	return int(input())

def getints():
	return [int(x) for x in input().split(' ')]
n = getint()
l = input()
r = input()
result = []
lBoots = collections.defaultdict(list)
rBoots = collections.defaultdict(list)
for (i, char) in enumerate(l):
	lBoots[char].append(i + 1)
for (i, char) in enumerate(r):
	rBoots[char].append(i + 1)
for (char, liList) in lBoots.items():
	if char != '?' and char in rBoots:
		riList = rBoots[char]
		while liList and riList:
			(li, ri) = (liList.pop(), riList.pop())
			result.append((li, ri))
liList = lBoots['?']
for (char, riList) in rBoots.items():
	if char != '?':
		while liList and riList:
			(li, ri) = (liList.pop(), riList.pop())
			result.append((li, ri))
riList = rBoots['?']
for (char, liList) in lBoots.items():
	if char != '?':
		while liList and riList:
			(li, ri) = (liList.pop(), riList.pop())
			result.append((li, ri))
liList = lBoots['?']
riList = rBoots['?']
while liList and riList:
	(li, ri) = (liList.pop(), riList.pop())
	result.append((li, ri))
print(str(len(result)))
for (li, ri) in result:
	print(str(li) + ' ' + str(ri))


from collections import defaultdict as dd, deque as dq
import math, string
import sys

def getInts():
	return [int(s) for s in input().split()]

def getInt():
	return int(input())

def getStrs():
	return [s for s in input().split()]

def getStr():
	return input()

def listStr():
	return list(input())
MOD = 10 ** 9 + 7

def solve():
	(N, D1, D2, S1, S2) = (getInt(), dd(list), dd(list), getStr(), getStr())
	for n in range(N):
		D1[S1[n]].append(n + 1)
		D2[S2[n]].append(n + 1)
	pairs = []
	letters = string.ascii_lowercase
	for letter in letters:
		while D1[letter] and D2[letter]:
			pairs.append((D1[letter].pop(), D2[letter].pop()))
	for letter in letters:
		while D1[letter] and D2['?']:
			pairs.append((D1[letter].pop(), D2['?'].pop()))
	for letter in letters:
		while D2[letter] and D1['?']:
			pairs.append((D1['?'].pop(), D2[letter].pop()))
	while D1['?'] and D2['?']:
		pairs.append((D1['?'].pop(), D2['?'].pop()))
	print(len(pairs))
	for p in pairs:
		print(*p)
	return
solve()


n = int(input())
l = list(input())
r = list(input())
setall = set.union(set(list(l)), set(list(r)))
bigmasL = dict()
bigmasR = dict()
bigmasLQ = dict()
bigmasRQ = dict()
mas = []
for each in setall:
	if each == '?':
		bigmasLQ = [i + 1 for i in range(n) if l[i] == each]
		bigmasRQ = [i + 1 for i in range(n) if r[i] == each]
	else:
		bigmasL[each] = [i + 1 for i in range(n) if l[i] == each]
		bigmasR[each] = [i + 1 for i in range(n) if r[i] == each]
		mymin = min(len(bigmasL[each]), len(bigmasR[each]))
		for i in range(mymin):
			mas.append((bigmasL[each][i], bigmasR[each][i]))
		bigmasR[each] = bigmasR[each][mymin:]
		bigmasL[each] = bigmasL[each][mymin:]
(j, k) = (0, 0)
setall.discard('?')
for each in setall:
	for i in range(len(bigmasL[each])):
		if j < len(bigmasRQ):
			mas.append((bigmasL[each][i], bigmasRQ[j]))
			j += 1
	for i in range(len(bigmasR[each])):
		if k < len(bigmasLQ):
			mas.append((bigmasLQ[k], bigmasR[each][i]))
			k += 1
if j > 0:
	bigmasRQ = bigmasRQ[j:]
if k > 0:
	bigmasLQ = bigmasLQ[k:]
for i in range(min(len(bigmasLQ), len(bigmasRQ))):
	mas.append((bigmasLQ[i], bigmasRQ[i]))
print(len(mas))
for each in mas:
	print(each[0], each[1])


def adder(d, shoe, i):
	if shoe in d:
		d[shoe].add(i + 1)
	else:
		d[shoe] = {i + 1}
	return d

def main():
	n = int(input())
	l = input()
	r = input()
	l_stat = {}
	r_stat = {}
	for i in range(n):
		l_shoe = l[i]
		r_shoe = r[i]
		l_stat = adder(l_stat, l_shoe, i)
		r_stat = adder(r_stat, r_shoe, i)
	ans = []
	for k in l_stat:
		if k in r_stat and k != '?':
			while l_stat[k] != set() and r_stat[k] != set():
				ans.append((l_stat[k].pop(), r_stat[k].pop()))
		else:
			continue
	if '?' in l_stat and len(l_stat['?']) != 0:
		for k in r_stat:
			if k != '?':
				while r_stat[k] != set() and l_stat['?'] != set():
					ans.append((l_stat['?'].pop(), r_stat[k].pop()))
				if l_stat['?'] == set():
					break
	if '?' in r_stat and len(r_stat['?']) != 0:
		for k in l_stat:
			if k != '?':
				while l_stat[k] != set() and r_stat['?'] != set():
					ans.append((l_stat[k].pop(), r_stat['?'].pop()))
				if r_stat['?'] == set():
					break
	if '?' in r_stat and '?' in l_stat:
		while r_stat['?'] != set() and l_stat['?'] != set():
			ans.append((l_stat['?'].pop(), r_stat['?'].pop()))
	print(len(ans))
	for k in ans:
		print(*k)
main()


n = int(input())
s1 = input()
s2 = input()
l1 = [[] for i in range(27)]
l2 = [[] for i in range(27)]
for i in range(n):
	if s1[i] == '?':
		l1[26].append(i)
	else:
		l1[ord(s1[i]) - ord('a')].append(i)
	if s2[i] == '?':
		l2[26].append(i)
	else:
		l2[ord(s2[i]) - ord('a')].append(i)
ans = []
for i in range(26):
	while l1[i] != [] and l2[i] != []:
		ans.append([l1[i][-1] + 1, l2[i][-1] + 1])
		l1[i].pop()
		l2[i].pop()
rem1 = []
rem2 = []
for i in range(26):
	rem1 += l1[i]
	rem2 += l2[i]
while rem1 != [] and l2[26] != []:
	ans.append([rem1[-1] + 1, l2[26][-1] + 1])
	rem1.pop()
	l2[26].pop()
while rem2 != [] and l1[26] != []:
	ans.append([l1[26][-1] + 1, rem2[-1] + 1])
	rem2.pop()
	l1[26].pop()
while len(l1[26]) != 0 and len(l2[26]) != 0:
	ans.append([l1[26][-1] + 1, l2[26][-1] + 1])
	l1[26].pop()
	l2[26].pop()
print(len(ans))
for i in range(len(ans)):
	print(*ans[i])


n = int(input())
a = input()
b = input()
pairs = []
alist = [None] * 27
blist = [None] * 27
for i in range(27):
	alist[i] = []
	blist[i] = []
for i in range(n):
	if a[i] == '?':
		alist[26].append(i)
	else:
		alist[ord(a[i]) - ord('a')].append(i)
	if b[i] == '?':
		blist[26].append(i)
	else:
		blist[ord(b[i]) - ord('a')].append(i)
rema = len(alist[26])
remb = len(blist[26])
for i in range(26):
	la = len(alist[i])
	lb = len(blist[i])
	if la > lb:
		for j in range(la):
			if j < lb:
				pairs.append([alist[i][j], blist[i][j]])
			elif remb > 0:
				pairs.append([alist[i][j], blist[26][remb - 1]])
				remb -= 1
	else:
		for j in range(lb):
			if j < la:
				pairs.append([alist[i][j], blist[i][j]])
			elif rema > 0:
				pairs.append([alist[26][rema - 1], blist[i][j]])
				rema -= 1
for i in range(min(rema, remb)):
	pairs.append([alist[26][i], blist[26][i]])
print(len(pairs))
for i in range(len(pairs)):
	print(pairs[i][0] + 1, pairs[i][1] + 1)


import math
import bisect

def argsort(ls):
	return sorted(range(len(ls)), key=ls.__getitem__)

def f(p=0):
	if p == 0:
		return int(input())
	if p == 1:
		return map(int, input().split())
	elif p == 2:
		return list(map(int, input().split()))
	else:
		return sorted(list(map(int, input().split())))
n = f()
l = input()
r = input()
dl1 = [0] * 26
dl2 = [0] * 26
pos1 = [list() for i in range(26)]
pos2 = [list() for i in range(26)]
posp1 = []
posp2 = []
count1 = 0
count2 = 0
for i in range(n):
	if l[i] == '?':
		count1 += 1
		posp1.append(i + 1)
	else:
		dl1[ord(l[i]) % 97] += 1
		pos1[ord(l[i]) % 97].append(i + 1)
	if r[i] == '?':
		count2 += 1
		posp2.append(i + 1)
	else:
		dl2[ord(r[i]) % 97] += 1
		pos2[ord(r[i]) % 97].append(i + 1)
mxpos = 0
mx = 0
res1 = []
res2 = []
old1 = list(dl1)
old2 = list(dl2)
for i in range(26):
	d = min(dl1[i], dl2[i])
	dl1[i] -= d
	dl2[i] -= d
	mx += d
	if d != 0:
		res1 += pos1[i][:d]
		res2 += pos2[i][:d]
bz1 = []
bz2 = []
ln1 = 0
ln2 = 0
for i in range(26):
	if dl1[i] != 0:
		for j in range(dl1[i]):
			bz1.append(pos1[i][old1[i] - 1 - j])
			ln1 += 1
	if dl2[i] != 0:
		for j in range(dl2[i]):
			bz2.append(pos2[i][old2[i] - 1 - j])
			ln2 += 1
ans = mx + min(n - mx, count1 + count2)
k1 = min(count1, ln2)
print(ans)
for i in range(k1):
	print(posp1[i], bz2[i])
cc1 = count1
cc2 = count2
count1 -= k1
ln1 += count1
for i in range(count1):
	bz1.append(posp1[cc1 - 1 - i])
ans -= mx + k1
k2 = min(count2, ln1)
for i in range(k2):
	print(bz1[i], posp2[i])
for i in range(mx):
	print(res1[i], res2[i])


nBoots = int(input())
left = input().strip()
right = input().strip()
SIZE = 27
CASE = ord('a')
leftChars = [[] for _ in range(SIZE)]
rightChars = [[] for _ in range(SIZE)]
for i in range(nBoots):
	if left[i] != '?':
		leftChars[ord(left[i]) - CASE].append(i)
	else:
		leftChars[-1].append(i)
	if right[i] != '?':
		rightChars[ord(right[i]) - CASE].append(i)
	else:
		rightChars[-1].append(i)
ret = []
for i in range(SIZE - 1):
	if leftChars[i] or rightChars[i]:
		j = 0
		while j < min(len(leftChars[i]), len(rightChars[i])):
			ret.append((leftChars[i][j], rightChars[i][j]))
			j += 1
		while j < len(leftChars[i]) and rightChars[-1]:
			ret.append((leftChars[i][j], rightChars[-1][-1]))
			rightChars[-1].pop()
			j += 1
		while j < len(rightChars[i]) and leftChars[-1]:
			ret.append((leftChars[-1][-1], rightChars[i][j]))
			leftChars[-1].pop()
			j += 1
i = 0
while i < min(len(leftChars[-1]), len(rightChars[-1])):
	ret.append((leftChars[-1][i], rightChars[-1][i]))
	i += 1
print(len(ret), '\n'.join(('%d %d' % (l + 1, r + 1) for (l, r) in ret)), sep='\n')


def main():
	n = int(input())
	word_1 = input()
	word_2 = input()
	ans_d1 = list()
	ans_d2 = list()
	ans = list()
	from collections import defaultdict
	d1 = defaultdict(list)
	d2 = defaultdict(list)
	for index in range(0, n):
		d1[word_1[index]].append(index + 1)
		d2[word_2[index]].append(index + 1)
	common_keys = [key for key in d1 if key in d2 and key != '?']
	d1_unique_indices = list()
	d2_unique_indices = list()
	d1_question_indices = d1['?']
	d2_question_indices = d2['?']
	for common_key in common_keys:
		d1_common_index = d1[common_key]
		d2_common_index = d2[common_key]
		while d1_common_index and d2_common_index:
			d1_value = d1_common_index.pop()
			d2_value = d2_common_index.pop()
			ans.append((d1_value, d2_value))
		if d1_common_index:
			d1_unique_indices.extend(d1_common_index)
		if d2_common_index:
			d2_unique_indices.extend(d2_common_index)
	for index in range(26):
		char = chr(97 + index)
		if char not in d1['?'] and char not in ans_d1:
			d1_unique_indices.extend(d1[char])
		if char not in d2['?'] and char not in ans_d2:
			d2_unique_indices.extend(d2[char])
	while d1_unique_indices and d2_question_indices:
		ans.append((d1_unique_indices.pop(), d2_question_indices.pop()))
	while d1_question_indices and d2_unique_indices:
		ans.append((d1_question_indices.pop(), d2_unique_indices.pop()))
	while d1_question_indices and d2_question_indices:
		ans.append((d1_question_indices.pop(), d2_question_indices.pop()))
	print(len(ans))
	for a in ans:
		print(str(a[0]) + ' ' + str(a[1]))
main()


from sys import setcheckinterval, stdin
setcheckinterval(1000)
from collections import defaultdict
iin = lambda : int(stdin.readline())
lin = lambda : list(map(int, stdin.readline().split()))
n = iin()
a = input()
b = input()
l = defaultdict(list)
cntl = defaultdict(int)
for i in range(n):
	l[a[i]].append(i + 1)
cl = len(l['?'])
left = defaultdict(list)
ans = []
for i in range(n):
	if b[i] == '?':
		left['?'].append(i + 1)
		continue
	if b[i] in l:
		if l[b[i]]:
			ans.append((l[b[i]].pop(), i + 1))
		else:
			left[b[i]].append(i + 1)
	else:
		left[b[i]].append(i + 1)
cr = len(left['?'])
if cl != 0:
	for i in left:
		if i == '?':
			continue
		if cl <= 0:
			break
		while left[i] and cl > 0:
			ans.append((l['?'].pop(), left[i].pop()))
			cl -= 1
if cr != 0:
	for i in l:
		if l[i]:
			while l[i] and cr > 0:
				ans.append((l[i].pop(), left['?'].pop()))
				cr -= 1
print(len(ans))
for (i, j) in ans:
	print(i, j)


num = int(input())
dicl = {}
dicr = {}
for letter in 'qwertyuiopasdfghjklzxcvbnm':
	dicl[letter] = []
	dicr[letter] = []
left = input()
right = input()
leftq = []
rightq = []
printl = []
for i in range(num):
	l = left[i]
	r = right[i]
	if l == '?':
		leftq.append(i)
	else:
		dicl[l].append(i)
		if len(dicl[l]) * len(dicr[l]) > 0:
			printl.append(str(dicl[l].pop() + 1) + ' ' + str(dicr[l].pop() + 1))
	if r == '?':
		rightq.append(i)
	else:
		dicr[r].append(i)
		if len(dicl[r]) * len(dicr[r]) > 0:
			printl.append(str(dicl[r].pop() + 1) + ' ' + str(dicr[r].pop() + 1))
finall = []
finalr = []
for letter in 'qwertyuiopasdfghjklzxcvbnm':
	if len(dicl[letter]) > 0:
		finall += dicl[letter]
	if len(dicr[letter]) > 0:
		finalr += dicr[letter]
for e in range(len(finall)):
	if len(rightq) == 0:
		break
	printl.append(str(finall.pop() + 1) + ' ' + str(rightq.pop() + 1))
for e in range(len(finalr)):
	if len(leftq) == 0:
		break
	printl.append(str(leftq.pop() + 1) + ' ' + str(finalr.pop() + 1))
for i in range(min(len(rightq), len(leftq))):
	printl.append(str(leftq.pop() + 1) + ' ' + str(rightq.pop() + 1))
print(len(printl))
for p in printl:
	print(p)


from sys import stdin
n = int(stdin.readline().strip())
s = stdin.readline().strip()
s1 = stdin.readline().strip()
d = dict()
d1 = dict()
for i in range(26):
	d[chr(97 + i)] = []
	d1[chr(97 + i)] = []
d['?'] = []
d1['?'] = []
for i in range(n):
	d[s[i]].append(i)
	d1[s1[i]].append(i)
ans = []
for i in range(97, 97 + 26):
	while len(d[chr(i)]) > 0 and len(d1[chr(i)]) > 0:
		ans.append([d[chr(i)][-1] + 1, d1[chr(i)][-1] + 1])
		d[chr(i)].pop()
		d1[chr(i)].pop()
x = 0
while len(d['?']) > 0 and x < 26:
	while len(d['?']) > 0 and len(d1[chr(x + 97)]) > 0:
		ans.append([d['?'][-1] + 1, d1[chr(x + 97)][-1] + 1])
		d['?'].pop()
		d1[chr(97 + x)].pop()
	x += 1
x = 0
while len(d1['?']) > 0 and x < 26:
	while len(d1['?']) > 0 and len(d[chr(x + 97)]) > 0:
		ans.append([d[chr(x + 97)][-1] + 1, d1['?'][-1] + 1])
		d1['?'].pop()
		d[chr(97 + x)].pop()
	x += 1
while len(d1['?']) > 0 and len(d['?']) > 0:
	ans.append([d['?'][-1] + 1, d1['?'][-1] + 1])
	d1['?'].pop()
	d['?'].pop()
print(len(ans))
for i in ans:
	print(i[0], i[1])


from sys import stdin, stdout
from collections import defaultdict, Counter, deque
from bisect import bisect, bisect_left
import math
from itertools import permutations
import queue
I = stdin.readline
n = int(I())
right = [[] for i in range(27)]
left = [[] for i in range(27)]
l = I().rstrip()
r = I().rstrip()
for i in range(n):
	if l[i] == '?':
		left[26].append(i + 1)
	else:
		left[ord(l[i]) - ord('a')].append(i + 1)
for i in range(n):
	if r[i] == '?':
		right[26].append(i + 1)
	else:
		right[ord(r[i]) - ord('a')].append(i + 1)
ans = []
left_left = []
right_left = []
for i in range(26):
	for j in range(min(len(left[i]), len(right[i]))):
		ans.append([left[i][j], right[i][j]])
	if len(left[i]) > len(right[i]):
		j = len(right[i])
		if j == 0:
			j -= 1
		for k in range(j + 1, len(left[i])):
			left_left.append(left[i][k])
	elif len(right[i]) > len(left[i]):
		j = len(left[i])
		if j == 0:
			j -= 1
		for k in range(j + 1, len(right[i])):
			right_left.append(right[i][k])
j = 0
left_left_left = []
right_left_left = []
for j in range(0, min(len(left[26]), len(right_left))):
	ans.append([left[26][j], right_left[j]])
j = len(right_left)
for k in range(j, len(left[26])):
	left_left_left.append(left[26][k])
for j in range(0, min(len(right[26]), len(left_left))):
	ans.append([left_left[j], right[26][j]])
j = len(left_left)
for k in range(j, len(right[26])):
	right_left_left.append(right[26][k])
for i in range(min(len(right_left_left), len(left_left_left))):
	ans.append([left_left_left[i], right_left_left[i]])
print(len(ans))
for i in ans:
	print(i[0], i[1])


from collections import defaultdict
input()
cl = defaultdict(list)
cr = defaultdict(list)
for (i, c) in enumerate(input()):
	cl[c].append(i)
for (i, c) in enumerate(input()):
	cr[c].append(i)
pairs = []
left_left = []
right_left = []
for c in 'abcdefghijklmnopqrstuvwxyz':
	ll = cl[c]
	lr = cr[c]
	np = min(len(ll), len(lr))
	for i in range(np):
		pairs.append((ll[i], lr[i]))
	if len(ll) > np:
		left_left.extend(ll[np:])
	elif len(lr) > np:
		right_left.extend(lr[np:])
pairs.extend(zip(left_left, cr['?']))
cr['?'] = cr['?'][len(left_left):]
pairs.extend(zip(cl['?'], right_left))
cl['?'] = cl['?'][len(right_left):]
pairs.extend(zip(cl['?'], cr['?']))
print(len(pairs))
for (a, b) in pairs:
	print(a + 1, b + 1)


n = int(input())
a = input()
b = input()
l = []
k = []
cnt = [0] * 28
ans = 0
f = s = 0
r = []
q1 = []
q2 = []
MAX = 150010
use1 = [0] * MAX
use2 = [0] * MAX
uuqq1 = [0] * MAX
uuqq2 = [0] * MAX
for i in range(n):
	if a[i] != '?':
		l.append([a[i], i])
	else:
		f += 1
		q1.append(i)
	if b[i] != '?':
		k.append([b[i], i])
	else:
		s += 1
		q2.append(i)
l.sort()
k.sort()
fi = se = 0
while fi < len(l) and se < len(k):
	if l[fi][0] == k[se][0]:
		r.append([l[fi][1], k[se][1]])
		use1[fi] = 1
		use2[se] = 1
		fi += 1
		se += 1
	elif ord(l[fi][0]) < ord(k[se][0]):
		fi += 1
	elif ord(l[fi][0]) > ord(k[se][0]):
		se += 1
z = 0
m = 0
while z < f and m < len(k):
	if use2[m] == 0 and uuqq1[q1[z]] == 0:
		uuqq1[q1[z]] = 1
		use2[m] = 1
		r.append([q1[z], k[m][1]])
		m += 1
		z += 1
	elif use2[m] == 1:
		m += 1
	elif uuqq1[q1[z]] == 1:
		z += 1
z = 0
m = 0
while z < s and m < len(l):
	if use1[m] == 0 and uuqq2[q2[z]] == 0:
		use1[m] = 1
		uuqq2[q2[z]] = 1
		r.append([l[m][1], q2[z]])
		m += 1
		z += 1
	elif use1[m] == 1:
		m += 1
	elif uuqq2[q2[z]] == 1:
		z += 1
z = 0
m = 0
while z < f and m < s:
	if uuqq1[q1[z]] == 0 and uuqq2[q2[m]] == 0:
		uuqq1[q1[z]] = 1
		uuqq2[q2[m]] = 1
		r.append([q1[z], q2[m]])
		z += 1
		m += 1
	elif uuqq1[q1[z]] == 1:
		z += 1
	elif uuqq2[q2[m]] == 1:
		m += 1
print(len(r))
for i in range(len(r)):
	print(r[i][0] + 1, r[i][1] + 1)


n = int(input())
l = input()
r = input()
idl = {}
idr = {}
frl = {}
frli = {}
frr = {}
frri = {}
c = 1
for i in l:
	try:
		frl[i] += 1
		frli[i].append(c)
	except KeyError:
		frl[i] = 1
		frli[i] = [c]
	c += 1
c = 1
for i in r:
	try:
		frr[i] += 1
		frri[i].append(c)
	except KeyError:
		frr[i] = 1
		frri[i] = [c]
	c += 1
out = []
ans = 0
for i in frl:
	for j in frr:
		if i == j and j != '?':
			ans += min(frl[i], frr[j])
			x = min(frl[i], frr[j])
			frl[i] -= x
			frr[j] -= x
			while x:
				out.append((frli[i][-1], frri[j][-1]))
				frli[i].pop()
				frri[j].pop()
				x -= 1
for i in frl:
	if frl[i] > 0:
		for j in frr:
			if frr[j] > 0:
				if i == j == '?':
					continue
				elif i == '?' or j == '?':
					ans += min(frl[i], frr[j])
					x = min(frl[i], frr[j])
					frl[i] -= x
					frr[j] -= x
					while x:
						out.append((frli[i][-1], frri[j][-1]))
						frli[i].pop()
						frri[j].pop()
						x -= 1
if '?' in l and '?' in r:
	ans += min(frr['?'], frl['?'])
	x = min(frr['?'], frl['?'])
	while x:
		out.append((frli['?'][-1], frri['?'][-1]))
		frli['?'].pop()
		frri['?'].pop()
		x -= 1
print(ans)
for i in out:
	print(*i)


n = int(input())
l = list(input())
r = list(input())
A = {}
for (i, x) in enumerate(r):
	if A.get(x):
		A[x][0] += 1
		A[x][1].append(i + 1)
	else:
		A[x] = [1, [i + 1]]
ans = 0
pairs = []
qm = 0
res = []
for (i, x) in enumerate(l):
	if x == '?':
		qm += 1
		res.append(i + 1)
		continue
	if A.get(x):
		if A[x][0] > 0:
			A[x][0] -= 1
			pairs.append([i + 1, A[x][1].pop()])
			ans += 1
			continue
	if A.get('?'):
		if A['?'][0] > 0:
			A['?'][0] -= 1
			pairs.append([i + 1, A['?'][1].pop()])
			ans += 1
			continue
tt = 0
z = 0
for key in A.keys():
	for j in range(A[key][0]):
		if tt == qm:
			break
		tt += 1
		pairs.append([res[z], A[key][1][j]])
		z += 1
print(ans + tt)
for i in pairs:
	print(*i)


qm1 = []
qm2 = []
dic = {}
rm1 = []
rm2 = []
pairs = []
n = int(input())
temp = input()
s1 = []
for i in temp:
	if i != ' ':
		s1 += i
temp = input()
s2 = []
for i in temp:
	if i != ' ':
		s2 += i
for i in range(n):
	if s1[i] == '?':
		qm1.append(i)
	elif s1[i] not in dic:
		dic[s1[i]] = [i]
	else:
		dic[s1[i]].append(i)
	if s2[i] == '?':
		qm2.append(i)
for i in range(n):
	if s2[i] in dic and len(dic[s2[i]]):
		pairs.append((dic[s2[i]].pop(), i))
	elif s2[i] != '?':
		rm2.append(i)
for i in dic:
	rm1 += dic[i]
while len(qm1) and len(rm2):
	pairs.append((qm1.pop(), rm2.pop()))
while len(qm2) and len(rm1):
	pairs.append((rm1.pop(), qm2.pop()))
while len(qm2) and len(qm1):
	pairs.append((qm1.pop(), qm2.pop()))
print(len(pairs))
for (a, b) in pairs:
	print(a + 1, b + 1)


import sys, math, queue
sys.setrecursionlimit(1000000)
n = int(input())
l = list(input())
r = list(input())
ls = {chr(i): [] for i in range(97, 97 + 26)}
rs = {chr(i): [] for i in range(97, 97 + 26)}
lc = []
rc = []
for i in range(n):
	if l[i] == '?':
		lc.append(i)
	else:
		ls[l[i]].append(i)
	if r[i] == '?':
		rc.append(i)
	else:
		rs[r[i]].append(i)
for k in ls:
	if len(ls[k]) > len(rs[k]):
		while len(rc) != 0 and len(ls[k]) != len(rs[k]):
			rs[k].append(rc[-1])
			rc.pop()
	elif len(ls[k]) < len(rs[k]):
		while len(lc) != 0 and len(rs[k]) != len(ls[k]):
			ls[k].append(lc[-1])
			lc.pop()
pairs = []
for k in ls:
	for i in range(min(len(rs[k]), len(ls[k]))):
		pairs.append((ls[k][i] + 1, rs[k][i] + 1))
for i in range(min(len(rc), len(lc))):
	pairs.append((lc[i] + 1, rc[i] + 1))
print(len(pairs))
for pair in pairs:
	print(*pair)


from collections import defaultdict
import string
input()
s1 = input()
s2 = input()
c1 = defaultdict(list)
c2 = defaultdict(list)
for (idx, x) in enumerate(s1):
	c1[x].append(idx)
for (idx, x) in enumerate(s2):
	c2[x].append(idx)
r = []
r1 = []
r2 = []
for c in string.ascii_lowercase:
	new_pairs = list(zip(c1[c], c2[c]))
	r.extend(new_pairs)
	if len(c1[c]) > len(c2[c]):
		r1.extend(c1[c][len(new_pairs):])
	elif len(c1[c]) < len(c2[c]):
		r2.extend(c2[c][len(new_pairs):])
u1 = list(zip(c1['?'], r2))
u2 = list(zip(r1, c2['?']))
r.extend(u1)
r.extend(u2)
r.extend(zip(c1['?'][len(u1):], c2['?'][len(u2):]))
print(len(r))
for (a, b) in r:
	print(a + 1, b + 1)


input()
l = list(input())
r = list(input())
chars = [[] for _ in range(26)]
matches = []
matched_l = [False] * len(l)
matched_r = [False] * len(r)
wildcards_l = []
wildcards_r = []
for (i, c) in enumerate(l):
	if c == '?':
		wildcards_l.append(i)
		continue
	chars[ord(c) - 97].append(i)
for (i, c) in enumerate(r):
	if c == '?':
		wildcards_r.append(i)
		continue
	if len(chars[ord(c) - 97]) == 0:
		continue
	li = chars[ord(c) - 97].pop()
	matches.append((li, i))
	matched_l[li] = True
	matched_r[i] = True
for i in range(len(r)):
	if not wildcards_l:
		break
	if matched_r[i] or r[i] == '?':
		continue
	matched_r[i] = True
	matches.append((wildcards_l.pop(), i))
for i in range(len(l)):
	if not wildcards_r:
		break
	if matched_l[i] or l[i] == '?':
		continue
	matched_l[i] = True
	matches.append((i, wildcards_r.pop()))
while wildcards_l and wildcards_r:
	matches.append((wildcards_l.pop(), wildcards_r.pop()))
print(len(matches))
for m in matches:
	print(m[0] + 1, m[1] + 1)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

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')
from collections import *
from operator import itemgetter, attrgetter
from decimal import *
import bisect
import math
import heapq as hq
MOD = 10 ** 9 + 7

def pow(a, b, m):
	ans = 1
	while b:
		if b & 1:
			ans = ans * a % m
		b //= 2
		a = a * a % m
	return ans
vis = []
graph = []

def dfs(v):
	if vis[v]:
		return 0
	vis[v] = True
	temp = 0
	for vv in graph[v]:
		temp += dfs(vv)
	return 1 + temp

def ispalindrome(s):
	if s[:] == s[::-1]:
		return 1
	return 0

def f(n):
	if n < 5:
		return max(1, n - 1)
	if n % 2 or n % 4 == 0:
		return n - f(n - 1)
	return n - f(n // 2)
n = int(input())
q1 = 0
q2 = 0
s1 = input()
s2 = input()
l1 = []
l2 = []
for i in range(n):
	if s1[i] == '?':
		q1 += 1
		l1.append(['zz', i + 1])
	else:
		l1.append([s1[i], i + 1])
	if s2[i] == '?':
		q2 += 1
		l2.append(['zz', i + 1])
	else:
		l2.append([s2[i], i + 1])
i = 0
j = 0
count = 0
ans = []
l1.sort()
l2.sort()
temp1 = [i for i in range(n + 1)]
temp2 = [i for i in range(n + 1)]
while i < n and j < n:
	if l1[i][0] == 'zz' or l2[j][0] == 'zz':
		break
	if l1[i][0] == l2[j][0]:
		count += 1
		ans.append([l1[i][1], l2[j][1]])
		temp1[l1[i][1]] = -1
		temp2[l2[j][1]] = -1
		i += 1
		j += 1
	elif l1[i][0] < l2[j][0]:
		i += 1
	else:
		j += 1
i = 0
t = min(n, count + q1 + q2) - len(ans)
for j in range(0, min(q1, t)):
	while temp2[l2[i][1]] == -1:
		i += 1
	temp2[l2[i][1]] = -1
	ans.append([l1[n - j - 1][1], l2[i][1]])
i = 0
t = min(n, count + q1 + q2) - len(ans)
for j in range(0, t):
	while temp1[l1[i][1]] == -1:
		i += 1
	temp1[l1[i][1]] = -1
	ans.append([l1[i][1], l2[n - j - 1][1]])
print(min(n, count + q1 + q2))
for i in range(len(ans)):
	print(*ans[i])


from collections import defaultdict
import string
(n, l, r) = [input(), input(), input()]
loc = defaultdict(lambda : [])
roc = defaultdict(lambda : [])
for (i, [lc, rc]) in enumerate(zip(l, r)):
	loc[lc].append(i)
	roc[rc].append(i)
ans = []
lleft = []
rleft = []
for color in string.ascii_lowercase:
	matches = list(zip(loc[color], roc[color]))
	ans += matches
	lleft.extend(loc[color][len(matches):])
	rleft.extend(roc[color][len(matches):])
lmatches = list(zip(loc['?'], rleft))
rmatches = list(zip(lleft, roc['?']))
ans += lmatches
ans += rmatches
ans += list(zip(loc['?'][len(lmatches):], roc['?'][len(rmatches):]))
print(len(ans))
for (x, y) in ans:
	print(str(x + 1) + ' ' + str(y + 1))


from collections import *
n = int(input())
a = list(input())
b = list(input())
ans = []
cb = dict()
cb['?'] = []
for i in range(n):
	if b[i] not in cb:
		cb[b[i]] = [i]
	else:
		cb[b[i]].append(i)
for i in range(n):
	if a[i] == '?':
		continue
	if a[i] in cb and len(cb[a[i]]) != 0:
		ans.append((i, cb[a[i]][-1]))
		cb[a[i]].pop()
visa = [0 for i in range(n)]
visb = [0 for i in range(n)]
for i in ans:
	visa[i[0]] = 1
	visb[i[1]] = 1
ch = []
for i in range(n):
	if visa[i] == 0:
		ch.append([a[i], i])
ch.sort()
for i in cb['?']:
	ans.append((ch[-1][1], i))
	visb[i] = 1
	ch.pop()
chb = []
for i in range(n):
	if visb[i] == 0:
		chb.append(i)
for i in ch:
	if i[0] != '?':
		break
	ans.append((i[1], chb[-1]))
	chb.pop()
print(len(ans))
for i in ans:
	print(i[0] + 1, i[1] + 1)


import sys

def input():
	return sys.stdin.readline().strip()

def print(*x, sep=' ', end='\n'):
	return sys.stdout.write(sep.join(map(str, x)) + end)

def read():
	return sys.stdin.read()

def get():
	return (int(input()), [i for i in input()], [i for i in input()])

def out(*arg):
	if type(arg[0]) == list:
		for i in arg[0]:
			print(i)
	else:
		print(arg)

def main():
	(n, s1, s2) = get()
	d1 = {'l': 0, 'h': 0, 'u': 0, 'p': 0, 'q': 0, 'g': 0, 'n': 0, 's': 0, 'f': 0, 'e': 0, 'y': 0, 'd': 0, 'x': 0, 'b': 0, 'm': 0, 'w': 0, 'r': 0, 'a': 0, 'k': 0, 'j': 0, 'c': 0, 't': 0, '?': 0, 'i': 0, 'z': 0, 'v': 0, 'o': 0}
	d2 = {'l': 0, 'h': 0, 'u': 0, 'p': 0, 'q': 0, 'g': 0, 'n': 0, 's': 0, 'f': 0, 'e': 0, 'y': 0, 'd': 0, 'x': 0, 'b': 0, 'm': 0, 'w': 0, 'r': 0, 'a': 0, 'k': 0, 'j': 0, 'c': 0, 't': 0, '?': 0, 'i': 0, 'z': 0, 'v': 0, 'o': 0}
	memlimit1 = {'i': [], 'e': [], 'u': [], 'q': [], 'v': [], 'c': [], 'w': [], 'o': [], 's': [], 'b': [], 'n': [], 'h': [], 'z': [], 'x': [], 'k': [], 'a': [], 'f': [], 'd': [], 'y': [], 'j': [], 't': [], 'r': [], 'l': [], 'g': [], 'm': [], '?': [], 'p': []}
	memlimit2 = {'i': [], 'e': [], 'u': [], 'q': [], 'v': [], 'c': [], 'w': [], 'o': [], 's': [], 'b': [], 'n': [], 'h': [], 'z': [], 'x': [], 'k': [], 'a': [], 'f': [], 'd': [], 'y': [], 'j': [], 't': [], 'r': [], 'l': [], 'g': [], 'm': [], '?': [], 'p': []}
	for i in range(n):
		d1[s1[i]] += 1
		memlimit1[s1[i]].append(i + 1)
		d2[s2[i]] += 1
		memlimit2[s2[i]].append(i + 1)
	count = 0
	ans = []
	for i in d1:
		if i != '?':
			e = min(d1[i], d2[i])
			if e != 0:
				count += e
				d1[i] -= e
				d2[i] -= e
				for j in range(e):
					ans.append((memlimit1[i][j], memlimit2[i][j]))
				memlimit1[i] = memlimit1[i][e:]
				memlimit2[i] = memlimit2[i][e:]
	for i in d2:
		if i != '?' and d2[i] != 0:
			e = min(d1['?'], d2[i])
			d1['?'] -= e
			d2[i] -= e
			count += e
			for j in range(e):
				ans.append((memlimit1['?'][j], memlimit2[i][j]))
			memlimit1['?'] = memlimit1['?'][e:]
			memlimit2[i] = memlimit2[i][e:]
		if d1['?'] == 0:
			break
	for i in d1:
		if i != '?' and d1[i] != 0:
			e = min(d2['?'], d1[i])
			d2['?'] -= e
			d1[i] -= e
			count += e
			for j in range(e):
				ans.append((memlimit1[i][j], memlimit2['?'][j]))
			memlimit1[i] = memlimit1[i][e:]
			memlimit2['?'] = memlimit2['?'][e:]
		if d2['?'] == 0:
			break
	if d1['?'] != 0 and d2['?'] != 0:
		e = min(d1['?'], d2['?'])
		count += e
		for j in range(e):
			ans.append((memlimit1['?'][j], memlimit2['?'][j]))
	print(count)
	for i in range(count):
		print(ans[i][0], ans[i][1])
main()


from collections import defaultdict
n = int(input())
l = input()
r = input()
l_dict = defaultdict(list)
r_dict = defaultdict(list)
for i in range(n):
	l_dict[l[i]].append(i + 1)
	r_dict[r[i]].append(i + 1)
ans = []
for i in range(26):
	c = chr(97 + i)
	while len(l_dict[c]) > 0 and len(r_dict[c]) > 0:
		ans.append((l_dict[c][-1], r_dict[c][-1]))
		l_dict[c].pop()
		r_dict[c].pop()
	while len(l_dict[c]) > 0 and len(r_dict['?']) > 0:
		ans.append((l_dict[c][-1], r_dict['?'][-1]))
		l_dict[c].pop()
		r_dict['?'].pop()
	while len(l_dict['?']) > 0 and len(r_dict[c]) > 0:
		ans.append((l_dict['?'][-1], r_dict[c][-1]))
		l_dict['?'].pop()
		r_dict[c].pop()
while len(l_dict['?']) > 0 and len(r_dict['?']) > 0:
	ans.append((l_dict['?'][-1], r_dict['?'][-1]))
	l_dict['?'].pop()
	r_dict['?'].pop()
print(len(ans))
for i in ans:
	print(str(i[0]) + ' ' + str(i[1]))


from collections import defaultdict

def solve(left, right):
	ans = 0
	pairs = []
	leftq = 0
	rightq = 0
	lookup = defaultdict(set)
	for (i, color) in enumerate(left):
		lookup[color].add(i + 1)
	right_qs = set()
	for (i, color) in enumerate(right):
		if color != '?':
			if color in lookup:
				pairs.append((lookup[color].pop(), i + 1))
				if not lookup[color]:
					del lookup[color]
			elif '?' in lookup:
				pairs.append((lookup['?'].pop(), i + 1))
				if not lookup['?']:
					del lookup['?']
		else:
			right_qs.add(i + 1)
	for (color, indexes) in lookup.items():
		if not right_qs:
			break
		for i in indexes:
			pairs.append((i, right_qs.pop()))
			if not right_qs:
				break
	print(len(pairs))
	for (l, r) in pairs:
		print('%d %d' % (l, r))
n = int(input())
left = input()
right = input()
solve(left, right)


n = int(input())
l1 = list(input())
r1 = list(input())
l = [None] * n
r = [None] * n
nl = 0
nr = 0
for i in range(n):
	l[i] = [l1[i], i + 1]
	if l1[i] == '?':
		nl += 1
	r[i] = [r1[i], i + 1]
	if r1[i] == '?':
		nr += 1
ans = 0
l.sort()
r.sort()
(x, y) = (n - 1, n - 1)
ar = [None] * n
ctr = 0
ctl = 0
while x >= 0 and y >= 0 and (l[x][0] != '?') and (r[y][0] != '?'):
	if l[x][0] == r[y][0]:
		ans += 1
		ar[ans - 1] = [l[x][1], r[y][1]]
		x -= 1
		y -= 1
	elif l[x][0] < r[y][0]:
		if nl - ctl > 0:
			ans += 1
			ctl += 1
			ar[ans - 1] = [l[ctl - 1][1], r[y][1]]
		y -= 1
	else:
		if nr - ctr > 0:
			ans += 1
			ctr += 1
			ar[ans - 1] = [l[x][1], r[ctr - 1][1]]
		x -= 1
if x >= 0 and y >= 0:
	while nl - ctl > 0 and x >= 0 and (y >= 0) and (l[x][0] == '?') and (r[y][0] != '?'):
		ans += 1
		ctl += 1
		ar[ans - 1] = [l[ctl - 1][1], r[y][1]]
		y -= 1
	while nr - ctr > 0 and x >= 0 and (y >= 0) and (l[x][0] != '?') and (r[y][0] == '?'):
		ans += 1
		ctr += 1
		ar[ans - 1] = [l[x][1], r[ctr - 1][1]]
		x -= 1
while nr - ctr > 0 and nl - ctl > 0:
	ans += 1
	ctl += 1
	ctr += 1
	ar[ans - 1] = [l[ctl - 1][1], r[ctr - 1][1]]
print(ans)
for i in range(ans):
	print(ar[i][0], ar[i][1])


n = int(input())
l = input()
r = input()
l = sorted(enumerate(l), key=lambda x: x[1])
l_ = []
lq = []
for (pos, c) in l:
	if c == '?':
		lq.append(pos)
	else:
		l_.append((pos, c))
r = sorted(enumerate(r), key=lambda x: x[1])
r_ = []
rq = []
for (pos, c) in r:
	if c == '?':
		rq.append(pos)
	else:
		r_.append((pos, c))
pairs = []
i = j = 0
while i < len(l_) and j < len(r_):
	(posl, cl) = l_[i]
	(posr, cr) = r_[j]
	if cl == cr:
		pairs.append((posl, posr))
		i += 1
		j += 1
	elif cl < cr:
		if rq:
			pairs.append((posl, rq.pop(0)))
		i += 1
	else:
		if lq:
			pairs.append((lq.pop(0), posr))
		j += 1
while i < len(l_) and rq:
	(posl, cl) = l_[i]
	pairs.append((posl, rq.pop(0)))
	i += 1
while j < len(r_) and lq:
	(posr, cr) = r_[j]
	pairs.append((lq.pop(0), posr))
	j += 1
for (posl, posr) in zip(lq, rq):
	pairs.append((posl, posr))
print(len(pairs))
for (posl, posr) in pairs:
	print(posl + 1, posr + 1)


def main():
	n = int(input())
	l = input()
	r = input()
	left = []
	right = []
	left_ques = []
	right_ques = []
	for i in range(n):
		if l[i] != '?':
			left.append((l[i], i + 1))
		else:
			left_ques.append(i + 1)
		if r[i] != '?':
			right.append((r[i], i + 1))
		else:
			right_ques.append(i + 1)
	left.sort()
	right.sort()
	ans = []
	li = 0
	ri = 0
	ql = 0
	qr = 0
	while li < len(left) and ri < len(right):
		if left[li][0] == right[ri][0]:
			ans.append((left[li][1], right[ri][1]))
			li += 1
			ri += 1
		elif left[li][0] > right[ri][0]:
			if ql < len(left_ques):
				ans.append((left_ques[ql], right[ri][1]))
				ql += 1
			ri += 1
		elif right[ri][0] > left[li][0]:
			if qr < len(right_ques):
				ans.append((left[li][1], right_ques[qr]))
				qr += 1
			li += 1
	while li < len(left):
		if qr < len(right_ques):
			ans.append((left[li][1], right_ques[qr]))
			qr += 1
		li += 1
	while ri < len(right):
		if ql < len(left_ques):
			ans.append((left_ques[ql], right[ri][1]))
			ql += 1
		ri += 1
	while ql < len(left_ques) and qr < len(right_ques):
		ans.append((left_ques[ql], right_ques[qr]))
		ql += 1
		qr += 1
	print(len(ans))
	for i in ans:
		print(i[0], i[1])
main()


def index(s):
	result = [[] for i in range(27)]
	for i in range(len(s)):
		if s[i] == '?':
			result[-1].append(i + 1)
		else:
			result[ord(s[i]) - ord('a')].append(i + 1)
	return result

def solve(s1, s2):
	(i1, i2) = (index(s1), index(s2))
	result = []
	for (a, b) in zip(i1[:26], i2[:26]):
		while a and b:
			result.append((a.pop(), b.pop()))
	for l in i2:
		if not i1[-1]:
			break
		while i1[-1] and l:
			result.append((i1[-1].pop(), l.pop()))
	for l in i1:
		if not i2[-1]:
			break
		while i2[-1] and l:
			result.append((l.pop(), i2[-1].pop()))
	while i1[-1] and i2[-1]:
		result.append(i1[-1].pop(), i2[-1].pop())
	return result

def main():
	n = int(input())
	s1 = input()
	s2 = input()
	result = solve(s1, s2)
	print(len(result))
	for (a, b) in result:
		print(a, b)
main()


n = int(input())
str1 = input()
str2 = input()
len1_ = len(str1)
len2_ = len(str2)
dict1 = {}
dict2 = {}
for i in range(len1_):
	mapp = dict1.get(str1[i], [])
	mapp.append(i)
	dict1[str1[i]] = mapp
	mapp = dict2.get(str2[i], [])
	mapp.append(i)
	dict2[str2[i]] = mapp
letters = [chr(i) for i in range(97, 123)]
res = []
for i in letters:
	if i in dict1 and i in dict2:
		l1 = dict1[i]
		l2 = dict2[i]
		while l1 and l2:
			res.append([l1.pop() + 1, l2.pop() + 1])
if '?' in dict1:
	l1 = dict1['?']
else:
	l1 = []
if '?' in dict2:
	l2 = dict2['?']
else:
	l2 = []
for i in letters:
	if i in dict1:
		ss = dict1[i]
		while ss and l2:
			res.append([ss.pop() + 1, l2.pop() + 1])
for i in letters:
	if i in dict2:
		ss = dict2[i]
		while l1 and ss:
			res.append([l1.pop() + 1, ss.pop() + 1])
while l1 and l2:
	res.append([l1.pop() + 1, l2.pop() + 1])
print(len(res))
for i in res:
	out = list(map(str, i))
	print(' '.join(out))


from sys import stdin
from collections import defaultdict
input = stdin.readline
(n, l, r) = (int(input()), input().strip(), input().strip())
(ld, rd, ans) = (defaultdict(lambda : []), defaultdict(lambda : []), [])
for (i, j) in enumerate(l):
	ld[j].append(i + 1)
for (i, j) in enumerate(r):
	rd[j].append(i + 1)
for i in range(26):
	c = chr(i + 97)
	while len(ld[c]) != 0 and len(rd[c]) != 0:
		ans.append((ld[c].pop(), rd[c].pop()))
for i in range(26):
	c = chr(i + 97)
	while len(ld['?']) != 0 and len(rd[c]) != 0:
		ans.append((ld['?'].pop(), rd[c].pop()))
	while len(ld[c]) != 0 and len(rd['?']) != 0:
		ans.append((ld[c].pop(), rd['?'].pop()))
while len(ld['?']) != 0 and len(rd['?']) != 0:
	ans.append((ld['?'].pop(), rd['?'].pop()))
print(len(ans))
[print(*i) for i in ans]


from functools import reduce
mi = lambda : [int(i) for i in input().split()]
flat = lambda l: reduce(lambda a, b: a + b, l)
n = int(input())
a = input()
b = input()
ad = [[] for _ in range(256)]
bd = [[] for _ in range(256)]
for i in range(n):
	ad[ord(a[i])].append(i + 1)
for i in range(n):
	bd[ord(b[i])].append(i + 1)
res = []
(aq, bq) = (ad[ord('?')], bd[ord('?')])
(ad[ord('?')], bd[ord('?')]) = ([], [])
for i in range(256):
	while len(ad[i]) and len(bd[i]):
		res.append((ad[i].pop(), bd[i].pop()))
ad = flat(ad)
bd = flat(bd)
while len(aq) and len(bd):
	res.append((aq.pop(), bd.pop()))
while len(bq) and len(ad):
	res.append((ad.pop(), bq.pop()))
while len(aq) and len(bq):
	res.append((aq.pop(), bq.pop()))
print(len(res))
print(*[str(i[0]) + ' ' + str(i[1]) for i in res], sep='\n')


Left_dict = {'?': []}
Right_dict = {'?': []}
for i in range(26):
	Left_dict[chr(97 + i)] = []
	Right_dict[chr(97 + i)] = []
n = int(input())
left = input()
right = input()
for i in range(n):
	Left_dict[left[i]].append(i)
	Right_dict[right[i]].append(i)
count = 0
answer = []
for i in Left_dict:
	if i == '?':
		continue
	while len(Left_dict[i]) != 0 and len(Right_dict[i]) != 0:
		answer.append([Left_dict[i].pop() + 1, Right_dict[i].pop() + 1])
		count += 1
	while len(Left_dict[i]) != 0 and len(Right_dict['?']) != 0:
		answer.append([Left_dict[i].pop() + 1, Right_dict['?'].pop() + 1])
		count += 1
	while len(Left_dict['?']) != 0 and len(Right_dict[i]) != 0:
		answer.append([Left_dict['?'].pop() + 1, Right_dict[i].pop() + 1])
		count += 1
while len(Left_dict['?']) != 0 and len(Right_dict['?']) != 0:
	answer.append([Left_dict['?'].pop() + 1, Right_dict['?'].pop() + 1])
	count += 1
print(count)
for i in answer:
	print(*i)


from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt, sin, cos, ceil, floor
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(1000000000)
INF = float('inf')
(YES, Yes, yes, NO, No, no) = ('YES', 'Yes', 'yes', 'NO', 'No', 'no')
(dy4, dx4) = ([0, 1, 0, -1], [1, 0, -1, 0])
(dy8, dx8) = ([0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1])

def inside(y, x, H, W):
	return 0 <= y < H and 0 <= x < W

def ceil(a, b):
	return (a + b - 1) // b

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

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

def euclidean_distance(y1, x1, y2, x2):
	return sqrt((y1 - y2) ** 2 + (x1 - x2) ** 2)

def manhattan_distance(y1, x1, y2, x2):
	return abs(y1 - y2) + abs(x1 - x2)

def main():
	N = int(input())
	L = input()
	R = input()
	color_set = set()
	(color_idx_L, color_idx_R) = (defaultdict(list), defaultdict(list))
	for (i, l) in enumerate(L, start=1):
		color_idx_L[l].append(i)
		color_set.add(l)
	for (i, r) in enumerate(R, start=1):
		color_idx_R[r].append(i)
		color_set.add(r)
	ans = []
	(not_used_L_idx, not_used_R_idx) = ([], [])
	for c in color_set:
		if c == '?':
			continue
		num_l = len(color_idx_L[c])
		num_r = len(color_idx_R[c])
		if num_l == num_r:
			for i in range(num_l):
				ans.append((color_idx_L[c][i], color_idx_R[c][i]))
		elif num_l > num_r:
			for i in range(num_l):
				if i < num_r:
					ans.append((color_idx_L[c][i], color_idx_R[c][i]))
				else:
					not_used_L_idx.append(color_idx_L[c][i])
		else:
			for i in range(num_r):
				if i < num_l:
					ans.append((color_idx_L[c][i], color_idx_R[c][i]))
				else:
					not_used_R_idx.append(color_idx_R[c][i])
	while len(color_idx_L['?']) != 0 and len(not_used_R_idx) != 0:
		a = color_idx_L['?'].pop()
		b = not_used_R_idx.pop()
		ans.append((a, b))
	while len(color_idx_R['?']) != 0 and len(not_used_L_idx) != 0:
		a = not_used_L_idx.pop()
		b = color_idx_R['?'].pop()
		ans.append((a, b))
	for i in range(min(len(color_idx_L['?']), len(color_idx_R['?']))):
		ans.append((color_idx_L['?'][i], color_idx_R['?'][i]))
	print(len(ans))
	for (l, r) in ans:
		print(l, r)
main()


INF = 100000000
from collections import defaultdict
t = 1
for test in range(t):
	n = int(input())
	l = input()
	r = input()
	L = defaultdict(list)
	usedL = [False for i in range(n)]
	usedR = [False for i in range(n)]
	quesL = []
	quesR = []
	for i in range(n):
		if l[i] == '?':
			quesL.append(i)
			usedL[i] = True
		else:
			L[l[i]].append(i)
	ans = []
	for i in range(n):
		if r[i] == '?':
			quesR.append(i)
			usedR[i] = True
		elif len(L[r[i]]) > 0:
			tmp = L[r[i]].pop()
			ans.append((tmp + 1, i + 1))
			usedL[tmp] = True
			usedR[i] = True
	usedL2 = []
	usedR2 = []
	for i in range(n):
		if not usedL[i]:
			usedL2.append(i)
		if not usedR[i]:
			usedR2.append(i)
	while len(quesL) > 0 and len(usedR2) > 0:
		ans.append((quesL.pop() + 1, usedR2.pop() + 1))
	while len(quesR) > 0 and len(usedL2) > 0:
		ans.append((usedL2.pop() + 1, quesR.pop() + 1))
	while len(quesL) > 0 and len(quesR) > 0:
		ans.append((quesL.pop() + 1, quesR.pop() + 1))
	print(len(ans))
	for i in ans:
		print(i[0], i[1])


from collections import defaultdict
n = int(input())
a = input().strip()
b = input().strip()
d = defaultdict(list)
e = defaultdict(list)
for j in range(n):
	d[a[j]].append(j + 1)
	e[b[j]].append(j + 1)
c = 0
res = []
for v in d:
	if v != '?' and v in e:
		while d[v] != [] and e[v] != []:
			c = c + 1
			p = d[v].pop()
			q = e[v].pop()
			res.append([p, q])
for v in e:
	if v != '?':
		while d['?'] != [] and e[v] != []:
			c = c + 1
			p = d['?'].pop()
			q = e[v].pop()
			res.append([p, q])
		if d['?'] == []:
			break
for v in d:
	if v != '?':
		while e['?'] != [] and d[v] != []:
			c = c + 1
			p = d[v].pop()
			q = e['?'].pop()
			res.append([p, q])
while d['?'] != [] and e['?'] != []:
	c = c + 1
	p = d['?'].pop()
	q = e['?'].pop()
	res.append([p, q])
print(c)
for j in range(c):
	print(res[j][0], res[j][1])


n = int(input())
l = input()
r = input()
ld = {'?': []}
rd = {'?': []}
for i in range(n):
	if l[i] in ld:
		ld[l[i]].append(i)
	else:
		ld[l[i]] = [i]
	if r[i] in rd:
		rd[r[i]].append(i)
	else:
		rd[r[i]] = [i]
res = []
for l_color in ld:
	if l_color == '?':
		continue
	if l_color in rd:
		while len(rd[l_color]) > 0 and len(ld[l_color]) > 0:
			l_index = ld[l_color].pop()
			r_index = rd[l_color].pop()
			res.append((l_index + 1, r_index + 1))
		if len(rd[l_color]) > 0 and len(ld['?']) > 0:
			while len(rd[l_color]) > 0 and len(ld['?']) > 0:
				l_index = ld['?'].pop()
				r_index = rd[l_color].pop()
				res.append((l_index + 1, r_index + 1))
		if len(rd['?']) > 0 and len(ld[l_color]) > 0:
			while len(rd['?']) > 0 and len(ld[l_color]) > 0:
				l_index = ld[l_color].pop()
				r_index = rd['?'].pop()
				res.append((l_index + 1, r_index + 1))
for r_color in rd:
	if r_color == '?':
		continue
	if len(ld['?']) == 0:
		break
	while len(rd[r_color]) > 0 and len(ld['?']) > 0:
		r_index = rd[r_color].pop()
		l_index = ld['?'].pop()
		res.append((l_index + 1, r_index + 1))
for l_color in ld:
	if l_color == '?':
		continue
	if len(rd['?']) == 0:
		break
	while len(ld[l_color]) > 0 and len(rd['?']) > 0:
		l_index = ld[l_color].pop()
		r_index = rd['?'].pop()
		res.append((l_index + 1, r_index + 1))
while len(rd['?']) > 0 and len(ld['?']) > 0:
	l_index = ld['?'].pop()
	r_index = rd['?'].pop()
	res.append((l_index + 1, r_index + 1))
print(len(res))
for r in res:
	print(r[0], r[1])


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import *
from copy import *
from collections import deque
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

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')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

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

def power(a, b):
	ans = 1
	while b > 0:
		if b % 2 == 1:
			ans *= a
		a *= a
		b //= 2
	return ans

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def solve():
	n = int(inp())
	a = list(inp())
	b = list(inp())
	cnt = [0] * 27
	taken = [0] * 27
	vis1 = [False] * n
	vis2 = [False] * n
	ans = []
	for i in range(n):
		if a[i] == '?':
			cnt[26] += 1
		else:
			cnt[ord(a[i]) - 97] += 1
	for i in range(n):
		if b[i] == '?':
			cnt[26] += 1
		elif cnt[ord(b[i]) - 97] > 0:
			cnt[ord(b[i]) - 97] -= 1
			taken[ord(b[i]) - 97] += 1
	for i in range(26):
		cnt = 0
		a1 = []
		ii = 0
		while cnt < taken[i]:
			if a[ii] == chr(i + 97):
				a1.append(ii)
				cnt += 1
				vis1[ii] = True
			ii += 1
		cnt = 0
		a2 = []
		ii = 0
		while cnt < taken[i]:
			if b[ii] == chr(i + 97):
				a2.append(ii)
				cnt += 1
				vis2[ii] = True
			ii += 1
		for j in range(taken[i]):
			ans.append([a1[j], a2[j]])
	a1questn = []
	a1 = []
	a2questn = []
	a2 = []
	for i in range(n):
		if vis1[i]:
			continue
		if a[i] == '?':
			a1questn.append(i)
		else:
			a1.append(i)
	for i in range(n):
		if vis2[i]:
			continue
		if b[i] == '?':
			a2questn.append(i)
		else:
			a2.append(i)
	l1 = -1
	l2 = -1
	for i in range(min(len(a1questn), len(a2))):
		ans.append([a1questn[i], a2[i]])
		l1 = i
	for i in range(min(len(a2questn), len(a1))):
		ans.append([a1[i], a2questn[i]])
		l2 = i
	l1 += 1
	l2 += 1
	while l1 < len(a1questn) and l2 < len(a2questn):
		ans.append([a1questn[l1], a2questn[l2]])
		l1 += 1
		l2 += 1
	print(len(ans))
	for i in range(len(ans)):
		print(ans[i][0] + 1, ans[i][1] + 1)
testcase(1)


n = int(input())
l = input()
r = input()
Dl = dict()
Dr = dict()
for i in list('qwertyuiopasdfghjklzxcvbnm?'):
	Dl[i] = []
	Dr[i] = []
for i in range(len(l)):
	Dl[l[i]].append(i)
for i in range(len(r)):
	Dr[r[i]].append(i)
double = list()
for i in range(97, 123):
	x = chr(i)
	L = len(Dl[x])
	R = len(Dr[x])
	for j in range(min(L, R)):
		double.append((Dl[x][L - j - 1] + 1, Dr[x][R - j - 1] + 1))
		Dr[x].pop()
		Dl[x].pop()
for i in range(97, 123):
	x = chr(i)
	L = len(Dl[x])
	R = len(Dr['?'])
	y = '?'
	for j in range(min(L, R)):
		double.append((Dl[x][L - j - 1] + 1, Dr[y][R - j - 1] + 1))
		Dr[y].pop()
		Dl[x].pop()
for i in range(97, 123):
	x = chr(i)
	R = len(Dr[x])
	L = len(Dl['?'])
	y = '?'
	for j in range(min(L, R)):
		double.append((Dl[y][L - j - 1] + 1, Dr[x][R - j - 1] + 1))
		Dr[x].pop()
		Dl[y].pop()
R = len(Dr['?'])
L = len(Dl['?'])
for j in range(min(L, R)):
	double.append((Dl['?'][L - j - 1] + 1, Dr['?'][R - j - 1] + 1))
	Dr['?'].pop()
	Dl['?'].pop()
print(len(double))
for i in double:
	print(*i)


n = int(input())
l = input()
r = input()
l1 = [[] for i in range(28)]
r1 = [[] for i in range(28)]
ans0 = []
ans1 = []
for i in range(n):
	if l[i] == '?':
		l1[27].append(i + 1)
	if r[i] == '?':
		r1[27].append(i + 1)
	if l[i] >= 'a' and l[i] <= 'z':
		l1[ord(l[i]) - ord('a')].append(i + 1)
	if r[i] >= 'a' and r[i] <= 'z':
		r1[ord(r[i]) - ord('a')].append(i + 1)
for i in range(26):
	while len(l1[i]) != 0 and len(r1[i]) != 0:
		ans0.append(l1[i].pop())
		ans1.append(r1[i].pop())
	while len(l1[i]) != 0 and len(r1[27]) != 0:
		ans0.append(l1[i].pop())
		ans1.append(r1[27].pop())
	while len(r1[i]) != 0 and len(l1[27]) != 0:
		ans0.append(l1[27].pop())
		ans1.append(r1[i].pop())
while len(r1[27]) != 0 and len(l1[27]) != 0:
	ans0.append(l1[27].pop())
	ans1.append(r1[27].pop())
print(len(ans0))
for i in range(len(ans0)):
	print(ans0[i], ans1[i])


n = int(input())
s1 = input()
s2 = input()
a1 = []
a2 = []
mas = []
ch = 0
q1 = 0
q2 = 0
for i in range(n):
	a1.append([s1[i], i + 1])
	a2.append([s2[i], i + 1])
	if s1[i] == '?':
		q1 += 1
	if s2[i] == '?':
		q2 += 1
a1.sort()
a2.sort()
i = n - 1
j = n - 1
arr1 = []
arr2 = []
while i >= 0 and j >= 0:
	if ord(a1[i][0]) > ord(a2[j][0]):
		arr1.append(a1[i][1])
		i -= 1
	if ord(a1[i][0]) < ord(a2[j][0]):
		arr2.append(a2[j][1])
		j -= 1
	if ord(a1[i][0]) == ord(a2[j][0]):
		if a1[i][0] != '?':
			mas.append([a1[i][1], a2[j][1]])
			ch += 1
			i -= 1
			j -= 1
		else:
			break
f1 = 0
f2 = 0
for k in range(len(arr1)):
	if q2 != 0:
		ch += 1
		mas.append([arr1[k], a2[k][1]])
		q2 -= 1
		f1 += 1
	else:
		break
for k in range(len(arr2)):
	if q1 != 0:
		ch += 1
		mas.append([a1[k][1], arr2[k]])
		q1 -= 1
		f2 += 1
	else:
		break
g = min(q1, q2)
for k in range(g):
	mas.append([a1[f1][1], a2[f2][1]])
	ch += 1
	f1 += 1
	f2 += 1
print(ch)
for i in range(len(mas)):
	print(mas[i][0], mas[i][1])


n = int(input())
s1 = input()
s2 = input()
freq_count = []
for i in range(27):
	freq_count.append([])
for i in range(n):
	if s1[i] == '?':
		freq_count[26].append(i)
	else:
		freq_count[ord(s1[i]) - 97].append(i)
ans = []
for i in range(n):
	if s2[i] == '?':
		continue
	if len(freq_count[ord(s2[i]) - 97]):
		ans.append((freq_count[ord(s2[i]) - 97][-1], i))
		freq_count[ord(s2[i]) - 97].pop()
	elif len(freq_count[26]):
		ans.append((freq_count[26][-1], i))
		freq_count[26].pop()
for i in range(n):
	if s2[i] == '?':
		for j in range(27):
			if len(freq_count[j]):
				ans.append((freq_count[j][-1], i))
				freq_count[j].pop()
				break
print(len(ans))
for i in ans:
	print(str(i[0] + 1) + ' ' + str(i[1] + 1))


from collections import defaultdict
(left, right) = (defaultdict(list), defaultdict(list))
indexs = []
n = int(input())
l = input()
r = input()
for i in range(n):
	left[l[i]].append(i)
	right[r[i]].append(i)
l = set(map(str, l))
r = set(map(str, r))
common = l.union(r)
if '?' in common:
	common.remove('?')
pairs = []
for i in common:
	while left[i] and right[i]:
		pairs.append([left[i].pop() + 1, right[i].pop() + 1])
	while left[i] and right['?']:
		pairs.append([left[i].pop() + 1, right['?'].pop() + 1])
	while left['?'] and right[i]:
		pairs.append([left['?'].pop() + 1, right[i].pop() + 1])
while left['?'] and right['?']:
	pairs.append([left['?'].pop() + 1, right['?'].pop() + 1])
print(len(pairs))
for i in pairs:
	print(*i)


from collections import defaultdict
indices_left = defaultdict(list)
indices_right = defaultdict(list)
n = int(input())
l = input()
r = input()
leftarr = [0] * 27
rightarr = [0] * 27
for i in range(n):
	if l[i] == '?':
		leftarr[26] += 1
	else:
		leftarr[ord(l[i]) - 97] += 1
	if r[i] == '?':
		rightarr[26] += 1
	else:
		rightarr[ord(r[i]) - 97] += 1
	indices_right[r[i]] += [i]
	indices_left[l[i]] += [i]
ans = []
count = 0
for i in indices_left:
	if i in indices_right and i != '?':
		m = min(len(indices_right[i]), len(indices_left[i]))
		somevar = 0
		for j in range(m):
			ans += [[indices_left[i][j], indices_right[i][j]]]
			count += 1
			somevar += 1
		indices_left[i] = indices_left[i][somevar:]
		indices_right[i] = indices_right[i][somevar:]
i = '?'
for j in indices_right:
	if j != '?':
		m = min(len(indices_right[j]), len(indices_left[i]))
		somevar = 0
		for k in range(m):
			ans += [[indices_left[i][k], indices_right[j][k]]]
			count += 1
			somevar += 1
		indices_left[i] = indices_left[i][somevar:]
		indices_right[j] = indices_right[j][somevar:]
i = '?'
for j in indices_left:
	if j != '?':
		m = min(len(indices_left[j]), len(indices_right[i]))
		somevar = 0
		for k in range(m):
			ans += [[indices_left[j][k], indices_right[i][k]]]
			count += 1
			somevar += 1
		indices_right[i] = indices_right[i][somevar:]
		indices_left[j] = indices_left[j][somevar:]
i = '?'
m = min(len(indices_left[i]), len(indices_right[i]))
somevar = 0
for j in range(m):
	ans += [[indices_left[i][j], indices_right[i][j]]]
	count += 1
	somevar += 1
print(count)
for abc in ans:
	print(abc[0] + 1, abc[1] + 1)


import math
import bisect
import itertools
import sys
I = lambda : sys.stdin.readline()
mod = 10 ** 9 + 7

def modu(a, m):
	if a % m:
		return a % m
	return m

def mindiff(a):
	b = a[:]
	b.sort()
	m = 10000000000
	for i in range(len(b) - 1):
		if b[i + 1] - b[i] < m:
			m = b[i + 1] - b[i]
	return m

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

def merge(a, b):
	i = 0
	j = 0
	c = 0
	ans = []
	while i < len(a) and j < len(b):
		if a[i] < b[j]:
			ans.append(a[i])
			i += 1
		else:
			ans.append(b[j])
			c += len(a) - i
			j += 1
	ans += a[i:]
	ans += b[j:]
	return (ans, c)

def mergesort(a):
	if len(a) == 1:
		return (a, 0)
	mid = len(a) // 2
	(left, left_inversion) = mergesort(a[:mid])
	(right, right_inversion) = mergesort(a[mid:])
	(m, c) = merge(left, right)
	c += left_inversion + right_inversion
	return (m, c)

def is_prime(num):
	if num == 1:
		return False
	if num == 2:
		return True
	if num == 3:
		return True
	if num % 2 == 0:
		return False
	if num % 3 == 0:
		return False
	t = 5
	a = 2
	while t <= int(math.sqrt(num)):
		if num % t == 0:
			return False
		t += a
		a = 6 - a
	return True

def ceil(a, b):
	if a % b == 0:
		return a // b
	else:
		return a // b + 1

def ncr1(n, r):
	s = 1
	for i in range(min(n - r, r)):
		s *= n - i
		s %= mod
		s *= pow(i + 1, mod - 2, mod)
		s %= mod
	return s
n = int(input())
a = list(input())
b = list(input())
a1 = [[] for i in range(26)]
b1 = [[] for i in range(26)]
q1 = 0
q2 = 0
a2 = []
b2 = []
for i in range(n):
	if ord(a[i]) <= ord('z') and ord(a[i]) >= ord('a'):
		a1[ord(a[i]) - ord('a')].append(i + 1)
	else:
		a2.append(i + 1)
		q1 += 1
	if ord(b[i]) <= ord('z') and ord(b[i]) >= ord('a'):
		b1[ord(b[i]) - ord('a')].append(i + 1)
	else:
		q2 += 1
		b2.append(i + 1)
ans = 0
ans1 = []
for i in range(26):
	x = len(a1[i])
	y = len(b1[i])
	j = 0
	while j < x and j < y:
		ans1.append((a1[i][j], b1[i][j]))
		j += 1
		ans += 1
	while j < x:
		a2.append(a1[i][j])
		j += 1
	while j < y:
		b2.append(b1[i][j])
		j += 1
k = min(q1 + q2, len(a2))
print(ans + k)
for i in range(q1):
	print(a2[i], b2[-1 - i])
for i in range(k - q1):
	print(a2[-1 - i], b2[i])
for i in ans1:
	print(*i)


from collections import defaultdict
(left, right) = (defaultdict(list), defaultdict(list))
indexs = []
n = int(input())
l = input()
r = input()
for i in range(n):
	left[l[i]].append(i)
	right[r[i]].append(i)
alpha = 'abcdefghijklmnopqrstuvwxyz'
pairs = []
for i in alpha:
	while left[i] and right[i]:
		pairs.append([left[i].pop() + 1, right[i].pop() + 1])
	while left[i] and right['?']:
		pairs.append([left[i].pop() + 1, right['?'].pop() + 1])
	while left['?'] and right[i]:
		pairs.append([left['?'].pop() + 1, right[i].pop() + 1])
while left['?'] and right['?']:
	pairs.append([left['?'].pop() + 1, right['?'].pop() + 1])
print(len(pairs))
for i in pairs:
	print(*i)


from sys import *
fi = lambda : stdin.readline()
n = int(fi())
a = fi().strip('\n')
b = fi().strip('\n')
d1 = dict()
d2 = dict()
for i in range(n):
	d1.setdefault(a[i], [])
	d2.setdefault(b[i], [])
	d1[a[i]].append(i)
	d2[b[i]].append(i)
l = []
for (q, v) in d1.items():
	if q != '?':
		if d2.get(q):
			p = min(len(d1[q]), len(d2[q]))
			for i in range(p):
				l.append([d1[q].pop() + 1, d2[q].pop() + 1])
if d1.get('?'):
	for (q, v) in d2.items():
		if q != '?' and len(v):
			p = len(v)
			for i in range(p):
				l.append([d1['?'].pop() + 1, d2[q].pop() + 1])
				if len(d1['?']) == 0:
					break
		if len(d1['?']) == 0:
			break
if d2.get('?'):
	for (q, v) in d1.items():
		if q != '?' and len(v):
			p = len(v)
			for i in range(p):
				l.append([d1[q].pop() + 1, d2['?'].pop() + 1])
				if len(d2['?']) == 0:
					break
		if len(d2['?']) == 0:
			break
if d1.get('?') and d2.get('?'):
	p = min(len(d1['?']), len(d2['?']))
	for i in range(p):
		l.append([d1['?'].pop() + 1, d2['?'].pop() + 1])
print(len(l))
for i in l:
	print(*i)


n = int(input())
left = [[] for _ in range(27)]
right = [[] for _ in range(27)]
for (i, c) in enumerate(input().strip()):
	if c == '?':
		left[26].append(i + 1)
	else:
		left[ord(c) - ord('a')].append(i + 1)
for (i, c) in enumerate(input().strip()):
	if c == '?':
		right[26].append(i + 1)
	else:
		right[ord(c) - ord('a')].append(i + 1)
result = []
for i in range(26):
	count = min(len(left[i]), len(right[i]))
	for j in range(count):
		result.append((left[i][j], right[i][j]))
i = 0
left_q = 0
while True:
	if left_q == len(left[26]) or i == 26:
		break
	for j in range(len(left[i]), len(right[i])):
		result.append((left[26][left_q], right[i][j]))
		left_q += 1
		if left_q == len(left[26]):
			break
	i += 1
i = 0
right_q = 0
while True:
	if right_q == len(right[26]) or i == 26:
		break
	for j in range(len(right[i]), len(left[i])):
		result.append((left[i][j], right[26][right_q]))
		right_q += 1
		if right_q == len(right[26]):
			break
	i += 1
while left_q < len(left[26]) and right_q < len(right[26]):
	result.append((left[26][left_q], right[26][right_q]))
	left_q += 1
	right_q += 1
print(len(result))
for (i, j) in result:
	print(i, j)


from collections import defaultdict
from queue import deque
n = int(input())
left = input()
right = input()
lcnt = defaultdict(deque)
rcnt = defaultdict(deque)
all_keys = set()
for i in range(n):
	lcnt[left[i]].append(i + 1)
	rcnt[right[i]].append(i + 1)
	all_keys.add(left[i])
	all_keys.add(right[i])
left_indef = lcnt.pop('?', deque())
right_indef = rcnt.pop('?', deque())
pairs = []
for k in all_keys:
	mn = min(len(lcnt[k]), len(rcnt[k]))
	for i in range(mn):
		(l, r) = (lcnt[k].popleft(), rcnt[k].popleft())
		pairs.append((l, r))
	if len(lcnt[k]) > 0:
		mn = min(len(lcnt[k]), len(right_indef))
		for i in range(mn):
			(l, r) = (lcnt[k].popleft(), right_indef.popleft())
			pairs.append((l, r))
	else:
		mn = min(len(rcnt[k]), len(left_indef))
		for i in range(mn):
			(l, r) = (left_indef.popleft(), rcnt[k].popleft())
			pairs.append((l, r))
mn = min(len(left_indef), len(right_indef))
for i in range(mn):
	(l, r) = (left_indef.popleft(), right_indef.popleft())
	pairs.append((l, r))
print(len(pairs))
for pair in pairs:
	print(*pair)


n = int(input())
a = input()
da = [[] for _ in range(26)]
za = []
for (i, v) in enumerate(a):
	if v == '?':
		za.append(i + 1)
	else:
		k = ord(v) - ord('a')
		da[k].append(i + 1)
b = input()
db = [[] for _ in range(26)]
zb = []
for (i, v) in enumerate(b):
	if v == '?':
		zb.append(i + 1)
	else:
		k = ord(v) - ord('a')
		db[k].append(i + 1)
ra = []
rb = []
cnt = min(n, sum(map(lambda x, y: min(len(x), len(y)), da, db)) + len(za) + len(zb))
print(cnt)
for (u, v) in zip(da, db):
	for uv in zip(u, v):
		print(*uv)
	k = min(len(u), len(v))
	ra.extend(u[k:])
	rb.extend(v[k:])
ka = min(len(za), len(rb))
kb = min(len(zb), len(ra))
for uv in zip(za, rb):
	print(*uv)
for uv in zip(ra, zb):
	print(*uv)
for uv in zip(za[ka:], zb[kb:]):
	print(*uv)


n = int(input())
l = input()
r = input()
ldict = {}
rdict = {}
lpos = []
rpos = []
for _ in range(27):
	lpos += [list()]
	rpos += [list()]
for i in range(len(l)):
	c = l[i]
	if c in ldict:
		ldict[c] += 1
	else:
		ldict[c] = 1
	if c == '?':
		lpos[26] += [i + 1]
	else:
		lpos[ord(c) - 97] += [i + 1]
for i in range(len(r)):
	c = r[i]
	if c in rdict:
		rdict[c] += 1
	else:
		rdict[c] = 1
	if c == '?':
		rpos[26] += [i + 1]
	else:
		rpos[ord(c) - 97] += [i + 1]
result = []
for k in ldict.keys():
	if k == '?':
		continue
	if k in rdict:
		count = min(ldict[k], rdict[k])
	else:
		continue
	while count != 0:
		result += [(lpos[ord(k) - 97].pop(), rpos[ord(k) - 97].pop())]
		count -= 1
lresidue = []
rresidue = []
pos = 0
while pos < 26:
	if len(lpos[pos]) != 0:
		for ele in lpos[pos]:
			lresidue += [ele]
	if len(rpos[pos]) != 0:
		for ele in rpos[pos]:
			rresidue += [ele]
	pos += 1
if '?' in ldict:
	for _ in range(ldict['?']):
		if len(rresidue) == 0:
			break
		else:
			result += [(lpos[26].pop(), rresidue.pop())]
if '?' in rdict:
	for _ in range(rdict['?']):
		if len(lresidue) == 0:
			break
		else:
			result += [(lresidue.pop(), rpos[26].pop())]
for _ in range(min(len(lpos[26]), len(rpos[26]))):
	result += [(lpos[26].pop(), rpos[26].pop())]
print(len(result))
for t in result:
	print(t[0], t[1])


n = int(input())
l = input()
r = input()
l_arr = []
r_arr = []
ql_arr = []
qr_arr = []
for i in range(len(l)):
	if l[i] == '?':
		ql_arr.append([l[i], i])
	else:
		l_arr.append([l[i], i])
for j in range(len(r)):
	if r[j] == '?':
		qr_arr.append([r[j], j])
	else:
		r_arr.append([r[j], j])
l_arr.sort()
r_arr.sort()
p1 = 0
p2 = 0
ans = []
while p1 < len(l_arr) and p2 < len(r_arr):
	if l_arr[p1][0] < r_arr[p2][0]:
		l_arr[p1][0] = 0
		p1 += 1
	elif l_arr[p1][0] > r_arr[p2][0]:
		r_arr[p2][0] = 0
		p2 += 1
	else:
		ans.append([l_arr[p1][1] + 1, r_arr[p2][1] + 1])
		l_arr[p1][0] = 1
		r_arr[p2][0] = 1
		p1 += 1
		p2 += 1
while p1 < len(l_arr):
	l_arr[p1][0] = 0
	p1 += 1
while p2 < len(r_arr):
	r_arr[p2][0] = 0
	p2 += 1
lind = 0
rind = 0
for x in range(len(l_arr)):
	if l_arr[x][0] == 0:
		if rind < len(qr_arr):
			ans.append([l_arr[x][1] + 1, qr_arr[rind][1] + 1])
			rind += 1
		else:
			break
for y in range(len(r_arr)):
	if r_arr[y][0] == 0:
		if lind < len(ql_arr):
			ans.append([ql_arr[lind][1] + 1, r_arr[y][1] + 1])
			lind += 1
		else:
			break
while lind < len(ql_arr) and rind < len(qr_arr):
	ans.append([ql_arr[lind][1] + 1, qr_arr[rind][1] + 1])
	lind += 1
	rind += 1
print(len(ans))
for e in range(len(ans)):
	print(ans[e][0], ans[e][1])


from collections import defaultdict as ddict
alphabets = 'abcdefghijklmnopqrstuvwxyz'

def main():
	n = int(input())
	a = input()
	b = input()
	(da, db) = (ddict(list), ddict(list))
	for (str_, dict_) in ((a, da), (b, db)):
		for (i, c) in enumerate(str_):
			dict_[c].append(i + 1)
	out = []
	for c in alphabets:
		while da[c] and db[c]:
			ia = da[c].pop()
			ib = db[c].pop()
			out.append((ia, ib))
	(daa, dba) = ([], [])
	for c in alphabets:
		daa.extend(da[c])
		dba.extend(db[c])
	while da['?'] and dba:
		ia = da['?'].pop()
		ib = dba.pop()
		out.append((ia, ib))
	while db['?'] and daa:
		ia = daa.pop()
		ib = db['?'].pop()
		out.append((ia, ib))
	while da['?'] and db['?']:
		ia = da['?'].pop()
		ib = db['?'].pop()
		out.append((ia, ib))
	print(len(out))
	out_str = []
	for (ia, ib) in out:
		out_str.append(str(ia) + ' ' + str(ib))
	print('\n'.join(out_str))
main()


n = int(input())
a1 = input()
a2 = input()
kol1 = {}
kol2 = {}
m1 = 0
m2 = 0
for i in a1:
	kol1[i] = []
for i in a2:
	kol2[i] = []
for i in range(n):
	kol1[a1[i]].append(i)
	if a1[i] == '?':
		m1 += 1
for i in range(n):
	kol2[a2[i]].append(i)
	if a2[i] == '?':
		m2 += 1
wspolne = {}
for i in kol1.keys():
	if i in kol2.keys():
		wspolne[i] = min(len(kol1[i]), len(kol2[i]))
wynik = 0
for i in wspolne:
	if i != '?':
		wynik += wspolne[i]
b = min(m2, n - m1 - wynik)
a = min(m1, n - m2 - wynik)
c = min(m1 - a, m2 - b)
print(wynik + a + b + c)
zajete1 = [0] * 150001
zajete2 = [0] * 150001
for i in wspolne.keys():
	for j in range(wspolne[i]):
		if i != '?':
			print(kol1[i][j] + 1, end=' ')
			print(kol2[i][j] + 1, end='\n')
			zajete2[kol2[i][j]] = 1
			zajete1[kol1[i][j]] = 1
for i in range(c):
	print(kol1['?'][i] + 1, end=' ')
	print(kol2['?'][i] + 1, end='\n')
	zajete1[kol1['?'][i]] = 1
	zajete2[kol2['?'][i]] = 1
wsk = 0
for i in range(a):
	print(kol1['?'][c + i] + 1, end=' ')
	zajete1[kol1['?'][c + i]] = 1
	while wsk < 200000:
		if zajete2[wsk] == 1 or a2[wsk] == '?':
			wsk += 1
		else:
			break
	print(wsk + 1, end='\n')
	zajete2[wsk] = 1
	wsk += 1
wsk = 0
for i in range(b):
	while wsk < 200000:
		if zajete1[wsk] == 1:
			wsk += 1
		else:
			break
	print(wsk + 1, end=' ')
	print(kol2['?'][c + i] + 1, end='\n')
	wsk += 1


def f(n, str1, str2):
	list1 = []
	for i in range(len(str1)):
		list1.append([str1[i], i + 1, False])
	list2 = []
	for i in range(len(str2)):
		list2.append([str2[i], i + 1, False])
	list1.sort(key=lambda x: x[0])
	list2.sort(key=lambda x: x[0])
	i = 0
	while i < len(list1) and list1[i][0] == '?':
		i += 1
	j = 0
	while j < len(list2) and list2[j][0] == '?':
		j += 1
	result = []
	while i < len(list1) and j < len(list2):
		if list1[i][0] == list2[j][0]:
			list1[i][2] = True
			list2[j][2] = True
			result.append((list1[i][1], list2[j][1]))
			i += 1
			j += 1
		elif list1[i][0] < list2[j][0]:
			i += 1
		else:
			j += 1
	list1 = list(filter(lambda x: not x[2], list1))
	list2 = list(filter(lambda x: not x[2], list2))
	list2.sort(key=lambda x: x[0], reverse=True)
	i = 0
	while i < len(list1) and i < len(list2):
		if list1[i][0] == '?' or list2[i][0] == '?':
			result.append((list1[i][1], list2[i][1]))
		i += 1
	print(len(result))
	for l_r in result:
		print(f'{l_r[0]} {l_r[1]}')
n = int(input())
str1 = input()
str2 = input()
f(n, str1, str2)


def convert(c):
	return ord(c) - ord('a')
n = int(input())
lhs = input()
rhs = input()
a = [list() for _ in range(26)]
unused = []
for (i, c) in enumerate(lhs):
	if c == '?':
		unused.append(i)
	else:
		a[convert(c)].append(i)
res = []
check = [True] * n
for (i, c) in enumerate(rhs):
	if c != '?':
		temp = convert(c)
		if len(a[temp]) > 0:
			last = a[temp].pop()
			res.append([last, i])
			check[i] = False
for (i, c) in enumerate(rhs):
	if c != '?' and check[i]:
		if len(unused) > 0:
			last = unused.pop()
			res.append([last, i])
for x in a:
	unused += x
for (i, c) in enumerate(rhs):
	if c == '?':
		if len(unused) > 0:
			last = unused.pop()
			res.append([last, i])
print(len(res))
for x in res:
	print(x[0] + 1, x[1] + 1)


import os
import sys
from collections import defaultdict
from string import ascii_lowercase
if 'DEBUG' in os.environ:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def parse(s):
	d = defaultdict(list)
	for (i, ch) in enumerate(s):
		d[ch].append(i)
	return d
input()
s1: str = input().strip()
s2: str = input().strip()
s1d = parse(s1)
s2d = parse(s2)
pairs = []
for ch in ascii_lowercase:
	while s1d[ch] and s2d[ch]:
		pairs.append((s1d[ch].pop(), s2d[ch].pop()))
	while s1d[ch] and s2d['?']:
		pairs.append((s1d[ch].pop(), s2d['?'].pop()))
	while s2d[ch] and s1d['?']:
		pairs.append((s1d['?'].pop(), s2d[ch].pop()))
while s2d['?'] and s1d['?']:
	pairs.append((s1d['?'].pop(), s2d['?'].pop()))
print(len(pairs))
for p in pairs:
	print(p[0] + 1, p[1] + 1)


from collections import deque, defaultdict, Counter, OrderedDict
n = int(input())
lrr = deque(input())
rrr = deque(input())
(lcnt, rcnt) = (defaultdict(list), defaultdict(list))
for i in range(0, n):
	lcnt[lrr[i]].append(i + 1)
	rcnt[rrr[i]].append(i + 1)
total = set(lcnt.keys()) | set(rcnt.keys())
if '?' in total:
	total.remove('?')
ans = 0
final = []
for i in total:
	now = min(len(lcnt[i]), len(rcnt[i]))
	for j in range(now):
		ans += 1
		final.append((lcnt[i].pop(), rcnt[i].pop()))
	if len(lcnt[i]) == 0:
		del lcnt[i]
	if len(rcnt[i]) == 0:
		del rcnt[i]
if len(rcnt['?']) > 0:
	for (i, j) in lcnt.items():
		if len(rcnt['?']) == 0:
			break
		else:
			now = min(len(j), len(rcnt['?']))
			for k in range(now):
				ans += 1
				final.append((lcnt[i].pop(), rcnt['?'].pop()))
if len(lcnt['?']) > 0:
	for (i, j) in rcnt.items():
		if len(lcnt['?']) == 0:
			break
		else:
			now = min(len(j), len(lcnt['?']))
			for k in range(now):
				ans += 1
				final.append((lcnt['?'].pop(), rcnt[i].pop()))
print(ans)
for qq in final:
	print(' '.join([str(qq[0]), str(qq[1])]))


n = int(input())
l = input()
r = input()
li = [0] * 27
li2 = [[] for i in range(27)]
ri = [0] * 27
ri2 = [[] for i in range(27)]
alth = 'qwertyuiopasdfghjklzxcvbnm?'
for i in range(n):
	i1 = alth.find(l[i])
	i2 = alth.find(r[i])
	li[i1] += 1
	ri[i2] += 1
	li2[i1] += [i]
	ri2[i2] += [i]
for i in range(27):
	li2[i] += [len(li2[i]) - 1]
	ri2[i] += [len(ri2[i]) - 1]
ans = [0] * n
num = 0
for i in range(26):
	while li2[i][-1] > -1 and ri2[i][-1] > -1:
		ans[num] = [li2[i][li2[i][-1]], ri2[i][ri2[i][-1]]]
		num += 1
		li2[i][-1] -= 1
		ri2[i][-1] -= 1
for i in range(26):
	while li2[i][-1] > -1 and ri2[-1][-1] > -1:
		ans[num] = [li2[i][li2[i][-1]], ri2[-1][ri2[-1][-1]]]
		num += 1
		li2[i][-1] -= 1
		ri2[-1][-1] -= 1
for i in range(26):
	while li2[-1][-1] > -1 and ri2[i][-1] > -1:
		ans[num] = [li2[-1][li2[-1][-1]], ri2[i][ri2[i][-1]]]
		num += 1
		li2[-1][-1] -= 1
		ri2[i][-1] -= 1
while li2[-1][-1] > -1 and ri2[-1][-1] > -1:
	ans[num] = [li2[-1][li2[-1][-1]], ri2[-1][ri2[-1][-1]]]
	num += 1
	li2[-1][-1] -= 1
	ri2[-1][-1] -= 1
print(num)
for i in range(num):
	print(ans[i][0] + 1, ans[i][1] + 1)


n = int(input())
right = list(input())
left = list(input())
rn = []
ln = []
ans = []
ir = 0
il = 0
exr = []
exl = []
spr = []
spl = []
for i in range(n):
	if right[i] == '?':
		spr.append(i)
	else:
		rn.append([right[i], i])
	if left[i] == '?':
		spl.append(i)
	else:
		ln.append([left[i], i])
rn.sort(reverse=True)
ln.sort(reverse=True)
flag = True
while ir < len(rn) and il < len(ln):
	if rn[ir][0] == ln[il][0]:
		ans.append([rn[ir][1] + 1, ln[il][1] + 1])
		ir += 1
		il += 1
	elif rn[ir][0] > ln[il][0]:
		exr.append(rn[ir][1])
		ir += 1
	elif rn[ir][0] < ln[il][0]:
		exl.append(ln[il][1])
		il += 1
for i in range(ir, len(rn)):
	exr.append(rn[i][1])
for i in range(il, len(ln)):
	exl.append(ln[i][1])
i1 = 0
while i1 < len(spr) and i1 < len(exl):
	ans.append([spr[i1] + 1, exl[i1] + 1])
	i1 += 1
i2 = 0
while i2 < len(spl) and i2 < len(exr):
	ans.append([exr[i2] + 1, spl[i2] + 1])
	i2 += 1
while i1 < len(spr) and i2 < len(spl):
	ans.append([spr[i1] + 1, spl[i2] + 1])
	i1 += 1
	i2 += 1
print(len(ans))
for i in range(len(ans)):
	print(*ans[i])


n = int(input())
l = str(input())
r = str(input())
dicl = {}
dicr = {}
for i in range(n):
	if l[i] in dicl.keys():
		dicl[l[i]].append(i)
	else:
		dicl.update({l[i]: [i]})
	if r[i] in dicr.keys():
		dicr[r[i]].append(i)
	else:
		dicr.update({r[i]: [i]})
pair = []
c = 0
for k in dicl.keys():
	if dicr.get(k, 0) == 0:
		continue
	elif k != '?':
		tem = min(len(dicl[k]), len(dicr[k]))
		c += tem
		for i in range(tem):
			pair.append([dicl[k][i] + 1, dicr[k][i] + 1])
		del dicl[k][:tem]
		del dicr[k][:tem]
if dicl.get('?', 0) == 0:
	dicl.update({'?': []})
if dicr.get('?', 0) == 0:
	dicr.update({'?': []})
test = len(dicl['?']) + len(dicr['?']) + c
if test <= n:
	c = c + len(dicl['?']) + len(dicr['?'])
	print(c)
	for i in dicl['?']:
		for key in dicr.keys():
			if len(dicr[key]) != 0 and key != '?':
				pair.append([i + 1, dicr[key][0] + 1])
				del dicr[key][:1]
				break
	for i in dicr['?']:
		for key in dicl.keys():
			if len(dicl[key]) != 0 and key != '?':
				pair.append([dicl[key][0] + 1, i + 1])
				del dicl[key][:1]
				break
else:
	man = len(dicl['?']) + len(dicr['?']) + c - n
	for i in range(man):
		pair.append([dicl['?'][i] + 1, dicr['?'][i] + 1])
	del dicl['?'][:man]
	del dicr['?'][:man]
	c = n
	print(c)
	for i in dicl['?']:
		for key in dicr.keys():
			if len(dicr[key]) != 0 and key != '?':
				pair.append([i + 1, dicr[key][0] + 1])
				del dicr[key][:1]
				break
	for i in dicr['?']:
		for key in dicl.keys():
			if len(dicl[key]) != 0 and key != '?':
				pair.append([dicl[key][0] + 1, i + 1])
				del dicl[key][:1]
				break
for i in range(len(pair)):
	print(pair[i][0], pair[i][1])


n = int(input())
s1 = input()
s2 = input()
s1_l = [[] for i in range(26)]
s1_q = []
s1_qi = 0
for (i, e) in enumerate(s1):
	if e == '?':
		s1_q.append(i)
	else:
		s1_l[ord(e) - ord('a')].append(i)
s2_l = [[] for i in range(26)]
s2_q = []
s2_qi = 0
for (i, e) in enumerate(s2):
	if e == '?':
		s2_q.append(i)
	else:
		s2_l[ord(e) - ord('a')].append(i)
pairs = []
for i in range(26):
	s1_i = s1_l[i]
	s2_i = s2_l[i]
	s1_j = 0
	s2_j = 0
	while s1_j < len(s1_i) and s2_j < len(s2_i):
		pairs.append((s1_i[s1_j], s2_i[s2_j]))
		s1_j += 1
		s2_j += 1
	while s1_j < len(s1_i) and s2_qi < len(s2_q):
		pairs.append((s1_i[s1_j], s2_q[s2_qi]))
		s1_j += 1
		s2_qi += 1
	while s2_j < len(s2_i) and s1_qi < len(s1_q):
		pairs.append((s1_q[s1_qi], s2_i[s2_j]))
		s2_j += 1
		s1_qi += 1
while s1_qi < len(s1_q) and s2_qi < len(s2_q):
	pairs.append((s1_q[s1_qi], s2_q[s2_qi]))
	s1_qi += 1
	s2_qi += 1
print(len(pairs))
for (p1, p2) in pairs:
	print(p1 + 1, p2 + 1)


n = int(input())
a = input()
b = input()
a1 = [[] for i in range(26)]
b1 = [[] for i in range(26)]
q1 = []
q2 = []
for i in range(n):
	if a[i] == '?':
		q1.append(i + 1)
	else:
		a1[ord(a[i]) - 97].append(i + 1)
	if b[i] == '?':
		q2.append(i + 1)
	else:
		b1[ord(b[i]) - 97].append(i + 1)
ans = []
resta = []
restb = []
ll = []
xx = []
for i in range(26):
	for j in range(min(len(a1[i]), len(b1[i]))):
		ans.append([a1[i][j], b1[i][j]])
	for j in range(min(len(a1[i]), len(b1[i])), len(a1[i])):
		resta.append(a1[i][j])
	for j in range(min(len(a1[i]), len(b1[i])), len(b1[i])):
		restb.append(b1[i][j])
for i in range(min(len(resta), len(q2))):
	ans.append([resta[i], q2[i]])
for i in range(min(len(q2), len(resta)), len(q2)):
	ll.append(q2[i])
for i in range(min(len(restb), len(q1))):
	ans.append([q1[i], restb[i]])
for i in range(min(len(q1), len(restb)), len(q1)):
	xx.append(q1[i])
for i in range(min(len(ll), len(xx))):
	ans.append([xx[i], ll[i]])
print(len(ans))
for i in range(len(ans)):
	print(*ans[i])


n = int(input())
(l, r) = (input(), input())
mr = {}
for i in range(n):
	c = r[i]
	if c in mr:
		mr[c].append(i)
	else:
		mr[c] = [i]
ans = []
(usedl, usedr) = ([False] * n, [False] * n)
for i in range(n):
	c = l[i]
	if c == '?':
		continue
	if c in mr and len(mr[c]):
		(a, b) = (i, mr[c].pop())
		ans.append((a, b))
		usedl[a] = True
		usedr[b] = True
(al, ar, spl, spr) = ([], [], [], [])
for i in range(n):
	if not usedl[i]:
		if l[i] == '?':
			spl.append(i)
		else:
			al.append(i)
	if not usedr[i]:
		if r[i] == '?':
			spr.append(i)
		else:
			ar.append(i)
for i in range(min(len(spl), len(ar))):
	ans.append((spl.pop(), ar[i]))
for i in range(min(len(spr), len(al))):
	ans.append((al[i], spr.pop()))
for i in range(min(len(spl), len(spr))):
	ans.append((spl[i], spr[i]))
print(len(ans))
for p in ans:
	print(p[0] + 1, p[1] + 1)


n = int(input())
x = input()
y = input()
l = {}
r = {}
for i in range(n):
	if x[i] in l:
		l[x[i]].append(i)
	else:
		l[x[i]] = [i]
	if y[i] in r:
		r[y[i]].append(i)
	else:
		r[y[i]] = [i]
count = 0
arr = []
for i in l.keys():
	if i == '?':
		continue
	elif i in r:
		minl = min(len(l[i]), len(r[i]))
		count += minl
		for j in range(minl):
			arr.append((l[i][-1] + 1, r[i][-1] + 1))
			l[i].pop()
			r[i].pop()
if '?' in l:
	for i in r.keys():
		if i == '?':
			continue
		minl = min(len(l['?']), len(r[i]))
		count += minl
		for j in range(minl):
			arr.append((l['?'][-1] + 1, r[i][-1] + 1))
			l['?'].pop()
			r[i].pop()
if '?' in r:
	for i in l.keys():
		if i == '?':
			continue
		minl = min(len(r['?']), len(l[i]))
		count += minl
		for j in range(minl):
			arr.append((l[i][-1] + 1, r['?'][-1] + 1))
			r['?'].pop()
			l[i].pop()
if '?' in l:
	if '?' in r:
		minl = min(len(r['?']), len(l['?']))
		count += minl
		for j in range(minl):
			arr.append((l['?'][-1] + 1, r['?'][-1] + 1))
			r['?'].pop()
			l['?'].pop()
print(count)
if count > 0:
	for i in range(count):
		print(*arr[i])


n = int(input())
le = input()
ri = input()
right = dict()
right_cnt = dict()
for i in range(26):
	alp = chr(ord('a') + i)
	right[alp] = []
	right_cnt[alp] = 0
right['?'] = []
right_cnt['?'] = 0
for i in range(n):
	right_alp = ri[i]
	right[right_alp].append(i + 1)
	right_cnt[right_alp] += 1
answer_cnt = 0
answer_pair = []
for i in range(n):
	left_alp = le[i]
	if left_alp == '?':
		continue
	right_index = right_cnt[left_alp] - 1
	if right_index >= 0:
		answer_cnt += 1
		answer_pair.append((i + 1, right[left_alp][right_index]))
		right_cnt[left_alp] -= 1
	else:
		right_index = right_cnt['?'] - 1
		if right_index >= 0:
			answer_cnt += 1
			answer_pair.append((i + 1, right['?'][right_index]))
			right_cnt['?'] -= 1
leftover = []
for i in range(26):
	alp = chr(ord('a') + i)
	for j in range(right_cnt[alp]):
		leftover.append(right[alp][j])
for i in range(right_cnt['?']):
	leftover.append(right['?'][i])
leftover_index = 0
for i in range(n):
	left_alp = le[i]
	if left_alp != '?':
		continue
	answer_cnt += 1
	answer_pair.append((i + 1, leftover[leftover_index]))
	leftover_index += 1
print(answer_cnt)
for p in answer_pair:
	print(p[0], p[1])


from collections import defaultdict, deque
n = input()
(left, right) = (input(), input())
left_location_map = defaultdict(list)
for (i, l) in enumerate(left, 1):
	left_location_map[l].append(i)
right_location_map = defaultdict(list)
right_count = defaultdict(int)
for (i, l) in enumerate(right, 1):
	right_location_map[l].append(i)
pairs = []
for l in left_location_map.keys():
	if l != '?' and left_location_map[l]:
		if l in right_location_map:
			p_n = min(len(left_location_map[l]), len(right_location_map[l]))
			pairs.extend(zip(left_location_map[l][:p_n], right_location_map[l][:p_n]))
			left_location_map[l] = left_location_map[l][p_n:]
			right_location_map[l] = right_location_map[l][p_n:]
for l in left_location_map.keys():
	r = '?'
	if l != '?' and left_location_map[l]:
		if r in right_location_map:
			p_n = min(len(left_location_map[l]), len(right_location_map[r]))
			pairs.extend(zip(left_location_map[l][:p_n], right_location_map[r][:p_n]))
			left_location_map[l] = left_location_map[l][p_n:]
			right_location_map[r] = right_location_map[r][p_n:]
for r in right_location_map:
	l = '?'
	if r != '?' and right_location_map[r]:
		if l in left_location_map:
			p_n = min(len(left_location_map[l]), len(right_location_map[r]))
			pairs.extend(zip(left_location_map[l][:p_n], right_location_map[r][:p_n]))
			left_location_map[l] = left_location_map[l][p_n:]
			right_location_map[r] = right_location_map[r][p_n:]
l = '?'
r = '?'
if r in right_location_map and l in left_location_map:
	p_n = min(len(left_location_map[l]), len(right_location_map[r]))
	if p_n > 0:
		pairs.extend(zip(left_location_map[l][:p_n], right_location_map[r][:p_n]))
		left_location_map[l] = left_location_map[l][p_n:]
		right_location_map[r] = right_location_map[r][p_n:]
		if not left_location_map[l]:
			del left_location_map[l]
		if not right_location_map[r]:
			del right_location_map[r]
print(len(pairs))
for (l, r) in pairs:
	print(l, r)


d = {}
n = int(input())
s = input()
i = 0
for c in s:
	if c not in d:
		d[c] = [i]
	else:
		d[c].append(i)
	i += 1
s = input()
res = []
i = 0
r = {}
for c in s:
	if c == '?':
		if c in r:
			r[c].append(i)
		else:
			r[c] = [i]
		i += 1
		continue
	if c in d:
		res.append((d[c][-1], i))
		d[c].pop()
		if len(d[c]) == 0:
			d.pop(c)
	elif c in r:
		r[c].append(i)
	else:
		r[c] = [i]
	i += 1
for c in d:
	if c == '?':
		continue
	for i in d[c]:
		if '?' not in r:
			break
		res.append((i, r['?'][-1]))
		r['?'].pop()
		if len(r['?']) == 0:
			r.pop('?')
for c in r:
	if c == '?':
		continue
	for i in r[c]:
		if '?' not in d:
			break
		res.append((d['?'][-1], i))
		d['?'].pop()
		if len(d['?']) == 0:
			d.pop('?')
while '?' in d and '?' in r:
	res.append((d['?'][-1], r['?'][-1]))
	d['?'].pop()
	r['?'].pop()
	if len(d['?']) == 0 or len(r['?']) == 0:
		break
print(len(res))
for i in res:
	print(i[0] + 1, i[1] + 1)


n = int(input())
s1 = input()
s2 = input()
a1 = [0 for i in range(26)]
b1 = [[] for i in range(26)]
a2 = [0 for i in range(26)]
b2 = [[] for i in range(26)]
v1 = []
v2 = []
cntv1 = 0
for i in range(n):
	if s1[i] == '?':
		cntv1 += 1
		v1.append(i)
	else:
		a1[ord(s1[i]) - 97] += 1
		b1[ord(s1[i]) - 97].append(i)
cntv2 = 0
for i in range(n):
	if s2[i] == '?':
		cntv2 += 1
		v2.append(i)
	else:
		a2[ord(s2[i]) - 97] += 1
		b2[ord(s2[i]) - 97].append(i)
cnt = 0
c = []
for i in range(26):
	while a1[i] > 0 and a2[i] > 0:
		cnt += 1
		a1[i] -= 1
		a2[i] -= 1
		c.append([b1[i].pop() + 1, b2[i].pop() + 1])
for i in range(26):
	while a1[i] > 0 and cntv2 > 0:
		cntv2 -= 1
		a1[i] -= 1
		cnt += 1
		c.append([b1[i].pop() + 1, v2.pop() + 1])
for i in range(26):
	while a2[i] > 0 and cntv1 > 0:
		cntv1 -= 1
		a2[i] -= 1
		cnt += 1
		c.append([v1.pop() + 1, b2[i].pop() + 1])
if cntv2 != 0 and cntv1 != 0:
	while cntv2 > 0 and cntv1 > 0:
		cnt += 1
		cntv1 -= 1
		cntv2 -= 1
		c.append([v1.pop() + 1, v2.pop() + 1])
print(cnt)
for i in range(len(c)):
	print(*c[i])


def main():
	n = int(input())
	a = input()
	b = input()
	a_map = {x: [] for x in a}
	b_map = {x: [] for x in b}
	a_good = [True for i in range(n)]
	b_good = [True for i in range(n)]
	for (i, x) in enumerate(a):
		a_map[x].append(i)
	for (i, x) in enumerate(b):
		b_map[x].append(i)
	a_lens = {x: len(a_map[x]) for x in a_map}
	b_lens = {x: len(b_map[x]) for x in b_map}
	ans = []
	for x in a_map:
		if x == '?':
			continue
		if x in b:
			for i in range(min(a_lens[x], b_lens[x])):
				ans.append((a_map[x][i] + 1, b_map[x][i] + 1))
				a_good[a_map[x][i]] = False
				b_good[b_map[x][i]] = False
	i = 0
	if '?' in a_map:
		for pos in a_map['?']:
			while i < n and (b_good[i] == False or b[i] == '?'):
				i += 1
			if i == n:
				break
			b_good[i] = False
			a_good[pos] = False
			ans.append((pos + 1, i + 1))
	i = 0
	if '?' in b_map:
		for pos in b_map['?']:
			while i < n and (a_good[i] == False or a[i] == '?'):
				i += 1
			if i == n:
				break
			a_good[i] = False
			b_good[pos] = False
			ans.append((i + 1, pos + 1))
	i = 0
	if '?' in a_map and '?' in b_map:
		for pos in a_map['?']:
			if not a_good[pos]:
				continue
			while i < b_lens['?'] and b_good[b_map['?'][i]] == False:
				i += 1
			if i == b_lens['?']:
				break
			b_good[b_map['?'][i]] = False
			a_good[pos] = False
			ans.append((pos + 1, b_map['?'][i] + 1))
	print(len(ans))
	for x in ans:
		print(x[0], x[1])
main()


n = int(input())
a = input().strip()
b = input().strip()
from collections import defaultdict
(da, db) = (defaultdict(list), defaultdict(list))
qa = []
qb = []
for i in range(n):
	if a[i] == '?':
		qa.append(i)
	else:
		da[a[i]].append(i)
	if b[i] == '?':
		qb.append(i)
	else:
		db[b[i]].append(i)
ans = []
for c in 'abcdefghijklmnopqrstuvwxyz':
	(u, v) = (da[c], db[c])
	while u and v:
		ans.append([u.pop(), v.pop()])
	while u and qb:
		ans.append([u.pop(), qb.pop()])
	while v and qa:
		ans.append([qa.pop(), v.pop()])
while qa and qb:
	ans.append([qa.pop(), qb.pop()])
print(len(ans))
for i in range(len(ans)):
	print(ans[i][0] + 1, ans[i][1] + 1)


n = int(input())
s = input()
t = input()
letter_index_s = {}
letter_index_t = {}
base_s = set()
base_t = set()
for i in set(s):
	letter_index_s[i] = set()
for i in set(t):
	letter_index_t[i] = set()
ans = []
for i in range(n):
	if s[i] != '?':
		letter_index_s[s[i]].add(i + 1)
	else:
		base_s.add(i + 1)
	if t[i] != '?':
		letter_index_t[t[i]].add(i + 1)
	else:
		base_t.add(i + 1)
for i in letter_index_s:
	if i in letter_index_t:
		while len(letter_index_s[i]) and len(letter_index_t[i]):
			z1 = letter_index_s[i].pop()
			z2 = letter_index_t[i].pop()
			ans.append((z1, z2))
for i in letter_index_s:
	for z in letter_index_s[i]:
		if base_t != set():
			ans.append((z, base_t.pop()))
		else:
			break
for i in letter_index_t:
	for z in letter_index_t[i]:
		if base_s != set():
			ans.append((base_s.pop(), z))
		else:
			break
for a in base_s:
	if base_t != set():
		ans.append((a, base_t.pop()))
	else:
		break
print(len(ans))
for i in ans:
	print(i[0], i[1])


n = int(input())
a = input()
b = input()
d1 = {}
d2 = {}
for i in range(n):
	if a[i] not in d1:
		d1[a[i]] = [1, [i + 1]]
	else:
		d1[a[i]][0] += 1
		d1[a[i]][1].append(i + 1)
for i in range(n):
	if b[i] not in d2:
		d2[b[i]] = [1, [i + 1]]
	else:
		d2[b[i]][0] += 1
		d2[b[i]][1].append(i + 1)
ans = 0
pairs = []
extras1 = []
extras2 = []
l1 = 0
l2 = 0
for i in d1.keys():
	if i != '?':
		if i in d2.keys():
			x = min(d1[i][0], d2[i][0])
			ans += x
			for j in range(x):
				u = d1[i][1].pop()
				v = d2[i][1].pop()
				pairs.append((u, v))
for i in d1.keys():
	if i != '?':
		while d1[i][1]:
			u = d1[i][1].pop()
			l1 += 1
			extras1.append(u)
for i in d2.keys():
	if i != '?':
		while d2[i][1]:
			u = d2[i][1].pop()
			l2 += 1
			extras2.append(u)
if '?' in d1.keys():
	x = min(d1['?'][0], l2)
	y = min(x, n - ans)
	ans += y
	for j in range(y):
		u = d1['?'][1].pop()
		v = extras2.pop()
		pairs.append((u, v))
	while d1['?'][1]:
		extras1.append(d1['?'][1][0])
		d1['?'][1].pop(0)
		l1 += 1
if ans < n:
	if '?' in d2.keys():
		x = min(d2['?'][0], l1)
		y = min(x, n - ans)
		ans += y
		for j in range(y):
			u = d2['?'][1].pop()
			v = extras1.pop()
			pairs.append((v, u))
print(ans)
for i in pairs:
	print(i[0], i[1])


n = int(input())
s = input()
b = input()
lst = [[] for i in range(26)]
lst_of_sorua = []
x = 1
for i in s:
	if i == '?':
		lst_of_sorua.append(x)
	else:
		lst[ord(i) - 97].append(x)
	x += 1
x = 1
lst_of_result = []
lst_of_sorub = []
lstb = []
for i in b:
	if i == '?':
		lst_of_sorub.append(x)
	elif lst[ord(i) - 97] != []:
		lst_of_result.append((lst[ord(i) - 97][-1], x))
		lst[ord(i) - 97].pop()
	else:
		lstb.append(x)
	x += 1
ptr1 = len(lst_of_sorua) - 1
lb = len(lstb)
ptrb = 0
while ptr1 >= 0 and ptrb < lb:
	lst_of_result.append((lst_of_sorua[ptr1], lstb[ptrb]))
	ptr1 -= 1
	ptrb += 1
lsorub = len(lst_of_sorub)
ptrsorub = 0
for i in range(26):
	if ptrsorub >= lsorub:
		break
	for j in lst[i]:
		lst_of_result.append((j, lst_of_sorub[ptrsorub]))
		ptrsorub += 1
		if ptrsorub == lsorub:
			break
while ptr1 >= 0 and ptrsorub < lsorub:
	lst_of_result.append((lst_of_sorua[ptr1], lst_of_sorub[ptrsorub]))
	ptr1 -= 1
	ptrsorub += 1
print(len(lst_of_result))
for i in lst_of_result:
	print(*i)


from collections import defaultdict
n = int(input())
a = input()
b = input()
a_dict = defaultdict(list)
b_dict = defaultdict(list)
for i in range(n):
	a_dict[a[i]].append(i + 1)
for i in range(n):
	b_dict[b[i]].append(i + 1)
ans = []
letters = 'qwertyuiopasdfghjklzxcvbnm'
for i in letters:
	while a_dict[i] and b_dict[i]:
		ans.append([a_dict[i].pop(), b_dict[i].pop()])
for i in letters:
	while a_dict['?'] and b_dict[i]:
		ans.append([a_dict['?'].pop(), b_dict[i].pop()])
for i in letters:
	while a_dict[i] and b_dict['?']:
		ans.append([a_dict[i].pop(), b_dict['?'].pop()])
for i in letters:
	while a_dict['?'] and b_dict['?']:
		ans.append([a_dict['?'].pop(), b_dict['?'].pop()])
print(len(ans))
for i in ans:
	print(*i)


import string
ap = string.ascii_lowercase[:]
app = ap + '?'
n = int(input())
a = input()
b = input()
da = dict()
db = dict()
ans = []
for i in app:
	da[i] = []
	db[i] = []
for i in range(n):
	da[a[i]].append(i + 1)
for i in range(n):
	db[b[i]].append(i + 1)
for i in ap:
	t = min(len(da[i]), len(db[i]))
	for j in range(t):
		(t1, t2) = (da[i].pop(), db[i].pop())
		ans.append([t1, t2])
reda = []
redb = []
for i in ap:
	if len(da[i]) > 0:
		reda += da[i]
	if len(db[i]) > 0:
		redb += db[i]
qa = da['?']
qb = db['?']
t = min(len(reda), len(qb))
for j in range(t):
	(t1, t2) = (reda.pop(), qb.pop())
	ans.append([t1, t2])
t = min(len(redb), len(qa))
for j in range(t):
	(t1, t2) = (redb.pop(), qa.pop())
	ans.append([t2, t1])
t = min(len(qb), len(qa))
for j in range(t):
	(t1, t2) = (qb.pop(), qa.pop())
	ans.append([t2, t1])
print(len(ans))
for i in ans:
	print(*i)


n = int(input())
l = list(input())
r = list(input())
la = [[] for i in range(26)]
ra = [[] for i in range(26)]
ir = []
il = []
for i in range(n):
	if l[i] != '?':
		la[ord(l[i]) - ord('a')].append(i)
	else:
		il.append(i)
for i in range(n):
	if r[i] != '?':
		ra[ord(r[i]) - ord('a')].append(i)
	else:
		ir.append(i)
ans = []
for i in range(26):
	while len(la[i]) > 0 and len(ra[i]) > 0:
		(x, y) = (la[i].pop(), ra[i].pop())
		ans.append([x + 1, y + 1])
for i in range(26):
	while len(ir) > 0 and len(la[i]) > 0:
		(x, y) = (la[i].pop(), ir.pop())
		ans.append([x + 1, y + 1])
for i in range(26):
	while len(il) > 0 and len(ra[i]) > 0:
		(x, y) = (il.pop(), ra[i].pop())
		ans.append([x + 1, y + 1])
while len(il) > 0 and len(ir) > 0:
	(x, y) = (il.pop(), ir.pop())
	ans.append([x + 1, y + 1])
print(len(ans))
for i in ans:
	print(*i)


from math import *
n = int(input())
s = input()
s1 = input()
dl = {}
dp = {}
for i in range(n):
	c = s[i]
	if c not in dl:
		dl[c] = []
	dl[c].append(i + 1)
	c = s1[i]
	if c not in dp:
		dp[c] = []
	dp[c].append(i + 1)
ans = []
ost1 = []
ost2 = []
p1 = []
p2 = []
for c in dl:
	if c == '?':
		p1 = dl[c][:]
		continue
	if c not in dp:
		ost1 += dl[c]
		continue
	while dl[c] and dp[c]:
		ans.append((dl[c].pop(), dp[c].pop()))
	ost1 += dl[c]
	ost2 += dp[c]
for c in dp:
	if c != '?':
		ost2 += dp[c]
	else:
		p2 = dp[c]
while p1 and ost2:
	ans.append((p1.pop(), ost2.pop()))
while p2 and ost1:
	ans.append((ost1.pop(), p2.pop()))
while p1 and p2:
	ans.append((p1.pop(), p2.pop()))
print(len(ans))
for c in ans:
	print(c[0], c[1])


def main():
	n = int(input())
	l = input().strip()
	r = input().strip()
	pairs = solve(n, l, r)
	print(len(pairs))
	print('\n'.join((' '.join((str(xx) for xx in x)) for x in pairs)))

def solve(n, l, r):
	from collections import deque
	answer = deque()
	l_enum_by_color = count([x for x in enumerate(l)])
	r_enum_by_color = count([x for x in enumerate(r)])
	l_q_list = l_enum_by_color.pop('?') if '?' in l_enum_by_color else deque()
	r_q_list = r_enum_by_color.pop('?') if '?' in r_enum_by_color else deque()
	for key in l_enum_by_color:
		if key not in r_enum_by_color:
			continue
		to_take_count = min(len(l_enum_by_color[key]), len(r_enum_by_color[key]))
		for _ in range(to_take_count):
			answer.append((l_enum_by_color[key].popleft(), r_enum_by_color[key].popleft()))
	for key in l_enum_by_color:
		if len(l_enum_by_color[key]) == 0:
			continue
		to_take_count = min(len(l_enum_by_color[key]), len(r_q_list))
		for _ in range(to_take_count):
			answer.append((l_enum_by_color[key].popleft(), r_q_list.popleft()))
	for key in r_enum_by_color:
		if len(r_enum_by_color[key]) == 0:
			continue
		to_take_count = min(len(r_enum_by_color[key]), len(l_q_list))
		for _ in range(to_take_count):
			answer.append((l_q_list.popleft(), r_enum_by_color[key].popleft()))
	for _ in range(min(len(l_q_list), len(r_q_list))):
		answer.append((l_q_list.popleft(), r_q_list.popleft()))
	return answer

def count(it):
	from collections import deque
	d = {}
	for a in it:
		if a[1] in d:
			d[a[1]].append(a[0] + 1)
		else:
			d[a[1]] = deque([a[0] + 1])
	return d
main()


n = int(input())
a = input()
b = input()
da = {}
db = {}
za = set()
zb = set()
for (i, x) in enumerate(a):
	if x == '?':
		za.add(i + 1)
	if x in da:
		da[x].append(i + 1)
	else:
		da[x] = [i + 1]
for (i, x) in enumerate(b):
	if x == '?':
		zb.add(i + 1)
	if x in db:
		db[x].append(i + 1)
	else:
		db[x] = [i + 1]
intersect = set(a) & set(b)
res = []
ra = set([x for x in range(1, n + 1)])
rb = set([x for x in range(1, n + 1)])
for c in intersect:
	if c != '?':
		length = min(len(da[c]), len(db[c]))
		for i in range(length):
			res.append([da[c][i], db[c][i]])
			ra.remove(da[c][i])
			rb.remove(db[c][i])
xb = rb - zb
if '?' in da:
	tmp = da['?']
	if len(tmp) <= len(xb):
		for (i, xb_) in enumerate(xb):
			if i < len(da['?']):
				res.append([da['?'][i], xb_])
				ra.remove(da['?'][i])
				rb.remove(xb_)
			else:
				break
	else:
		for (i, xb_) in enumerate(xb):
			res.append([da['?'][i], xb_])
			ra.remove(da['?'][i])
			rb.remove(xb_)
		rb2 = db['?'][:len(tmp) - len(xb)]
		db['?'] = db['?'][len(tmp) - len(xb):]
		for i in range(len(xb), len(tmp)):
			res.append([da['?'][i], rb2[i - len(xb)]])
			ra.remove(da['?'][i])
		rb = set(list(rb)[len(tmp) - len(xb):])
xa = ra - za
if '?' in db:
	tmp = db['?']
	for (i, xa_) in enumerate(xa):
		if i < len(db['?']):
			res.append([xa_, db['?'][i]])
			rb.remove(db['?'][i])
			ra.remove(xa_)
		else:
			break
print(len(res))
for x in res:
	print(*x)


from math import factorial, ceil, pow, sqrt, floor
from sys import stdin, stdout
from collections import defaultdict, Counter, deque
n = int(stdin.readline())
(a, b) = (stdin.readline().strip('\n'), stdin.readline().strip('\n'))
siri = [[] for i in range(27)]
shan = [[] for e in range(27)]
for i in range(n):
	if a[i] == '?':
		siri[26].append(i)
	else:
		siri[ord(a[i]) - ord('a')].append(i)
	if b[i] == '?':
		shan[26].append(i)
	else:
		shan[ord(b[i]) - ord('a')].append(i)
mps = []
for i in range(27):
	while siri[i] and shan[i]:
		mps.append((siri[i].pop(), shan[i].pop()))
	while siri[i] and shan[26]:
		mps.append((siri[i].pop(), shan[26].pop()))
	while siri[26] and shan[i]:
		mps.append((siri[26].pop(), shan[i].pop()))
print(len(mps))
for e in mps:
	print(e[0] + 1, e[1] + 1)