#include <bits/stdc++.h>
using namespace std;
int const maxn = 800005;
int const maxc = 'z' - 'a' + 1;
int tree[maxn][maxc];
int size_tree = 1;
vector<int> words_pos[maxn];
vector<int> words_neg[maxn];
inline void add(string const& s, int num) {
  int n = s.length();
  int cur = 0;
  for (int i = 0; i < n; ++i) {
    if (tree[cur][s[i] - 'a'] == 0) tree[cur][s[i] - 'a'] = size_tree++;
    cur = tree[cur][s[i] - 'a'];
  }
  if (num > 0)
    words_pos[cur].push_back(num);
  else
    words_neg[cur].push_back(-num);
}
string words_string[maxn];
int lcp(int x, int y) {
  int n = min(words_string[x].length(), words_string[y].length());
  int i = 0;
  for (; i < n && words_string[x][i] == words_string[y][i]; ++i)
    ;
  return i;
}
int ans_number = 0;
vector<pair<int, int> > ans;
int n;
inline void add_in_ans(int x, int y) {
  ans.push_back(make_pair(x, y));
  ans_number += lcp(x, y + n);
}
void dfs(int cur, int prev = -1) {
  for (int i = 0; i < maxc; ++i)
    if (tree[cur][i] != 0) dfs(tree[cur][i], cur);
  while (!(words_pos[cur].empty() || words_neg[cur].empty())) {
    add_in_ans(words_pos[cur].back(), words_neg[cur].back());
    words_pos[cur].pop_back();
    words_neg[cur].pop_back();
  }
  while (!words_pos[cur].empty()) {
    words_pos[prev].push_back(words_pos[cur].back());
    words_pos[cur].pop_back();
  }
  while (!words_neg[cur].empty()) {
    words_neg[prev].push_back(words_neg[cur].back());
    words_neg[cur].pop_back();
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> words_string[i];
    add(words_string[i], i);
  }
  for (int i = 1; i <= n; ++i) {
    cin >> words_string[i + n];
    add(words_string[i + n], -i);
  }
  dfs(0);
  cout << ans_number << endl;
  for (int i = 0; i < ans.size(); ++i)
    cout << ans[i].first << ' ' << ans[i].second << '\n';
}
