// Try Hitachi Hokudai Labo & Hokkaido University Contest 2019-1
// author: Leonardone @ NEETSDKASU

macro_rules! ewriteln {
    ($fmt:expr)  => {{
        use std::io::Write;
        writeln!(&mut std::io::stderr(), $fmt).unwrap();
    }};
    ($fmt:expr, $($arg:tt)*) => {{
        use std::io::Write;
        writeln!(&mut std::io::stderr(), $fmt, $($arg)*).unwrap();
    }};
}

fn main() {
    let start_time = std::time::SystemTime::now();
    let time_limit = std::time::Duration::from_millis(29500);

    let io = StandardIO::new();
    let mut io = io.get_io();

    let problem = Problem::read(&mut io);

    if problem.is_interactive {
        let mut solver = SolverB::new(start_time, time_limit, &problem);
        let mut achived_orders = vec![];
        for time in 0..problem.time_max {
            let new_orders = read_new_orders(&mut io);
            let put_orders = read_put_orders(&mut io);
            let cmd = solver.get_command(time, &new_orders, &put_orders, &achived_orders);
            io.println(cmd);
            if io.gets() != "OK" {
                break;
            }
            achived_orders = read_achived_orders(&mut io);
        }
    } else {
        let solver = SolverA::new(start_time, time_limit, &problem);
        let mut new_orders = vec![];
        for _ in 0..problem.time_max {
            new_orders.push(read_new_orders(&mut io));
        }
        let commands = solver.get_commands(&new_orders);
        for cmd in commands.iter() {
            io.println(cmd);
        }
    }

    ewriteln!("solution time: {:?}", start_time.elapsed().unwrap());
}

struct SolverA<'a> {
    start_time: std::time::SystemTime,
    time_limit: std::time::Duration,
    problem: &'a Problem,
}

impl<'a> SolverA<'a> {
    fn new(stime: std::time::SystemTime, limit: std::time::Duration, prob: &'a Problem) -> Self {
        SolverA {
            start_time: stime,
            time_limit: limit,
            problem: prob,
        }
    }
    fn get_commands(&self, new_orders: &[Vec<Order>]) -> Vec<Command> {
        let problem = &self.problem;
        let mut commands = vec![Command::Stay; self.problem.time_max];
        for (time, nos) in new_orders.iter().enumerate() {
            if let Some(order) = nos.first() {
                ewriteln!("time: {}", time);
                ewriteln!("order {} {}", order.id, order.destination);
                ewriteln!("steps: {}", problem.fw_table[1][order.destination].distance);
                ewriteln!("sc: {}", {
                    let t1 = problem.time_max * problem.time_max;
                    let wt = problem.time_max - time;
                    let t2 = wt * wt;
                    t1 - t2
                });
                let mut pos = problem.time_max;
                pos -= problem.fw_table[1][order.destination].distance as usize;
                let start_pos = pos;
                let path = problem.get_path(1, order.destination);
                for conn in path.iter() {
                    for _ in 0..conn.distance {
                        commands[pos] = Command::Move(conn.vertex_id);
                        pos += 1;
                    }
                }
                if start_pos > time {
                    commands.swap(time, start_pos);
                }
                break;
            }
        }
        commands
    }
}

struct SolverB<'a> {
    start_time: std::time::SystemTime,
    time_limit: std::time::Duration,
    problem: &'a Problem,
    commands: Vec<usize>,
    command_pos: usize,
    orders: std::collections::BTreeMap<usize, i32>,
    order_table: std::collections::HashMap<usize, usize>,
}

