#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:32000")
using namespace std;
const double pi = 3.1415926535897932384626433832795;
char ch[1 << 20];
inline string gs() {
  scanf("%s", ch);
  return string(ch);
}
inline string gl() {
  gets(ch);
  return string(ch);
}
inline int gi() {
  int x;
  scanf("%d", &x);
  return x;
}
int gcd(int a, int b) { return !a ? b : gcd(b % a, a); }
const int mod = 1000 * 1000 * 1000 + 9;
inline int MUL(int a, int b) { return 1LL * a * b % mod; }
inline int ADD(int a, int b) { return (a + b >= mod ? a + b - mod : a + b); }
map<string, pair<int, int> > A[3];
map<string, vector<string> > G;
map<string, int> D[3];
int main() {
  int n = gi();
  for (int i = (0), _b(n); i < _b; ++i) {
    string s = gs();
    string t = gs();
    int a, b, c, z;
    scanf("%d%d%d%d", &a, &b, &c, &z);
    if (t == "weapon") {
      A[0][s] = make_pair(a, z);
    } else if (t == "armor") {
      A[1][s] = make_pair(b, z);
    } else {
      A[2][s] = make_pair(c, z);
    }
  }
  int m = gi();
  for (int i = (0), _b(m); i < _b; ++i) {
    string s, t;
    s = gs();
    t = gs();
    int v = gi();
    if (t == "gladiator") {
      D[0][s] = v;
    } else if (t == "sentry") {
      D[1][s] = v;
    } else {
      D[2][s] = v;
    }
    string gh = gs();
    G[gh].push_back(s);
  }
  int ok = 0;
  for (int i = (0), _b(3); i < _b; ++i)
    for (map<string, pair<int, int> >::iterator it = A[i].begin();
         it != A[i].end(); ++it) {
      if (G[it->first].size() != it->second.second) ok = 1;
    }
  if (ok) {
    string used[3];
    vector<string> in[3];
    for (int t = (0), _b(3); t < _b; ++t) {
      vector<pair<int, string> > V;
      for (map<string, int>::iterator it = D[t].begin(); it != D[t].end();
           ++it) {
        V.push_back(make_pair(it->second, it->first));
      }
      sort(V.rbegin(), V.rend());
      string best = "";
      int bestv = -1;
      vector<string> put;
      for (map<string, pair<int, int> >::iterator it = A[t].begin();
           it != A[t].end(); ++it) {
        int ad = 0;
        for (int i = (0), _b(min(it->second.second, (int)V.size())); i < _b;
             ++i) {
          ad += V[i].first;
        }
        ad += it->second.first;
        if (ad > bestv) {
          bestv = ad;
          put.clear();
          for (int i = (0), _b(min(it->second.second, (int)V.size())); i < _b;
               ++i) {
            put.push_back(V[i].second);
          }
          best = it->first;
        }
      }
      used[t] = best;
      in[t] = put;
    }
    G.clear();
    set<string> all;
    for (int t = (0), _b(3); t < _b; ++t)
      for (map<string, int>::iterator it = D[t].begin(); it != D[t].end(); ++it)
        all.insert(it->first);
    for (int t = (0), _b(3); t < _b; ++t) {
      for (int i = (0), _b(in[t].size()); i < _b; ++i) {
        G[used[t]].push_back(in[t][i]);
        all.erase(in[t][i]);
      }
    }
    for (int t = (0), _b(3); t < _b; ++t)
      for (map<string, pair<int, int> >::iterator it = A[t].begin();
           it != A[t].end(); ++it) {
        while (all.size() && it->second.second > G[it->first].size()) {
          G[it->first].push_back(*all.begin());
          all.erase(all.begin());
        }
      }
  }
  for (int t = (0), _b(3); t < _b; ++t) {
    string bests = "";
    int bestv = -1;
    for (map<string, pair<int, int> >::iterator it = A[t].begin();
         it != A[t].end(); ++it) {
      string s = it->first;
      int v = it->second.first;
      for (int i = (0), _b(G[s].size()); i < _b; ++i) {
        if (D[t].count(G[s][i])) v += D[t][G[s][i]];
      }
      if (v > bestv) {
        bestv = v;
        bests = s;
      }
    }
    printf("%s %d", bests.c_str(), G[bests].size());
    for (int i = (0), _b(G[bests].size()); i < _b; ++i)
      printf(" %s", G[bests][i].c_str());
    printf("\n");
  }
  return 0;
}
