#include <iostream>
#include <vector>
#include <algorithm>
#define MAXN 9
using namespace std;
char map[3][3][10] = { {"CIRCLE","SQUARE","TRIANGLE"},
{"RED","YELLOW","BLUE"},
{"BLACK","GRAY","WHITE"} };
int arr[MAXN][3];
bool hap[MAXN][MAXN][MAXN];
bool visited[MAXN][MAXN][MAXN];
bool check_hap(int arr1[] , int arr2[], int arr3[]){
int cnt = 0;
for (int i = 0; i < 3; i++){
if (arr1[i] == arr2[i] && arr2[i] == arr3[i]) cnt++;
else if (arr1[i]!= arr2[i] && arr2[i]!=arr3[i] && arr1[i] != arr3[i]) cnt++;
}
return (cnt==3);
}
bool is_finished;
int hap_cnt;
int N;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
for (int i = 0 ;i < MAXN; i++){
for (int j =0 ;j < 3; j++){
string s;
cin >> s;
for (int k = 0; k < 3; k++){
if (s == map[j][k]){
arr[i][j] = k;
break;
}
}
}
}
for (int i= 0 ; i < MAXN; i++){
for (int j = i+1; j < MAXN; j++){
for (int k = j+1; k<MAXN; k++){
if (check_hap(arr[i],arr[j],arr[k])){
hap[i][j][k] = true;
hap_cnt++;
}
}
}
}
cin >> N;
int ans = 0 ;
for (int i = 0 ; i < N;i++){
char tmp;
cin >> tmp;
if (tmp == 'H'){
int order[3]={0,};
cin >> order[0] >> order[1] >> order[2];
order[0]--;
order[1]--;
order[2]--;
sort(order,order+3);
if (!visited[order[0]][order[1]][order[2]] && hap[order[0]][order[1]][order[2]]){
visited[order[0]][order[1]][order[2]] = true;
hap_cnt--;
ans++;
}
else{
ans--;
}
}
else{
if (!is_finished && hap_cnt==0) {
is_finished = true;
ans+=3;
}
else ans--;
}
}
cout << ans <<"\n";
return 0;
}