#include <bits/stdc++.h>
using namespace std;
struct Item {
  char name[10 + 1];
  int typ, val, now;
  int size, left;
};
struct Resident {
  char name[10 + 1], home[10 + 1];
  int typ, bonus;
  friend bool operator<(const Resident &a, const Resident &b) {
    return a.bonus > b.bonus;
  }
};
vector<Item> item[3];
vector<Resident> resident[3];
const char classes[][7] = {"weapon", "armor", "orb"};
inline int find_class(char *name) {
  for (int i = 0; i < 3; ++i)
    if (strcmp(classes[i], name) == 0) return i;
  return 0;
}
const char residents[][10] = {"gladiator", "sentry", "physician"};
inline int find_resident(char *name) {
  for (int i = 0; i < 3; ++i)
    if (strcmp(residents[i], name) == 0) return i;
  return 0;
}
int cnt = 0;
inline void org_resident(char *name, const Resident &p) {
  for (int i = 0; i < 3; ++i) {
    for (size_t j = 0; j < item[i].size(); ++j) {
      if (strcmp(name, item[i][j].name) == 0) {
        if (i == p.typ) item[i][j].now += p.bonus;
        if (--item[i][j].left == 0) ++cnt;
      }
    }
  }
}
bool readin() {
  char tmp[10 + 1];
  int val[3];
  int n, m;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    Item p;
    scanf("%s%s%d%d%d%d", p.name, tmp, val, val + 1, val + 2, &p.size);
    p.left = p.size;
    p.typ = find_class(tmp);
    p.val = p.now = val[p.typ];
    item[p.typ].push_back(p);
  }
  scanf("%d", &m);
  for (int i = 0; i < m; ++i) {
    Resident p;
    scanf("%s%s%d%s", p.name, tmp, &p.bonus, p.home);
    p.typ = find_resident(tmp);
    org_resident(p.home, p);
    resident[p.typ].push_back(p);
  }
  for (int i = 0; i < 3; ++i) sort(resident[i].begin(), resident[i].end());
  return n == cnt;
}
vector<int> rem;
void putitem() {
  int ans[3];
  for (int i = 0; i < 3; ++i) {
    for (size_t j = 0; j < resident[i].size(); ++j) resident[i][j].home[0] = 0;
  }
  for (int i = 0; i < 3; ++i) {
    int tmp = -1;
    for (size_t j = 0; j < item[i].size(); ++j) {
      item[i][j].now = item[i][j].val;
      for (int k = 0; k < min(item[i][j].size, (int)resident[i].size()); ++k) {
        item[i][j].now += resident[i][k].bonus;
      }
      if (item[i][j].now > tmp) {
        tmp = item[i][j].now;
        ans[i] = j;
      }
    }
  }
  for (int i = 0; i < 3; ++i) {
    item[i][ans[i]].left = item[i][ans[i]].size;
    for (size_t j = 0; j < resident[i].size(); ++j) {
      if (item[i][ans[i]].left > 0) {
        strcpy(resident[i][j].home, item[i][ans[i]].name);
        --item[i][ans[i]].left;
      } else {
        rem.push_back(i * 1000 + j);
      }
    }
  }
  for (int i = 0; i < 3; ++i) {
    while (item[i][ans[i]].left > 0 && !rem.empty()) {
      int val = rem.back();
      rem.pop_back();
      strcpy(resident[val / 1000][val % 1000].home, item[i][ans[i]].name);
      --item[i][ans[i]].left;
    }
  }
}
void output() {
  for (int i = 0; i < 3; ++i) {
    int tmp = -1, id = 0;
    for (size_t j = 0; j < item[i].size(); ++j) {
      if (item[i][j].now > tmp) {
        tmp = item[i][j].now;
        id = j;
      }
    }
    printf("%s %d", item[i][id].name, item[i][id].size - item[i][id].left);
    for (int j = 0; j < 3; ++j) {
      for (size_t k = 0; k < resident[j].size(); ++k) {
        if (strcmp(resident[j][k].home, item[i][id].name) == 0) {
          printf(" %s", resident[j][k].name);
        }
      }
    }
    puts("");
  }
}
int main() {
  if (!readin()) {
    putitem();
  }
  output();
  return 0;
}
