PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FW0000040. Phát hiện sự kiện đồng thời (Identifying Concurrent Events)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Phát hiện sự kiện đồng thời (Identifying Concurrent Events)

Identifying Concurrent Events

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

Given sequences of ordered events and inter-sequence messages, two events are concurrent if neither precedes the other. Count unordered concurrent pairs and report the first two pairs.

Input

Integer cc computation sequences; each sequence line starts with tt then tt event names in chronological order; then mm and mm sender/receiver event-name pairs.

Output

Print the count of concurrent pairs, then up to two lines (event1,event2) ordered by the input occurrence of events.

Subtasks

  • Subtask 1 (40 points): c≤5c\le5; 1≤c≤121\le c\le12, 1≤t≤151\le t\le15 per sequence, at most 200200 events overall, 0≤m≤400\le m\le40; unique 1–5 alphanumeric event names, acyclic precedence relation.
  • Subtask 2 (60 points): 1≤c≤121\le c\le12, 1≤t≤151\le t\le15 per sequence, at most 200200 events overall, 0≤m≤400\le m\le40; unique 1–5 alphanumeric event names, acyclic precedence relation.

Examples

Input

2
2 e1 e2
2 e3 e4
1
e3 e1

Output

2
(e1,e4)
(e2,e4)

Explanation

Sequence order and the additional message imply all relevant precedence except for (e1,e4) and (e2,e4), leaving exactly two concurrent unordered pairs.