#include <bits/stdc++.h>
using namespace std;
constexpr int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
constexpr int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline void dump(T& v) {
  for (auto i = v.begin(); i != v.end(); i++) {
    cout << *i << ((i == --v.end()) ? '\n' : ' ');
  }
}
inline string getline() {
  string s;
  getline(cin, s);
  return s;
}
inline void yn(const bool b) {
  b ? cout << ("yes") << '\n' : cout << ("no") << '\n';
}
inline void Yn(const bool b) {
  b ? cout << ("Yes") << '\n' : cout << ("No") << '\n';
}
inline void YN(const bool b) {
  b ? cout << ("YES") << '\n' : cout << ("NO") << '\n';
}
struct io {
  io() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
};
const int INF = INT_MAX;
constexpr long long int LLINF = 1LL << 60;
constexpr long long int MOD = 1000000007;
constexpr double EPS = 1e-9;
int main() {
  int Q;
  cin >> Q;
  while (Q--) {
    int n, m;
    cin >> n >> m;
    vector<string> v(n);
    for (int i = 0; i < (n); ++i) cin >> v[i];
    if (n == 1) {
      cout << (v[0]) << '\n';
      continue;
    }
    string ans = "";
    for (int i = 0; i < (m); ++i) {
      string t = v[0];
      for (int j = 0; j < (26); ++j) {
        char c = 'a' + j;
        t[i] = c;
        int out = 0;
        for (int k = 0; k < (n); ++k) {
          int diff = 0;
          for (int l = 0; l < (m); ++l) {
            diff += t[l] != v[k][l];
          }
          out += diff > 1;
        }
        if (out == 0) {
          ans = t;
          break;
        }
      }
      if (ans != "") break;
    }
    if (ans == "")
      cout << (-1) << '\n';
    else
      cout << (ans) << '\n';
  }
}
