import java.io.*;
import java.util.*;
import java.math.*;

public class fractran
{
		static StreamTokenizer in;

		static int NP;
		static int cs;
		static int[] PRIMES = new int[200];

		static boolean isPrime(int n)
		{
				for (int i = 2; i * i <= n; ++i)
						if (n % i == 0) return false;
				return true;
		}

		static void init()
		{
				NP = 1; // [0] unused
				for (int i = 2; i < 1000; ++i)
						if (isPrime(i))
								PRIMES[NP++] = i;
		}


		static int nextInt()
				throws IOException
		{
				if (in.nextToken() != StreamTokenizer.TT_NUMBER)
						throw new RuntimeException("bumm");
				return (int) in.nval;
		}


		static int[] xx(int n)
		{
				int[] r = new int[200];
				int ti = 1;
				while (n > 1 && ti < NP) {
						int t = PRIMES[ti];
						if (t * t > n) break;
						while (n % t == 0) {
								n /= t;
								++r[ti];
						}
						++ti;
				}
				if (n > 1) {
						while (ti < NP && PRIMES[ti] < n) ++ti;
						++r[ti];
				}
				r[0] = ti;
				return r;
		}


		static boolean fits(int[] into, int[] what)
		{
				for (int j = what[0]; j > 0; --j)
						if (what[j] > into[j])
								return false;
				return true;
		}


		static void add(int[] to, int[] what, int sign)
		{
				for (int j = what[0]; j > 0; --j) {
						int a = what[j] * sign;
						to[j] += a;
						if (j > 1) cs += a;
				}
		}


		static void print(int[] v)
		{
				for (int i = v[0] + 10; i > 0; --i)
						if (v[i] > 0)
								System.out.print(PRIMES[i] + "^" + v[i] + " * ");
		}


		static int nextE(int[] b, int k, int[][] zz, int[][] nn)
		{
				if (cs < 0) {
						cs = 0;
						for (int j = b[0]; j > 1; --j)
								cs += b[j];
						if (cs == 0)
								return b[1];
				}
				while (true) {
						for (int j = 0; j < k; ++j)
								if (fits(b, nn[j])) {
										add(b, nn[j], -1);
										add(b, zz[j], 1);
										break;
								}
						if (cs == 0)
								return b[1];
				}
		}


		public static void main(String[] args)
				throws IOException
		{
				in = new StreamTokenizer(new InputStreamReader(System.in));
				init();

				while (true) {
						int m = nextInt();
						if (m == 0) break;
						int[] b = xx(nextInt());
						int k = nextInt();

						int[][] zz = new int[k][];
						int[][] nn = new int[k][];
						for (int j = 0; j < k; ++j) {
								zz[j] = xx(nextInt());
								nn[j] = xx(nextInt());
						}

						cs = -1;
						while (true) {
								System.out.print(nextE(b, k, zz, nn));
								if (--m <= 0) break;
								System.out.print(' ');
						}
						System.out.println();
				}
		}
}
