#include <bits/stdc++.h>
using namespace std;
int tr[800005][26], tot, match[100005];
vector<int> s[800005][2];
void ins(char *a, int id) {
  int p = 0, n = strlen(a + 1);
  for (int i = 1; i <= n; ++i) {
    int cur = a[i] - 'a';
    if (!tr[p][cur]) tr[p][cur] = ++tot;
    p = tr[p][cur];
  }
  if (id > 0)
    s[p][0].push_back(id);
  else
    s[p][1].push_back(-id);
}
int fuck = 0;
void merge(vector<int> &x, vector<int> &y) {
  if (x.size() < y.size()) swap(x, y);
  for (int it : y) x.push_back(it), ++fuck;
  y.clear();
}
long long ans;
void solve(int p, int c) {
  for (int i = 0; i < 26; ++i)
    if (tr[p][i]) {
      solve(tr[p][i], c + 1);
      merge(s[p][0], s[tr[p][i]][0]), merge(s[p][1], s[tr[p][i]][1]);
    }
  while (!s[p][0].empty() && !s[p][1].empty()) {
    ans += c;
    match[s[p][0].back()] = s[p][1].back();
    s[p][0].pop_back(), s[p][1].pop_back();
  }
}
char a[800005];
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> (a + 1), ins(a, i);
  for (int i = 1; i <= n; ++i) cin >> (a + 1), ins(a, -i);
  solve(0, 0);
  cout << ans << "\n";
  for (int i = 1; i <= n; ++i) cout << i << " " << match[i] << "\n";
}
