#include <bits/stdc++.h>
using namespace std;
vector<int> V1[810000], V2[810000];
vector<int>::iterator it1, it2;
pair<int, int> P[120000];
bool b1[120000], b2[120000];
int tree[810000][26];
char c[810000];
int i, j, k, l, n, p, s, sum_node;
inline void buildtrie() {
  sum_node = 1;
  for (i = 1; i <= n; i++) {
    gets(c + 1);
    l = strlen(c + 1);
    p = 1;
    V1[p].push_back(i);
    for (j = 1; j <= l; j++) {
      if (!tree[p][c[j] - 97]) {
        sum_node++;
        tree[p][c[j] - 97] = sum_node;
      }
      p = tree[p][c[j] - 97];
      V1[p].push_back(i);
    }
  }
  for (i = 1; i <= n; i++) {
    gets(c + 1);
    l = strlen(c + 1);
    p = 1;
    V2[p].push_back(i);
    for (j = 1; j <= l; j++) {
      if (!tree[p][c[j] - 97]) {
        sum_node++;
        tree[p][c[j] - 97] = sum_node;
      }
      p = tree[p][c[j] - 97];
      V2[p].push_back(i);
    }
  }
  return;
}
inline void dfs(int x, int y) {
  for (int i = 0; i < 26; i++)
    if (tree[x][i]) dfs(tree[x][i], y + 1);
  for (it1 = V1[x].begin(), it2 = V2[x].begin();
       (it1 != V1[x].end()) && (it2 != V2[x].end());) {
    while ((it1 != V1[x].end()) && (b1[*it1])) it1++;
    while ((it2 != V2[x].end()) && (b2[*it2])) it2++;
    if ((it1 != V1[x].end()) && (it2 != V2[x].end()))
      k++, P[k] = make_pair(*it1, *it2), b1[*it1] = true, b2[*it2] = true,
           it1++, it2++, s = s + y;
  }
  return;
}
int main() {
  scanf("%d", &n);
  getchar();
  buildtrie();
  dfs(1, 0);
  printf("%d\n", s);
  for (i = 1; i <= n; i++) printf("%d %d\n", P[i].first, P[i].second);
  return 0;
}
