#include <bits/stdc++.h>
using namespace std;
string ans = "-1";
int n, m;
bool checking(string& str, string str_q[]) {
  for (int i = 0; i < n; i++) {
    int counter = 0;
    for (int j = 0; j < m; j++) {
      if (str_q[i][j] != str[j]) {
        counter++;
      }
    }
    if (counter >= 2) return 0;
  }
  ans = str;
  return 1;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    ans = "-1";
    cin >> n >> m;
    string str_q[n];
    for (int i = 0; i < n; i++) cin >> str_q[i];
    if (checking(str_q[0], str_q)) {
      cout << ans << "\n";
    } else {
      char c_temp;
      for (int i = 1; i < n; i++) {
        for (int j = 0; j < m; j++) {
          if (str_q[0][j] != str_q[i][j]) {
            c_temp = str_q[0][j];
            str_q[0][j] = str_q[i][j];
            if (checking(str_q[0], str_q)) {
              break;
            }
            str_q[0][j] = c_temp;
          }
        }
        if (ans != "-1") break;
      }
      cout << ans << "\n";
    }
  }
}
