#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;

#define rep(i, n) for(int i = 0 ; i < n ; i++)
#define f first
#define s second

typedef pair<int, int> P;
typedef map<string, P> Program;

Program prog;
vector<string> all;
vector<string> fav;
vector<string> normal;
bool watched[7][1800];
int ans;

int toMinute(string t){
  int ret = 0;
  if(t.size() == 3){
    ret += (t[0]-'0') * 60;
    ret += (t[1]-'0') * 10 + (t[2]-'0');
  }
  else if(t.size() == 4){
    int h =(t[0]-'0') * 10 + (t[1]-'0');
    ret += h * 60;
    ret += (t[2]-'0') * 10 + (t[3]-'0');
  }
  return ret;
}

bool watchFavAnime(){
  rep(i, fav.size()){
    P t = prog[fav[i]];
    for(int T = t.s ; T < t.s+30 ; T++){
      if(watched[t.f][T] == 1) return false;
      watched[t.f][T] = 1;
    }
    ans++;
  }
  return true;
}

void init(){
  prog.clear();
  fav.clear();
  all.clear();
  normal.clear();
  memset(watched, 0, sizeof(watched));
  ans = 0;
}

int main(){
  int n, p;  
  while(cin >> n, n){
    init();
    string name, tim;
    P t;
    rep(i, n){
      cin >> name >> t.f >> tim;
      int m = toMinute(tim);
      t.s = m;
      prog[name] = t;
      all.push_back(name);
    }
    
    cin >> p;
    rep(i, p){
      cin >> name;
      fav.push_back(name);
    }
    // input finish
    
    bool flag = true;
    
    flag = watchFavAnime();
    if(!flag){
      cout << "-1" << endl;
      continue;
    }
    
    rep(i, n){
      bool fl = true;
      rep(j, fav.size()){
	if(all[i] == fav[j]){
	  fl = false;
	  break;
	}
      }
      if(fl) normal.push_back(all[i]);
    }
    
    vector<P> nor;
    rep(i, normal.size()){
      nor.push_back(prog[normal[i]]);
    }
    
    sort(nor.begin(), nor.end());
    
    rep(i, nor.size()){
      int w = nor[i].f;
      int m = nor[i].s;
      bool f = true;
      for(int T = m ; T < m+30 ; T++){
	if(watched[w][T] == 1){
	  f = false;
	  break;
	}
      }
      if(f){
	for(int T = m ; T < m+30 ; T++){
	  watched[w][T] = 1;
	}
	//cout << normal[i] << endl;
	ans++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}