#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 1e2;
const int maxs = 8e5 + 10;
struct node {
  int ch[27];
  int deep;
} e[maxs];
struct answ {
  int x, y;
} ans[maxn];
char str[maxs];
int size = 0, tot = 0, totlen = 0;
vector<int> s[maxs][2];
int getint() {
  char ch;
  int b = 1;
  for (ch = getchar(); ch > '9' || ch < '0'; ch = getchar())
    if (ch == '-') b = -1;
  int num = ch - '0';
  for (ch = getchar(); ch >= '0' && ch <= '9'; ch = getchar())
    num = num * 10 + ch - '0';
  return num * b;
}
void insert(int now, int len, int col, int bh) {
  e[now].deep = len - 1;
  if (!str[len]) {
    s[now][col].push_back(bh);
    return;
  }
  int wh = str[len] - 'a' + 1;
  if (e[now].ch[wh])
    insert(e[now].ch[wh], len + 1, col, bh);
  else {
    e[now].ch[wh] = ++size;
    insert(e[now].ch[wh], len + 1, col, bh);
  }
  str[len] = 0;
}
void dfs(int x) {
  int tt;
  for (int i = 1; i <= 26; i++) {
    int ch = e[x].ch[i];
    if (ch) {
      dfs(ch);
      tt = s[ch][0].size();
      for (int i = 0; i < tt; i++) s[x][0].push_back(s[ch][0][i]);
      tt = s[ch][1].size();
      for (int i = 0; i < tt; i++) s[x][1].push_back(s[ch][1][i]);
      s[ch][0].clear();
      s[ch][1].clear();
    }
  }
  int tn = s[x][0].size() - 1, tm = s[x][1].size() - 1;
  tt = min(tn, tm);
  for (int i = 0; i <= tt; i++) {
    totlen += e[x].deep;
    ans[++tot].x = s[x][0][tn];
    ans[tot].y = s[x][1][tm];
    s[x][0].pop_back();
    s[x][1].pop_back();
    tn--;
    tm--;
  }
}
int main() {
  int n = getint();
  for (int i = 1; i <= n; i++) {
    scanf("%s", str + 1);
    insert(0, 1, 0, i);
  }
  for (int i = 1; i <= n; i++) {
    scanf("%s", str + 1);
    insert(0, 1, 1, i);
  }
  dfs(0);
  printf("%d\n", totlen);
  for (int i = 1; i <= tot; i++) {
    printf("%d %d\n", ans[i].x, ans[i].y);
  }
  return 0;
}
