#include <bits/stdc++.h>
using namespace std;
const int N = 8e5 + 5;
int n, p = 1, as[100005][2], pa[N];
int ch[N][26];
set<int> cnt[N][2];
long long ans;
inline void erase(int p, int id, int x) {
  while (p) {
    cnt[p][id].erase(x);
    p = pa[p];
  }
  cnt[1][id].erase(x);
}
inline void insert(string a, int v, int id) {
  cnt[1][v].insert(id);
  int u = 1;
  for (int i = 0; i < a.size(); ++i) {
    int c = a[i] - 'a';
    if (!ch[u][c]) ch[u][c] = ++p, pa[p] = u;
    u = ch[u][c];
    cnt[u][v].insert(id);
  }
}
inline void dfs(int u, int dep) {
  for (int i = 0; i <= 25; ++i)
    if (ch[u][i]) dfs(ch[u][i], dep + 1);
  while (!cnt[u][0].empty() && !cnt[u][1].empty()) {
    ans += dep;
    int x = *cnt[u][0].begin(), y = *cnt[u][1].begin();
    as[++as[0][0]][0] = x;
    as[as[0][0]][1] = y;
    erase(u, 0, x);
    erase(u, 1, y);
  }
}
string a;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) cin >> a, insert(a, 0, i);
  for (int i = 1; i <= n; ++i) cin >> a, insert(a, 1, i);
  dfs(1, 0);
  cout << ans << endl;
  for (int i = 1; i <= as[0][0]; ++i)
    cout << as[i][0] << " " << as[i][1] << endl;
}
