Problem:
Problem B: Watchin' TVA

Animation is one of methods for making movies and in Japan, it is popular to broadcast as a television program or perform as a movie. Many people, especially the young, love one. And here is an anime lover called Jack. We say he is an mysterious guy with uncertain age. He likes anime which are broadcasted in midnight and early morning especially.

In his room, there is a huge piece of paper on the wall. He writes a timetable of TV anime on it. In his house, he can watch all Japanese TV anime programs that are broadcasted in Japan using a secret and countrywide live system. However he can not watch all anime and must give up to watch some programs because they are "broadcasted at the same time" in a season. Here, programs are "broadcasted at the same time" means that two or more programs have one or more common minutes in broadcasting time. Increasing the number of anime programs in recent makes him nervous. Actually, some people buy DVDs after the program series ends or visit a web site called vhefoo. Anyway, he loves to watch programs on his live system. Of course, he is not able to watch two or more programs at the same time. However, as described above, he must give up some programs broadcasted at the same time. Therefore, he has a set of programs F and he watches programs in a set F absolutely.

Your task is to write a program that reads a timetable and outputs the maximum number of watchable programs, keeping that Jack watches all programs in the set F. Of course, there are multiple choices of programs, so we want the number of programs he can watch. If two or more programs in a set F are broadcasted at the same time, you must give Jack an unfortunate announcement. In this case, your program outputs -1. In addition, each anime program is a program of 30 minutes.

Input

Input consists of multiple datasets.

A dataset is given in a following format.

N
PROGRAM1
PROGRAM2
...
PROGRAMN
P
FAV1
FAV2
...
FAVP

N is the number of programs in a season.

PROGRAMi(1≤i≤N)is a string which has the following format.

name weekday start

name is a program name. This is a a string having between 1 and 32 characters and these names do not overlap each other program. A name consists of alphanumeric characters and '_'(underscore).

weekday is a broadcasting weekday about the corresponding program. This is an integer. 0 means Sunday and 1 is Monday and so on (2:Tuesday, 3:Wednesday, 4:Thursday, 5:Friday, 6:Saturday).

start is a starting time of the program broadcasting. This is an integer between 600 and 2929. First one or two digits represent hour and the last two digits represent minute. If the hour has one digit it will be a representation "900" for example. Note: a program may have an integer more than or equal to 2400 as start, if the program begins the next day. For example, a program begins from 2500 on Monday should be interpreted as a program begins from 0100 on Tuesday. There are no input the minute of start exceeds 59. And when the hour of start is equal to 29, there are no input the minute of start exceeds 29.

P is an integer and represents the number of elements in the set F. And FAVi(1≤i≤P≤N) is a string for a program name which Jack watches absolutely. You can assume names which are not given in program descriptions will not appear in the set F.

The last line contains a single 0 which represents the end of input.

Constraints

The number of datasets is less than or equal to 400.

1≤N≤500

Output

For each dataset, output an integer S that represents maximum number of programs Jack can watch in the following format.

S

Sample Input

1
galaxy_angel 0 600
1
galaxy_angel
11
A 0 600
B 0 610
C 0 610
D 0 610
E 0 640
EE 0 700
F 0 710
G 0 710
H 0 710
I 0 740
J 0 750
2
B
H
42
nchj 6 2620
anhnnnmewbktthsrn 4 2515
gntmdsh 1 1800
achnnl 4 2540
hnskirh 0 2200
aonexrcst 0 1700
dgdys 6 2330
hdnnara 4 2525
dnpaonntssotk 4 2555
ddmnwndrlnd 6 2500
C 4 2445
astrttnomch 0 2330
seknnqasr 1 2630
sftnt 4 2630
stnsgt 2 2605
drrnenmknmrmr 4 2610
hnzm 6 2713
yndmsoazzlsn 6 2658
mrahlcalv 4 2615
hshzrhkkrhs 1 900
ortchntsbshni 0 2430
kmnmzshrski 1 2530
sktdnc 4 1800
gykkybrkjhkirkhn 2 2459
trk 0 900
30zzsinhkntiik 3 2700
sngkotmmmirprdx 1 2600
yran 2 2529
tntissygntinybu 1 2614
skiichhtki 5 2505
tgrbnny 6 2558
dnbrsnki 3 1927
yugozxl 1 1930
frbllchrmn 1 1928
fjrg 1 1955
shwmngtr 0 2200
xmn 5 2200
rngnkkrskitikihn 0 2100
szysz 0 1254
prttyrythmaulrdrm 6 1000
sckiesfrntrqst 5 1820
mshdr 1 2255
1
mrahlcalv
0

Output for the Sample Input

1
4
31

Hint

Second dataset: He can watch program E after watching B. Then he can choose a program either I or J after watching H. Therefore he can watch maximum 4 programs.

Solution in Ruby (judge verdict: Accepted)

### subroutines

def bsearch(ranges, tm)
  i = 0
  j = ranges.length - 1

  while i <= j
    k = (i + j) / 2
    r0, r1 = ranges[k]
    if tm + 30 > r0 && tm < r1
      return k
    end

    if tm >= r1
      i = k + 1
    else
      j = k - 1
    end
  end

  nil
end

### main

loop do
  n = gets.to_i
  break if n == 0

  prgs = 7.times.map{[]}
  prghash = {}

  n.times do
    name, wd, st = gets.strip.split
    wdi = wd.to_i
    sti = st.to_i
    tm = (sti / 100 - 6) * 60 + (sti % 100)

    prghash[name] = [wdi, tm]
    prgs[wdi] << tm
  end
  #p prgs
  #p prghash

  for prg in prgs
    prg.sort!.uniq!
  end
  #p prgs

  pn = gets.to_i
  anchors = 7.times.map{[]}

  pn.times do
    wdi, sti = prghash[gets.strip]
    anchors[wdi] << sti
  end
  #p anchors

  count = 0

  for anc in anchors
    anc.sort!

    for i in (0...(anc.length - 1))
      if anc[i + 1] - anc[i] < 30
        count = -1
        break
      end
    end
    break if count == -1
  end

  if count == -1
    puts -1
    next
  end

  for i in (0..6)
    prg = prgs[i]
    anc = anchors[i]
    #p [prg, anc]

    ranges = [[0, 24 * 60]]
    for an in anc
      k = bsearch(ranges, an)
      if ! k.nil?
        r0, r1 = ranges[k]
        rs = []
        if r0 < an && an - r0 >= 30
          rs << [r0, an]
        end
        if an + 30 < r1 && r1 - an >= 60
          rs << [an + 30, r1]
        end
        ranges[k, 1] = rs
      end
    end
    count += anc.length

    for range in ranges
      r0, r1 = range

      for tm in prg
        break if tm + 30 > r1
        if tm >= r0
          #p [r0, r1, tm]
          count += 1
          r0 = tm + 30
        end
      end
    end
    #p ranges
  end

  puts count
end