#include <iostream>
using namespace std;
int main() {
int n, q, count;
char ungulate;
bool b;
cin >> n;
for ( int i = 0; i < n; i++ ) {
int count = 0;
b = true;
cin >> q;
for ( int j = 0; j < q; j++ ) {
cin >> ungulate;
if ( ungulate == '>' ) {
count++;
}
else {
count--;
}
if ( count < 0 ){
b = false;
}
}
if ( count != 0 ) {
b = false;
}
if ( b ) {
cout << "legal" << endl;
}
else {
cout << "illegal" << endl;
}
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IG1haW4oKSB7CiAgaW50IG4sIHEsIGNvdW50OwogIGNoYXIgdW5ndWxhdGU7CiAgYm9vbCBiOwogIGNpbiA+PiBuOwogCiAgZm9yICggaW50IGkgPSAwOyBpIDwgbjsgaSsrICkgewogICAgaW50IGNvdW50ID0gMDsKICAgIGIgPSB0cnVlOwogICAgY2luID4+IHE7CiAgIAogICAgZm9yICggaW50IGogPSAwOyBqIDwgcTsgaisrICkgewogICAgICBjaW4gPj4gdW5ndWxhdGU7CiAgICAgIGlmICggdW5ndWxhdGUgPT0gJz4nICkgewogICAgICAJY291bnQrKzsKICAgICAgfQogICAgICBlbHNlIHsKICAgICAgCWNvdW50LS07CiAgICAgIH0KICAgICAgaWYgKCBjb3VudCA8IDAgKXsKICAgICAgCWIgPSBmYWxzZTsKICAgICAgfQogICAgfQogICAgCiAgICBpZiAoIGNvdW50ICE9IDAgKSB7CiAgICAJYiA9IGZhbHNlOwogICAgfQogICAgCiAgICBpZiAoIGIgKSB7CiAgICAgICAgY291dCA8PCAibGVnYWwiIDw8IGVuZGw7IAogICAgICB9IAogICAgZWxzZSB7CiAgICAJY291dCA8PCAiaWxsZWdhbCIgPDwgZW5kbDsKICAgICAgfQogIH0KCXJldHVybiAwOwp9
NQo0ICZndDsmbHQ7Jmd0OyZsdDsKMTAgJmd0OyZndDsmZ3Q7Jmx0OyZsdDsmZ3Q7Jmd0OyZsdDsmbHQ7Jmx0Owo4ICZndDsmZ3Q7Jmx0OyZsdDsmbHQ7Jmd0OyZndDsmbHQ7CjMgJmd0OyZndDsmbHQ7CjEyICZndDsmbHQ7Jmd0OyZndDsmbHQ7Jmd0OyZndDsmZ3Q7Jmx0OyZsdDsmbHQ7Jmx0Ow==
5
4 ><><
10 >>><<>><<<
8 >><<<>><
3 >><
12 ><>><>>><<<<