#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using pi = pair<int, int>;
const int mod = 1e9 + 7;
const ll inf = ll(1e18);
const double PI = acos(-1), eps = 1e-9;
const int mxn = 1e2, mxa = 1e5, mxk = 10;
int n, m;
string a[mxn];
int calc(string &s1, string &s2) {
  int res = 0;
  for (int i = 0; i < m; ++i) {
    if (s1[i] != s2[i]) ++res;
  }
  return res;
}
void solve() {
  cin >> n >> m;
  for (int i = 0; i < n; ++i) cin >> a[i];
  for (int i = 0; i < m; ++i) {
    string ans = a[0];
    for (int j = 0; j < n; ++j) {
      if (calc(ans, a[j]) > 1 && ans[i] != a[j][i]) {
        ans[i] = a[j][i];
        break;
      }
    }
    int j = 0;
    while (j < n && calc(ans, a[j]) <= 1) ++j;
    if (j == n) {
      cout << ans << '\n';
      return;
    }
  }
  cout << "-1\n";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
}
