#include <bits/stdc++.h>
using namespace std;
const int N = 2e5;
int n, m;
string s[10];
void solve() {
  cin >> n >> m;
  for (long long int i = 0; i < n; i++) cin >> s[i];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      bool changed = 0, ok = 1;
      char c = s[i][j];
      for (int k = 0; k < n; k++) {
        int dist = 0;
        for (int l = 0; l < m; l++) {
          if (k == i) continue;
          if (l == j) {
            if (changed)
              if (c != s[k][l]) dist++;
          } else {
            if (s[k][l] != s[i][l]) dist++;
          }
        }
        if (dist >= 2) {
          ok = 0;
          break;
        } else if (dist == 1 && !changed) {
          c = s[k][j];
          changed = 1;
        }
      }
      if (ok) {
        for (long long int k = 0; k < m; k++) {
          if (k == j)
            cout << c;
          else
            cout << s[i][k];
        }
        cout << '\n';
        return;
      }
    }
  }
  cout << "-1\n";
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int T;
  cin >> T;
  ;
  while (T--) {
    solve();
  }
  return 0;
}
