def nested_pairs2dict(pairs):
d = {}
for k, v in pairs:
if isinstance(v, list): # assumes v is also list of pairs
v = nested_pairs2dict(v)
d[k] = v
return d
import ast
import json
import sys
print(json.dumps(nested_pairs2dict(ast.literal_eval(sys.stdin.read())),
sort_keys=True, indent=4))
ZGVmIG5lc3RlZF9wYWlyczJkaWN0KHBhaXJzKToKICAgIGQgPSB7fQogICAgZm9yIGssIHYgaW4gcGFpcnM6CiAgICAgICAgaWYgaXNpbnN0YW5jZSh2LCBsaXN0KTogIyBhc3N1bWVzIHYgaXMgYWxzbyBsaXN0IG9mIHBhaXJzCiAgICAgICAgICAgdiA9IG5lc3RlZF9wYWlyczJkaWN0KHYpCiAgICAgICAgZFtrXSA9IHYKICAgIHJldHVybiBkCgppbXBvcnQgYXN0CmltcG9ydCBqc29uCmltcG9ydCBzeXMKCnByaW50KGpzb24uZHVtcHMobmVzdGVkX3BhaXJzMmRpY3QoYXN0LmxpdGVyYWxfZXZhbChzeXMuc3RkaW4ucmVhZCgpKSksIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgc29ydF9rZXlzPVRydWUsIGluZGVudD00KSk=
WwogIFsna2V5MScsICd2YWx1ZTEnXSwKICBbJ2tleTInLCAKICAgICBbCiAgICAgICBbJ2tleTMnLCAndmFsdWUzJ10sCiAgICAgICBbJ2tleTQnLCAKICAgICAgICAgIFsKIAogICAgICAgICAgXQogICAgICAgXSwKICAgICAgIFsna2V5NScsICd2YWx1ZTUnXQogICAgIF0KICBdLAogIFsna2V5NicsICd2YWx1ZTYnXSwKXQ==
[
['key1', 'value1'],
['key2',
[
['key3', 'value3'],
['key4',
[
]
],
['key5', 'value5']
]
],
['key6', 'value6'],
]