#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000006;
struct tnode {
  vector<int> ds;
  tnode *ke[26];
};
typedef tnode *pnode;
pnode r1, r2, nilT;
vector<pair<int, int> > kq;
bool ok1[MAXN], ok2[MAXN];
int N, M;
char S[MAXN];
long long res;
void create(pnode &p) {
  p = new (tnode);
  for (int i = 0; i < 26; i++) p->ke[i] = nilT;
}
void init() {
  nilT = new (tnode);
  create(r1);
  create(r2);
}
void docS() {
  M = 0;
  char c = getchar();
  while (c != '\n') {
    S[++M] = c;
    c = getchar();
  }
}
void build(int po, int vt, pnode p) {
  p->ds.push_back(po);
  if (vt > M) return;
  if (p->ke[S[vt] - 'a'] == nilT) create(p->ke[S[vt] - 'a']);
  build(po, vt + 1, p->ke[S[vt] - 'a']);
}
void nhap() {
  scanf("%d\n", &N);
  for (int i = 1; i <= N; i++) {
    docS();
    build(i, 1, r1);
  }
  for (int i = 1; i <= N; i++) {
    docS();
    build(i, 1, r2);
  }
}
bool noEmpty1(vector<int> &a) {
  while (!a.empty()) {
    int t = a.back();
    if (!ok1[t])
      a.pop_back();
    else
      break;
  }
  return (!a.empty());
}
bool noEmpty2(vector<int> &a) {
  while (!a.empty()) {
    int t = a.back();
    if (!ok2[t])
      a.pop_back();
    else
      break;
  }
  return (!a.empty());
}
void dfs(pnode p1, pnode p2, int l) {
  for (int j = 0; j < 26; j++) {
    if (noEmpty1(p1->ke[j]->ds) && noEmpty2(p2->ke[j]->ds))
      dfs(p1->ke[j], p2->ke[j], l + 1);
  }
  while (1) {
    int u = -1, v = -1;
    while (!p1->ds.empty()) {
      int t = p1->ds.back();
      if (ok1[t]) {
        u = t;
        break;
      } else
        p1->ds.pop_back();
    }
    if (u == -1) break;
    while (!p2->ds.empty()) {
      int t = p2->ds.back();
      if (ok2[t]) {
        v = t;
        break;
      } else
        p2->ds.pop_back();
    }
    if (v == -1) break;
    kq.push_back(make_pair(u, v));
    p1->ds.pop_back();
    p2->ds.pop_back();
    ok1[u] = 0;
    ok2[v] = 0;
    res += l;
  }
}
void tinh() {
  res = 0;
  memset(ok1, 1, sizeof(ok1));
  memset(ok2, 1, sizeof(ok2));
  dfs(r1, r2, 0);
  cout << res << endl;
  for (int i = 0; i < N; i++) printf("%d %d\n", kq[i].first, kq[i].second);
}
int main() {
  init();
  nhap();
  tinh();
  fclose(stdin);
  fclose(stdout);
  return 0;
}