impl<'a> SolverB<'a> {
    fn new(stime: std::time::SystemTime, limit: std::time::Duration, prob: &'a Problem) -> Self {
        SolverB::<'a> {
            start_time: stime,
            time_limit: limit,
            problem: prob,
            commands: vec![],
            command_pos: 0,
            orders: std::collections::BTreeMap::new(),
            order_table: std::collections::HashMap::new(),
        }
    }
    fn get_command(
        &mut self,
        time: usize,
        new_orders: &[Order],
        put_orders: &[usize],
        achived_orders: &[usize],
    ) -> Command {
        self.update_orders(new_orders, put_orders, achived_orders);
        if let Some(id) = self.next_move_to() {
            return Command::Move(id);
        }
        if self.orders.is_empty() {
            return Command::Stay;
        }
        self.make_commands();
        self.next_move_to().map_or(Command::Stay, Command::Move)
    }
    fn update_orders(
        &mut self,
        new_orders: &[Order],
        put_orders: &[usize],
        achived_orders: &[usize],
    ) {
        let orders = &mut self.orders;
        let order_table = &mut self.order_table;
        for order in new_orders {
            order_table.insert(order.id, order.destination);
        }
        for order_id in achived_orders.iter() {
            let vertex_id = order_table.get(order_id).unwrap();
            if let std::collections::btree_map::Entry::Occupied(mut oe) = orders.entry(*vertex_id) {
                *oe.get_mut() -= 1;
                if *oe.get() == 0 {
                    oe.remove();
                }
            }
        }
        for order_id in put_orders.iter() {
            let vertex_id = order_table.get(order_id).unwrap();
            *orders.entry(*vertex_id).or_insert(0) += 1;
        }
    }
    fn next_move_to(&mut self) -> Option<usize> {
        if self.command_pos < self.commands.len() {
            let id = self.commands[self.command_pos];
            self.command_pos += 1;
            Some(id)
        } else {
            None
        }
    }
    fn make_commands(&mut self) {
        self.commands.clear();
        self.command_pos = 0;
        let goal_id = self.orders.iter().max_by_key(|e| e.1).unwrap().0;
        let path = self.problem.get_path(1, *goal_id);
        for conn in path.iter() {
            for _ in 0..conn.distance {
                self.commands.push(conn.vertex_id);
            }
        }
        for i in 0..path.len() {
            let i = path.len() - 1 - i;
            let id = if i > 0 { path[i - 1].vertex_id } else { 1 };
            for _ in 0..path[i].distance {
                self.commands.push(id);
            }
        }
    }
}

#[derive(Clone)]
enum Command {
    Move(usize),
    Stay,
}

impl std::fmt::Display for Command {
    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        match *self {
            Command::Move(v) => v.fmt(formatter),
            Command::Stay => "-1".fmt(formatter),
        }
    }
}

#[derive(Clone)]
struct Connection {
    vertex_id: usize,
    distance: i32,
}

impl Connection {
    fn new(i: usize, d: i32) -> Self {
        Connection {
            vertex_id: i,
            distance: d,
        }
    }
}

struct Problem {
    vertex_count: usize,
    graph: Vec<Vec<Connection>>,
    frequency: Vec<usize>,
    time_max: usize,
    is_interactive: bool,
    fw_table: Vec<Vec<Connection>>,
}

impl Problem {
    fn read(io: &mut IO) -> Self {
        let (vc, ec) = io.get2();
        let mut fw = vec![];
        for i in 0..vc + 1 {
            let mut tmp = vec![];
            for j in 0..vc + 1 {
                tmp.push(Connection::new(j, std::i32::MAX / 2));
            }
            tmp[i].distance = 0;
            fw.push(tmp);
        }
        let mut g = vec![vec![]; vc + 1];
        for _ in 0..ec {
            let (u, v, d): (usize, usize, i32) = io.get3();
            g[u].push(Connection::new(v, d));
            g[v].push(Connection::new(u, d));
            fw[u][v] = Connection::new(v, d);
            fw[v][u] = Connection::new(u, d);
        }
        for i in 0..vc + 1 {
            for j in 0..vc + 1 {
                for k in 0..vc + 1 {
                    let dist = fw[j][i].distance + fw[i][k].distance;
                    let mut tmp = &mut fw[j][k];
                    if tmp.distance > dist {
                        tmp.vertex_id = i;
                        tmp.distance = dist;
                    }
                }
            }
        }
        let freq = io.get_vec();
        let interacive = freq[0] == 0;
        let t_max = if !interacive { freq[0] } else { io.get() };
        Problem {
            vertex_count: vc,
            graph: g,
            frequency: freq,
            time_max: t_max,
            is_interactive: interacive,
            fw_table: fw,
        }
    }
    fn get_path(&self, start_id: usize, goal_id: usize) -> Vec<Connection> {
        let fw = &self.fw_table;
        let mut path = vec![];
        let mut stack = vec![];
        stack.push((start_id, goal_id));
        while let Some((s, g)) = stack.pop() {
            let conn = &fw[s][g];
            if conn.vertex_id == g {
                path.push(conn.clone());
                continue;
            }
            stack.push((conn.vertex_id, g));
            stack.push((s, conn.vertex_id));
        }
        path
    }
}

