def f(s, cost, route, ans):

    route.append(s)

    ans += cost[s]

    del cost[s]

    for k, c in list(data.items()):

        a, b = k

        if s == a:

            if not b in route:

                if not b in cost or c < cost[b]:

                    cost[b] = c

        elif s == b:

            if not a in route:

                if not a in cost or c < cost[a]:

                    cost[a] = c

    if cost:

        s = sorted(list(cost.items()), key=lambda x: x[1])[0][0]

        return f(s, cost, route, ans)

    else:

        return ans



while True:

    n, m = list(map(int, input().split()))

    if n == m == 0: break

    data = {}

    for i in range(m):

        a, b, c = list(map(int, input().split()))

        data[(a, b)] = c

    else:

        print(f(0, {0:0}, [], 0))
def f(s, cost, route, ans):

    route.append(s)

    ans += cost[s]

    del cost[s]

    for k, c in list(data.items()):

        if s in k:

            a, b = k

            b = b if s == a else a

            if not b in route:

                if not b in cost or c < cost[b]:

                    cost[b] = c

    if cost:

        s = sorted(list(cost.items()), key=lambda x: x[1])[0][0]

        return f(s, cost, route, ans)

    else:

        return ans



while True:

    n, m = list(map(int, input().split()))

    if n == m == 0: break

    data = {}

    for i in range(m):

        a, b, c = list(map(int, input().split()))

        data[(a, b)] = c

    else:

        print(f(0, {0:0}, [], 0))
def f(s, cost, route, ans):
    route.append(s)
    ans += cost[s]
    del cost[s]
    for k, c in list(data.items()):
        a, b = k
        if s == a:
            if not b in route:
                if not b in cost or c < cost[b]:
                    cost[b] = c
        elif s == b:
            if not a in route:
                if not a in cost or c < cost[a]:
                    cost[a] = c
    if cost:
        s = sorted(list(cost.items()), key=lambda x: x[1])[0][0]
        return f(s, cost, route, ans)
    else:
        return ans


while True:
    n, m = list(map(int, input().split()))
    if n == m == 0:
        break
    data = {}
    for i in range(m):
        a, b, c = list(map(int, input().split()))
        data[(a, b)] = c
    else:
        print(f(0, {0: 0}, [], 0))

def f(s, cost, route, ans):
    route.append(s)
    ans += cost[s]
    del cost[s]
    for k, c in list(data.items()):
        if s in k:
            a, b = k
            b = b if s == a else a
            if not b in route:
                if not b in cost or c < cost[b]:
                    cost[b] = c
    if cost:
        s = sorted(list(cost.items()), key=lambda x: x[1])[0][0]
        return f(s, cost, route, ans)
    else:
        return ans


while True:
    n, m = list(map(int, input().split()))
    if n == m == 0:
        break
    data = {}
    for i in range(m):
        a, b, c = list(map(int, input().split()))
        data[(a, b)] = c
    else:
        print(f(0, {0: 0}, [], 0))

-        a, b = k -        if s == a: +        if s in k: +            a, b = k +            b = b if s == a else a -        elif s == b: -            if not a in route: -                if not a in cost or c < cost[a]: -                    cost[a] = c