// Hust 14047 Bora Bora
// LiJunLe
// Accepted
// http://acm.hust.edu.cn:8080/judge/problem/viewProblem.action?id=14047

#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>

#define C 1
#define D 2
#define H 3
#define S 4

using namespace std;

typedef unsigned int UI;

struct Card {
	int number;
	int suit;
	bool effect;

	bool fix(Card o) {
		if (number == o.number || suit == o.suit) {
			return true;
		} else {
			return false;
		}
	}

	bool operator<(Card o) const {
		return (number * 10 + suit) > (o.number * 10 + o.suit);
	}
};

queue<Card> stock;
Card discard;
int curP;
int direction;
int p, m, n;

struct Player {
	multiset<Card> hand; // needed to use multiset STL!!!

	void draw() {
		Card temp = stock.front();
		stock.pop();
		hand.insert(temp);
	}

	void debug() { // used for debuging
		set<Card>::iterator it;
		for (it = hand.begin(); it != hand.end(); it++) {
			cout << it->number << "^" << it->suit << " ";
		}
		cout << endl;
	}

	bool doIt() {
		set<Card>::iterator it;
		for (it = hand.begin(); it != hand.end(); it++) {
			if (discard.fix(*it)) {
				discard = *it;
				hand.erase(it);
				return true;
			}
		}
		return false;
	}

	bool win() {
		if (hand.empty()) {
			return true;
		}
		else {
			return false;
		}
	}

} player[12];

void init() {
	int i;
	for (i = 0; i < 12; i++) {
		player[i].hand.clear();
	}
	while (stock.empty() == false) {
		stock.pop();
	}
}

int nextP(int next) {
	return (curP + p + next) % p;
}

int main() {
	while (cin >> p >> m >> n, p || m || n) {
		init();
		int i;
		for (i = 0; i < n; i++) {
			char s[2];
			Card temp;
			temp.effect = true;
			cin >> temp.number >> s;
			if (s[0] == 'C') {
				temp.suit = C;
			}
			else if (s[0] == 'D') {
				temp.suit = D;
			}
			else if (s[0] == 'H') {
				temp.suit = H;
			}
			else if (s[0] == 'S') {
				temp.suit = S;
			}
			stock.push(temp);
		}

		for (i = 0; i < p; i++) {
			for (int j = 0; j < m; j++) {
				Card temp = stock.front();
				stock.pop();
				player[i].hand.insert(temp);
			}
		}

		discard = stock.front();
		stock.pop();
		direction = 1;
		if (discard.number == 12 && discard.effect) {
			discard.effect = false;
			direction *= -1;
		}
		curP = 0;

		while (true) {
			if (discard.number == 1 && discard.effect) {
				discard.effect = false;
				player[curP].draw();
			} else if (discard.number == 7 && discard.effect) {
				discard.effect = false;
				player[curP].draw();
				player[curP].draw();
			} else if (discard.number == 11 && discard.effect) {
				discard.effect = false;
			} else {
				if (player[curP].doIt() == false) {
					player[curP].draw();
					player[curP].doIt();
				}
				if (player[curP].win()) {
					cout << curP + 1 << endl;
					break;
				}
			}
			if (discard.number == 12 && discard.effect) {
				discard.effect = false;
				direction *= -1;
			}
			curP = nextP(direction);
		}
	}
	return 0;
}