Buggy python submission (verdict: Runtime Error):
while 1:
    n = input()
    if n == 0:
        break
    s = raw_input()
    lock = 0
    P = set()
    for c in s:
        if c is 'u':
            lock = 0
        else:
            P.add((lock, 1 << int(c)))
            lock |= 1 << int(c)
    while 1:
        Q = set()
        for lock1, c1 in P:
            for lock2, c2 in P:
                if lock1 & lock2 == 0 and c1 & lock2 == c1:
                    Q.add((lock1 | lock2, c2))
        if not Q - P:
            break
        P |= Q
    ok = 1
    for lock1, c1 in P:
        for lock2, c2 in P:
            if lock1 & lock2 == 0 and c1 & lock2 == c1 and c2 & lock1 == c2:
                ok = 0
    for lock, c in P:
        if lock & c == c:
            ok = 0
    print "SAFE"*ok or "UNSAFE"

Fixed submission:
while 1:
    n = input()
    if n == 0:
        break
    s = raw_input()
    Q = set()
    lock = 0
    P = []
    for c in s:
        if c is 'u':
            lock = 0
        else:
            v = (lock, 1 << int(c))
            if v not in Q:
                P.append(v)
                Q.add(v)
            lock |= 1 << int(c)
    i = 0; ok = 1
    while i < len(P) and ok:
        lock1, t1 = P[i]
        if lock1 & t1 == t1:
            ok = 0
        for j in xrange(i):
            lock2, t2 = P[j]
            if lock1 & lock2 == 0 and t1 & lock2 == t1:
                if t2 & lock1 == t2:
                    ok = 0
                v = (lock1 | lock2, t2)
                if v not in Q:
                    P.append(v)
                    Q.add(v)
        i += 1
    print "SAFE"*ok or "UNSAFE"

Buggy python submission (verdict: Runtime Error):
while 1:
    n = input()
    if n == 0:
        break
    s = raw_input()
    lock = 0
    P = set()
    for c in s:
        if c is 'u':
            lock = 0
        else:
            P.add((lock, 1 << int(c)))
            lock |= 1 << int(c)
    while 1:
        Q = set()
        for lock1, c1 in P:
            for lock2, c2 in P:
                if lock1 & lock2 == 0 and c1 & lock2 == c1:
                    v = (lock1 | lock2, c2)
                    if v not in P:
                        Q.add(v)
        if not Q:
            break
        P |= Q
    ok = 1
    for lock1, c1 in P:
        for lock2, c2 in P:
            if lock1 & lock2 == 0 and c1 & lock2 == c1 and c2 & lock1 == c2:
                ok = 0
    for lock, c in P:
        if lock & c == c:
            ok = 0
    print "SAFE"*ok or "UNSAFE"

Fixed submission:
while 1:
    n = input()
    if n == 0:
        break
    s = raw_input()
    Q = set()
    lock = 0
    P = []
    for c in s:
        if c is 'u':
            lock = 0
        else:
            v = (lock, 1 << int(c))
            if v not in Q:
                P.append(v)
                Q.add(v)
            lock |= 1 << int(c)
    i = 0; ok = 1
    while i < len(P) and ok:
        lock1, t1 = P[i]
        if lock1 & t1 == t1:
            ok = 0
        for j in xrange(i):
            lock2, t2 = P[j]
            if lock1 & lock2 == 0 and t1 & lock2 == t1:
                if t2 & lock1 == t2:
                    ok = 0
                v = (lock1 | lock2, t2)
                if v not in Q:
                    P.append(v)
                    Q.add(v)
        i += 1
    print "SAFE"*ok or "UNSAFE"

Buggy python submission (verdict: Runtime Error):
while 1:
    n = input()
    if n == 0:
        break
    s = raw_input()
    lock = 0
    P = []
    for c in s:
        if c is 'u':
            lock = 0
        else:
            P.append((lock, 1 << int(c)))
            lock |= 1 << int(c)
    Q = set()
    i = 0
    while i < len(P):
        lock1, t1 = P[i]
        for j in xrange(i):
            lock2, t2 = P[j]
            if lock1 & lock2 == 0 and t1 & lock2 == t1:
                v = (lock1 | lock2, t2)
                if v not in Q:
                    P.append(v)
        i += 1
    ok = 1
    for lock1, c1 in P:
        for lock2, c2 in P:
            if lock1 & lock2 == 0 and c1 & lock2 == c1 and c2 & lock1 == c2:
                ok = 0
    for lock, c in P:
        if lock & c == c:
            ok = 0
    print "SAFE"*ok or "UNSAFE"

