#include <bits/stdc++.h>
using namespace std;
const int M = 800010;
int n, sum, ans[2][M], tot, q[2][M], l[2], r[2], tmp, cnt;
char c[M];
struct node {
  int ch[26];
  vector<pair<int, int> > vt;
} t[M];
void add(int pos, int num) {
  int len = strlen(c), 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].vt.push_back(make_pair(num, pos));
}
void dfs(int pos, int fa) {
  int tl[2] = {l[0], l[1]};
  l[0] = r[0] + 1, l[1] = r[1] + 1;
  for (int i = 0; i < 26; i++)
    if (t[pos].ch[i]) dfs(t[pos].ch[i], fa + 1);
  for (int i = 0; i < t[pos].vt.size(); i++) {
    int typ = t[pos].vt[i].second;
    q[typ][++r[typ]] = t[pos].vt[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 += fa;
  }
  l[0] = tl[0], l[1] = tl[1];
}
int main() {
  cin >> 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);
  cout << sum << endl;
  for (int i = 1; i <= n; i++) cout << ans[0][i] << " " << ans[1][i] << endl;
}
