#include <bits/stdc++.h>
using namespace std;
int diff(string a, string b) {
  int i, c = 0, n = a.length();
  for (i = 0; i < n; i++) {
    if (a[i] != b[i]) c++;
  }
  return c;
}
void solve() {
  long long int n, m, i;
  cin >> n >> m;
  vector<string> v(n);
  for (i = 0; i < n; i++) cin >> v[i];
  string ans = v[0], ans1 = v[0];
  if (n == 1) {
    cout << ans;
    return;
  }
  for (i = 1; i < n; i++) {
    long long int f = 0, f1 = 0, j;
    long long int k = diff(ans, v[i]);
    if (k > 2) {
      cout << "-1";
      return;
    }
    if (k == 2) {
      for (j = 0; j < m; j++) {
        if (ans[j] != v[i][j]) {
          if (f == 0) {
            ans[j] = v[i][j];
            f = 1;
            continue;
          }
          if (f1 == 0) {
            ans1[j] = v[i][j];
            f1 = 1;
          }
        }
      }
    }
    if (f && f1) break;
  }
  long long int f = 0, f1 = 0;
  for (i = 0; i < n; i++) {
    long long int k = diff(ans, v[i]);
    long long int k1 = diff(ans1, v[i]);
    if (k > 1) f = 1;
    if (k1 > 1) f1 = 1;
    if (f && f1) {
      cout << "-1";
      return;
    }
  }
  if (!f)
    cout << ans;
  else
    cout << ans1;
}
int main() {
  long long int t;
  cin >> t;
  while (t--) {
    solve();
    cout << "\n";
  }
}
