//Siddharth Jha #include //#include //#include //#include //using namespace __gnu_pbds; using namespace std; //typedef tree, rb_tree_tag, tree_order_statistics_node_update> pbds; //typedef trie,pat_trie_tag,trie_prefix_search_node_update> pbtrie; #define ll long long int #define mod 1000000007 #define inf 1e18 #define pb push_back #define vi vector #define vs vector #define pii pair #define ump unordered_map #define mp make_pair #define pq_max priority_queue #define pq_min priority_queue > #define all(n) n.begin(),n.end() #define ff first #define ss second #define mid(l,r) (l+(r-l)/2) #define bitc(n) __builtin_popcount(n) #define SET(a) memset( a, -1, sizeof a ) #define CLR(a) memset( a, 0, sizeof a ) #define Pi 3.141592653589793 #define loop(i,a,b) for(int i=(a);i<=(b);i++) #define looprev(i,a,b) for(int i=(a);i>=(b);i--) #define _fast ios_base::sync_with_stdio(0); cin.tie(0); #define iter(container,it) for(__typeof(container.begin()) it = container.begin(); it != container.end(); it++) #define log(args...) {string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator _it(_ss); err(_it, args); } #define logarr(arr,a,b) for(int z=(a);z<=(b);z++) cout<<(arr[z])<<" ";cout< T gcd(T a, T b){if(a%b) return gcd(b,a%b);return b;} template T lcm(T a, T b){return (a*(b/gcd(a,b)));} vs tokenizer(string str,char ch) {std::istringstream var((str)); vs v; string t; while(getline((var), t, (ch))) {v.pb(t);} return v;} void err(istream_iterator it) {} template void err(istream_iterator it, T a, Args... args) { cout << *it << " = " << a << endl; err(++it, args...); } void solve() { ll n,m; cin >> n; vector s(n); loop (i,0,n-1) cin >> s[i]; cin >> m; string guess,cor; vector ans(5,'0'); map yellow,black; loop (i,1,m) { cin >> guess >> cor; loop (j,0,4) { if (cor[j] == 'b') { black[guess[j]] = true; continue; } if (cor[j] == 'g') { ans[j] = guess[j]; yellow[guess[j]] = true; continue; } else { yellow[guess[j]] = true; } } } ll cnt = 0; bool f = 1; loop (i,0,n-1) { f = 1; loop (j,0,4) { if (s[i][j] != ans[j] && ans[j] != '0') { f = 0; break; } if (black.find(s[i][j]) != black.end()) { f = 0; break; } } map present; loop (j,0,4) { present[s[i][j]] = true; } ll siz = 0; for (auto k : yellow) { if (present.find(k.ff) != present.end()) { siz++; } } if (f && siz == yellow.size()) cnt++; } cout << cnt; } int main(int argc, char const *argv[]){ _fast //#ifndef ONLINE_JUDGE //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); //#endif ll t=1; while(t--){ solve(); } return 0; }