#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const long double EPS = 0.00000001;
const long double PI = acos(-1.);
const int N = 15;
int n, m;
string a[N];
int test_case() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    int ok = 1;
    for (int j = 0; j < n; j++) {
      int cnt = 0;
      for (int k = 0; k < m; k++) {
        if (a[i][k] != a[j][k]) {
          cnt++;
        }
      }
      if (cnt > 1) {
        ok = 0;
        break;
      }
    }
    if (ok) {
      cout << a[i] << endl, 0;
      return 0;
    }
  }
  for (int k = 0; k < m; k++) {
    for (int i = 0; i < n; i++) {
      if (a[i][k] != a[0][k]) {
        string ans = a[0];
        ans[k] = a[i][k];
        int ok = 1;
        for (int j = 0; j < n; j++) {
          int cnt = 0;
          for (int jj = 0; jj < m; jj++) {
            if (a[j][jj] != ans[jj]) {
              cnt++;
            }
          }
          if (cnt > 1) {
            ok = 0;
            break;
          }
        }
        if (ok) {
          cout << ans << endl, 0;
          return 0;
        }
        ans = a[i];
        ans[k] = a[0][k];
        ok = 1;
        for (int j = 0; j < n; j++) {
          int cnt = 0;
          for (int jj = 0; jj < m; jj++) {
            if (a[j][jj] != ans[jj]) {
              cnt++;
            }
          }
          if (cnt > 1) {
            ok = 0;
            break;
          }
        }
        if (ok) {
          cout << ans << endl, 0;
          return 0;
        }
      }
    }
  }
  return cout << -1 << endl, 0;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    test_case();
  }
  return 0;
}
