#include <bits/stdc++.h>
using namespace std;
const int N = 8e5 + 5;
int n, m, sum, nxt[N][30];
int par[N], h[N], ans[N];
set<int> d[N];
inline void add(string s, int id) {
  int cur = 0;
  for (auto c : s) {
    if (!nxt[cur][c - 'a']) {
      par[nxt[cur][c - 'a'] = ++m] = cur;
      h[m] = h[cur] + 1;
    }
    cur = nxt[cur][c - 'a'];
  }
  d[cur].insert(id);
}
int main() {
  cin >> n;
  for (int i = 0; i < n + n; i++) {
    string s;
    cin >> s;
    add(s, i);
  }
  for (int i = m; ~i; i--) {
    while (!d[i].empty() && *d[i].begin() < n && *d[i].rbegin() >= n) {
      ans[*d[i].begin()] = *d[i].rbegin() - n, sum += h[i];
      d[i].erase(d[i].begin()), d[i].erase(*d[i].rbegin());
    }
    d[par[i]].insert(d[i].begin(), d[i].end());
  }
  cout << sum << endl;
  for (int i = 0; i < n; i++) cout << i + 1 << ' ' << ++ans[i] << endl;
}
