s = 0
unseen = 1
seen(v) = bittest(s, v)
see(v) = s = bitor(s, 2^v); while (seen(unseen), unseen++)

mx = 10 000

other(k) = {
	while (mm[k] <= mx,
		if (!seen(mm[k]),
				return (mm[k]),
				mm[k] += vv[k]
		);
	);
	return (-1);
}

a = vector(mx);
u = 1;

nb = 0;
nn = vector(mx);
vv = vector(mx);
mm = vector(mx);

emit(n, v) = {
	if (v > 0 && v <= mx,
		see(v);

		nn[nb++] = n;
		vv[nb  ] = v;
		mm[nb  ] = 2*v;

		a[v] = n;
		while (a[u],
			print (u " " a[u]);
			if (u++ > #a,
				quit;
			);
		);
	);
}

{
	apply (see, powers(2, logint(mx, 2)));
	for (n = 0, oo,
		nnb = nb;
		emit(2^n, 2^n);
		for (k = 1, nnb,
			emit(2^n + nn[k], other(k));
		);

		\\ compress
		newnb = 0;

		for (k = 1, nb,
			if (mm[k] <= mx,
				nn[newnb++] = nn[k];
				vv[newnb  ] = vv[k];
				mm[newnb  ] = mm[k];
			);
		);

		nb = newnb;
	);
}

quit