#include <bits/stdc++.h>
using namespace std;
int n;
map<string, int> cnt;
map<string, string> str;
int main() {
  cin >> n;
  string s;
  for (int i = 0; i < n; ++i) {
    cin >> s;
    set<string> var;
    for (int j = 0; j < s.size(); ++j)
      for (int k = j; k < s.size(); ++k) var.insert(s.substr(j, k - j + 1));
    for (string ss : var) {
      cnt[ss]++;
      str[ss] = s;
    }
  }
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> s;
    int c = cnt[s];
    cout << cnt[s] << " " << (c > 0 ? str[s] : "-") << endl;
  }
  return 0;
}
