import sys
from collections import deque

def solve():
    data = list(map(int,sys.stdin.read().split()))
    ptr = 0
    T = int(data[ptr])
    ptr += 1
    for _ in range (T):
        a = int(data[ptr])
        ptr += 1
        b = int(data[ptr])
        ptr += 1
        # 用 deque 初始化牌堆
        num_a = deque(map(int, data[ptr:ptr+a]))
        ptr += a
        num_b = deque(map(int, data[ptr:ptr+b]))
        ptr += b    # 最后这里要调整执政位置,是下一次循环读入数据时的指针位置正确

        A = 0
        B = 0

        while num_a and num_b:
            alice_card = num_a.popleft()
            bob_card = num_b.popleft()
            if alice_card > bob_card:
                A += 1
                num_a.append(alice_card)
            elif alice_card < bob_card:
                B += 1
                num_b.append(bob_card)


        if A > B:
            print('alice')
        elif A < B:
            print('bob')
        else:
            print('draw')

solve()

... ...
数据结构替换
用 deque 替代了普通的 list。
deque.popleft() 是 O (1),而 list.pop(0) 是 O (n),这是优化的核心
输入优化
使用 sys.stdin.read() 一次性读取所有输入,避免了多次 I/O 操作,进一步提升了速度
... ...