#include <bits/stdc++.h>
using namespace std;
const int N = 800010;
struct node {
  int ch[26];
  vector<pair<int, int> > per;
} t[N];
char c[N];
int tot, n;
void add(int typ, int num) {
  int len = strlen(c);
  int now = 0;
  for (int i = 0; i < len; i++) {
    if (!t[now].ch[c[i] - 'a']) t[now].ch[c[i] - 'a'] = ++tot;
    now = t[now].ch[c[i] - 'a'];
  }
  t[now].per.push_back(make_pair(num, typ));
}
int q[2][N], l[2], r[2], ans[2][N], cnt, sum;
void dfs(int now, int dep) {
  int tl[2];
  tl[0] = l[0], tl[1] = l[1];
  l[0] = r[0] + 1;
  l[1] = r[1] + 1;
  for (int i = 0; i < 26; i++)
    if (t[now].ch[i]) dfs(t[now].ch[i], dep + 1);
  for (int i = 0; i < t[now].per.size(); i++) {
    int typ = t[now].per[i].second;
    q[typ][++r[typ]] = t[now].per[i].first;
  }
  while (l[0] <= r[0] && l[1] <= r[1]) {
    ans[0][++cnt] = q[0][r[0]--];
    ans[1][cnt] = q[1][r[1]--];
    sum += dep;
  }
  l[0] = tl[0];
  l[1] = tl[1];
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%s", c);
    add(0, i);
  }
  for (int i = 1; i <= n; i++) {
    scanf("%s", c);
    add(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;
}
