//============================================================================
// Name        : TopCoderCompetition.cpp
// Author      : taguchi
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <bitset>
#include <algorithm>
#include <map>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <numeric>
#include <list>
using namespace std;
//#define P pair<int,int>
#define rep(i,n) for(int i = 0;i<n;i++)
#define pb(n) push_back(n)
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<string> vs;
struct edge{int cost,to;};

vector<string> split(string &in,char delimiter){
	vector<string> re; uint cur = 0,next;
	while((next = in.find_first_of(delimiter,cur)) != string::npos){
		re.pb(string(in,cur,next-cur));
		cur = next + 1;
	}
	re.pb(string(in,cur,in.size()-cur));
	return re;
}
vector<int> split_int(string &in, char delimiter){
	vector<string> str = split(in,delimiter);
	vector<int> re;
	rep(i,str.size()){
		re.pb(strtol(str[i].c_str(),NULL,10));
	}
	return re;
}

class Range{
public:
	int a,b;
	Range(int an,int bn) : a(an),b(bn){}

	bool operator()(int n){
		return a<=n&&n<=b;
	}
	bool isCross(Range x){
		return x(a) || x(b);
	}
	bool contains(Range x){
		return this->a <= x.a && x.b <= this->b;
	}
};

class RangeHandler{
public:
	list<Range> Ranges;

	bool insert(Range x){
		if(Ranges.empty()){
			Ranges.push_back(x);
			return true;
		}

		if(x.b <= Ranges.front().a){
			Ranges.push_front(x);
			return true;
		}
		if(Ranges.back().b <= x.a){
			Ranges.push_back(x);
			return true;
		}
		for(list<Range>::iterator i = Ranges.begin();i != Ranges.end();i++){
			list<Range>::iterator l = i;
			l++;
			//cout << "dbg" << i->b << " " << i->a << endl;
			if(Range(i->b,l->a).contains(x)){
				Ranges.insert(l,x);
				return true;
			}
		}
		return false;
	}
};
int N,P;
int main(){
	while(true){
		int result = 0;
		cin >> N;
		if(N == 0)break;
		vector<pair<int,string> > Programs,Table;
		vector<pair<int,string> >::iterator ip;
		rep(i,N){
			string name;
			cin >> name;
			int weekday,start;
			cin >> weekday >> start;
			start -=600;
			int time = 1440 * weekday + (start / 100) * 60 + (start % 100);
			Programs.pb(make_pair(time,name));
		}
		cin >> P;
		rep(i,P){
			string name;
			cin >> name;
			for(ip = Programs.begin();ip!=Programs.end();ip++){
				if(ip->second == name){
					Table.pb(*ip);
					Programs.erase(ip);
					break;
				}
			}
		}
		sort(Programs.begin(),Programs.end());
		rep(i,Programs.size()){
			Table.pb(Programs[i]);
		}
		RangeHandler rg;
		bool ans = true;
		rep(i,Table.size()){
			if(rg.insert(Range(Table[i].first,Table[i].first+30))){
				ans &= true;
				result++;
			}
			else ans = false;
			if(i < P && !ans) {
				result = -1;
				break;
			}
		}
		cout << result << endl;
	}
	return 0;
}