#include <bits/stdc++.h>
using namespace std;
const int N = 900000;
int n, sum = 0, node = 1, trie[N][26], used[N];
char buf[N];
vector<int> ids[N];
vector<pair<int, int> > ans;
void dfs(int x, int dep = 0) {
  for (int i = 0; i < 26; i++) {
    if (trie[x][i] != 0) {
      dfs(trie[x][i], dep + 1);
    }
  }
  vector<int> v0;
  vector<int> v1;
  for (int i = 0; i < ids[x].size(); i++) {
    int id = ids[x][i];
    if (used[id] == 1) {
      continue;
    }
    if (id < n) {
      v0.push_back(id);
    } else {
      v1.push_back(id);
    }
  }
  int tmp = min(v0.size(), v1.size());
  for (int i = 0; i < tmp; i++) {
    int a = v0[i];
    int b = v1[i];
    sum += dep;
    used[a] = 1;
    used[b] = 1;
    ans.push_back(make_pair(a, b));
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < 2 * n; i++) {
    scanf("%s", buf);
    int cur = 1;
    string s = buf;
    ids[cur].push_back(i);
    for (int j = 0; j < s.size(); j++) {
      int x = s[j] - 'a';
      if (trie[cur][x] == 0) {
        trie[cur][x] = ++node;
      }
      cur = trie[cur][x];
      ids[cur].push_back(i);
    }
  }
  dfs(1);
  printf("%d\n", sum);
  for (int i = 0; i < ans.size(); i++) {
    printf("%d %d\n", ans[i].first + 1, ans[i].second - n + 1);
  }
  return 0;
}
