#include <bits/stdc++.h>
using namespace std;
const int N = (int)(1e5 + 5);
const int M = (int)(8e5 + 5);
const int LOG = (int)(20);
struct trie {
  int v[26];
  vector<int> vec[2];
} tree[N * 10];
int n, sz, answ;
char s[M];
vector<pair<int, int> > ans;
void add(int in, int ty) {
  int ind = 0, len = strlen(s + 1);
  for (int i = 1; i <= len; i++) {
    int c = s[i] - 'a';
    if (!tree[ind].v[c]) tree[ind].v[c] = ++sz;
    ind = tree[ind].v[c];
  }
  tree[ind].vec[ty].push_back(in);
}
void dfs(int x, int back, int dep) {
  for (int i = 0; i < 26; i++)
    if (tree[x].v[i]) dfs(tree[x].v[i], x, dep + 1);
  while (tree[x].vec[0].size() && tree[x].vec[1].size()) {
    answ += dep;
    ans.push_back(make_pair(tree[x].vec[0].back(), tree[x].vec[1].back()));
    tree[x].vec[0].pop_back();
    tree[x].vec[1].pop_back();
  }
  if (x == 0) return;
  while (tree[x].vec[0].size()) {
    int g = tree[x].vec[0].back();
    tree[x].vec[0].pop_back();
    tree[back].vec[0].push_back(g);
  }
  while (tree[x].vec[1].size()) {
    int g = tree[x].vec[1].back();
    tree[x].vec[1].pop_back();
    tree[back].vec[1].push_back(g);
  }
}
int main() {
  scanf("%d ", &n);
  for (int i = 1; i <= n; i++) {
    scanf(" %s", s + 1);
    add(i, 0);
  }
  for (int i = 1; i <= n; i++) {
    scanf(" %s", s + 1);
    add(i, 1);
  }
  dfs(0, -1, 0);
  printf("%d\n", answ);
  for (auto i : ans) printf("%d %d\n", i.first, i.second);
}
