#include <bits/stdc++.h>
using namespace std;
char x[800006];
struct trie {
  vector<int> pass;
  int child[26];
  trie() {
    for (int i = 0; i < 26; i++) child[i] = -1;
    pass.clear();
  }
} arr[800006];
int tot, res, n, xx[100006], yy[100006], l;
bool used[200006];
void add(int idx, int p = 0, int i = 0) {
  arr[p].pass.push_back(idx);
  if (i >= l) return;
  if (-1 == arr[p].child[x[i] - 'a']) arr[p].child[x[i] - 'a'] = ++tot;
  add(idx, arr[p].child[x[i] - 'a'], i + 1);
}
void dfs(int p = 0, int d = 0) {
  for (int i = 0; i < 26; i++) {
    if (-1 != arr[p].child[i]) dfs(arr[p].child[i], d + 1);
  }
  vector<int> black, red;
  for (int i = 0; i < arr[p].pass.size(); i++) {
    if (used[arr[p].pass[i]]) continue;
    if (arr[p].pass[i] < n)
      black.push_back(arr[p].pass[i]);
    else
      red.push_back(arr[p].pass[i]);
  }
  for (int i = 0; i < min(black.size(), red.size()); i++) {
    xx[tot] = black[i];
    yy[tot++] = red[i];
    used[black[i]] = true;
    used[red[i]] = true;
    res += d;
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < 2 * n; i++) {
    scanf("%s", x);
    l = strlen(x);
    add(i);
  }
  tot = 0;
  dfs();
  printf("%d\n", res);
  for (int i = 0; i < n; i++) printf("%d %d\n", xx[i] + 1, yy[i] - n + 1);
  return 0;
}