#[derive(Clone)]
struct Order {
    id: usize,
    destination: usize,
}

impl Order {
    fn new(i: usize, d: usize) -> Self {
        Order {
            id: i,
            destination: d,
        }
    }
}

fn read_new_orders(io: &mut IO) -> Vec<Order> {
    let mut orders = vec![];
    let n = io.get();
    for _ in 0..n {
        let (i, d) = io.get2();
        orders.push(Order::new(i, d));
    }
    orders
}

fn read_put_orders(io: &mut IO) -> Vec<usize> {
    let mut orders = vec![];
    let n = io.get();
    for _ in 0..n {
        orders.push(io.get());
    }
    orders
}

fn read_achived_orders(io: &mut IO) -> Vec<usize> {
    let mut orders = vec![];
    let n = io.get();
    for _ in 0..n {
        orders.push(io.get());
    }
    orders
}

struct StandardIO {
    stdin: std::io::Stdin,
    stdout: std::io::Stdout,
}

struct IO<'a> {
    stdin: std::io::StdinLock<'a>,
    stdout: std::io::StdoutLock<'a>,
}

impl StandardIO {
    fn new() -> Self {
        StandardIO {
            stdin: std::io::stdin(),
            stdout: std::io::stdout(),
        }
    }
    fn get_io(&self) -> IO {
        IO {
            stdin: self.stdin.lock(),
            stdout: self.stdout.lock(),
        }
    }
}

impl<'a> IO<'a> {
    fn get_line(&mut self) -> String {
        let mut line = String::new();
        std::io::BufRead::read_line(&mut self.stdin, &mut line).unwrap();
        line
    }
    fn gets(&mut self) -> String {
        self.get_line().trim().to_string()
    }
    fn get<A>(&mut self) -> A
    where
        A: std::str::FromStr,
        A::Err: std::fmt::Debug,
    {
        self.get_line().trim().parse().unwrap()
    }
    fn get2<A, B>(&mut self) -> (A, B)
    where
        A: std::str::FromStr,
        B: std::str::FromStr,
        A::Err: std::fmt::Debug,
        B::Err: std::fmt::Debug,
    {
        let line = self.get_line();
        let mut tokens = line.split_whitespace();
        let a: A = tokens.next().unwrap().parse().unwrap();
        let b: B = tokens.next().unwrap().parse().unwrap();
        (a, b)
    }
    fn get3<A, B, C>(&mut self) -> (A, B, C)
    where
        A: std::str::FromStr,
        B: std::str::FromStr,
        C: std::str::FromStr,
        A::Err: std::fmt::Debug,
        B::Err: std::fmt::Debug,
        C::Err: std::fmt::Debug,
    {
        let line = self.get_line();
        let mut tokens = line.split_whitespace();
        let a: A = tokens.next().unwrap().parse().unwrap();
        let b: B = tokens.next().unwrap().parse().unwrap();
        let c: C = tokens.next().unwrap().parse().unwrap();
        (a, b, c)
    }
    fn get_vec<A>(&mut self) -> Vec<A>
    where
        A: std::str::FromStr,
        A::Err: std::fmt::Debug,
    {
        self.get_line()
            .split_whitespace()
            .map(|v| v.parse().unwrap())
            .collect()
    }
    fn println<A: std::fmt::Display>(&mut self, v: A) {
        use std::io::Write;
        writeln!(&mut self.stdout, "{}", v).unwrap();
        self.stdout.flush().unwrap();
    }
}


Measured memory_bytes: 9296