#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long int i, j, k, t, p, w, q, n, m, a[101][101];
  cin >> t;
  while (t--) {
    p = 0;
    cin >> n >> m;
    string s[n];
    for (i = 0; i < n; i++) {
      cin >> s[i];
    }
    for (w = 0; w < n; w++) {
      long long int d = 0, q, x = 1;
      if (p == 0) {
        string ans = s[w];
        long long int arr[n];
        vector<long long int> v;
        for (long long int i = 0; i < n; i++) {
          long long int c = 0;
          for (long long int j = 0; j < m; j++) {
            if (s[i][j] != ans[j]) {
              c++;
            }
          }
          arr[i] = c;
          if (arr[i] <= 1) {
            d++;
          }
          if (c > 2) {
            x = 0;
            break;
          }
        }
        if (d == n) {
          p = 1;
          cout << ans << endl;
        } else if (x) {
          d = 0;
          for (long long int i = 0; i < n; i++) {
            if (arr[i] == 2) {
              for (long long int j = 0; j < m; j++) {
                if (s[i][j] != ans[j]) {
                  v.push_back(j);
                }
              }
              q = i;
              break;
            }
          }
          ans[v[0]] = s[q][v[0]];
          for (long long int i = 0; i < n; i++) {
            long long int c = 0;
            for (long long int j = 0; j < m; j++) {
              if (s[i][j] != ans[j]) {
                c++;
              }
            }
            arr[i] = c;
            if (arr[i] <= 1) {
              d++;
            }
            if (c > 2) {
              x = 0;
              break;
            }
          }
          if (d == n) {
            p = 1;
            cout << ans << endl;
          } else if (x) {
            d = 0;
            ans = s[w];
            ans[v[1]] = s[q][v[1]];
            for (long long int i = 0; i < n; i++) {
              long long int c = 0;
              for (long long int j = 0; j < m; j++) {
                if (s[i][j] != ans[j]) {
                  c++;
                }
              }
              arr[i] = c;
              if (arr[i] <= 1) {
                d++;
              }
              if (c > 2) {
                x = 0;
                break;
              }
            }
            if (x) {
              if (d == n) {
                p = 1;
                cout << ans << endl;
              }
            }
          }
        }
      }
    }
    if (p == 0) {
      cout << -1 << endl;
    }
  }
}
