#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, m, flag = 0, i, j, k;
  cin >> n >> m;
  string s[n];
  vector<string> v;
  for (i = 0; i < n; i++) {
    cin >> s[i];
    v.push_back(s[i]);
  }
  for (i = 0; i < n; i++) {
    for (j = i + 1; j < n; j++) {
      int cnt = 0;
      for (k = 0; k < m; k++) {
        if (s[i][k] != s[j][k]) {
          cnt++;
        }
      }
      if (cnt <= 2) {
        if (cnt == 0)
          v.push_back(s[i]);
        else if (cnt == 1) {
          v.push_back(s[i]);
          v.push_back(s[j]);
        } else if (cnt == 2) {
          string str1 = "", str2 = "", str3 = "", str4 = "";
          int f = 0, f1 = 0;
          for (k = 0; k < m; k++) {
            if ((s[i][k] == s[j][k]) || (s[i][k] != s[j][k] && f == 1)) {
              str1 += s[i][k];
              str2 += s[j][k];
            } else if (s[i][k] != s[j][k] && f == 0) {
              str1 += s[j][k];
              str2 += s[i][k];
              f = 1;
            }
          }
          for (k = m - 1; k >= 0; k--) {
            if ((s[i][k] == s[j][k]) || (s[i][k] != s[j][k] && f1 == 1)) {
              str3 += s[i][k];
              str4 += s[j][k];
            } else if (s[i][k] != s[j][k] && f1 == 0) {
              str3 += s[j][k];
              str4 += s[i][k];
              f1 = 1;
            }
          }
          v.push_back(str1);
          v.push_back(str2);
          v.push_back(str3);
          v.push_back(str4);
        }
      } else {
        flag = 1;
        break;
      }
    }
    if (flag == 1) break;
  }
  if (flag == 1)
    cout << "-1\n";
  else {
    int mark = 0;
    string ans;
    for (i = 0; i < v.size(); i++) {
      int cnt3 = 0;
      for (j = 0; j < n; j++) {
        int cnt1 = 0;
        for (k = 0; k < m; k++) {
          if (v[i][k] != s[j][k]) cnt1++;
        }
        if (cnt1 <= 1) cnt3++;
      }
      if (cnt3 == n) {
        ans = v[i];
        mark = 1;
        break;
      }
    }
    if (mark == 1)
      cout << ans << "\n";
    else
      cout << "-1\n";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
