#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define DEBUG(x) cerr << ">>> " << #x << " : " << x << endl;
#define REP(i,a) for (int i = 0; i < (a); ++i)
#define FOR(i,a,b) for (int i = (a); i <= (b); ++i)
#define FORD(i,a,b) for (int i = (a); i >= (b); --i)
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
const int INF = 1<<29;
typedef long long ll;
//////////////////////////////////
typedef vector<pair<int, int> > ruka;
pair<int, int> convert(char c[]){
pair<int, int> ans;
ans.first = c[0] - '0';
if(c[0] == 'X') ans.first = 10;
if(c[0] == 'J') ans.first = 11;
if(c[0] == 'Q') ans.first = 12;
if(c[0] == 'K') ans.first = 13;
if(c[0] == 'A') ans.first = 14;
ans.second = c[1];
return ans;
}
bool One(ruka &r){
sort(r.begin(), r.end(), greater<pair<int, int> >());
return true;
}
bool Flush(ruka &r){
REP(i, 5) if(r[i].second != r[0].second) return false;
One(r);
return true;
}
bool FullHouse(ruka &r){
One(r);
if(r[0].first == r[1].first && r[1].first == r[2].first && r[3].first == r[4].first) return true;
reverse(r.begin(), r.end());
if(r[0].first == r[1].first && r[1].first == r[2].first && r[3].first == r[4].first) return true;
return false;
}
bool Straight(ruka &r){
One(r);
if(r[0].first == 14 && r[1].first == 5 && r[2].first == 4 && r[3].first == 3 && r[4].first == 2){
r[0].first = 0;
One(r);
r[4].first = 14;
return true;
}
REP(i, 4) if(r[i+1].first != r[i].first + 1) return false;
return true;
}
bool RoyalFlush(ruka &r){ return Flush(r) && Straight(r); }
bool Poker(ruka &r){
One(r);
bool ok = true;
REP(i, 4) if(r[i].first != r[0].first){ ok = false; break; }
if(ok) return true;
reverse(r.begin(), r.end());
ok = true;
REP(i, 4) if(r[i].first != r[0].first){ ok = false; break; }
if(ok) return true;
return false;
}
bool Three(ruka &r){
One(r);
if(r[0].first == r[1].first && r[1].first == r[2].first) return true;
if(r[3].first == r[1].first && r[1].first == r[2].first){ swap(r[0], r[3]); return true; }
if(r[3].first == r[4].first && r[4].first == r[2].first){ swap(r[0], r[3]); swap(r[1], r[4]); return true; }
return false;
}
bool Pair(ruka &r){
One(r);
if(r[0].first == r[1].first) return true;
if(r[2].first == r[1].first){ swap(r[0], r[2]); return true; }
if(r[3].first == r[2].first){ swap(r[0], r[2]); swap(r[1], r[3]); return true; }
if(r[3].first == r[4].first){ swap(r[0], r[3]); swap(r[1], r[4]); sort(r.begin() + 2, r.end(), greater<pair<int, int> >()); return true; }
return false;
}
bool TwoPairs(ruka &r){
One(r);
if(r[0].first == r[1].first && r[2].first == r[3].first) return true;
if(r[0].first == r[1].first && r[3].first == r[4].first){ swap(r[2], r[4]); return true; }
if(r[1].first == r[2].first && r[3].first == r[4].first){ swap(r[0], r[2]); swap(r[2], r[4]); return true; }
return false;
}
int eval(ruka &a){
if(RoyalFlush(a)) return 8;
if(Poker(a)) return 7;
if(FullHouse(a)) return 6;
if(Flush(a)) return 5;
if(Straight(a)) return 4;
if(Three(a)) return 3;
if(TwoPairs(a)) return 2;
if(Pair(a)) return 1;
One(a);
return 0;
}
bool cmp(ruka &a, ruka &b){
int vA = eval(a), vB = eval(b);
if(vA > vB) return true;
if(vB > vA) return false;
REP(i, 5){
if(a[i].first > b[i].first) return true;
if(a[i].first < b[i].first) return false;
}
return true;
}
ruka stul;
ruka hraci[111];
ruka nejlepsi;
vector<int> odpoved;
int N;
int main() {
while(scanf("%d", &N) != EOF){
char tmp[5];
stul.clear();
odpoved.clear();
REP(i, 5){ scanf("%s", tmp); stul.push_back(convert(tmp)); }
REP(i, N){
hraci[i].clear();
REP(j, 2){ scanf("%s", tmp); hraci[i].push_back(convert(tmp)); }
ruka best = stul;
REP(j, 5){
ruka tmp = stul;
tmp[j] = hraci[i][0];
if(cmp(tmp, best)) best = tmp;
}
REP(j, 5){
ruka tmp = stul;
tmp[j] = hraci[i][1];
if(cmp(tmp, best)) best = tmp;
}
REP(j, 5) REP(k, j){
ruka tmp = stul;
tmp[j] = hraci[i][0];
tmp[k] = hraci[i][1];
if(cmp(tmp, best)) best = tmp;
}
if(i == 0 || cmp(best, nejlepsi)){ if(i && !cmp(nejlepsi, best)) odpoved.clear(); odpoved.push_back(i); nejlepsi = best; }
}
REP(i, odpoved.size()){
if(i) printf(" ");
printf("%d", odpoved[i] + 1);
}
printf("\n");
}
return 0;
}