#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int MOD = 1000000007;
int diff(string a, string b) {
  int ans = 0;
  for (int i = 0; i < (long long)a.size(); ++i) ans += (a[i] != b[i]);
  return ans;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, m, d = 0;
    cin >> n >> m;
    vector<string> vec(n);
    for (int i = 0; i < n; ++i) cin >> vec[i];
    for (int i = 0; i < n; ++i)
      for (int j = i + 1; j < n; ++j) d = max(d, diff(vec[i], vec[j]));
    if (d > 2)
      cout << "-1\n";
    else if (d <= 1)
      cout << vec[0] << "\n";
    else {
      string h = "", h2 = "";
      for (int i = 0; i < n; ++i) {
        bool st = 0;
        for (int j = i + 1; j < n; ++j)
          if (diff(vec[i], vec[j]) == 2) {
            int s = 0;
            for (int z = 0; z < m; ++z) {
              if (vec[i][z] != vec[j][z]) {
                if (!s)
                  h.push_back(vec[i][z]), h2.push_back(vec[j][z]);
                else
                  h.push_back(vec[j][z]), h2.push_back(vec[i][z]);
                s++;
              } else
                h.push_back(vec[i][z]), h2.push_back(vec[i][z]);
            }
            st = 1;
            break;
          }
        if (st) break;
      }
      bool f = 1;
      for (int i = 0; i < n; ++i)
        if (diff(h, vec[i]) > 1) {
          f = 0;
          break;
        }
      if (!f) {
        f = 1;
        for (int i = 0; i < n; ++i)
          if (diff(h2, vec[i]) > 1) {
            f = 0;
            break;
          }
        if (!f)
          cout << "-1\n";
        else
          cout << h2 << "\n";
      } else
        cout << h << "\n";
    }
  }
  return 0;
}
