#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Vi = vector<int>;
using Vl = vector<ll>;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
constexpr int I_INF = numeric_limits<int>::max();
constexpr ll L_INF = numeric_limits<ll>::max();
using bs = bitset<10>;
ll N, M;
vector<char> ans;
vector<string> S;
void dfs(int idx, bs used) {
  if (idx == M) {
    return;
  }
  unordered_set<char> st;
  for (ll i = 0; i < N; i++) {
    st.emplace(S[i][idx]);
  }
  for (const auto& c : st) {
    if (ans[M - 1] != 'A') {
      break;
    }
    bool ok = true;
    bs nused = used;
    for (ll i = 0; i < N; i++) {
      if (c != S[i][idx]) {
        if (nused[i] == true) {
          ok = false;
          break;
        } else {
          nused[i] = true;
        }
      }
    }
    if (!ok) continue;
    ans[idx] = c;
    dfs(idx + 1, nused);
  }
}
void solve() {
  int Q;
  cin >> Q;
  while (Q--) {
    cin >> N >> M;
    S.resize(N);
    for (ll i = 0; i < N; i++) {
      cin >> S[i];
    }
    ans.assign(M, 'A');
    dfs(0, bs());
    if (ans[M - 1] == 'A') {
      cout << -1 << "\n";
    } else {
      for (ll i = 0; i < M; i++) {
        cout << ans[i];
      }
      cout << "\n";
    }
  }
}
int main() {
  cin.tie(0), cout.tie(0);
  ios::sync_with_stdio(false);
  solve();
  cout << flush;
  return 0;
}
