import std.stdio;
import std.array;
import std.conv;
import std.algorithm;
import std.string;
import std.container;
import std.typecons;

void log(A...)(A arg) {
    stderr.writeln(arg);
}

struct Dice {
    enum FACE { TOP, FRONT, RIGHT, BOTTOM, BACK, LEFT };
    /*
                                           z??????
                                           |
                                           |
                                           |
                                           |
                                          / ------------------/
                                         /                   /|
                                        /                   / |
                                       /        TOP        /  |
                                      /                   /   |
                                     /                   /    |
                                    |-------------------|     |
                                    |                   |RIGHT|
                                    |                   |     |_______________ x??????
                                    |                   |    /
                                    |       FRONT       |   /
                                    |                   |  /
                                    |                   | /
                                    |                   |/
                                    |-------------------|
                                   /
                                  /
                                 /
                                /
                              y??????
    */
    int[6] v;
    this(int top, int front, int right) {
        v[FACE.TOP]   = top;    v[FACE.BOTTOM] = 7 - top;
        v[FACE.FRONT] = front;  v[FACE.BACK]   = 7 - front;
        v[FACE.RIGHT] = right;  v[FACE.LEFT]   = 7 - right;
    }
    // clockwise
    void rollX() { roll(FACE.TOP,  FACE.FRONT,  FACE.BOTTOM, FACE.BACK); }
    void rollY() { roll(FACE.TOP,  FACE.LEFT,   FACE.BOTTOM, FACE.RIGHT); }
    void rollZ() { roll(FACE.FRONT, FACE.RIGHT, FACE.BACK,   FACE.LEFT); }
    void roll(int a, int b, int c, int d) {
        int t = v[a];
        v[a] = v[b]; v[b] = v[c]; v[c] = v[d]; v[d] = t;
    }
    Dice[] allStates() immutable {
        Dice d = this;
        Dice[] r;
        foreach (i; 0 .. 6) {
            if (i % 2 == 0) d.rollX();
            else            d.rollY();
            foreach (j; 0 .. 4) {
                d.rollZ();
                r ~= d;
            }
        }
        return r;
    }
    bool sameDice(in Dice d) immutable {
        Dice[] states = allStates();
        return states.any!((a) => a.v == d.v);
    }
    string toString() {
        return "Dice" ~ v.to!string;
    }
}

void main() {
    alias F = Dice.FACE;
    immutable F[] input_order = [F.LEFT, F.RIGHT, F.FRONT, F.BACK, F.BOTTOM, F.TOP];

    alias P = Tuple!(int, "y", int, "x");

    auto order = [
        'L': (ref Dice d) { foreach (_; 0 .. 3) d.rollY(); },
        'R': (ref Dice d) { d.rollY(); },
        'F': (ref Dice d) { foreach (_; 0 .. 3) d.rollX(); },
        'B': (ref Dice d) { d.rollX(); },
    ];


    int[char] dy = [ 'L': 0, 'R': 0, 'F': -1, 'B': 1 ];
    int[char] dx = [ 'L': -1, 'R': 1, 'F': 0, 'B': 0 ];

    while (true) {
        int N;
        scanf("%d\n", &N);
        if (N == 0) break;
        auto as = new int[P][N];
        foreach (n; 0 .. N) {
            int x, y;
            scanf("%d %d\n", &x, &y);
            int[] faces = readln.chomp.split(" ").map!(to!int).array;
            string rot = readln.chomp;
            Dice d;
            foreach (i; 0 .. 6) {
                d.v[ input_order[i] ] = faces[i];
            }
            as[n][P(y, x)] = d.v[F.BOTTOM];
            foreach (c; rot) {
                y += dy[c];
                x += dx[c];
                order[c](d);
                as[n][P(y, x)] = d.v[F.BOTTOM];
            }
        }

        auto dp = new int[1<<N];
        dp[] = 0;
        foreach (int k; 0 .. N) {
            int bit = (1<<k) - 1;
            while (bit < (1<<N)) {

                foreach (i; 0 .. N) {
                    // try to drop the i-th dice
                    if ((1 << i) & bit) continue; // already dropped

                    int r = dp[bit]; // r: current score when drop the i-th dice after dropped 'bit' dices

                    foreach (p, v; as[i]) {
                        bool overwritten() {
                            foreach (int j; 0 .. N) {
                                if (bit & (1 << j)) continue;
                                if (i == j) continue;
                                if (p in as[j]) return true;
                            }
                            return false;
                        }
                        if (overwritten()) continue;
                        r += v;
                    }
                    dp[bit | 1<<i] = max(dp[bit | 1<<i], r);
                }

                int x = bit & -bit;
                int y = bit + x;
                if (x == 0) break;
                bit = ((bit & ~y) / x >> 1) | y;
            }
        }
        writeln(dp[(1<<N) - 1]);
    }


}

Measured memory_bytes: 3468