#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>

typedef struct{
    int x;
    int y;
    int z;
}edge;

typedef struct{
    int x;
    int y;
    int z;
}point;

typedef struct{
    int nedge;
    edge *edges[512];
}edge_set;

edge_set *get_edge_set();
void free_edge_set(edge_set *s);
edge *make_edge(int x, int y, int z);
edge_set *convert_edge_set(const edge_set *s, int nth, int dir);
int compare_keys(edge_set *in1, edge_set *in2);
int compare_edge_set(const edge_set *s1, const edge_set *s2);
void point_copy(point *dest, const point *src);
void copy_edge_set(edge_set *in1, edge_set *in2);
void sort_edge_set(edge_set *s);
void rotate_edge(edge *e, int x, int y, int z);
int edge_comp(const void *e1, const void *e2);
void test();
void solve();

int main()
{
    solve();

    return 0;
}

void test()
{
    edge_set *s1, *s2;

    s1 = get_edge_set();
    s2 = get_edge_set();
    if(compare_keys(s1, s2))
        printf("SAME\n");
    else printf("DIFFERENT\n");

    free_edge_set(s1);
    free_edge_set(s2);
}

void solve()
{
    edge_set *s1, *s2;

    while(1){
        if((s1 = get_edge_set()) == NULL) break;
        s2 = get_edge_set();
        if(compare_keys(s1, s2))
            printf("SAME\n");
        else printf("DIFFRENT\n");

        free_edge_set(s1);
        free_edge_set(s2);
    }

    return;
}

void sort_edge_set(edge_set *s)
{
    qsort(s->edges, s->nedge, sizeof(edge*), edge_comp);
}

int edge_comp(const void *e1, const void *e2)
{
    if((*(edge**)e1)->x != (*(edge**)e2)->x){
        return (*(edge**)e1)->x > (*(edge**)e2)->x;
    }else if((*(edge**)e1)->y != (*(edge**)e2)->y){
        return (*(edge**)e1)->y > (*(edge**)e2)->y;
    }else{
        return (*(edge**)e1)->z > (*(edge**)e2)->z;
    }
}

edge_set *get_edge_set()
{
    char buf[128];
    int used[512];
    int cnt_elem;
    int n;
    int i;
    int len;
    int num;
    point cur, new;
    point *vertices[512];
    edge_set *s;

    // initialize
    s = malloc(sizeof(edge_set));
    s->nedge = 0;
    for(i = 0; i < 512; i++) used[i] = 0;
    cnt_elem = 0;
    cur.x = 0; cur.y = 0; cur.z = 0;
    point_copy(&new, &cur);

    scanf("%d", &n);
    if(n == 0) return NULL;

    while(1){
        fgets(buf, sizeof(buf), stdin);
        len = strlen(buf);

        for(i = 0; i < len; i++){
            if(buf[i] == ' '){
                continue;
            }else if(buf[i] == '+'){
                i++;
                if(buf[i] == 'x') new.x = cur.x+1;
                else if(buf[i] == 'y') new.y = cur.y+1;
                else if(buf[i] == 'z') new.z = cur.z+1;
                s->edges[s->nedge] = make_edge(new.x+cur.x, new.y+cur.y, new.z+cur.z);
                s->nedge++;
                point_copy(&cur, &new);
                cnt_elem++;
            }else if(buf[i] == '-'){
                i++;
                if(buf[i] == 'x') new.x = cur.x-1;
                else if(buf[i] == 'y') new.y = cur.y-1;
                else if(buf[i] == 'z') new.z = cur.z-1;
                s->edges[s->nedge] = make_edge(new.x+cur.x, new.y+cur.y, new.z+cur.z);
                s->nedge++;
                point_copy(&cur, &new);
                cnt_elem++;
            }else if(isdigit(buf[i])){
                num = atoi(buf+i);
                while(isdigit(buf[++i]));
                if(used[num]){
                    point_copy(&cur, vertices[num]);
                    point_copy(&new, &cur);
                }else{
                    used[num] = 1;
                    vertices[num] = malloc(sizeof(point));
                    point_copy(vertices[num], &cur);
                }
                cnt_elem++;
            }
        }

        if(cnt_elem == n) break;
    }

    return s;
}

void free_edge_set(edge_set *s)
{
    int i;
    int nedge = s->nedge;
    for(i = 0; i < nedge; i++) free(s->edges[i]);
    free(s);
}

void point_copy(point *dest, const point *src)
{
    dest->x = src->x;
    dest->y = src->y;
    dest->z = src->z;
}

edge *make_edge(int x, int y, int z)
{
    edge *e = malloc(sizeof(edge));
    e->x = x;
    e->y = y;
    e->z = z;
    return e;
}

int compare_keys(edge_set *in1, edge_set *in2)
{
    int nedge;
    int i, j, k, l, m;
    edge *e;
    edge_set *s1, *s2;
    int dx, dy, dz;

    if(in1->nedge != in2->nedge) return 0;
    nedge = in1->nedge;

    s1 = malloc(sizeof(edge_set));
    s2 = malloc(sizeof(edge_set));
    for(i = 0; i < 512; i++) s1->edges[i] = malloc(sizeof(edge));
    for(i = 0; i < 512; i++) s2->edges[i] = malloc(sizeof(edge));


    copy_edge_set(s1, in1);
    sort_edge_set(s1);

    for(i = 0; i < nedge; i++){
        copy_edge_set(s2, in2);
        e = s2->edges[i];
        dx = e->x/2*2; dy = e->y/2*2; dz = e->z/2*2;
        for(j = 0; j < nedge; j++){
            e = s2->edges[j];
            e->x -= dx; e->y -= dy; e->z -= dz;
        }
        for(j = 0; j < 4; j++) for(k = 0; k < 4; k++) for(l = 0; l < 4; l++){
            for(m = 0; m < nedge; m++)
                rotate_edge(s2->edges[m], j, k, l);
            sort_edge_set(s2);
            if(compare_edge_set(s1, s2)) return 1;
        }
    }

    free_edge_set(s1);
    free_edge_set(s2);

    return 0;
}

void rotate_edge(edge *e, int x, int y, int z)
{
    int i;
    int tmp;
    for(i = 0; i < x; i++){
        tmp = e->y;
        e->y = -e->z;
        e->z = tmp;
    }
    for(i = 0; i < y; i++){
        tmp = e->x;
        e->x = e->z;
        e->z = -tmp;
    }
    for(i = 0; i < z; i++){
        tmp = e->x;
        e->x = -e->y;
        e->y = tmp;
    }
}

void copy_edge_set(edge_set *dest, edge_set *src)
{
    int i;
    int nedge = src->nedge;
    dest->nedge = nedge;
    for(i = 0; i < nedge; i++){
        dest->edges[i]->x = src->edges[i]->x; 
        dest->edges[i]->y = src->edges[i]->y; 
        dest->edges[i]->z = src->edges[i]->z; 
    }
}

int compare_edge_set(const edge_set *s1, const edge_set *s2)
{
    int nedge;
    int i;
    edge *e1, *e2;
    if(s1->nedge != s2->nedge) return 0;
    nedge = s1->nedge;
    for(i = 0; i < nedge; i++){
        e1 = s1->edges[i];
        e2 = s2->edges[i];
        if(e1->x != e2->x || e1->y != e2->y || e1->z != e2->z)
            return 0;
    }
    return 1;
}

