#include <bits/stdc++.h>
using namespace std;
int N;
struct node {
  node* ch[26];
  vector<int> ind0, ind1;
  node() {
    for (int g = 0; g < 26; g++) ch[g] = NULL;
  }
};
node* root;
void insert(const string& x, int pos, node*& a, int t, int xx) {
  if (a == NULL) a = new node();
  if (pos == (int)x.length()) {
    if (t == 1)
      a->ind1.push_back(xx);
    else
      a->ind0.push_back(xx);
    return;
  }
  insert(x, pos + 1, a->ch[x[pos] - 'a'], t, xx);
}
long long answer = 0;
int match[100005];
pair<vector<int>, vector<int> > solve(node*& root, int cnt = 0) {
  vector<int> z = root->ind0, o = root->ind1;
  for (int t = 0; t < 26; t++) {
    if (root->ch[t] == NULL) continue;
    pair<vector<int>, vector<int> > ax = solve(root->ch[t], cnt + 1);
    if (ax.first.size()) {
      for (int g : ax.first) z.push_back(g);
    } else {
      for (int g : ax.second) o.push_back(g);
    }
  }
  while (z.size() && o.size()) {
    match[z.back()] = o.back();
    o.pop_back();
    z.pop_back();
    answer += cnt;
  }
  return pair<vector<int>, vector<int> >(z, o);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> N;
  for (int g = 1; g <= N; g++) {
    string x;
    cin >> x;
    insert(x, 0, root, 0, g);
  }
  for (int g = 1; g <= N; g++) {
    string x;
    cin >> x;
    insert(x, 0, root, 1, N + g);
  }
  solve(root);
  cout << answer << '\n';
  for (int g = 1; g <= N; g++) cout << g << ' ' << match[g] - N << '\n';
  return 0;
}
