#include <bits/stdc++.h>
using namespace std;
int n, d;
string s;
string b[700000];
long long pw[700000];
long long hash_s[700000];
int f[700000];
struct edge {
  long long x;
  int cnt;
  int pos;
} a[700000];
void build_hash() {
  pw[0] = 1;
  for (int i = 1; i < s.length(); ++i) pw[i] = pw[i - 1] * 38;
  for (int i = 1; i < s.length(); ++i) {
    bool bit = 0;
    for (int j = 0; j <= 9; ++j)
      if (s[i] - 48 == j) {
        hash_s[i] = hash_s[i - 1] * 38 + j + 1;
        bit = 1;
        break;
      }
    if (bit) continue;
    if (s[i] == '.')
      hash_s[i] = hash_s[i - 1] * 38 + 37;
    else
      hash_s[i] = hash_s[i - 1] * 38 + (s[i] - 'a' + 1 + 10);
  }
}
void chiacat(int cn) {
  for (int i = 1; i < s.length(); ++i)
    for (int j = i; j < s.length(); ++j) {
      long long ta = hash_s[j] - hash_s[i - 1] * pw[j - i + 1];
      a[++d] = {ta, 1, cn};
    }
}
bool ss(const edge &a, const edge &b) {
  if (a.x != b.x)
    return a.x < b.x;
  else
    return a.pos < b.pos;
}
void input() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> s;
    b[i] = s;
    s = ' ' + s;
    build_hash();
    chiacat(i);
  }
  sort(a + 1, a + 1 + d, ss);
  int j = 1;
  f[a[1].pos] = 1;
  for (int i = 2; i <= d; ++i) {
    if (a[j].x != a[i].x) {
      a[++j].x = a[i].x;
      a[j].cnt = a[i].cnt;
      a[j].pos = a[i].pos;
      f[a[j].pos] = j;
    } else {
      if (f[a[i].pos] != j) {
        ++a[j].cnt;
        f[a[i].pos] = j;
      }
    }
  }
  d = j;
}
int binary(long long t, int l, int r) {
  int i = l, j = r, mid;
  while (i <= j) {
    mid = (i + j) / 2;
    if (a[mid].x == t) return mid;
    if (a[mid].x > t)
      j = mid - 1;
    else
      i = mid + 1;
  }
  return -1;
}
void input2() {
  int m;
  cin >> m;
  for (int i = 1; i <= m; ++i) {
    cin >> s;
    s = ' ' + s;
    build_hash();
    int ta = binary(hash_s[s.length() - 1], 1, d);
    if (ta == -1)
      cout << 0 << " -\n";
    else
      cout << a[ta].cnt << " " << b[a[ta].pos] << "\n";
  }
}
int main() {
  input();
  input2();
  return 0;
}
