using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("2");
WillReturn.Add("3 1 200 1000");
WillReturn.Add("5 20 30 40 50 2");
WillReturn.Add("5");
WillReturn.Add("1 1 1 2 2");
//20
//30
//40
//200
//1000
}
else if (InputPattern == "Input2") {
WillReturn.Add("10");
WillReturn.Add("6 8 24 47 29 73 13");
WillReturn.Add("1 4");
WillReturn.Add("5 72 15 68 49 16");
WillReturn.Add("5 65 20 93 64 91");
WillReturn.Add("6 100 88 63 50 90 44");
WillReturn.Add("2 6 1");
WillReturn.Add("10 14 2 76 28 21 78 43 11 97 70");
WillReturn.Add("5 31 9 62 84 40");
WillReturn.Add("8 10 46 96 23 98 19 38 51");
WillReturn.Add("2 37 77");
WillReturn.Add("20");
WillReturn.Add("1 1 1 1 2 2 2 1 1 2 2 2 2 2 1 2 1 2 2 1");
//100
//88
//72
//65
//93
//77
//68
//63
//50
//90
//64
//91
//49
//46
//44
//96
//37
//31
//62
//20
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static void Main()
{
List<string> InputList = GetInputList();
long N = long.Parse(InputList[0]);
long[] wkArr = { };
Action<string> SplitAct = pStr =>
wkArr = pStr.Split(' ').Select(pX => long.Parse(pX)).ToArray();
var InsSegmentTree = new SegmentTree(N * 2, long.MinValue);
// 棚ごとの消費期限のキュー
var QueList = new List<Queue<long>>();
// 棚[消費期限]なDict
var TanaDict = new Dictionary<long, long>();
long TanaID = 0;
foreach (string EachStr in InputList.Skip(1).Take((int)N)) {
SplitAct(EachStr);
long[] CurrKArr = wkArr.Skip(1).ToArray();
long UB = CurrKArr.GetUpperBound(0);
foreach (long EachK in CurrKArr) {
TanaDict[EachK] = TanaID;
}
var CurrQue = new Queue<long>(CurrKArr);
QueList.Add(CurrQue);
TanaID++;
}
for (long I = 0; I <= QueList.Count - 1; I++) {
if (QueList[(int)I].Count > 0) {
long K1 = QueList[(int)I].Dequeue();
InsSegmentTree.Update(I, K1);
}
if (QueList[(int)I].Count > 0) {
long K2 = QueList[(int)I].Dequeue();
InsSegmentTree.Update(I + N, K2);
}
}
var sb = new System.Text.StringBuilder();
int[] AArr = InputList.Last().Split(' ').Select(pX => int.Parse(pX)).ToArray();
foreach (long EachA in AArr) {
long MaxKouho1 = InsSegmentTree.Internal_Query(0, N - 1);
long MaxKouho2 = InsSegmentTree.Internal_Query(0, N * 2 - 1);
long MaxVal;
if (EachA == 1) {
MaxVal = MaxKouho1;
}
else {
MaxVal = Math.Max(MaxKouho1, MaxKouho2);
}
sb.AppendLine(MaxVal.ToString());
long ExtractTanaID = TanaDict[MaxVal];
long ExtractKouho1 = InsSegmentTree.Internal_Query(ExtractTanaID, ExtractTanaID);
long ExtractKouho2 = InsSegmentTree.Internal_Query(ExtractTanaID + N, ExtractTanaID + N);
Queue<long> CurrQue = QueList[(int)TanaDict[MaxVal]];
if (MaxVal == ExtractKouho1) {
InsSegmentTree.Update(ExtractTanaID, ExtractKouho2);
}
long Dequeued = long.MinValue;
if (CurrQue.Count > 0) {
Dequeued = CurrQue.Dequeue();
}
InsSegmentTree.Update(ExtractTanaID + N, Dequeued);
}
Console.Write(sb.ToString());
}
}
#region SegmentTree
// SegmentTreeクラス (RMaxQ and 1点更新)
internal class SegmentTree
{
private long[] mTreeNodeArr;
private long UB; // 木のノードの配列のUB
private long mLeafCnt; // 葉ノードの数
private long mExternalArrUB;
// ノードの添字を引数とし、範囲の開始添字と終了添字を持つ配列
private struct RangeInfoDef
{
internal long StaInd;
internal long EndInd;
}
private RangeInfoDef[] mRangeInfo;
// ノードのIndexの列挙を返す
internal IEnumerable<long> GetNodeIndEnum()
{
for (long I = 0; I <= mExternalArrUB; I++) {
yield return I;
}
}
// 木のノードのUBを返す
internal long GetUB()
{
return mExternalArrUB;
}
// コンストラクタ
internal SegmentTree(long pExternalArrUB, long pInitVal)
{
mExternalArrUB = pExternalArrUB;
// 簡単のため、葉ノード数を2のべき乗に
long ArrLength = 0;
for (long I = 1; I < long.MaxValue; I *= 2) {
ArrLength += I;
mLeafCnt = I;
if (pExternalArrUB + 1 < mLeafCnt) break;
}
// すべての値をpInitValに
UB = ArrLength - 1;
mTreeNodeArr = new long[UB + 1];
for (long I = 0; I <= UB; I++) {
mTreeNodeArr[I] = pInitVal;
}
// ノードの添字を引数とし、範囲の開始添字と終了添字を持つ配列の作成
mRangeInfo = new RangeInfoDef[UB + 1];
for (long I = 0; I <= UB; I++) {
if (I == 0) {
RangeInfoDef WillSet1;
WillSet1.StaInd = 0;
WillSet1.EndInd = mLeafCnt - 1;
mRangeInfo[I] = WillSet1;
continue;
}
long ParentNode = DeriveParentNode(I);
RangeInfoDef ParentRangeInfo = mRangeInfo[ParentNode];
RangeInfoDef WillSet2;
long Mid = (ParentRangeInfo.StaInd + ParentRangeInfo.EndInd) / 2;
if (I % 2 == 1) { // 奇数ノードの場合
WillSet2.StaInd = ParentRangeInfo.StaInd;
WillSet2.EndInd = Mid;
}
else { // 偶数ノードの場合
WillSet2.StaInd = Mid + 1;
WillSet2.EndInd = ParentRangeInfo.EndInd;
}
mRangeInfo[I] = WillSet2;
}
}
// 親ノードの添字を取得
private long DeriveParentNode(long pTarget)
{
return (pTarget - 1) / 2;
}
// 子ノードの添字(小さいほう)を取得
private long DeriveChildNode(long pTarget)
{
return pTarget * 2 + 1;
}
// 葉ノードの配列の添字を木の添字に変換して返す
private long DeriveTreeNode(long pLeafArrInd)
{
long BaseInd = UB - mLeafCnt + 1;
return BaseInd + pLeafArrInd;
}
// 葉ノードの配列のK番目の値をNewValに変更
internal void Update(long pK, long pNewVal)
{
long CurrNode = DeriveTreeNode(pK);
mTreeNodeArr[CurrNode] = pNewVal;
// 登りながら更新
while (CurrNode > 0) {
CurrNode = DeriveParentNode(CurrNode);
long ChildNode1 = DeriveChildNode(CurrNode);
long ChildNode2 = ChildNode1 + 1;
mTreeNodeArr[CurrNode] =
Math.Max(mTreeNodeArr[ChildNode1], mTreeNodeArr[ChildNode2]);
}
}
// 開始添字と終了添字とカレントノードを引数として、最大値を返す
internal long Internal_Query(long pSearchStaInd, long pSearchEndInd)
{
return Private_Query(pSearchStaInd, pSearchEndInd, 0);
}
private long Private_Query(long pSearchStaInd, long pSearchEndInd, long pCurrNode)
{
long CurrNodeStaInd = mRangeInfo[pCurrNode].StaInd;
long CurrNodeEndInd = mRangeInfo[pCurrNode].EndInd;
// OverLapしてなければ、long.MinValue
if (CurrNodeEndInd < pSearchStaInd || pSearchEndInd < CurrNodeStaInd)
return long.MinValue;
// 完全に含んでいれば、このノードの値
if (pSearchStaInd <= CurrNodeStaInd && CurrNodeEndInd <= pSearchEndInd)
return mTreeNodeArr[pCurrNode];
// そうでなければ、2つの子の最大値
long ChildNode1 = DeriveChildNode(pCurrNode);
long ChildNode2 = ChildNode1 + 1;
long ChildVal1 = Private_Query(pSearchStaInd, pSearchEndInd, ChildNode1);
long ChildVal2 = Private_Query(pSearchStaInd, pSearchEndInd, ChildNode2);
return Math.Max(ChildVal1, ChildVal2);
}
}
#endregion