#include <iostream>
#include <vector>
#include <tuple>

using namespace std;

int n;
int s, i, o;
vector<pair<int, int>> q;

void improvised_dfs(vector<char>& v, int r, int l) {
    switch (v[r-1]) {
    case 'S':s++;
        break;
    case'I':i++;
        break;
    case'O':o++;
        break;
    }
    if (s != 0 && s == i && i == o)q.push_back({ l,r });
    if (r < n)improvised_dfs(v, r + 1, l);
}

int main()
{
    cin >> n;
    vector<char> e(n);
    int a, b, x;
    char c;
    for (int j = 0;j < n-1;j++) {
        cin >> a >> b >> c;
        if (a < b)x = a;
        else x = b;
        e[x] = c;
    }
    for (int j = 1;j < n;j++) {
        s = 0;
        i = 0;
        o = 0;
        improvised_dfs(e, j + 1, j);
    }
    for (auto w : q)
        cout << w.first << ' ' << w.second << endl;
}