/**
 * Author: Sergey Kopeliovich (Burunduk30@gmail.com)
 * Date: 2013.04.15
 *
 * Time: O(n * 2^k * log)
 * Memory usage: O(n) = 4 mb
 * AC: 0.734 seconds
 */

#include <ctime>
#include <cstdio>
#include <cassert>
#include <algorithm>

#define forn(i, n) for (int i = 0; i < (int)(n); i++)

const int maxN = 50000;
const int maxK = 6;

typedef unsigned short word;
typedef unsigned long long ll;

const int P = (int)1e9 + 9;

int n, k;
char s[maxN][maxK + 1]; // 350 K
char res[maxN]; // 50 K
word ri[maxN]; // 100 K
ll deg[maxK], hash[maxN]; // 400 K
int p[maxN]; // 200 K

inline bool hless( int i, int j )
{
  return hash[i] < hash[j];
}

void go( int i, int bn )
{
  if (i == k)
  {
    std::sort(p, p + n, hless);
    forn(i, n - 1)
      if (hash[p[i]] == hash[p[i + 1]])
      {
        int j = p[i], l = p[i + 1];
        if (res[l] > bn) res[l] = bn, ri[l] = j;
        if (res[j] > bn) res[j] = bn, ri[j] = l;
      }
    return;
  }
  go(i + 1, bn + 1);
  forn(j, n)
    hash[j] += deg[i] * s[j][i];
  go(i + 1, bn);
  forn(j, n)
    hash[j] -= deg[i] * s[j][i];
}

int main()
{
  #define NAME "similar"
  assert(freopen(NAME ".in", "r", stdin));
  assert(freopen(NAME ".out", "w", stdout));

  assert(scanf("%d%d " , &n, &k) == 2 && n <= maxN && k <= maxK);
  forn(i, n)
    assert(gets(s[i])), res[i] = k + 1;
  deg[0] = 1;
  forn(i, k - 1)
    deg[i + 1] = deg[i] * P;
  forn(i, n)
    p[i] = i;
  go(0, 0);
  forn(i, n)
    printf("%d %d\n", res[i], ri[i] + 1);
  fprintf(stderr, "time = %.2f\n", 1.0 * clock() / CLOCKS_PER_SEC);
  return 0;
}