#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void run() {
  int n, m;
  scanf("%d%d", &n, &m);
  vector<string> v;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    v.push_back(s);
  }
  string ans;
  bool chk[11] = {};
  for (int i = 0; i < m; i++) {
    map<char, int> cnt;
    int mx = 0;
    char c;
    int t;
    for (int j = 0; j < n; j++) {
      cnt[v[j][i]]++;
      if (mx < cnt[v[j][i]]) {
        mx = cnt[v[j][i]];
        c = v[j][i];
        t = j;
      }
    }
    chk[t] = true;
    ans.push_back(c);
  }
  bool gg = false;
  for (int i = 0; i < n; i++) {
    int diff = 0;
    for (int j = 0; j < m; j++) {
      if (ans[j] != v[i][j]) diff++;
    }
    if (diff > 1) {
      gg = true;
      string tmp;
      for (int j = 0; j < m; j++) {
        if (ans[j] != v[i][j]) {
          tmp.push_back(v[i][j]);
          for (int jj = j + 1; jj < m; jj++) {
            tmp.push_back(ans[jj]);
          }
          bool pass = true;
          for (int ii = 0; ii < n; ii++) {
            int diff = 0;
            for (int jj = 0; jj < m; jj++) {
              if (tmp[jj] != v[ii][jj]) diff++;
            }
            if (diff > 1) {
              pass = false;
            }
          }
          if (pass) {
            cout << tmp << endl;
            return;
          } else {
            while (tmp.size() > j) tmp.pop_back();
            tmp.push_back(ans[j]);
          }
        } else {
          tmp.push_back(ans[j]);
        }
      }
    }
  }
  if (!gg)
    cout << ans << endl;
  else
    cout << "-1" << endl;
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    run();
  }
}
