#include <bits/stdc++.h>
using namespace std;
char s[800000];
struct trie {
  trie *a[26], *p;
  vector<int> v;
} * T;
bool used[200000];
void init(trie *p) {
  for (int i = 0; i < 26; i++) p->a[i] = NULL;
}
void add(trie *p, int len, int k, int num) {
  if (k == len) {
    p = NULL;
    return;
  }
  if (p->a[s[k] - 'a'] == NULL) {
    p->a[s[k] - 'a'] = new trie;
    init(p->a[s[k] - 'a']);
  }
  trie *tmp;
  tmp = p->a[s[k] - 'a'];
  (tmp->v).push_back(num);
  add(tmp, len, k + 1, num);
}
int tot = 0, n;
vector<int> x, y;
vector<pair<int, int> > ans;
void dfs(trie *p, int d) {
  int i;
  for (i = 0; i < 26; i++)
    if (p->a[i] != NULL) dfs(p->a[i], d + 1);
  x.clear();
  y.clear();
  for (i = 0; i < p->v.size(); i++)
    if (!used[p->v[i]]) {
      if (p->v[i] < n)
        x.push_back(p->v[i]);
      else
        y.push_back(p->v[i]);
    }
  int m = min(x.size(), y.size());
  for (i = 0; i < m; i++) {
    ans.push_back(make_pair(x[i], y[i]));
    used[x[i]] = true;
    used[y[i]] = true;
    tot += d;
  }
}
int main() {
  int i, len;
  scanf("%d", &n);
  T = new trie;
  init(T);
  for (i = 0; i < n * 2; i++) {
    scanf("%s", s);
    len = strlen(s);
    add(T, len, 0, i);
    T->v.push_back(i);
  }
  memset(used, false, sizeof(used));
  dfs(T, 0);
  printf("%d\n", tot);
  for (i = 0; i < n; i++)
    printf("%d %d\n", ans[i].first + 1, ans[i].second - n + 1);
  return 0;
}
