#include <bits/stdc++.h>
using namespace std;
int ans[100005], n, Ans = 0;
struct Trie {
  vector<int> a[2];
  int Val;
  Trie *Son[26];
  Trie() {
    for (int i = 0; i < 26; i++) Son[i] = NULL;
  }
  void Up(string &p, int cur, int type, int pos) {
    Val = cur;
    if (cur == p.size()) {
      a[type].push_back(pos);
      return;
    }
    if (Son[p[cur] - 'a'] == NULL) Son[p[cur] - 'a'] = new Trie();
    Son[p[cur] - 'a']->Up(p, cur + 1, type, pos);
  }
  void Slove() {
    for (int i = 0; i < 26; i++)
      if (Son[i] != NULL) Son[i]->Slove();
    for (int i = 0; i < 26; i++)
      if (Son[i] != NULL) {
        for (int k = 0; k < 2; k++)
          for (int j = 0; j < Son[i]->a[k].size(); j++)
            a[k].push_back(Son[i]->a[k][j]);
      }
    while (a[1].size() && a[0].size()) {
      Ans += Val;
      ans[a[0].back()] = a[1].back();
      a[0].pop_back();
      a[1].pop_back();
    }
  }
};
Trie *Root;
int main() {
  ios_base::sync_with_stdio(0);
  string p;
  cin >> n;
  Root = new Trie();
  for (int j = 0; j <= 1; j++)
    for (int i = 1; i <= n; i++) {
      cin >> p;
      Root->Up(p, 0, j, i);
    }
  Root->Slove();
  cout << Ans << endl;
  for (int i = 1; i <= n; i++) cout << i << " " << ans[i] << endl;
}
