#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
#define fr first
#define sc second
#define INF (1<<30)
int main(){
  int N,P;
  while(cin >> N , N){
    int ans = 0;
    vector<pair<int,string> > vc(N);
    for(int i = 0 ; i < N ; i++ ){
      string name;
      int weekday , start;
      cin >> name >> weekday >> start;
      vc[i] = make_pair(weekday * 24 * 60 + (start/100)*60 + start % 100,name);
    }
    sort(vc.begin(),vc.end());
    cin >> P;
    for(int q = 0 ; q < P ; q++ ){
      string fav;
      bool OR = false;
      cin >> fav;
      for(int i = 0 ; i < (int)vc.size() ; i++ ){
        if(vc[i].sc == fav){
          OR = true;
          pair<int,string> r = make_pair(vc[i].fr-29,"0"),l = make_pair(vc[i].fr+30,"0");
          vc.erase(lower_bound(vc.begin(),vc.end(),r),lower_bound(vc.begin(),vc.end(),l));
          break;
        }
      }
      if(!OR) ans = -INF;
    }
    int now = 0;
    for(int i = 0 ; i < (int)vc.size() ; i++ ){
      if(vc[i].fr >= now) now = vc[i].fr + 30 , ans++;
    }
    cout << max(-1,ans+P) << endl;
  }
}