#include <iostream>
#include <algorithm>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int main(){
int I_n, R_n;
while(cin >> I_n){
vector<string> I_seq(I_n,"");//I序列
for(int i = 0 ; i < I_n; i++){
cin >> I_seq[i];
}
cin >> R_n;
set<int> R_seq;
int s;
for(int i = 0; i < R_n; i++){//R序列set去重排序
cin >> s;
R_seq.insert(s);
}
int len = R_seq.size();
int sum = 0;//记录总的个数
vector<int> count(len,0);//记录分支个数
vector<vector<pair<int, string> > > seq(len,vector<pair<int, string> >(I_n,{0,""}));//一维对应R序列,二维储存索引与整数
int m = 0;
for(const auto & i : R_seq){
int n = 0;
for(int j = 0; j < I_n; j++){
if(I_seq[j].find(to_string(i)) != -1){//如果I整数中包含R整数
seq[m][n++] = {j,I_seq[j]};
count[m]++;
}
}
m++;
}
for(int i = 0; i < len; i++){
if(count[i] != 0)
sum += count[i] * 2 + 2;
}
cout << sum << ' ';
int k = 0;
for(auto i = R_seq.begin(); i != R_seq.end(); i++){
if(count[k] != 0){
cout << *i << ' ' << count[k]<< ' ';
for(int j = 0; j < count[k]; j++){
cout << seq[k][j].first << ' ' << seq[k][j].second << ' ';
}
}
k++;
}
}
}