#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long multiply(long long a, long long b) {
  return ((a % mod) * (b % mod)) % mod;
}
long long add(long long a, long long b) {
  return ((a % mod) + (b % mod)) % mod;
}
long long sub(long long a, long long b) {
  return ((a % mod) - (b % mod) + mod) % mod;
}
void solve() {
  int n, m;
  cin >> n >> m;
  vector<string> a(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  int mask = (1 << n);
  int dp[m][n][mask];
  memset(dp, -1, sizeof dp);
  for (int i = 0; i < n; i++) {
    char x = a[i][0];
    int bs = 0;
    for (int j = 0; j < n; j++) {
      if (x != a[j][0]) {
        bs += (1 << j);
      }
    }
    dp[0][i][bs] = 1;
  }
  for (int j = 1; j < m; j++) {
    for (int i = 0; i < n; i++) {
      char x = a[i][j];
      int bs = 0;
      for (int k = 0; k < n; k++) {
        if (x != a[k][j]) {
          bs += (1 << k);
        }
      }
      for (int k = 0; k < n; k++) {
        for (int bit = 0; bit < mask; bit++) {
          if (dp[j - 1][k][bit] == 1 && (bit & bs) == 0) {
            int nbs = (bit | bs);
            if (nbs < mask) dp[j][i][nbs] = 1;
          }
        }
      }
    }
  }
  string ans = "";
  int start = -1;
  int sbs = -1;
  for (int j = 0; j < n; j++) {
    for (int bit = 0; bit < mask; bit++)
      if (dp[m - 1][j][bit] == 1) {
        start = j;
        sbs = bit;
        break;
      }
  }
  if (start == -1)
    cout << "-1\n";
  else {
    for (int j = m - 1; j >= 0; j--) {
      ans += a[start][j];
      if (j == 0) break;
      int bs = 0;
      for (int k = 0; k < n; k++) {
        if (a[start][j] != a[k][j]) bs += (1 << k);
      }
      for (int bit = 0; bit < mask; bit++) {
        bool flag = false;
        if ((bit | bs) == sbs) {
          for (int k = 0; k < n; k++) {
            if (dp[j - 1][k][bit] == 1) {
              start = k;
              sbs = bit;
              flag = true;
              break;
            }
          }
          if (flag) break;
        }
      }
    }
    reverse(ans.begin(), ans.end());
    cout << ans << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
}
