#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const long long K = 2;
long long n, m;
string s[12];
vector<long long> v;
set<char> ss, we[13];
string re = "";
bool help() {
  bool ch = true;
  for (long long i = 0; i < n; i++) {
    long long cc = 0;
    for (long long j = 0; j < m; j++) {
      if (s[i][j] != re[j]) {
        cc++;
      }
    }
    if (cc > 1) return false;
  }
  return true;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    v.clear();
    ss.clear();
    re.clear();
    for (long long i = 0; i < 12; i++) we[i].clear();
    cin >> n >> m;
    for (long long i = 0; i < n; i++) cin >> s[i];
    bool ch = true;
    for (long long i = 0; i < m; i++) {
      ch = true;
      set<char> tt;
      for (long long j = 0; j < n; j++) {
        tt.insert(s[j][i]);
      }
      for (auto ii : tt) {
        we[i].insert(ii);
      }
      if (tt.size() > 1) v.push_back(i);
    }
    if (v.size() <= 1) {
      cout << s[0] << "\n";
    } else {
      bool ch = false;
      for (long long i = 0; i < m; i++) {
        for (auto ii : we[i]) {
          re = s[0];
          re[i] = ii;
          ch = help();
          if (ch) break;
        }
        if (ch) break;
      }
      if (ch)
        cout << re;
      else
        cout << -1;
      cout << "\n";
    }
  }
  return 0;
}
