Language: C++

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <functional>
#include <cmath>
#include <complex>
#include <cctype>
#include <cassert>
#include <sstream>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <deque>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second

template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pii> pip;

const ll INF = 1ll<<29;
const ll MOD = 1000000007;
const double EPS = 1e-10;

int n;
int cnt[26];
bool g[26][26];
bool done[26];

int main() {
	while (cin >> n, n) {
		
		memset(cnt, 0, sizeof(cnt));
		memset(g, 0, sizeof(g));
		memset(done, 0, sizeof(done));
		
		int st = -1;
		REP(i, n) {
			string str; cin >> str;
			int u = *(str.begin()) - 'a';
			int v = *--str.end() - 'a';
			
			cnt[u]++; cnt[v]++;
			g[u][v] = true;
			if (st == -1) st = u;
		}
		
		queue<int> que;
		que.push(st);
		done[st] = true;
		
		while (!que.empty()) {
			int now = que.front(); que.pop();
			REP(i, 26) if (g[now][i] && !done[i]) {
				done[i] = true;
				que.push(i);
			}
		}
		
		bool ans = true;
		
		REP(i, 26) {
			if (cnt[i] & 1) ans = false;
			if (cnt[i] > 0 && !done[i]) ans = false;
		}
		
		puts(ans ? "OK" : "NG");
		
	}
	return 0;
}

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <functional>
#include <cmath>
#include <complex>
#include <cctype>
#include <cassert>
#include <sstream>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <deque>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second

template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pii> pip;

const ll INF = 1ll<<29;
const ll MOD = 1000000007;
const double EPS = 1e-10;

int n;
int cnt[26];
bool g[26][26];
bool done[26];

int main() {
	while (cin >> n, n) {
		
		memset(cnt, 0, sizeof(cnt));
		memset(g, 0, sizeof(g));
		memset(done, 0, sizeof(done));
		
		int st = -1;
		REP(i, n) {
			string str; cin >> str;
			int u = *(str.begin()) - 'a';
			int v = *--str.end() - 'a';
			
			cnt[u]++; cnt[v]++;
			g[u][v] = true;
			if (st == -1) st = u;
		}
		
		queue<int> que;
		que.push(st);
		done[st] = true;
		
		while (!que.empty()) {
			int now = que.front(); que.pop();
			REP(i, 26) if (g[now][i] && !done[i]) {
				done[i] = true;
				que.push(i);
			}
		}
		
		bool ans = true;
		
		REP(i, 26) {
			if (cnt[i] & 1) ans = false;
			if (cnt[i] > 0 && !done[i]) ans = false;
		}
		
		puts(ans ? "OK" : "NG");
		
	}
	return 0;
}

Compiler output:
a.cc:115:46: error: redefinition of 'template<class A, class B> bool chmax(A&, B)'
  115 | template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
      |                                              ^~~~~
a.cc:28:46: note: 'template<class A, class B> bool chmax(A&, B)' previously declared here
   28 | template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
      |                                              ^~~~~
a.cc:116:46: error: redefinition of 'template<class A, class B> bool chmin(A&, B)'
  116 | template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }
      |                                              ^~~~~
a.cc:29:46: note: 'template<class A, class B> bool chmin(A&, B)' previously declared here
   29 | template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }
      |                                              ^~~~~
a.cc:123:10: error: redefinition of 'const ll INF'
  123 | const ll INF = 1ll<<29;
      |          ^~~
a.cc:36:10: note: 'const ll INF' previously defined here
   36 | const ll INF = 1ll<<29;
      |          ^~~
a.cc:124:10: error: redefinition of 'const ll MOD'
  124 | const ll MOD = 1000000007;
      |          ^~~
a.cc:37:10: note: 'const ll MOD' previously defined here
   37 | const ll MOD = 1000000007;
      |          ^~~
a.cc:125:14: error: redefinition of 'const double EPS'
  125 | const double EPS = 1e-10;
      |              ^~~
a.cc:38:14: note: 'const double EPS' previously defined here
   38 | const double EPS = 1e-10;
      |              ^~~
a.cc:127:5: error: redefinition of 'int n'
  127 | int n;
      |     ^
a.cc:40:5: note: 'int n' previously declared here
   40 | int n;
      |     ^
a.cc:128:5: error: redefinition of 'int cnt [26]'
  128 | int cnt[26];
      |     ^~~
a.cc:41:5: note: 'int cnt [26]' previously declared here
   41 | int cnt[26];
      |     ^~~
a.cc:129:6: error: redefinition of 'bool g [26][26]'
  129 | bool g[26][26];
      |      ^
a.cc:42:6: note: 'bool g [26][26]' previously declared here
   42 | bool g[26][26];
      |      ^
a.cc:130:6: error: redefinition of 'bool done [26]'
  130 | bool done[26];
      |      ^~~~
a.cc:43:6: note: 'bool done [26]' previously declared here
   43 | bool done[26];
      |      ^~~~
a.cc:132:5: error: redefinition of 'int main()'
  132 | int main() {
      |     ^~~~
a.cc:45:5: note: 'int main()' previously defined here
   45 | int main() {
      |     ^~~~