#include <bits/stdc++.h>
using namespace std;
const int maxn = 100050;
const int maxnode = 800050;
const int sigma_size = 26;
int n;
int ans[maxn];
int res;
char cc[maxn * 10];
int stka[maxn], stkb[maxn];
int ta, tb;
struct Trie {
  vector<int> vv[maxnode][2];
  int ch[maxnode][sigma_size];
  int sz;
  Trie() {
    sz = 1;
    memset(ch[0], 0, sizeof ch[0]);
  }
  int idx(char c) { return c - 'a'; }
  void _insert(char s[], int ki, int id) {
    int u = 0, len = strlen(s);
    for (int i = 0; i < len; i++) {
      int c = idx(s[i]);
      if (!ch[u][c]) {
        memset(ch[sz], 0, sizeof ch[sz]);
        vv[sz][0].clear();
        vv[sz][1].clear();
        ch[u][c] = sz++;
      }
      u = ch[u][c];
    }
    vv[u][ki].push_back(id);
  }
} root;
void dfs(int p, int dd, int x, int y) {
  for (int i = 0; i < root.vv[p][0].size(); i++) stka[ta++] = root.vv[p][0][i];
  for (int i = 0; i < root.vv[p][1].size(); i++) stkb[tb++] = root.vv[p][1][i];
  for (int i = 0; i < 26; i++) {
    if (root.ch[p][i] != 0) dfs(root.ch[p][i], dd + 1, ta, tb);
  }
  while (ta > x && tb > y) {
    res += dd;
    int u = stka[--ta];
    int v = stkb[--tb];
    ans[u] = v;
  }
}
int main() {
  scanf("%d", &n);
  res = 0;
  for (int i = 0; i < n; i++) scanf("%s", cc), root._insert(cc, 0, i);
  for (int i = 0; i < n; i++) scanf("%s", cc), root._insert(cc, 1, i);
  ta = tb = 0;
  dfs(0, 0, ta, tb);
  printf("%d\n", res);
  for (int i = 0; i < n; i++) printf("%d %d\n", i + 1, ans[i] + 1);
  return 0;
}
