#include <bits/stdc++.h>
using namespace std;
long long t, n, m, u, v, q, r, ql, qr, k, l, s, w, h, c, z, d, y, x;
const int N = 8e5 + 500;
const long long mod = 1e9 + 7;
const long long INF = 1LL << 62LL;
const bool JUDGE = false;
string A[N], B[N], str;
int H;
int F[N];
struct node {
  vector<int> s[2];
  int c[26];
  node() { memset(c, -1, sizeof(c)); }
} trie[N];
inline void insert(string x, int idx, int sid) {
  int cur = 0;
  for (int i = 0; i < x.size(); ++i) {
    if (trie[cur].c[x[i] - 'a'] == -1) {
      trie[cur].c[x[i] - 'a'] = ++H;
    }
    cur = trie[cur].c[x[i] - 'a'];
  }
  trie[cur].s[idx].push_back(sid + 1);
}
long long sol;
void dfs(int cur, int common) {
  for (int i = 0; i < 26; ++i) {
    if (trie[cur].c[i] == -1) continue;
    dfs(trie[cur].c[i], common + 1);
    for (int j = 0; j < 2; ++j) {
      while (trie[trie[cur].c[i]].s[j].size() > 0) {
        trie[cur].s[j].push_back(trie[trie[cur].c[i]].s[j].back());
        trie[trie[cur].c[i]].s[j].pop_back();
      }
    }
  }
  while (trie[cur].s[0].size() > 0 && trie[cur].s[1].size() > 0) {
    F[trie[cur].s[0].back()] = trie[cur].s[1].back();
    trie[cur].s[0].pop_back();
    trie[cur].s[1].pop_back();
    sol += common;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  if (JUDGE) {
    freopen("angry.in", "r", stdin);
    freopen("angry.out", "w", stdout);
  }
  cin >> n;
  H = 0;
  for (int i = 0; i < n; ++i) {
    cin >> A[i];
    insert(A[i], 0, i);
  }
  for (int i = 0; i < n; ++i) {
    cin >> B[i];
    insert(B[i], 1, i);
  }
  sol = 0;
  dfs(0, 0);
  cout << sol << '\n';
  for (int i = 1; i <= n; ++i) cout << i << " " << F[i] << "\n";
  return 0;
}
