#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long MOD = 998244353;
inline long long add(long long x, long long y) {
  x += y;
  if (x >= mod) x -= mod;
  return x;
}
inline long long sub(long long x, long long y) {
  x -= y;
  if (x < 0) x += mod;
  return x;
}
inline long long mul(long long x, long long y) { return (x * 1ll * y) % mod; }
long long sol(vector<long long> a, vector<long long> b) {
  if (a.size() == 0 || b.size() == 0) return 0;
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  long long n = a.size();
  long long m = b.size();
  long long pre[n + 2];
  pre[n] = 0;
  for (long long i = n - 1; i >= 0; i--) {
    pre[i] = pre[i + 1];
    long long l = lower_bound(b.begin(), b.end(), a[i]) - b.begin();
    if (l < m && b[l] == a[i]) pre[i]++;
  }
  long long ans = 0;
  for (long long i = 0; i < m; i++) {
    long long k = upper_bound(a.begin(), a.end(), b[i]) - a.begin();
    long long curr = 0;
    if (k < n) curr += pre[k];
    if (k == 0) {
      ans = max(ans, curr);
      continue;
    }
    long long r = b[i], l = b[i] - k + 1;
    long long c1 = b.end() - lower_bound(b.begin(), b.end(), l);
    long long c2 = b.end() - upper_bound(b.begin(), b.end(), r);
    curr += c1 - c2;
    ans = max(ans, curr);
  }
  return ans;
}
signed main() {
  long long t;
  cin >> t;
  while (t--) {
    long long m, n;
    cin >> m >> n;
    vector<string> v;
    string s;
    for (long long i = 0; i < m; i++) {
      cin >> s;
      v.push_back(s);
    }
    bool ok = false;
    for (long long i = 0; i < n; i++) {
      for (long long z = 0; z < 26; z++) {
        string s = v[0];
        s[i] = 'a' + z;
        long long cnt = 0;
        for (long long j = 0; j < m; j++) {
          cnt = 0;
          for (long long d = 0; d < n; d++)
            if (s[d] != v[j][d]) cnt++;
          if (cnt > 1) break;
        }
        if (cnt <= 1) {
          cout << s, ok = true;
          break;
        }
      }
      if (ok) break;
    }
    if (!ok) cout << -1;
    cout << endl;
  }
}
