#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-ffloat-store")
using namespace std;
long long int power_(long long int a, long long int b, long long int mod) {
  long long int p = mod;
  if (!b) return 1;
  long long int ans = power_(a, b / 2, mod);
  ans = (ans * ans) % p;
  if (b % 2) ans = (ans * a) % p;
  return ans;
}
bool fun_(vector<string> &temp, vector<char> &s) {
  int n = temp.size();
  string s1 = "";
  for (int i = 0; i < (s.size()); ++i) s1 += s[i];
  for (int j = 0; j < n; j++) {
    if (temp[j] != s1) {
      int a = 0;
      for (int i = 0; i < s.size(); i++) {
        if (s[i] == temp[j][i]) a++;
      }
      if (a != (s.size() - 1)) return false;
    }
  }
  return true;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    vector<string> temp;
    string s;
    cin >> n >> m;
    for (int i = 0; i < (n); ++i) {
      cin >> s;
      temp.push_back(s);
    }
    if (n == 1) {
      int r = s[m - 1] - 'a';
      r = (r + 1) % 26;
      s[m - 1] = 'a' + r;
      cout << s << endl;
      continue;
    }
    vector<char> ans(m), ans1(m);
    int a = 0;
    sort(temp.begin(), temp.end());
    bool flag = false;
    for (int j = 0; j < n && flag == false; j++) {
      for (int k = j + 1; k < n && flag == false; k++) {
        a = 0;
        for (int i = 0; i < m; i++) {
          if (temp[j][i] == temp[k][i]) {
            ans[i] = temp[j][i];
            ans1[i] = temp[j][i];
          } else {
            a++;
            if (a == 1) {
              ans[i] = temp[j][i];
              ans1[i] = temp[k][i];
            } else {
              ans[i] = temp[k][i];
              ans1[i] = temp[j][i];
            }
          }
        }
        if (fun_(temp, ans)) {
          for (int i = 0; i < (m); ++i) cout << ans[i];
          cout << endl;
          flag = true;
        } else if (fun_(temp, ans1)) {
          for (int i = 0; i < (m); ++i) cout << ans1[i];
          cout << endl;
          flag = true;
        }
      }
    }
    if (!flag) cout << -1 << endl;
  }
}
