#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    string a[n];
    for (string &i : a) cin >> i;
    int count = 0;
    int index[4]{};
    string s = a[0], s1 = a[0], s2 = a[0];
    bool flag = true;
    bool filled = false;
    for (int i = 1; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (a[i][j] != s[j]) {
          count++;
          index[count - 1] = j;
          if (count == 2 && !filled) {
            s1[index[0]] = a[i][index[0]];
            s2[index[1]] = a[i][index[1]];
            filled = true;
          } else if (count > 2) {
            cout << -1 << endl;
            flag = false;
            break;
          }
        }
      }
      count = 0;
      if (!flag) break;
    }
    int c1 = 0, c2 = 0;
    if (filled && flag) {
      int m1 = 0, m2 = 0;
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
          if ((s1[j] != a[i][j])) c1++;
          if ((s2[j] != a[i][j])) c2++;
        }
        m1 = max(m1, c1);
        m2 = max(m2, c2);
        if (c1 > 1 && c2 > 1)
          break;
        else {
          c1 = 0;
          c2 = 0;
        }
      }
      if (m1 <= 1)
        cout << s1 << endl;
      else if (m2 <= 1)
        cout << s2 << endl;
      else
        cout << -1 << endl;
    } else if (flag)
      cout << s << endl;
  }
}
