{$A+,B-,D+,E+,F-,G-,I+,L+,N-,O-,P-,Q-,R-,S-,T-,V+,X+,Y+}
{$M 60000,0,655360}
{ Solution made by Vaidas GASIúNAS. Panevóýio J.Balßikonio gimnazija, Lithuania }


program Shop;
   uses Dos;
   type bytemas = array [1..5] of byte;
        wordmas = array [1..5] of word;
    var f : text;
        ch : array [1..104] of word;
        pr : array [1..104] of word;
        minpr : array [0..7775] of word;
        needed : bytemas;
        s : byte;
        t : word;

    function number ( m : bytemas) : word;
         var n : word;
             i : byte;
    begin
       n := 0;
       for i := 5 downto 1 do
          n := n * (needed[i]+1) + m[i];
       number := n
    end;

    procedure FixTime;
          var h, m, s, s100 : word;
    begin
       Gettime (h,m,s,s100);
       writeln ('time - ',h, ':', m, ':', s, '.', s100)
    end;

    procedure Input;
       var f : text;
           code : word;
           r : bytemas;
           codes : wordmas;
           i, j, i1, n, count : byte;
           b : integer;
           ok : boolean;
    begin
       Assign (f, 'INPUT.TXT');
       Reset (f);
       read (f, b);
       for i := 1 to 5 do needed[i] := 0;
       for i := 1 to b do
          begin
             read (f,codes[i],needed[i],pr[i]);
             for j := 1 to 5 do
                r [j] := 0;
             r[i] := 1;
             ch[i] := number(r);
          end;
       Close (f);
       Assign (f, 'OFFER.TXT');
       Reset(f);
       read (f, s);
       inc (s, b);
       for i := 1 to s-b do
          begin
             read (f, n);
             for i1 := 1 to 5 do r[i1] := 0;
             for j := 1 to n do
                begin
                   read (f, code, count);
                   ok := true;
                   for i1 := 1 to 5 do
                      if codes[i1] = code
                         then if count <= needed[i1]
                           then r[i1] := count
                           else ok := False
                end;
             if ok then ch [b+i] := number(r)
                   else dec (b);
             read (f, pr [i+b])
          end;
       Close (f);
       t := number(needed)
    end;

{****************************************************************************}

    function Posible_Addition (a, b : word) : boolean;
         var i : byte;
    begin
       for i := 1 to 5 do
          if a mod (needed[i]+1) + b mod (needed[i]+1) > needed[i]
             then begin
                Posible_Addition := False;
                exit
             end
             else begin
                a := a div (needed[i]+1);
                b := b div (needed[i]+1)
             end;
       Posible_Addition := True;
    end;

    procedure Deiskra;
          var min, i, ep : word;
              eil, pos : array [0..7775] of word;

       procedure move (it : word);
             var p, m, prc : word;
       begin
          p := pos [it];
          if p > ep
             then begin
               eil[p] := eil[ep+1];
               pos[eil[p]] := p;
               p := ep+1;
               inc (ep)
             end;
          dec(p);
          prc := minpr [it];
          while prc < minpr[eil[p]] do
             begin
                eil[p+1] := eil[p];
                pos[eil[p]] := p+1;
                dec (p);
             end;
          eil[p+1] := it;
          pos[it] := p+1
       end;

    begin
       for i := 1 to t do minpr[i] := 26000;
       for i := 0 to t do begin eil[i] := i; pos[i] := i end;
       minpr [0] := 0;
       min := 0;
       ep := 0;
       while min <= ep do
          begin
             for i := 1 to s do
                if Posible_Addition (eil[min],ch[i])
                  then if minpr[eil[min]] + pr[i]
                          < minpr[eil[min]+ch[i]]
                    then begin
                       minpr[eil[min]+ch[i]] := minpr[eil[min]] + pr[i];
                       move (eil[min]+ch[i]);
                    end;
             inc (min);
          end
    end;

begin
   Input;
   fixtime;
   Deiskra;
   fixtime;
   Assign (f, 'OUTPUT.TXT');
   Rewrite (f);
   writeln (f, minpr[t]);
   Close (f)
end.