#include <bits/stdc++.h>
using namespace std;
int main() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int t;
  cin >> t;
  while (t--) {
    long long int n, m;
    cin >> n >> m;
    string str[n];
    ;
    for (long int i = 0; i < n; i++) cin >> str[i];
    string ans = str[0];
    long long int d = 0;
    vector<long long int> v[3];
    ;
    for (long int i = 0; i < n; i++) {
      long long int count = 0;
      ;
      for (long int j = 0; j < m; j++) {
        if (str[i][j] != ans[j]) count++;
      }
      if (count > 2)
        break;
      else
        v[count].push_back(i);
    }
    if (v[0].size() + v[1].size() + v[2].size() != n) {
      cout << "-1" << '\n';
      continue;
    }
    if (v[2].size() == 0) {
      cout << ans << '\n';
      continue;
    }
    long long int l = -1, r;
    ;
    for (long int i = 0; i < m; i++) {
      if (str[v[2][0]][i] != ans[i]) {
        if (l == -1)
          l = i;
        else
          r = i;
      }
    }
    ans[l] = str[v[2][0]][l];
    ;
    for (long int i = 0; i < n; i++) {
      long long int count = 0;
      ;
      for (long int j = 0; j < m; j++) {
        if (str[i][j] != ans[j]) count++;
      }
      if (count >= 2) d++;
    }
    if (d == 0) {
      cout << ans << '\n';
      continue;
    }
    d = 0;
    ans = str[0];
    ans[r] = str[v[2][0]][r];
    ;
    for (long int i = 0; i < n; i++) {
      long long int count = 0;
      ;
      for (long int j = 0; j < m; j++) {
        if (str[i][j] != ans[j]) count++;
      }
      if (count >= 2) d++;
    }
    if (d == 0) {
      cout << ans << '\n';
      continue;
    }
    cout << "-1" << '\n';
  }
  return 0;
}
