#include <bits/stdc++.h>
using namespace std;
long long x, y, z, i, j, n, m, k, s1, s2, s3, s4, s5, mod, l, r, t, maxx, minn,
    sx, so, sum;
double u, v, w, x1, x2, x3, z1, z2, z3, h, huh;
long long a[3000045];
long long b[3000045];
long long c[3000045];
long long d[1000045];
pair<long long, long long> aa[300045];
string s;
string q;
string p;
string poop;
string sq[1045];
char ch;
vector<long long> vc;
queue<long long> qu;
map<long long, long long> mp;
map<long long, long long> pm;
int main() {
  scanf("%lld", &t);
  for (l = 1; l <= t; l++) {
    scanf("%lld%lld", &n, &m);
    s = "";
    for (i = 0; i < n; i++) {
      cin >> sq[i];
    }
    q = "";
    so = 0;
    s = sq[0];
    for (r = 0; r < n; r++) {
      for (i = 0; i < n; i++) {
        for (j = 0; j < m; j++) {
          s = sq[r];
          s[j] = sq[i][j];
          x = 0;
          for (s1 = 0; s1 < n; s1++) {
            y = 0;
            for (s2 = 0; s2 < m; s2++) {
              if (s[s2] != sq[s1][s2]) y++;
            }
            x = max(x, y);
          }
          if (x <= 1) {
            so++;
            q = s;
          }
        }
      }
    }
    if (so > 0)
      cout << q << endl;
    else
      cout << -1 << endl;
  }
}
