#include <bits/stdc++.h>
using namespace std;
int tope = 2;
int hijo[1000000][26];
int padre[1000000];
int cuantos[1000000];
int profundidad[1000000];
vector<int> lista[1000000];
int busca(int ind) {
  while (int(lista[ind].size()) == 0) {
    for (int i = 0; i < 26; i++) {
      if (hijo[ind][i]) {
        ind = hijo[ind][i];
        break;
      }
    }
  }
  return ind;
}
int coincide(vector<int> &v) {
  int ind = 1;
  for (int i = 0; i < int(v.size()); i++) {
    if (hijo[ind][v[i]] == 0) return ind;
    ind = hijo[ind][v[i]];
  }
  return ind;
}
int arregla(vector<int> &v, int ind) {
  while (cuantos[ind] == 0) ind = padre[ind];
  return ind;
}
int inserta(vector<int> &v, int elem) {
  int ind = 1;
  cuantos[ind]++;
  for (int i = 0; i < int(v.size()); i++) {
    if (hijo[ind][v[i]] == 0) hijo[ind][v[i]] = tope++;
    padre[hijo[ind][v[i]]] = ind;
    profundidad[hijo[ind][v[i]]] = profundidad[ind] + 1;
    ind = hijo[ind][v[i]];
    cuantos[ind]++;
  }
  lista[ind].push_back(elem);
  return ind;
}
void elimina(vector<int> &v, int ind) {
  lista[ind].pop_back();
  cuantos[ind]--;
  while (ind != 1) {
    if (cuantos[ind] == 0) {
      hijo[padre[ind]][v[profundidad[ind] - 1]] = 0;
    }
    ind = padre[ind];
    cuantos[ind]--;
  }
}
int n;
vector<int> v1[200000], v2[200000];
int posicion[2000000];
int coin[2000000];
char buff[2000000];
int sol[2000000];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%s", buff);
    int ibuff = 0;
    while (buff[ibuff]) {
      v1[i].push_back(buff[ibuff] - 'a');
      ibuff++;
    }
    posicion[i] = inserta(v1[i], i);
  }
  priority_queue<pair<int, int> > q;
  for (int i = 1; i <= n; i++) {
    scanf("%s", buff);
    int ibuff = 0;
    while (buff[ibuff]) {
      v2[i].push_back(buff[ibuff] - 'a');
      ibuff++;
    }
    coin[i] = coincide(v2[i]);
    q.push(pair<int, int>(profundidad[coin[i]], i));
  }
  int solucion = 0;
  while (not q.empty()) {
    pair<int, int> p = q.top();
    q.pop();
    int x = p.second;
    int c = arregla(v2[x], coin[x]);
    if (c != coin[x]) {
      coin[x] = c;
      q.push(pair<int, int>(profundidad[coin[x]], x));
    } else {
      solucion += profundidad[coin[x]];
      int ind = busca(coin[x]);
      sol[lista[ind].back()] = x;
      elimina(v1[lista[ind].back()], ind);
    }
  }
  cout << solucion << endl;
  for (int i = 1; i <= n; i++) cout << i << " " << sol[i] << endl;
}
