#include <bits/stdc++.h>
using namespace std;
const string a1[3] = {"weapon", "armor", "orb"},
             a2[4] = {"gladiator", "sentry", "physician"};
struct ww {
  int a, b, id;
} b[1010], c[1010];
int i, j, k, n, m, t, S, sheng;
int zhi[1010], id[1010], in[1010], an[3], re[3], F[1010];
string s[1010], p[1010];
inline int get1() {
  string ss;
  cin >> ss;
  int i;
  for (i = 1; i <= n; i++)
    if (s[i] == ss) return i;
}
inline int get2() {
  int i;
  string s;
  cin >> s;
  for (i = 0; i <= 2; i++)
    if (a1[i] == s) return i;
}
inline int get3() {
  int i;
  string s;
  cin >> s;
  for (i = 0; i <= 2; i++)
    if (a2[i] == s) return i;
}
inline void Pan() {
  for (i = 0; i <= 2; i++) {
    int ma = -1, id;
    for (j = 1; j <= n; j++)
      if (b[j].id == i && zhi[j] > ma) ma = zhi[j], id = j;
    cout << s[id];
    printf(" %d", in[id]);
    for (j = 1; j <= m; j++)
      if (c[j].b == id) cout << " " << p[j];
    printf("\n");
  }
  exit(0);
}
inline bool cc1(const int &A, const int &B) { return c[A].a > c[B].a; }
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    cin >> s[i];
    int B = get2(), c[3], x;
    for (j = 0; j <= 2; j++) scanf("%d", &c[j]);
    scanf("%d", &x);
    b[i] = (ww){c[B], x, B};
    zhi[i] = c[B];
    S += x;
  }
  scanf("%d", &m);
  for (i = 1; i <= m; i++) {
    cin >> p[i];
    int A = get3(), x;
    scanf("%d", &x);
    int B = get1();
    if (A == b[B].id) zhi[B] += x;
    c[i] = (ww){x, B, A};
    id[i] = i;
    in[B]++;
  }
  if (S == m) Pan();
  sort(id + 1, id + m + 1, cc1);
  sheng = m;
  for (i = 0; i <= 2; i++) {
    int ma = -1, Id, ge;
    for (j = 1; j <= n; j++)
      if (b[j].id == i) {
        int s = b[j].a, Size = b[j].b;
        for (k = 1; k <= m; k++) {
          int A = id[k];
          if (c[A].id == i && Size) {
            Size--;
            s += c[A].a;
          }
        }
        if (s > ma) ma = s, Id = j, ge = b[j].b - Size;
      }
    an[i] = Id;
    re[i] = ge;
    {
      int Size = b[Id].b;
      for (k = 1; k <= m; k++) {
        int A = id[k];
        if (c[A].id == i && Size) {
          Size--;
          F[A] = i + 1;
          sheng--;
        }
      }
    }
  }
  for (i = 0; i <= 2; i++) {
    int Sh = min(b[an[i]].b - re[i], sheng);
    cout << s[an[i]];
    printf(" %d", re[i] + Sh);
    for (j = 1; j <= m; j++)
      if (!F[j] && Sh) {
        cout << " " << p[j];
        Sh--;
        sheng--;
        F[j] = -1;
      }
    for (j = 1; j <= m; j++)
      if (F[j] == i + 1) cout << " " << p[j];
    printf("\n");
  }
  return 0;
}
