#include <bits/stdc++.h>
using namespace std;
int sz = 1600200;
long long answer = 0;
int go[800020][26];
int N = 1, n;
char xx[800020];
int dep[800020];
int ins(char *x) {
  int i, v = 1;
  for (i = 0; x[i]; i++) {
    if (!go[v][x[i] - 'a']) {
      go[v][x[i] - 'a'] = ++N;
      dep[N] = dep[v] + 1;
    }
    v = go[v][x[i] - 'a'];
  }
  return v;
}
int ans[100200];
vector<int> zzd[800800];
void pb(int x, int y, int d) {
  if (zzd[x].size() == 0 || (y > 0) == (zzd[x][0] > 0))
    zzd[x].push_back(y);
  else {
    if (y > 0)
      ans[y] = -zzd[x][0];
    else
      ans[zzd[x][0]] = -y;
    answer += d;
    zzd[x].erase(zzd[x].begin());
  }
}
void init() {
  int i, j;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%s", xx);
    j = ins(xx);
    pb(j, i, dep[j]);
  }
  for (i = 1; i <= n; i++) {
    scanf("%s", xx);
    j = ins(xx);
    pb(j, -i, dep[j]);
  }
}
int q[808080], inq[808080];
void bfs() {
  int i, j, k, l, r;
  q[l = r = 1] = 1;
  while (l <= r) {
    k = q[l++];
    for (j = 0; j < 26; j++)
      if (go[k][j]) q[++r] = go[k][j];
  }
  for (i = r; i; i--) {
    k = q[i];
    for (j = 0; j < 26; j++)
      if (go[k][j])
        for (l = zzd[go[k][j]].size() - 1; l >= 0; l--)
          pb(k, zzd[go[k][j]][l], dep[k]);
  }
}
int main() {
  init();
  bfs();
  printf("%I64d\n", answer);
  for (int i = 1; i <= n; i++) printf("%d %d\n", i, ans[i]);
  return 0;
}
