#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 100;
const int mod = 1e9 + 7;
const int inf = 1e9;
int n, e[2][maxn], h[maxn], g[26][maxn], t, x;
long long ans;
vector<int> a[2][maxn];
string s;
void upd(int id) {
  a[ans][id].push_back(x);
  if (h[id] == s.size()) return;
  if (g[s[h[id]] - 'a'][id] == 0) {
    g[s[h[id]] - 'a'][id] = t;
    h[t++] = h[id] + 1;
  }
  upd(g[s[h[id]] - 'a'][id]);
  return;
}
void solve(int id) {
  for (int i = 0; i < 26; i++) {
    if (g[i][id]) solve(g[i][id]);
  }
  int p1 = 0, p2 = 0;
  while (p1 != a[0][id].size() && p2 != a[1][id].size()) {
    if (e[0][a[0][id][p1]])
      p1++;
    else if (e[1][a[1][id][p2]])
      p2++;
    else {
      ans += h[id];
      e[0][a[0][id][p1]] = a[1][id][p2];
      e[1][a[1][id][p2]] = a[0][id][p1];
      p1++, p2++;
    }
  }
  return;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  t++;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s;
    x = i + 1;
    upd(0);
  }
  ans = 1;
  for (int i = 0; i < n; i++) {
    cin >> s;
    x = i + 1;
    upd(0);
  }
  ans = 0;
  solve(0);
  cout << ans << '\n';
  for (int i = 1; i <= n; i++) {
    cout << i << " " << e[0][i] << '\n';
  }
  return 0;
}
