#include <bits/stdc++.h>
using namespace std;
const int N = 8e5 + 5;
struct trie {
  int a[26];
  vector<int> val[2];
} a[N];
int vis[N][2];
char ch[800005];
int cnt, n;
void insert(char ch[], int op, int k) {
  int u = 0;
  int len = strlen(ch + 1);
  a[u].val[op].push_back(k);
  for (int i = 1; i <= len; i++) {
    if (!a[u].a[ch[i] - 'a']) a[u].a[ch[i] - 'a'] = ++cnt;
    u = a[u].a[ch[i] - 'a'];
    a[u].val[op].push_back(k);
  }
}
int dfs(int k, int dep) {
  int ans = 0;
  for (int i = 0; i < 26; i++)
    if (a[k].a[i]) ans += dfs(a[k].a[i], dep + 1);
  vector<int> t[2];
  for (int i = 0; i <= 1; i++)
    for (int j = 0; j < a[k].val[i].size(); j++)
      if (!vis[a[k].val[i][j]][i]) t[i].push_back(a[k].val[i][j]);
  int tmp = min(t[0].size(), t[1].size());
  ans += tmp * dep;
  for (int i = 0; i < tmp; i++) {
    vis[t[0][i]][0] = t[1][i];
    vis[t[1][i]][1] = t[0][i];
  }
  return ans;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%s", ch + 1);
    insert(ch, 0, i);
  }
  for (int i = 1; i <= n; i++) {
    scanf("%s", ch + 1);
    insert(ch, 1, i);
  }
  printf("%d\n", dfs(0, 0));
  for (int i = 1; i <= n; i++) printf("%d %d\n", i, vis[i][0]);
  return 0;
}
