#include <iostream>
 #include <sstream>
 #include <vector>
 #include <regex>
 #include <string>
using namespace std;
int main() 
 {
     // 创建一个字符串,用于存储输入
     string input;
     // 从标准输入读取一行
     getline(cin, input);
     // 创建一个正则表达式,用于匹配输入
     regex re("A=\\{(.+)},B=\\{(.+)},R=(.+)");
     smatch match;
    // 检查输入是否匹配正则表达式
     if (regex_search(input, match, re)) 
     {
         // 从匹配结果中提取事件、设备和范围
         istringstream eventsStream(match[1].str());
         istringstream devicesStream(match[2].str());
         int range = stoi(match[3].str());
        vector<int> events, devices;
         string temp;
        while (getline(eventsStream, temp, ',')) 
         {
             events.push_back(stoi(temp));
         }
        while (getline(devicesStream, temp, ',')) 
         {
             devices.push_back(stoi(temp));
         }
        // 创建一个字符串流,用于存储结果对
         ostringstream resultPairs;
        // 遍历每个事件
         for (int event : events) 
         {
             int count = 0;
             // 遍历每个设备
             for (int device : devices) 
             {
                 // 如果设备小于事件,则继续下一个设备
                 if (device < event) continue;
                 // 如果设备与事件的距离小于等于范围,或者这是第一个设备,则将其添加到结果对中
                 if (device - event <= range || count == 0) 
                 {
                     resultPairs << "(" << event << "," << device << ")";
                     count++;
                 } 
                 else 
                 {
                     // 否则,跳出循环
                     break;
                 }
             }
         }
        // 打印结果对
         cout << resultPairs.str() << endl;
     }
    return 0;
 }