#include <bits/stdc++.h>
using namespace std;
long long mod_exp(long long x, long long y, long long mm) {
  if (y == 0)
    return (1);
  else if (y % 2 == 0)
    return (mod_exp((x * x) % mm, y / 2, mm));
  else
    return ((x * mod_exp((x * x) % mm, (y - 1) / 2, mm)) % mm);
}
bool check(string ans, vector<string> hi, int n) {
  int m = ans.size();
  for (int i = 1; i < n; i++) {
    int cnt = 0;
    for (int j = 0; j < m; j++) {
      if (hi[i][j] != ans[j]) cnt++;
    }
    if (cnt > 1) return 0;
  }
  return 1;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    vector<string> v(n);
    for (int i = 0; i < n; i++) cin >> v[i];
    string ans = v[0];
    int flag = 0;
    for (int i = 0; i < m; i++) {
      for (char j = 'a'; j <= 'z'; j++) {
        string k = ans;
        k[i] = j;
        if (check(k, v, n)) {
          cout << k << "\n";
          i = m - 1;
          flag = 1;
          break;
        }
      }
    }
    if (!flag) cout << -1 << "\n";
  }
  return 0;
}
