#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
void fre() {
  freopen("D:\\1.txt", "r", stdin);
  freopen("D:\\3.txt", "w", stdout);
}
map<string, int> cnt, pos, vis;
string s[N];
void sub(int p, string t) {
  for (int i = 0; i < t.size(); i++) {
    if (i >= 8) return;
    if (!((t[i] >= 'a' && t[i] <= 'z') || (t[i] >= '0' && t[i] <= '9') ||
          t[i] == '.'))
      return;
  }
  vis.clear();
  for (int i = 0; i < t.size(); i++)
    for (int j = 1; j <= t.size() - i; j++)
      if (vis[t.substr(i, j)] == 0) {
        cnt[t.substr(i, j)]++;
        pos[t.substr(i, j)] = p;
        vis[t.substr(i, j)] = 1;
      }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, q;
  string t;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    sub(i, s[i]);
  }
  cin >> q;
  while (q--) {
    cin >> t;
    cout << cnt[t] << " ";
    if (cnt[t] == 0)
      cout << "-" << endl;
    else
      cout << s[pos[t]] << endl;
  }
  return 0;
}
