#include <bits/stdc++.h>
using namespace std;
int fun(string a, string b, int &c, int &d) {
  int i, n = a.size(), e = 0;
  for (i = 0; i < n; i++) {
    if (a[i] != b[i]) {
      e++;
      if (e == 1)
        c = i;
      else
        d = i;
    }
  }
  return e;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, m, i, j, c, d, e;
    cin >> n >> m;
    string s[n];
    for (i = 0; i < n; i++) cin >> s[i];
    string v = s[0], p, q;
    for (i = 1; i < n; i++) {
      e = fun(v, s[i], c, d);
      if (e < 2) continue;
      if (e > 2) break;
      p = v;
      p[c] = s[i][c];
      q = v;
      q[d] = s[i][d];
      break;
    }
    if (i == n)
      cout << v << endl;
    else if (i < n && e > 2)
      cout << -1 << endl;
    else {
      for (i = 0; i < n; i++) {
        if (fun(p, s[i], c, d) > 1) break;
      }
      if (i == n)
        cout << p << endl;
      else {
        for (i = 0; i < n; i++) {
          if (fun(q, s[i], c, d) > 1) break;
        }
        if (i == n)
          cout << q << endl;
        else
          cout << -1 << endl;
      }
    }
  }
}
