import os
import sys
from datetime import datetime,timezone
# 请在此输入您的代码
T=int(input())
for i in range(T):
s=input().split()
time=f"{s[0]} {s[1]}"
x=int(s[2])
dt=datetime.strptime(time,"%Y-%m-%d %H:%M:%S").replace(tzinfo=timezone.utc)
ts=dt.timestamp()
step=x*60
target_ts = (ts // step) * step
target_dt = datetime.fromtimestamp(target_ts, tz=timezone.utc)
print(target_dt.strftime("%Y-%m-%d %H:%M:%S"))