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