#include <bits/stdc++.h>
using namespace std;
const int N = 50001, M = 500001;
int n, m, t, now, pos, node[N], son[M][37], p[M], sum[M], idx[M], flag[M];
char a[N][20], b[N][20];
queue<int> q;
inline int Index(char c) {
  if (c >= 'a' && c <= 'z')
    return c - 97;
  else if (c >= '0' && c <= '9')
    return 26 + c - 48;
  else
    return 36;
}
int Insert(char s[]) {
  int p = 1, t;
  for (int i = 0; s[i]; i++) {
    t = Index(s[i]);
    if (!son[p][t]) son[p][t] = ++pos;
    p = son[p][t];
  }
  return p;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%s", a[i]);
  pos = 1;
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) {
    scanf("%s", b[i]);
    node[i] = Insert(b[i]);
  }
  for (int i = 0; i < 37; i++) son[0][i] = 1;
  q.push(1);
  while (!q.empty()) {
    now = q.front();
    q.pop();
    for (int i = 0; i < 37; i++)
      if (son[now][i]) {
        p[son[now][i]] = son[p[now]][i];
        q.push(son[now][i]);
      } else
        son[now][i] = son[p[now]][i];
  }
  for (int i = 1; i <= n; i++) {
    t = 1;
    for (int j = 0; a[i][j]; j++) {
      t = son[t][Index(a[i][j])];
      for (int k = t; k > 1 && flag[k] != i; k = p[k]) {
        sum[k]++;
        idx[k] = i;
        flag[k] = i;
      }
    }
  }
  for (int i = 1; i <= m; i++)
    printf("%d %s\n", sum[node[i]], sum[node[i]] ? a[idx[node[i]]] : "-");
  return 0;
}
