#include <bits/stdc++.h>
using namespace std;
inline long long qr() {
  long long x = 0, f = 1;
  char ch;
  while (!isdigit(ch = getchar()))
    if (ch == '-') f = -1;
  for (; isdigit(ch); x = x * 10 + ch - 48, ch = getchar())
    ;
  return x * f;
}
const int N = 800010;
int c[N][26];
vector<pair<int, int> > p[N];
char s[N];
int ct, n;
void ins(int k, int x) {
  int u = 0;
  for (int i = 0; s[i]; ++i) {
    int v = s[i] - 'a';
    if (!c[u][v]) c[u][v] = ++ct;
    u = c[u][v];
  }
  p[u].push_back(make_pair(x, k));
}
int q[2][N], l[2], r[2], ans[2][N], num, sum;
void dfs(int u, int D) {
  int x = l[0], y = l[1];
  l[0] = r[0] + 1;
  l[1] = r[1] + 1;
  int i;
  for (i = 0; i < 26; ++i)
    if (c[u][i]) dfs(c[u][i], D + 1);
  for (i = 0; i < p[u].size(); ++i) {
    int k = p[u][i].second;
    q[k][++r[k]] = p[u][i].first;
  }
  while (l[0] <= r[0] && l[1] <= r[1]) {
    ans[0][++num] = q[0][r[0]--];
    ans[1][num] = q[1][r[1]--];
    sum += D;
  }
  l[0] = x;
  l[1] = y;
}
int main() {
  n = qr();
  for (int i = 1; i <= n; ++i) scanf("%s", s), ins(0, i);
  for (int i = 1; i <= n; ++i) scanf("%s", s), ins(1, i);
  dfs(0, 0);
  printf("%d\n", sum);
  for (int i = 1; i <= n; ++i) printf("%d %d\n", ans[0][i], ans[1][i]);
  return 0;
}