Fixed submission:
while 1:
    n = input()
    if n == 0:
        break
    s = raw_input()
    Q = set()
    lock = 0
    P = []
    for c in s:
        if c is 'u':
            lock = 0
        else:
            v = (lock, 1 << int(c))
            if v not in Q:
                P.append(v)
                Q.add(v)
            lock |= 1 << int(c)
    i = 0; ok = 1
    while i < len(P) and ok:
        lock1, t1 = P[i]
        if lock1 & t1 == t1:
            ok = 0
        for j in xrange(i):
            lock2, t2 = P[j]
            if lock1 & lock2 == 0 and t1 & lock2 == t1:
                if t2 & lock1 == t2:
                    ok = 0
                v = (lock1 | lock2, t2)
                if v not in Q:
                    P.append(v)
                    Q.add(v)
        i += 1
    print "SAFE"*ok or "UNSAFE"

Buggy python submission (verdict: Runtime Error):
while 1:
    n = input()
    if n == 0:
        break
    s = raw_input()
    lock = 0
    P = []
    for c in s:
        if c is 'u':
            lock = 0
        else:
            P.append((lock, 1 << int(c)))
            lock |= 1 << int(c)
    Q = set()
    i = 0; ok = 1
    while i < len(P) and ok:
        lock1, t1 = P[i]
        if lock1 & t1 == t1:
            ok = 0
        for j in xrange(i):
            lock2, t2 = P[j]
            if lock1 & lock2 == 0 and t1 & lock2 == t1:
                if t2 & lock1 == t2:
                    ok = 0
                v = (lock1 | lock2, t2)
                if v not in Q:
                    P.append(v)
        i += 1
    print "SAFE"*ok or "UNSAFE"

Fixed submission:
while 1:
    n = input()
    if n == 0:
        break
    s = raw_input()
    Q = set()
    lock = 0
    P = []
    for c in s:
        if c is 'u':
            lock = 0
        else:
            v = (lock, 1 << int(c))
            if v not in Q:
                P.append(v)
                Q.add(v)
            lock |= 1 << int(c)
    i = 0; ok = 1
    while i < len(P) and ok:
        lock1, t1 = P[i]
        if lock1 & t1 == t1:
            ok = 0
        for j in xrange(i):
            lock2, t2 = P[j]
            if lock1 & lock2 == 0 and t1 & lock2 == t1:
                if t2 & lock1 == t2:
                    ok = 0
                v = (lock1 | lock2, t2)
                if v not in Q:
                    P.append(v)
                    Q.add(v)
        i += 1
    print "SAFE"*ok or "UNSAFE"

Buggy python submission (verdict: Runtime Error):
while 1:
    n = input()
    if n == 0:
        break
    s = raw_input()
    Q = set()
    lock = 0
    P = []
    for c in s:
        if c is 'u':
            lock = 0
        else:
            v = (lock, 1 << int(c))
            P.append(v)
            Q.add(v)
            lock |= 1 << int(c)
    i = 0; ok = 1
    while i < len(P) and ok:
        lock1, t1 = P[i]
        if lock1 & t1 == t1:
            ok = 0
        for j in xrange(i):
            lock2, t2 = P[j]
            if lock1 & lock2 == 0 and t1 & lock2 == t1:
                if t2 & lock1 == t2:
                    ok = 0
                v = (lock1 | lock2, t2)
                if v not in Q:
                    P.append(v)
                    Q.add(v)
        i += 1
    print "SAFE"*ok or "UNSAFE"

Fixed submission:
while 1:
    n = input()
    if n == 0:
        break
    s = raw_input()
    Q = set()
    lock = 0
    P = []
    for c in s:
        if c is 'u':
            lock = 0
        else:
            v = (lock, 1 << int(c))
            if v not in Q:
                P.append(v)
                Q.add(v)
            lock |= 1 << int(c)
    i = 0; ok = 1
    while i < len(P) and ok:
        lock1, t1 = P[i]
        if lock1 & t1 == t1:
            ok = 0
        for j in xrange(i):
            lock2, t2 = P[j]
            if lock1 & lock2 == 0 and t1 & lock2 == t1:
                if t2 & lock1 == t2:
                    ok = 0
                v = (lock1 | lock2, t2)
                if v not in Q:
                    P.append(v)
                    Q.add(v)
        i += 1
    print "SAFE"*ok or "UNSAFE"