#include<iostream>
#include <set>
#include <vector>
using namespace std;
int main() {
int ICnt, RCnt;
string str;
cin >> ICnt;
vector<string> I;
for (int i = 0; i < ICnt; ++i) {
cin >> str;
I.push_back(str);
}
cin >> RCnt;
set<int> R;
for (int i = 0; i < RCnt; ++i) {
cin >> str;
R.insert(stoi(str));
}
vector<int> ans;
for (auto itR : R) {
int cnt = 0;
bool bisOk = false;
for (auto itI : I) {
if (itI.find(to_string(itR)) != string::npos) {
++cnt;
if (!bisOk) {
ans.push_back((itR));
bisOk = true;
}
}
}
if (cnt > 0) {
ans.push_back(cnt);
for (int i = 0; i < ICnt; ++i) {
if (I[i].find(to_string(itR)) != string::npos) {
ans.push_back(i);
ans.push_back(stoi(I[i]));
}
}
}
}
// 开始打印结果
cout << ans.size() << " ";
for(auto it : ans){
cout << it << " ";
}
return 0;
}