#include <bits/stdc++.h>
using namespace std;
const int N = 200100, M = 800200;
const int A = 26;
vector<int> idx[M];
int trie[M][A + 1], nexfree, par[M], n;
char s[M];
int sol[N], ans;
bool used[N];
inline void dfs(int v, int d = 0) {
  for (int i = 0; i < A; i++)
    if (trie[v][i] != -1) dfs(trie[v][i], d + 1);
  vector<int> vec[2];
  for (int j : idx[v]) {
    if (used[j]) continue;
    if (j <= n)
      vec[0].push_back(j);
    else
      vec[1].push_back(j);
  }
  int cnt = min((int)vec[0].size(), (int)vec[1].size());
  for (int i = 0; i < cnt; i++) {
    ans += d;
    int p = vec[0][i], q = vec[1][i];
    sol[p] = q;
    used[p] = used[q] = 1;
  }
}
inline void ins(int id) {
  int v = 0;
  idx[0].push_back(id);
  int l = strlen(s + 1);
  for (int j = 1; j <= l; j++) {
    if (trie[v][s[j] - 'a'] == -1) trie[v][s[j] - 'a'] = ++nexfree;
    v = trie[v][s[j] - 'a'];
    idx[v].push_back(id);
  }
}
int main() {
  memset(trie, -1, sizeof trie);
  scanf("%d", &n);
  for (int i = 1; i <= 2 * n; i++) {
    scanf("%s", s + 1);
    ins(i);
  }
  dfs(0);
  printf("%d\n", ans);
  for (int i = 1; i <= n; i++) printf("%d %d\n", i, sol[i] - n);
  return 0;
}
