#include <bits/stdc++.h>
using namespace std;
int dx4[] = {-1, 0, 1, 0};
int dy4[] = {0, 1, 0, -1};
int dx8[] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dy8[] = {0, 1, 1, 1, 0, -1, -1, -1};
void Duxar(int _this_line) {}
bool AreEqual(double a, double b) { return (fabs(a - b) < 1e-10); }
template <class T>
bool GetNr(T& _value) {
  T _sign = 1;
  char ch;
  _value = 0;
  while (!isdigit(ch = getchar())) {
    if (ch == -1) {
      return false;
    }
    ch == '-' ? _sign = -1 : _sign = 1;
  }
  do {
    _value = _value * 10 + (ch - '0');
  } while (isdigit(ch = getchar()));
  _value *= _sign;
  return true;
}
class Trie {
 public:
  vector<Trie*> child;
  list<int> names, pseudos;
  Trie* from;
  Trie(Trie* from = 0) {
    this->child.resize(26, 0);
    this->from = from;
  }
};
int N, id, where, total;
Trie* root;
string word;
vector<pair<int, int> > ans;
void InsertTrie(Trie* root, int pos) {
  if (pos == word.length()) {
    if (where) {
      root->pseudos.push_back(id);
    } else {
      root->names.push_back(id);
    }
    return;
  }
  int to = word[pos] - 'a';
  if (!root->child[to]) {
    root->child[to] = new Trie(root);
  }
  InsertTrie(root->child[to], ++pos);
}
void Solve(Trie* root, int level) {
  int i;
  for (i = 0; i < 26; ++i) {
    if (root->child[i]) {
      Solve(root->child[i], level + 1);
    }
  }
  Trie* from = root->from;
  while (root->names.size() && root->pseudos.size()) {
    ans.push_back(make_pair(root->names.front(), root->pseudos.front()));
    total += level;
    root->names.pop_front();
    root->pseudos.pop_front();
  }
  for (; root->names.size();
       from->names.push_back(root->names.front()), root->names.pop_front())
    ;
  for (; root->pseudos.size(); from->pseudos.push_back(root->pseudos.front()),
                               root->pseudos.pop_front())
    ;
}
int main() {
  string fileInput = "sum";
  int i;
  root = new Trie();
  cin >> N;
  where = 0;
  for (i = 0; i < N; ++i) {
    cin >> word;
    id = i + 1;
    InsertTrie(root, 0);
  }
  where = 1;
  for (i = 0; i < N; ++i) {
    cin >> word;
    id = i + 1;
    InsertTrie(root, 0);
  }
  Solve(root, 0);
  printf("%d\n", total);
  for (i = 0; i < N; ++i) {
    printf("%d %d\n", ans[i].first, ans[i].second);
  }
  return 0;
}
